blob: 7ea6176e969c2c858a276fdeef163f56a901e2e5 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010047class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Calin Juravle9aec02f2014-11-18 23:06:35 +000075static constexpr uint32_t kMaxIntShiftValue = 0x1f;
76static constexpr uint64_t kMaxLongShiftValue = 0x3f;
77
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010078static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070079static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010080
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010081static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
82
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060083static constexpr uint32_t kNoDexPc = -1;
84
Dave Allison20dfc792014-06-16 20:44:29 -070085enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070086 // All types.
87 kCondEQ, // ==
88 kCondNE, // !=
89 // Signed integers and floating-point numbers.
90 kCondLT, // <
91 kCondLE, // <=
92 kCondGT, // >
93 kCondGE, // >=
94 // Unsigned integers.
95 kCondB, // <
96 kCondBE, // <=
97 kCondA, // >
98 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070099};
100
Vladimir Markof9f64412015-09-02 14:05:49 +0100101class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100102 public:
103 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
104
105 void AddInstruction(HInstruction* instruction);
106 void RemoveInstruction(HInstruction* instruction);
107
David Brazdilc3d743f2015-04-22 13:40:50 +0100108 // Insert `instruction` before/after an existing instruction `cursor`.
109 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
110 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
111
Roland Levillain6b469232014-09-25 10:10:38 +0100112 // Return true if this list contains `instruction`.
113 bool Contains(HInstruction* instruction) const;
114
Roland Levillainccc07a92014-09-16 14:48:16 +0100115 // Return true if `instruction1` is found before `instruction2` in
116 // this instruction list and false otherwise. Abort if none
117 // of these instructions is found.
118 bool FoundBefore(const HInstruction* instruction1,
119 const HInstruction* instruction2) const;
120
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000121 bool IsEmpty() const { return first_instruction_ == nullptr; }
122 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
123
124 // Update the block of all instructions to be `block`.
125 void SetBlockOfInstructions(HBasicBlock* block) const;
126
127 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
128 void Add(const HInstructionList& instruction_list);
129
David Brazdil2d7352b2015-04-20 14:52:42 +0100130 // Return the number of instructions in the list. This is an expensive operation.
131 size_t CountSize() const;
132
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100133 private:
134 HInstruction* first_instruction_;
135 HInstruction* last_instruction_;
136
137 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000138 friend class HGraph;
139 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100141 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100142
143 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
144};
145
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000146// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100147class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000148 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100149 HGraph(ArenaAllocator* arena,
150 const DexFile& dex_file,
151 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100152 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100154 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100155 bool debuggable = false,
156 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000157 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100158 blocks_(arena->Adapter(kArenaAllocBlockList)),
159 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
160 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700161 entry_block_(nullptr),
162 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100163 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100164 number_of_vregs_(0),
165 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000166 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400167 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000168 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000169 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000170 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100171 dex_file_(dex_file),
172 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100173 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100174 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100175 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000177 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100178 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
179 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
180 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
181 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
182 cached_current_method_(nullptr) {
183 blocks_.reserve(kDefaultNumberOfBlocks);
184 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000185
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000186 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100187 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
188
David Brazdil69ba7b72015-06-23 18:27:30 +0100189 bool IsInSsaForm() const { return in_ssa_form_; }
190
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000191 HBasicBlock* GetEntryBlock() const { return entry_block_; }
192 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100193 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000194
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000195 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
196 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000197
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000198 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000200 // Try building the SSA form of this graph, with dominance computation and loop
201 // recognition. Returns whether it was successful in doing all these steps.
202 bool TryBuildingSsa() {
203 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000204 // The SSA builder requires loops to all be natural. Specifically, the dead phi
205 // elimination phase checks the consistency of the graph when doing a post-order
206 // visit for eliminating dead phis: a dead phi can only have loop header phi
207 // users remaining when being visited.
208 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100209 // Precompute per-block try membership before entering the SSA builder,
210 // which needs the information to build catch block phis from values of
211 // locals at throwing instructions inside try blocks.
212 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100214 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000215 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000216 }
217
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100218 void ComputeDominanceInformation();
219 void ClearDominanceInformation();
220
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000222 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100223 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100224 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000225
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000226 // Analyze all natural loops in this graph. Returns false if one
227 // loop is not natural, that is the header does not dominate the
228 // back edge.
229 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100230
David Brazdilffee3d32015-07-06 11:48:53 +0100231 // Iterate over blocks to compute try block membership. Needs reverse post
232 // order and loop information.
233 void ComputeTryBlockInformation();
234
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000235 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000236 // Returns the instruction used to replace the invoke expression or null if the
237 // invoke is for a void method.
238 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000239
Mingyao Yang3584bce2015-05-19 16:01:59 -0700240 // Need to add a couple of blocks to test if the loop body is entered and
241 // put deoptimization instructions, etc.
242 void TransformLoopHeaderForBCE(HBasicBlock* header);
243
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000244 // Removes `block` from the graph. Assumes `block` has been disconnected from
245 // other blocks and has no instructions or phis.
246 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000247
David Brazdilfc6a86a2015-06-26 10:33:45 +0000248 // Splits the edge between `block` and `successor` while preserving the
249 // indices in the predecessor/successor lists. If there are multiple edges
250 // between the blocks, the lowest indices are used.
251 // Returns the new block which is empty and has the same dex pc as `successor`.
252 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
253
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100254 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
255 void SimplifyLoop(HBasicBlock* header);
256
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000257 int32_t GetNextInstructionId() {
258 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000259 return current_instruction_id_++;
260 }
261
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000262 int32_t GetCurrentInstructionId() const {
263 return current_instruction_id_;
264 }
265
266 void SetCurrentInstructionId(int32_t id) {
267 current_instruction_id_ = id;
268 }
269
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100270 uint16_t GetMaximumNumberOfOutVRegs() const {
271 return maximum_number_of_out_vregs_;
272 }
273
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000274 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
275 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100276 }
277
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100278 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
279 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
280 }
281
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000282 void UpdateTemporariesVRegSlots(size_t slots) {
283 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100284 }
285
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000286 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100287 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000288 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100289 }
290
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100291 void SetNumberOfVRegs(uint16_t number_of_vregs) {
292 number_of_vregs_ = number_of_vregs;
293 }
294
295 uint16_t GetNumberOfVRegs() const {
296 return number_of_vregs_;
297 }
298
299 void SetNumberOfInVRegs(uint16_t value) {
300 number_of_in_vregs_ = value;
301 }
302
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100303 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100304 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100305 return number_of_vregs_ - number_of_in_vregs_;
306 }
307
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100308 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100309 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100310 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100311
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100312 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100313 return linear_order_;
314 }
315
Mark Mendell1152c922015-04-24 17:06:35 -0400316 bool HasBoundsChecks() const {
317 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800318 }
319
Mark Mendell1152c922015-04-24 17:06:35 -0400320 void SetHasBoundsChecks(bool value) {
321 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800322 }
323
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100324 bool ShouldGenerateConstructorBarrier() const {
325 return should_generate_constructor_barrier_;
326 }
327
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000328 bool IsDebuggable() const { return debuggable_; }
329
David Brazdil8d5b8b22015-03-24 10:51:52 +0000330 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000331 // already, it is created and inserted into the graph. This method is only for
332 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600333 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000334
335 // TODO: This is problematic for the consistency of reference type propagation
336 // because it can be created anytime after the pass and thus it will be left
337 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600338 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000339
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600340 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
341 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600343 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
344 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000345 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600346 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
347 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000348 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600349 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
350 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000351 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000352
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100353 HCurrentMethod* GetCurrentMethod();
354
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100355 const DexFile& GetDexFile() const {
356 return dex_file_;
357 }
358
359 uint32_t GetMethodIdx() const {
360 return method_idx_;
361 }
362
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100363 InvokeType GetInvokeType() const {
364 return invoke_type_;
365 }
366
Mark Mendellc4701932015-04-10 13:18:51 -0400367 InstructionSet GetInstructionSet() const {
368 return instruction_set_;
369 }
370
David Brazdil77a48ae2015-09-15 12:34:04 +0000371 bool HasTryCatch() const { return has_try_catch_; }
372 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100373
David Brazdil2d7352b2015-04-20 14:52:42 +0100374 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100375 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000376 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100377 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000378
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000379 template <class InstructionType, typename ValueType>
380 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600381 ArenaSafeMap<ValueType, InstructionType*>* cache,
382 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000383 // Try to find an existing constant of the given value.
384 InstructionType* constant = nullptr;
385 auto cached_constant = cache->find(value);
386 if (cached_constant != cache->end()) {
387 constant = cached_constant->second;
388 }
389
390 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100391 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000392 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600393 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000394 cache->Overwrite(value, constant);
395 InsertConstant(constant);
396 }
397 return constant;
398 }
399
David Brazdil8d5b8b22015-03-24 10:51:52 +0000400 void InsertConstant(HConstant* instruction);
401
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000402 // Cache a float constant into the graph. This method should only be
403 // called by the SsaBuilder when creating "equivalent" instructions.
404 void CacheFloatConstant(HFloatConstant* constant);
405
406 // See CacheFloatConstant comment.
407 void CacheDoubleConstant(HDoubleConstant* constant);
408
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000409 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000410
411 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100412 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100414 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100415 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000416
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100417 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100418 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100419
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000420 HBasicBlock* entry_block_;
421 HBasicBlock* exit_block_;
422
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100423 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100424 uint16_t maximum_number_of_out_vregs_;
425
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100426 // The number of virtual registers in this method. Contains the parameters.
427 uint16_t number_of_vregs_;
428
429 // The number of virtual registers used by parameters of this method.
430 uint16_t number_of_in_vregs_;
431
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000432 // Number of vreg size slots that the temporaries use (used in baseline compiler).
433 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100434
Mark Mendell1152c922015-04-24 17:06:35 -0400435 // Has bounds checks. We can totally skip BCE if it's false.
436 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800437
David Brazdil77a48ae2015-09-15 12:34:04 +0000438 // Flag whether there are any try/catch blocks in the graph. We will skip
439 // try/catch-related passes if false.
440 bool has_try_catch_;
441
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000442 // Indicates whether the graph should be compiled in a way that
443 // ensures full debuggability. If false, we can apply more
444 // aggressive optimizations that may limit the level of debugging.
445 const bool debuggable_;
446
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000447 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000448 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000449
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100450 // The dex file from which the method is from.
451 const DexFile& dex_file_;
452
453 // The method index in the dex file.
454 const uint32_t method_idx_;
455
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100456 // If inlined, this encodes how the callee is being invoked.
457 const InvokeType invoke_type_;
458
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100459 // Whether the graph has been transformed to SSA form. Only used
460 // in debug mode to ensure we are not using properties only valid
461 // for non-SSA form (like the number of temporaries).
462 bool in_ssa_form_;
463
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100464 const bool should_generate_constructor_barrier_;
465
Mathieu Chartiere401d142015-04-22 13:56:20 -0700466 const InstructionSet instruction_set_;
467
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000468 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000469 HNullConstant* cached_null_constant_;
470 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000471 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000472 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000473 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000474
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100475 HCurrentMethod* cached_current_method_;
476
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000477 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100478 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000479 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000480 DISALLOW_COPY_AND_ASSIGN(HGraph);
481};
482
Vladimir Markof9f64412015-09-02 14:05:49 +0100483class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 public:
485 HLoopInformation(HBasicBlock* header, HGraph* graph)
486 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100487 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100488 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100489 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100490 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
491 back_edges_.reserve(kDefaultNumberOfBackEdges);
492 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493
494 HBasicBlock* GetHeader() const {
495 return header_;
496 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000497
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000498 void SetHeader(HBasicBlock* block) {
499 header_ = block;
500 }
501
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100502 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
503 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
504 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
505
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000506 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100507 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000508 }
509
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100510 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100511 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100512 }
513
David Brazdil46e2a392015-03-16 17:31:52 +0000514 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100515 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100516 }
517
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000518 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100519 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000520 }
521
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100523
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100524 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100525 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100526 }
527
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100528 // Returns the lifetime position of the back edge that has the
529 // greatest lifetime position.
530 size_t GetLifetimeEnd() const;
531
532 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100533 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100534 }
535
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100536 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100537 // that is the header dominates the back edge.
538 bool Populate();
539
David Brazdila4b8c212015-05-07 09:59:30 +0100540 // Reanalyzes the loop by removing loop info from its blocks and re-running
541 // Populate(). If there are no back edges left, the loop info is completely
542 // removed as well as its SuspendCheck instruction. It must be run on nested
543 // inner loops first.
544 void Update();
545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100546 // Returns whether this loop information contains `block`.
547 // Note that this loop information *must* be populated before entering this function.
548 bool Contains(const HBasicBlock& block) const;
549
550 // Returns whether this loop information is an inner loop of `other`.
551 // Note that `other` *must* be populated before entering this function.
552 bool IsIn(const HLoopInformation& other) const;
553
Aart Bik73f1f3b2015-10-28 15:28:08 -0700554 // Returns true if instruction is not defined within this loop or any loop nested inside
555 // this loop. If must_dominate is set, only definitions that actually dominate the loop
556 // header can be invariant. Otherwise, any definition outside the loop, including
557 // definitions that appear after the loop, is invariant.
558 bool IsLoopInvariant(HInstruction* instruction, bool must_dominate) const;
559
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 const ArenaBitVector& GetBlocks() const { return blocks_; }
561
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000562 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000563 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000564
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000565 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100566 // Internal recursive implementation of `Populate`.
567 void PopulateRecursive(HBasicBlock* block);
568
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000569 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100570 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100571 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100572 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000573
574 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
575};
576
David Brazdilec16f792015-08-19 15:04:01 +0100577// Stores try/catch information for basic blocks.
578// Note that HGraph is constructed so that catch blocks cannot simultaneously
579// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100580class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100581 public:
582 // Try block information constructor.
583 explicit TryCatchInformation(const HTryBoundary& try_entry)
584 : try_entry_(&try_entry),
585 catch_dex_file_(nullptr),
586 catch_type_index_(DexFile::kDexNoIndex16) {
587 DCHECK(try_entry_ != nullptr);
588 }
589
590 // Catch block information constructor.
591 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
592 : try_entry_(nullptr),
593 catch_dex_file_(&dex_file),
594 catch_type_index_(catch_type_index) {}
595
596 bool IsTryBlock() const { return try_entry_ != nullptr; }
597
598 const HTryBoundary& GetTryEntry() const {
599 DCHECK(IsTryBlock());
600 return *try_entry_;
601 }
602
603 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
604
605 bool IsCatchAllTypeIndex() const {
606 DCHECK(IsCatchBlock());
607 return catch_type_index_ == DexFile::kDexNoIndex16;
608 }
609
610 uint16_t GetCatchTypeIndex() const {
611 DCHECK(IsCatchBlock());
612 return catch_type_index_;
613 }
614
615 const DexFile& GetCatchDexFile() const {
616 DCHECK(IsCatchBlock());
617 return *catch_dex_file_;
618 }
619
620 private:
621 // One of possibly several TryBoundary instructions entering the block's try.
622 // Only set for try blocks.
623 const HTryBoundary* try_entry_;
624
625 // Exception type information. Only set for catch blocks.
626 const DexFile* catch_dex_file_;
627 const uint16_t catch_type_index_;
628};
629
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100630static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100631static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100632
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000633// A block in a method. Contains the list of instructions represented
634// as a double linked list. Each block knows its predecessors and
635// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100636
Vladimir Markof9f64412015-09-02 14:05:49 +0100637class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600639 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000640 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000641 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
642 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 loop_information_(nullptr),
644 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000645 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100646 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100647 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100648 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000649 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000650 try_catch_information_(nullptr) {
651 predecessors_.reserve(kDefaultNumberOfPredecessors);
652 successors_.reserve(kDefaultNumberOfSuccessors);
653 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
654 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000655
Vladimir Marko60584552015-09-03 13:35:12 +0000656 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100657 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000658 }
659
Vladimir Marko60584552015-09-03 13:35:12 +0000660 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100661 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000662 }
663
David Brazdild26a4112015-11-10 11:07:31 +0000664 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
665 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
666
Vladimir Marko60584552015-09-03 13:35:12 +0000667 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
668 return ContainsElement(successors_, block, start_from);
669 }
670
671 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100672 return dominated_blocks_;
673 }
674
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100675 bool IsEntryBlock() const {
676 return graph_->GetEntryBlock() == this;
677 }
678
679 bool IsExitBlock() const {
680 return graph_->GetExitBlock() == this;
681 }
682
David Brazdil46e2a392015-03-16 17:31:52 +0000683 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000684 bool IsSingleTryBoundary() const;
685
686 // Returns true if this block emits nothing but a jump.
687 bool IsSingleJump() const {
688 HLoopInformation* loop_info = GetLoopInformation();
689 return (IsSingleGoto() || IsSingleTryBoundary())
690 // Back edges generate a suspend check.
691 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
692 }
David Brazdil46e2a392015-03-16 17:31:52 +0000693
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000694 void AddBackEdge(HBasicBlock* back_edge) {
695 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000696 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000697 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100698 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000699 loop_information_->AddBackEdge(back_edge);
700 }
701
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000702 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000703 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000704
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100705 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000706 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600707 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000708
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000709 HBasicBlock* GetDominator() const { return dominator_; }
710 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000711 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
712
713 void RemoveDominatedBlock(HBasicBlock* block) {
714 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100715 }
Vladimir Marko60584552015-09-03 13:35:12 +0000716
717 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
718 ReplaceElement(dominated_blocks_, existing, new_block);
719 }
720
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100721 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000722
723 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100724 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000725 }
726
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100727 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
728 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100729 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100730 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100731 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
732 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000733
734 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000735 successors_.push_back(block);
736 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000737 }
738
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100739 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
740 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100741 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000742 new_block->predecessors_.push_back(this);
743 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000744 }
745
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000746 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
747 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000748 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000749 new_block->successors_.push_back(this);
750 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000751 }
752
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100753 // Insert `this` between `predecessor` and `successor. This method
754 // preserves the indicies, and will update the first edge found between
755 // `predecessor` and `successor`.
756 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
757 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100758 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000759 successor->predecessors_[predecessor_index] = this;
760 predecessor->successors_[successor_index] = this;
761 successors_.push_back(successor);
762 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100763 }
764
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100765 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000766 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100767 }
768
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000769 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000770 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000771 }
772
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000774 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100775 }
776
777 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000778 predecessors_.push_back(block);
779 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780 }
781
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100782 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000783 DCHECK_EQ(predecessors_.size(), 2u);
784 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100785 }
786
David Brazdil769c9e52015-04-27 13:54:09 +0100787 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 DCHECK_EQ(successors_.size(), 2u);
789 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100790 }
791
David Brazdilfc6a86a2015-06-26 10:33:45 +0000792 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000793 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100794 }
795
David Brazdilfc6a86a2015-06-26 10:33:45 +0000796 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000797 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100798 }
799
David Brazdilfc6a86a2015-06-26 10:33:45 +0000800 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000801 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100802 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000803 }
804
805 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000806 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100807 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000808 }
809
810 // Returns whether the first occurrence of `predecessor` in the list of
811 // predecessors is at index `idx`.
812 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100813 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000814 return GetPredecessorIndexOf(predecessor) == idx;
815 }
816
David Brazdild7558da2015-09-22 13:04:14 +0100817 // Create a new block between this block and its predecessors. The new block
818 // is added to the graph, all predecessor edges are relinked to it and an edge
819 // is created to `this`. Returns the new empty block. Reverse post order or
820 // loop and try/catch information are not updated.
821 HBasicBlock* CreateImmediateDominator();
822
David Brazdilfc6a86a2015-06-26 10:33:45 +0000823 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100824 // created, latter block. Note that this method will add the block to the
825 // graph, create a Goto at the end of the former block and will create an edge
826 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100827 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000828 HBasicBlock* SplitBefore(HInstruction* cursor);
829
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000830 // Split the block into two blocks just after `cursor`. Returns the newly
831 // created block. Note that this method just updates raw block information,
832 // like predecessors, successors, dominators, and instruction list. It does not
833 // update the graph, reverse post order, loop information, nor make sure the
834 // blocks are consistent (for example ending with a control flow instruction).
835 HBasicBlock* SplitAfter(HInstruction* cursor);
836
David Brazdil9bc43612015-11-05 21:25:24 +0000837 // Split catch block into two blocks after the original move-exception bytecode
838 // instruction, or at the beginning if not present. Returns the newly created,
839 // latter block, or nullptr if such block could not be created (must be dead
840 // in that case). Note that this method just updates raw block information,
841 // like predecessors, successors, dominators, and instruction list. It does not
842 // update the graph, reverse post order, loop information, nor make sure the
843 // blocks are consistent (for example ending with a control flow instruction).
844 HBasicBlock* SplitCatchBlockAfterMoveException();
845
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846 // Merge `other` at the end of `this`. Successors and dominated blocks of
847 // `other` are changed to be successors and dominated blocks of `this`. Note
848 // that this method does not update the graph, reverse post order, loop
849 // information, nor make sure the blocks are consistent (for example ending
850 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100851 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000852
853 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
854 // of `this` are moved to `other`.
855 // Note that this method does not update the graph, reverse post order, loop
856 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000857 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000858 void ReplaceWith(HBasicBlock* other);
859
David Brazdil2d7352b2015-04-20 14:52:42 +0100860 // Merge `other` at the end of `this`. This method updates loops, reverse post
861 // order, links to predecessors, successors, dominators and deletes the block
862 // from the graph. The two blocks must be successive, i.e. `this` the only
863 // predecessor of `other` and vice versa.
864 void MergeWith(HBasicBlock* other);
865
866 // Disconnects `this` from all its predecessors, successors and dominator,
867 // removes it from all loops it is included in and eventually from the graph.
868 // The block must not dominate any other block. Predecessors and successors
869 // are safely updated.
870 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000871
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000872 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100873 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100874 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100875 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100876 // Replace instruction `initial` with `replacement` within this block.
877 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
878 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100879 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100880 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000881 // RemoveInstruction and RemovePhi delete a given instruction from the respective
882 // instruction list. With 'ensure_safety' set to true, it verifies that the
883 // instruction is not in use and removes it from the use lists of its inputs.
884 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
885 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100886 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100887
888 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100889 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100890 }
891
Roland Levillain6b879dd2014-09-22 17:13:44 +0100892 bool IsLoopPreHeaderFirstPredecessor() const {
893 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100894 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100895 }
896
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100897 HLoopInformation* GetLoopInformation() const {
898 return loop_information_;
899 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000901 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100902 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000903 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 void SetInLoop(HLoopInformation* info) {
905 if (IsLoopHeader()) {
906 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100907 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100908 loop_information_ = info;
909 } else if (loop_information_->Contains(*info->GetHeader())) {
910 // Block is currently part of an outer loop. Make it part of this inner loop.
911 // Note that a non loop header having a loop information means this loop information
912 // has already been populated
913 loop_information_ = info;
914 } else {
915 // Block is part of an inner loop. Do not update the loop information.
916 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
917 // at this point, because this method is being called while populating `info`.
918 }
919 }
920
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000921 // Raw update of the loop information.
922 void SetLoopInformation(HLoopInformation* info) {
923 loop_information_ = info;
924 }
925
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100926 bool IsInLoop() const { return loop_information_ != nullptr; }
927
David Brazdilec16f792015-08-19 15:04:01 +0100928 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
929
930 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
931 try_catch_information_ = try_catch_information;
932 }
933
934 bool IsTryBlock() const {
935 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
936 }
937
938 bool IsCatchBlock() const {
939 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
940 }
David Brazdilffee3d32015-07-06 11:48:53 +0100941
942 // Returns the try entry that this block's successors should have. They will
943 // be in the same try, unless the block ends in a try boundary. In that case,
944 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100945 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100946
David Brazdild7558da2015-09-22 13:04:14 +0100947 bool HasThrowingInstructions() const;
948
David Brazdila4b8c212015-05-07 09:59:30 +0100949 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100950 bool Dominates(HBasicBlock* block) const;
951
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100952 size_t GetLifetimeStart() const { return lifetime_start_; }
953 size_t GetLifetimeEnd() const { return lifetime_end_; }
954
955 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
956 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
957
David Brazdil8d5b8b22015-03-24 10:51:52 +0000958 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000959 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100960 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000961 bool HasSinglePhi() const;
962
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000964 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000965 ArenaVector<HBasicBlock*> predecessors_;
966 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100967 HInstructionList instructions_;
968 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000969 HLoopInformation* loop_information_;
970 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000971 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100972 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100973 // The dex program counter of the first instruction of this block.
974 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100975 size_t lifetime_start_;
976 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100977 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100978
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000979 friend class HGraph;
980 friend class HInstruction;
981
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000982 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
983};
984
David Brazdilb2bd1c52015-03-25 11:17:37 +0000985// Iterates over the LoopInformation of all loops which contain 'block'
986// from the innermost to the outermost.
987class HLoopInformationOutwardIterator : public ValueObject {
988 public:
989 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
990 : current_(block.GetLoopInformation()) {}
991
992 bool Done() const { return current_ == nullptr; }
993
994 void Advance() {
995 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100996 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000997 }
998
999 HLoopInformation* Current() const {
1000 DCHECK(!Done());
1001 return current_;
1002 }
1003
1004 private:
1005 HLoopInformation* current_;
1006
1007 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1008};
1009
Alexandre Ramesef20f712015-06-09 10:29:30 +01001010#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001011 M(Above, Condition) \
1012 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001013 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001014 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001015 M(ArrayGet, Instruction) \
1016 M(ArrayLength, Instruction) \
1017 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001018 M(Below, Condition) \
1019 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001020 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001021 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001022 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001023 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001024 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001025 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001026 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001027 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001028 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001029 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001030 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001031 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001032 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001033 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001034 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001035 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001036 M(FloatConstant, Constant) \
1037 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(GreaterThan, Condition) \
1039 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001040 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001041 M(InstanceFieldGet, Instruction) \
1042 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001043 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001044 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001045 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001046 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001047 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001048 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001049 M(LessThan, Condition) \
1050 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001051 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001052 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001054 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001055 M(Local, Instruction) \
1056 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001057 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001058 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001059 M(Mul, BinaryOperation) \
1060 M(Neg, UnaryOperation) \
1061 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001062 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001063 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001064 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001065 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001066 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001067 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001068 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001069 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001070 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001071 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001072 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001073 M(Return, Instruction) \
1074 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001075 M(Shl, BinaryOperation) \
1076 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001077 M(StaticFieldGet, Instruction) \
1078 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001079 M(UnresolvedInstanceFieldGet, Instruction) \
1080 M(UnresolvedInstanceFieldSet, Instruction) \
1081 M(UnresolvedStaticFieldGet, Instruction) \
1082 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001083 M(StoreLocal, Instruction) \
1084 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001085 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001086 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001087 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001088 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001089 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001090 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001091 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001092
Alexandre Ramesef20f712015-06-09 10:29:30 +01001093#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1094
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001095#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001096#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001097#else
1098#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1099 M(Arm64IntermediateAddress, Instruction)
1100#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001101
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001102#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1103
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001104#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1105
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001106#ifndef ART_ENABLE_CODEGEN_x86
1107#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1108#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001109#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1110 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001111 M(X86LoadFromConstantTable, Instruction) \
1112 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001113#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001114
1115#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1116
1117#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1118 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1119 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1120 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001121 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001122 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001123 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1124 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1125
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001126#define FOR_EACH_INSTRUCTION(M) \
1127 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1128 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001129 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001130 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001131 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001132
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001133#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001134FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1135#undef FORWARD_DECLARATION
1136
Roland Levillainccc07a92014-09-16 14:48:16 +01001137#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001138 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1139 const char* DebugName() const OVERRIDE { return #type; } \
1140 const H##type* As##type() const OVERRIDE { return this; } \
1141 H##type* As##type() OVERRIDE { return this; } \
1142 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001143 return other->Is##type(); \
1144 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001145 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001146
David Brazdiled596192015-01-23 10:39:45 +00001147template <typename T> class HUseList;
1148
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001149template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001150class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001151 public:
David Brazdiled596192015-01-23 10:39:45 +00001152 HUseListNode* GetPrevious() const { return prev_; }
1153 HUseListNode* GetNext() const { return next_; }
1154 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001155 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001156 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001157
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001158 private:
David Brazdiled596192015-01-23 10:39:45 +00001159 HUseListNode(T user, size_t index)
1160 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1161
1162 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001163 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001164 HUseListNode<T>* prev_;
1165 HUseListNode<T>* next_;
1166
1167 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001168
1169 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1170};
1171
David Brazdiled596192015-01-23 10:39:45 +00001172template <typename T>
1173class HUseList : public ValueObject {
1174 public:
1175 HUseList() : first_(nullptr) {}
1176
1177 void Clear() {
1178 first_ = nullptr;
1179 }
1180
1181 // Adds a new entry at the beginning of the use list and returns
1182 // the newly created node.
1183 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001184 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001185 if (IsEmpty()) {
1186 first_ = new_node;
1187 } else {
1188 first_->prev_ = new_node;
1189 new_node->next_ = first_;
1190 first_ = new_node;
1191 }
1192 return new_node;
1193 }
1194
1195 HUseListNode<T>* GetFirst() const {
1196 return first_;
1197 }
1198
1199 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001200 DCHECK(node != nullptr);
1201 DCHECK(Contains(node));
1202
David Brazdiled596192015-01-23 10:39:45 +00001203 if (node->prev_ != nullptr) {
1204 node->prev_->next_ = node->next_;
1205 }
1206 if (node->next_ != nullptr) {
1207 node->next_->prev_ = node->prev_;
1208 }
1209 if (node == first_) {
1210 first_ = node->next_;
1211 }
1212 }
1213
David Brazdil1abb4192015-02-17 18:33:36 +00001214 bool Contains(const HUseListNode<T>* node) const {
1215 if (node == nullptr) {
1216 return false;
1217 }
1218 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1219 if (current == node) {
1220 return true;
1221 }
1222 }
1223 return false;
1224 }
1225
David Brazdiled596192015-01-23 10:39:45 +00001226 bool IsEmpty() const {
1227 return first_ == nullptr;
1228 }
1229
1230 bool HasOnlyOneUse() const {
1231 return first_ != nullptr && first_->next_ == nullptr;
1232 }
1233
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001234 size_t SizeSlow() const {
1235 size_t count = 0;
1236 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1237 ++count;
1238 }
1239 return count;
1240 }
1241
David Brazdiled596192015-01-23 10:39:45 +00001242 private:
1243 HUseListNode<T>* first_;
1244};
1245
1246template<typename T>
1247class HUseIterator : public ValueObject {
1248 public:
1249 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1250
1251 bool Done() const { return current_ == nullptr; }
1252
1253 void Advance() {
1254 DCHECK(!Done());
1255 current_ = current_->GetNext();
1256 }
1257
1258 HUseListNode<T>* Current() const {
1259 DCHECK(!Done());
1260 return current_;
1261 }
1262
1263 private:
1264 HUseListNode<T>* current_;
1265
1266 friend class HValue;
1267};
1268
David Brazdil1abb4192015-02-17 18:33:36 +00001269// This class is used by HEnvironment and HInstruction classes to record the
1270// instructions they use and pointers to the corresponding HUseListNodes kept
1271// by the used instructions.
1272template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001273class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001274 public:
1275 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1276 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1277
1278 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1279 : instruction_(old_record.instruction_), use_node_(use_node) {
1280 DCHECK(instruction_ != nullptr);
1281 DCHECK(use_node_ != nullptr);
1282 DCHECK(old_record.use_node_ == nullptr);
1283 }
1284
1285 HInstruction* GetInstruction() const { return instruction_; }
1286 HUseListNode<T>* GetUseNode() const { return use_node_; }
1287
1288 private:
1289 // Instruction used by the user.
1290 HInstruction* instruction_;
1291
1292 // Corresponding entry in the use list kept by 'instruction_'.
1293 HUseListNode<T>* use_node_;
1294};
1295
Aart Bik854a02b2015-07-14 16:07:00 -07001296/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001297 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001298 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001299 * For write/read dependences on fields/arrays, the dependence analysis uses
1300 * type disambiguation (e.g. a float field write cannot modify the value of an
1301 * integer field read) and the access type (e.g. a reference array write cannot
1302 * modify the value of a reference field read [although it may modify the
1303 * reference fetch prior to reading the field, which is represented by its own
1304 * write/read dependence]). The analysis makes conservative points-to
1305 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1306 * the same, and any reference read depends on any reference read without
1307 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001308 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001309 * The internal representation uses 38-bit and is described in the table below.
1310 * The first line indicates the side effect, and for field/array accesses the
1311 * second line indicates the type of the access (in the order of the
1312 * Primitive::Type enum).
1313 * The two numbered lines below indicate the bit position in the bitfield (read
1314 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001315 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001316 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1317 * +-------------+---------+---------+--------------+---------+---------+
1318 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1319 * | 3 |333333322|222222221| 1 |111111110|000000000|
1320 * | 7 |654321098|765432109| 8 |765432109|876543210|
1321 *
1322 * Note that, to ease the implementation, 'changes' bits are least significant
1323 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001324 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001325class SideEffects : public ValueObject {
1326 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001327 SideEffects() : flags_(0) {}
1328
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001329 static SideEffects None() {
1330 return SideEffects(0);
1331 }
1332
1333 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001334 return SideEffects(kAllChangeBits | kAllDependOnBits);
1335 }
1336
1337 static SideEffects AllChanges() {
1338 return SideEffects(kAllChangeBits);
1339 }
1340
1341 static SideEffects AllDependencies() {
1342 return SideEffects(kAllDependOnBits);
1343 }
1344
1345 static SideEffects AllExceptGCDependency() {
1346 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1347 }
1348
1349 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001350 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001351 }
1352
Aart Bik34c3ba92015-07-20 14:08:59 -07001353 static SideEffects AllWrites() {
1354 return SideEffects(kAllWrites);
1355 }
1356
1357 static SideEffects AllReads() {
1358 return SideEffects(kAllReads);
1359 }
1360
1361 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1362 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001363 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001364 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001365 }
1366
Aart Bik854a02b2015-07-14 16:07:00 -07001367 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1368 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001369 }
1370
Aart Bik34c3ba92015-07-20 14:08:59 -07001371 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1372 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001373 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001374 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001375 }
1376
1377 static SideEffects ArrayReadOfType(Primitive::Type type) {
1378 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1379 }
1380
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001381 static SideEffects CanTriggerGC() {
1382 return SideEffects(1ULL << kCanTriggerGCBit);
1383 }
1384
1385 static SideEffects DependsOnGC() {
1386 return SideEffects(1ULL << kDependsOnGCBit);
1387 }
1388
Aart Bik854a02b2015-07-14 16:07:00 -07001389 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001390 SideEffects Union(SideEffects other) const {
1391 return SideEffects(flags_ | other.flags_);
1392 }
1393
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001394 SideEffects Exclusion(SideEffects other) const {
1395 return SideEffects(flags_ & ~other.flags_);
1396 }
1397
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001398 void Add(SideEffects other) {
1399 flags_ |= other.flags_;
1400 }
1401
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001402 bool Includes(SideEffects other) const {
1403 return (other.flags_ & flags_) == other.flags_;
1404 }
1405
1406 bool HasSideEffects() const {
1407 return (flags_ & kAllChangeBits);
1408 }
1409
1410 bool HasDependencies() const {
1411 return (flags_ & kAllDependOnBits);
1412 }
1413
1414 // Returns true if there are no side effects or dependencies.
1415 bool DoesNothing() const {
1416 return flags_ == 0;
1417 }
1418
Aart Bik854a02b2015-07-14 16:07:00 -07001419 // Returns true if something is written.
1420 bool DoesAnyWrite() const {
1421 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001422 }
1423
Aart Bik854a02b2015-07-14 16:07:00 -07001424 // Returns true if something is read.
1425 bool DoesAnyRead() const {
1426 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001427 }
1428
Aart Bik854a02b2015-07-14 16:07:00 -07001429 // Returns true if potentially everything is written and read
1430 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001431 bool DoesAllReadWrite() const {
1432 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1433 }
1434
Aart Bik854a02b2015-07-14 16:07:00 -07001435 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001436 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001437 }
1438
1439 // Returns true if this may read something written by other.
1440 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001441 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1442 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001443 }
1444
1445 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001446 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001447 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001448 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001449 for (int s = kLastBit; s >= 0; s--) {
1450 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1451 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1452 // This is a bit for the GC side effect.
1453 if (current_bit_is_set) {
1454 flags += "GC";
1455 }
Aart Bik854a02b2015-07-14 16:07:00 -07001456 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001457 } else {
1458 // This is a bit for the array/field analysis.
1459 // The underscore character stands for the 'can trigger GC' bit.
1460 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1461 if (current_bit_is_set) {
1462 flags += kDebug[s];
1463 }
1464 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1465 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1466 flags += "|";
1467 }
1468 }
Aart Bik854a02b2015-07-14 16:07:00 -07001469 }
1470 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001471 }
1472
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001473 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001474
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001475 private:
1476 static constexpr int kFieldArrayAnalysisBits = 9;
1477
1478 static constexpr int kFieldWriteOffset = 0;
1479 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1480 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1481 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1482
1483 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1484
1485 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1486 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1487 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1488 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1489
1490 static constexpr int kLastBit = kDependsOnGCBit;
1491 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1492
1493 // Aliases.
1494
1495 static_assert(kChangeBits == kDependOnBits,
1496 "the 'change' bits should match the 'depend on' bits.");
1497
1498 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1499 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1500 static constexpr uint64_t kAllWrites =
1501 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1502 static constexpr uint64_t kAllReads =
1503 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001504
Aart Bik854a02b2015-07-14 16:07:00 -07001505 // Work around the fact that HIR aliases I/F and J/D.
1506 // TODO: remove this interceptor once HIR types are clean
1507 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1508 switch (type) {
1509 case Primitive::kPrimInt:
1510 case Primitive::kPrimFloat:
1511 return TypeFlag(Primitive::kPrimInt, offset) |
1512 TypeFlag(Primitive::kPrimFloat, offset);
1513 case Primitive::kPrimLong:
1514 case Primitive::kPrimDouble:
1515 return TypeFlag(Primitive::kPrimLong, offset) |
1516 TypeFlag(Primitive::kPrimDouble, offset);
1517 default:
1518 return TypeFlag(type, offset);
1519 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001520 }
1521
Aart Bik854a02b2015-07-14 16:07:00 -07001522 // Translates type to bit flag.
1523 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1524 CHECK_NE(type, Primitive::kPrimVoid);
1525 const uint64_t one = 1;
1526 const int shift = type; // 0-based consecutive enum
1527 DCHECK_LE(kFieldWriteOffset, shift);
1528 DCHECK_LT(shift, kArrayWriteOffset);
1529 return one << (type + offset);
1530 }
1531
1532 // Private constructor on direct flags value.
1533 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1534
1535 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001536};
1537
David Brazdiled596192015-01-23 10:39:45 +00001538// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001539class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001540 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001541 HEnvironment(ArenaAllocator* arena,
1542 size_t number_of_vregs,
1543 const DexFile& dex_file,
1544 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001545 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001546 InvokeType invoke_type,
1547 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001548 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1549 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001550 parent_(nullptr),
1551 dex_file_(dex_file),
1552 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001553 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001554 invoke_type_(invoke_type),
1555 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001556 }
1557
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001558 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001559 : HEnvironment(arena,
1560 to_copy.Size(),
1561 to_copy.GetDexFile(),
1562 to_copy.GetMethodIdx(),
1563 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001564 to_copy.GetInvokeType(),
1565 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001566
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001567 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001568 if (parent_ != nullptr) {
1569 parent_->SetAndCopyParentChain(allocator, parent);
1570 } else {
1571 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1572 parent_->CopyFrom(parent);
1573 if (parent->GetParent() != nullptr) {
1574 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1575 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001576 }
David Brazdiled596192015-01-23 10:39:45 +00001577 }
1578
Vladimir Marko71bf8092015-09-15 15:33:14 +01001579 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001580 void CopyFrom(HEnvironment* environment);
1581
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001582 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1583 // input to the loop phi instead. This is for inserting instructions that
1584 // require an environment (like HDeoptimization) in the loop pre-header.
1585 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001586
1587 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001588 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001589 }
1590
1591 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001592 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001593 }
1594
David Brazdil1abb4192015-02-17 18:33:36 +00001595 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001596
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001597 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001598
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001599 HEnvironment* GetParent() const { return parent_; }
1600
1601 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001602 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001603 }
1604
1605 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001606 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001607 }
1608
1609 uint32_t GetDexPc() const {
1610 return dex_pc_;
1611 }
1612
1613 uint32_t GetMethodIdx() const {
1614 return method_idx_;
1615 }
1616
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001617 InvokeType GetInvokeType() const {
1618 return invoke_type_;
1619 }
1620
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001621 const DexFile& GetDexFile() const {
1622 return dex_file_;
1623 }
1624
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001625 HInstruction* GetHolder() const {
1626 return holder_;
1627 }
1628
David Brazdiled596192015-01-23 10:39:45 +00001629 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001630 // Record instructions' use entries of this environment for constant-time removal.
1631 // It should only be called by HInstruction when a new environment use is added.
1632 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1633 DCHECK(env_use->GetUser() == this);
1634 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001635 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001636 }
David Brazdiled596192015-01-23 10:39:45 +00001637
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001638 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1639 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001640 HEnvironment* parent_;
1641 const DexFile& dex_file_;
1642 const uint32_t method_idx_;
1643 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001644 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001645
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001646 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001647 HInstruction* const holder_;
1648
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001649 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001650
1651 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1652};
1653
Calin Juravleacf735c2015-02-12 15:25:22 +00001654class ReferenceTypeInfo : ValueObject {
1655 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001656 typedef Handle<mirror::Class> TypeHandle;
1657
Calin Juravle2e768302015-07-28 14:41:11 +00001658 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1659 // The constructor will check that the type_handle is valid.
1660 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001661 }
1662
Calin Juravle2e768302015-07-28 14:41:11 +00001663 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1664
1665 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1666 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001667 }
1668
Calin Juravle2e768302015-07-28 14:41:11 +00001669 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1670 return IsValidHandle(type_handle_);
1671 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001672
Calin Juravleacf735c2015-02-12 15:25:22 +00001673 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001674
1675 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1676 DCHECK(IsValid());
1677 return GetTypeHandle()->IsObjectClass();
1678 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001679
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001680 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001681 DCHECK(IsValid());
1682 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001683 }
1684
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001685 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1686 DCHECK(IsValid());
1687 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1688 }
1689
Mathieu Chartier90443472015-07-16 20:32:27 -07001690 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001691 DCHECK(IsValid());
1692 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001693 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001694
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001695 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001696 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001697 return GetTypeHandle()->IsArrayClass();
1698 }
1699
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001700 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1701 DCHECK(IsValid());
1702 return GetTypeHandle()->IsPrimitiveArray();
1703 }
1704
1705 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1706 DCHECK(IsValid());
1707 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1708 }
1709
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001710 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001711 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001712 if (!IsExact()) return false;
1713 if (!IsArrayClass()) return false;
1714 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1715 }
1716
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001717 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1718 DCHECK(IsValid());
1719 if (!IsExact()) return false;
1720 if (!IsArrayClass()) return false;
1721 if (!rti.IsArrayClass()) return false;
1722 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1723 rti.GetTypeHandle()->GetComponentType());
1724 }
1725
Calin Juravleacf735c2015-02-12 15:25:22 +00001726 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1727
Mathieu Chartier90443472015-07-16 20:32:27 -07001728 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001729 DCHECK(IsValid());
1730 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001731 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1732 }
1733
1734 // Returns true if the type information provide the same amount of details.
1735 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001736 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001737 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001738 if (!IsValid() && !rti.IsValid()) {
1739 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001740 return true;
1741 }
Calin Juravle2e768302015-07-28 14:41:11 +00001742 if (!IsValid() || !rti.IsValid()) {
1743 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001744 return false;
1745 }
Calin Juravle2e768302015-07-28 14:41:11 +00001746 return IsExact() == rti.IsExact()
1747 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001748 }
1749
1750 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001751 ReferenceTypeInfo();
1752 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001753
Calin Juravleacf735c2015-02-12 15:25:22 +00001754 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001755 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001756 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001757 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001758 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001759};
1760
1761std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1762
Vladimir Markof9f64412015-09-02 14:05:49 +01001763class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001764 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001765 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001766 : previous_(nullptr),
1767 next_(nullptr),
1768 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001769 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001770 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001771 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001772 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001773 locations_(nullptr),
1774 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001775 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001776 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001777 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001778
Dave Allison20dfc792014-06-16 20:44:29 -07001779 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001780
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001781#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001782 enum InstructionKind {
1783 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1784 };
1785#undef DECLARE_KIND
1786
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001787 HInstruction* GetNext() const { return next_; }
1788 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001789
Calin Juravle77520bc2015-01-12 18:45:46 +00001790 HInstruction* GetNextDisregardingMoves() const;
1791 HInstruction* GetPreviousDisregardingMoves() const;
1792
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001793 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001794 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001795 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001796 bool IsInBlock() const { return block_ != nullptr; }
1797 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001798 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001799
Roland Levillain6b879dd2014-09-22 17:13:44 +01001800 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001801 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001802
1803 virtual void Accept(HGraphVisitor* visitor) = 0;
1804 virtual const char* DebugName() const = 0;
1805
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001806 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001807 void SetRawInputAt(size_t index, HInstruction* input) {
1808 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1809 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001810
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001811 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001812
1813 uint32_t GetDexPc() const { return dex_pc_; }
1814
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001815 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001816
Roland Levillaine161a2a2014-10-03 12:45:18 +01001817 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001818 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001819
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001820 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001821 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001822
Calin Juravle10e244f2015-01-26 18:54:32 +00001823 // Does not apply for all instructions, but having this at top level greatly
1824 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001825 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001826 virtual bool CanBeNull() const {
1827 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1828 return true;
1829 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001830
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001831 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001832 return false;
1833 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001834
Calin Juravle2e768302015-07-28 14:41:11 +00001835 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001836
Calin Juravle61d544b2015-02-23 16:46:57 +00001837 ReferenceTypeInfo GetReferenceTypeInfo() const {
1838 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1839 return reference_type_info_;
1840 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001841
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001842 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001843 DCHECK(user != nullptr);
1844 HUseListNode<HInstruction*>* use =
1845 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1846 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001847 }
1848
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001849 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001850 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001851 HUseListNode<HEnvironment*>* env_use =
1852 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1853 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001854 }
1855
David Brazdil1abb4192015-02-17 18:33:36 +00001856 void RemoveAsUserOfInput(size_t input) {
1857 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1858 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1859 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860
David Brazdil1abb4192015-02-17 18:33:36 +00001861 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1862 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001863
David Brazdiled596192015-01-23 10:39:45 +00001864 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1865 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001866 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001867 bool HasOnlyOneNonEnvironmentUse() const {
1868 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1869 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001870
Roland Levillain6c82d402014-10-13 16:10:27 +01001871 // Does this instruction strictly dominate `other_instruction`?
1872 // Returns false if this instruction and `other_instruction` are the same.
1873 // Aborts if this instruction and `other_instruction` are both phis.
1874 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001875
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001876 int GetId() const { return id_; }
1877 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001878
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001879 int GetSsaIndex() const { return ssa_index_; }
1880 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1881 bool HasSsaIndex() const { return ssa_index_ != -1; }
1882
1883 bool HasEnvironment() const { return environment_ != nullptr; }
1884 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001885 // Set the `environment_` field. Raw because this method does not
1886 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001887 void SetRawEnvironment(HEnvironment* environment) {
1888 DCHECK(environment_ == nullptr);
1889 DCHECK_EQ(environment->GetHolder(), this);
1890 environment_ = environment;
1891 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001892
1893 // Set the environment of this instruction, copying it from `environment`. While
1894 // copying, the uses lists are being updated.
1895 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001896 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001897 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001898 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001899 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001900 if (environment->GetParent() != nullptr) {
1901 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1902 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001903 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001904
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001905 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1906 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001907 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001908 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001909 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001910 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001911 if (environment->GetParent() != nullptr) {
1912 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1913 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001914 }
1915
Nicolas Geoffray39468442014-09-02 15:17:15 +01001916 // Returns the number of entries in the environment. Typically, that is the
1917 // number of dex registers in a method. It could be more in case of inlining.
1918 size_t EnvironmentSize() const;
1919
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001920 LocationSummary* GetLocations() const { return locations_; }
1921 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001922
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001923 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001924 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001925
Alexandre Rames188d4312015-04-09 18:30:21 +01001926 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1927 // uses of this instruction by `other` are *not* updated.
1928 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1929 ReplaceWith(other);
1930 other->ReplaceInput(this, use_index);
1931 }
1932
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001933 // Move `this` instruction before `cursor`.
1934 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001935
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001936#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001937 bool Is##type() const { return (As##type() != nullptr); } \
1938 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001939 virtual H##type* As##type() { return nullptr; }
1940
1941 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1942#undef INSTRUCTION_TYPE_CHECK
1943
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001944 // Returns whether the instruction can be moved within the graph.
1945 virtual bool CanBeMoved() const { return false; }
1946
1947 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001948 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001949 return false;
1950 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001951
1952 // Returns whether any data encoded in the two instructions is equal.
1953 // This method does not look at the inputs. Both instructions must be
1954 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001955 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001956 return false;
1957 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001958
1959 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001960 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001961 // 2) Their inputs are identical.
1962 bool Equals(HInstruction* other) const;
1963
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001964 virtual InstructionKind GetKind() const = 0;
1965
1966 virtual size_t ComputeHashCode() const {
1967 size_t result = GetKind();
1968 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1969 result = (result * 31) + InputAt(i)->GetId();
1970 }
1971 return result;
1972 }
1973
1974 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001975 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001976
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001977 size_t GetLifetimePosition() const { return lifetime_position_; }
1978 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1979 LiveInterval* GetLiveInterval() const { return live_interval_; }
1980 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1981 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1982
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001983 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1984
1985 // Returns whether the code generation of the instruction will require to have access
1986 // to the current method. Such instructions are:
1987 // (1): Instructions that require an environment, as calling the runtime requires
1988 // to walk the stack and have the current method stored at a specific stack address.
1989 // (2): Object literals like classes and strings, that are loaded from the dex cache
1990 // fields of the current method.
1991 bool NeedsCurrentMethod() const {
1992 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1993 }
1994
Vladimir Markodc151b22015-10-15 18:02:30 +01001995 // Returns whether the code generation of the instruction will require to have access
1996 // to the dex cache of the current method's declaring class via the current method.
1997 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001998
Mark Mendellc4701932015-04-10 13:18:51 -04001999 // Does this instruction have any use in an environment before
2000 // control flow hits 'other'?
2001 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2002
2003 // Remove all references to environment uses of this instruction.
2004 // The caller must ensure that this is safe to do.
2005 void RemoveEnvironmentUsers();
2006
David Brazdil1abb4192015-02-17 18:33:36 +00002007 protected:
2008 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2009 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2010
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002011 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002012 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2013
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002014 HInstruction* previous_;
2015 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002016 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002017 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002019 // An instruction gets an id when it is added to the graph.
2020 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002021 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002022 int id_;
2023
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002024 // When doing liveness analysis, instructions that have uses get an SSA index.
2025 int ssa_index_;
2026
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002027 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002028 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002029
2030 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002031 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002032
Nicolas Geoffray39468442014-09-02 15:17:15 +01002033 // The environment associated with this instruction. Not null if the instruction
2034 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002035 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002036
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002037 // Set by the code generator.
2038 LocationSummary* locations_;
2039
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002040 // Set by the liveness analysis.
2041 LiveInterval* live_interval_;
2042
2043 // Set by the liveness analysis, this is the position in a linear
2044 // order of blocks where this instruction's live interval start.
2045 size_t lifetime_position_;
2046
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002047 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002048
Calin Juravleacf735c2015-02-12 15:25:22 +00002049 // TODO: for primitive types this should be marked as invalid.
2050 ReferenceTypeInfo reference_type_info_;
2051
David Brazdil1abb4192015-02-17 18:33:36 +00002052 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002053 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002054 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002055 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002056 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002057
2058 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2059};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002060std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002061
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002062class HInputIterator : public ValueObject {
2063 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002064 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002065
2066 bool Done() const { return index_ == instruction_->InputCount(); }
2067 HInstruction* Current() const { return instruction_->InputAt(index_); }
2068 void Advance() { index_++; }
2069
2070 private:
2071 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002072 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002073
2074 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2075};
2076
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002077class HInstructionIterator : public ValueObject {
2078 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002079 explicit HInstructionIterator(const HInstructionList& instructions)
2080 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002081 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002082 }
2083
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002084 bool Done() const { return instruction_ == nullptr; }
2085 HInstruction* Current() const { return instruction_; }
2086 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002087 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002088 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002089 }
2090
2091 private:
2092 HInstruction* instruction_;
2093 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002094
2095 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002096};
2097
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002098class HBackwardInstructionIterator : public ValueObject {
2099 public:
2100 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2101 : instruction_(instructions.last_instruction_) {
2102 next_ = Done() ? nullptr : instruction_->GetPrevious();
2103 }
2104
2105 bool Done() const { return instruction_ == nullptr; }
2106 HInstruction* Current() const { return instruction_; }
2107 void Advance() {
2108 instruction_ = next_;
2109 next_ = Done() ? nullptr : instruction_->GetPrevious();
2110 }
2111
2112 private:
2113 HInstruction* instruction_;
2114 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002115
2116 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002117};
2118
Vladimir Markof9f64412015-09-02 14:05:49 +01002119template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002120class HTemplateInstruction: public HInstruction {
2121 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002122 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002123 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002124 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002125
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002126 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002128 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002129 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2130 DCHECK_LT(i, N);
2131 return inputs_[i];
2132 }
David Brazdil1abb4192015-02-17 18:33:36 +00002133
2134 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002135 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002136 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002137 }
2138
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002139 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002140 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002141
2142 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002143};
2144
Vladimir Markof9f64412015-09-02 14:05:49 +01002145// HTemplateInstruction specialization for N=0.
2146template<>
2147class HTemplateInstruction<0>: public HInstruction {
2148 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002149 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002150 : HInstruction(side_effects, dex_pc) {}
2151
Vladimir Markof9f64412015-09-02 14:05:49 +01002152 virtual ~HTemplateInstruction() {}
2153
2154 size_t InputCount() const OVERRIDE { return 0; }
2155
2156 protected:
2157 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2158 LOG(FATAL) << "Unreachable";
2159 UNREACHABLE();
2160 }
2161
2162 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2163 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2164 LOG(FATAL) << "Unreachable";
2165 UNREACHABLE();
2166 }
2167
2168 private:
2169 friend class SsaBuilder;
2170};
2171
Dave Allison20dfc792014-06-16 20:44:29 -07002172template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002173class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002174 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002175 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002176 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002177 virtual ~HExpression() {}
2178
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002179 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002180
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002181 protected:
2182 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002183};
2184
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2186// instruction that branches to the exit block.
2187class HReturnVoid : public HTemplateInstruction<0> {
2188 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002189 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2190 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002191
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002192 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002193
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002194 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002195
2196 private:
2197 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2198};
2199
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002200// Represents dex's RETURN opcodes. A HReturn is a control flow
2201// instruction that branches to the exit block.
2202class HReturn : public HTemplateInstruction<1> {
2203 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002204 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2205 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002206 SetRawInputAt(0, value);
2207 }
2208
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002209 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002210
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002211 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002212
2213 private:
2214 DISALLOW_COPY_AND_ASSIGN(HReturn);
2215};
2216
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002217// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002218// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002219// exit block.
2220class HExit : public HTemplateInstruction<0> {
2221 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002222 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002223
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002224 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002226 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002227
2228 private:
2229 DISALLOW_COPY_AND_ASSIGN(HExit);
2230};
2231
2232// Jumps from one block to another.
2233class HGoto : public HTemplateInstruction<0> {
2234 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002235 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002236
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002237 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002238
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002239 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002240 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002241 }
2242
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002243 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002244
2245 private:
2246 DISALLOW_COPY_AND_ASSIGN(HGoto);
2247};
2248
Roland Levillain9867bc72015-08-05 10:21:34 +01002249class HConstant : public HExpression<0> {
2250 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002251 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2252 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002253
2254 bool CanBeMoved() const OVERRIDE { return true; }
2255
2256 virtual bool IsMinusOne() const { return false; }
2257 virtual bool IsZero() const { return false; }
2258 virtual bool IsOne() const { return false; }
2259
David Brazdil77a48ae2015-09-15 12:34:04 +00002260 virtual uint64_t GetValueAsUint64() const = 0;
2261
Roland Levillain9867bc72015-08-05 10:21:34 +01002262 DECLARE_INSTRUCTION(Constant);
2263
2264 private:
2265 DISALLOW_COPY_AND_ASSIGN(HConstant);
2266};
2267
2268class HNullConstant : public HConstant {
2269 public:
2270 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2271 return true;
2272 }
2273
David Brazdil77a48ae2015-09-15 12:34:04 +00002274 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2275
Roland Levillain9867bc72015-08-05 10:21:34 +01002276 size_t ComputeHashCode() const OVERRIDE { return 0; }
2277
2278 DECLARE_INSTRUCTION(NullConstant);
2279
2280 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002281 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002282
2283 friend class HGraph;
2284 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2285};
2286
2287// Constants of the type int. Those can be from Dex instructions, or
2288// synthesized (for example with the if-eqz instruction).
2289class HIntConstant : public HConstant {
2290 public:
2291 int32_t GetValue() const { return value_; }
2292
David Brazdil9f389d42015-10-01 14:32:56 +01002293 uint64_t GetValueAsUint64() const OVERRIDE {
2294 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2295 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002296
Roland Levillain9867bc72015-08-05 10:21:34 +01002297 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2298 DCHECK(other->IsIntConstant());
2299 return other->AsIntConstant()->value_ == value_;
2300 }
2301
2302 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2303
2304 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2305 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2306 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2307
2308 DECLARE_INSTRUCTION(IntConstant);
2309
2310 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002311 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2312 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2313 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2314 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002315
2316 const int32_t value_;
2317
2318 friend class HGraph;
2319 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2320 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2321 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2322};
2323
2324class HLongConstant : public HConstant {
2325 public:
2326 int64_t GetValue() const { return value_; }
2327
David Brazdil77a48ae2015-09-15 12:34:04 +00002328 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2329
Roland Levillain9867bc72015-08-05 10:21:34 +01002330 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2331 DCHECK(other->IsLongConstant());
2332 return other->AsLongConstant()->value_ == value_;
2333 }
2334
2335 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2336
2337 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2338 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2339 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2340
2341 DECLARE_INSTRUCTION(LongConstant);
2342
2343 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002344 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2345 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002346
2347 const int64_t value_;
2348
2349 friend class HGraph;
2350 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2351};
Dave Allison20dfc792014-06-16 20:44:29 -07002352
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002353// Conditional branch. A block ending with an HIf instruction must have
2354// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002355class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002356 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002357 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2358 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002359 SetRawInputAt(0, input);
2360 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002361
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002362 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002363
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002364 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002365 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002366 }
2367
2368 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002369 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002370 }
2371
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002372 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002373
2374 private:
2375 DISALLOW_COPY_AND_ASSIGN(HIf);
2376};
2377
David Brazdilfc6a86a2015-06-26 10:33:45 +00002378
2379// Abstract instruction which marks the beginning and/or end of a try block and
2380// links it to the respective exception handlers. Behaves the same as a Goto in
2381// non-exceptional control flow.
2382// Normal-flow successor is stored at index zero, exception handlers under
2383// higher indices in no particular order.
2384class HTryBoundary : public HTemplateInstruction<0> {
2385 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002386 enum BoundaryKind {
2387 kEntry,
2388 kExit,
2389 };
2390
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002391 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2392 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002393
2394 bool IsControlFlow() const OVERRIDE { return true; }
2395
2396 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002397 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002398
David Brazdild26a4112015-11-10 11:07:31 +00002399 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2400 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2401 }
2402
David Brazdilfc6a86a2015-06-26 10:33:45 +00002403 // Returns whether `handler` is among its exception handlers (non-zero index
2404 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002405 bool HasExceptionHandler(const HBasicBlock& handler) const {
2406 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002407 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002408 }
2409
2410 // If not present already, adds `handler` to its block's list of exception
2411 // handlers.
2412 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002413 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002414 GetBlock()->AddSuccessor(handler);
2415 }
2416 }
2417
David Brazdil56e1acc2015-06-30 15:41:36 +01002418 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002419
David Brazdilffee3d32015-07-06 11:48:53 +01002420 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2421
David Brazdilfc6a86a2015-06-26 10:33:45 +00002422 DECLARE_INSTRUCTION(TryBoundary);
2423
2424 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002425 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002426
2427 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2428};
2429
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002430// Deoptimize to interpreter, upon checking a condition.
2431class HDeoptimize : public HTemplateInstruction<1> {
2432 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002433 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2434 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002435 SetRawInputAt(0, cond);
2436 }
2437
2438 bool NeedsEnvironment() const OVERRIDE { return true; }
2439 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002440
2441 DECLARE_INSTRUCTION(Deoptimize);
2442
2443 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002444 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2445};
2446
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002447// Represents the ArtMethod that was passed as a first argument to
2448// the method. It is used by instructions that depend on it, like
2449// instructions that work with the dex cache.
2450class HCurrentMethod : public HExpression<0> {
2451 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002452 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2453 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002454
2455 DECLARE_INSTRUCTION(CurrentMethod);
2456
2457 private:
2458 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2459};
2460
Mark Mendellfe57faa2015-09-18 09:26:15 -04002461// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2462// have one successor for each entry in the switch table, and the final successor
2463// will be the block containing the next Dex opcode.
2464class HPackedSwitch : public HTemplateInstruction<1> {
2465 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002466 HPackedSwitch(int32_t start_value,
2467 uint32_t num_entries,
2468 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002469 uint32_t dex_pc = kNoDexPc)
2470 : HTemplateInstruction(SideEffects::None(), dex_pc),
2471 start_value_(start_value),
2472 num_entries_(num_entries) {
2473 SetRawInputAt(0, input);
2474 }
2475
2476 bool IsControlFlow() const OVERRIDE { return true; }
2477
2478 int32_t GetStartValue() const { return start_value_; }
2479
Vladimir Marko211c2112015-09-24 16:52:33 +01002480 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002481
2482 HBasicBlock* GetDefaultBlock() const {
2483 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002484 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002485 }
2486 DECLARE_INSTRUCTION(PackedSwitch);
2487
2488 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002489 const int32_t start_value_;
2490 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002491
2492 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2493};
2494
Roland Levillain88cb1752014-10-20 16:36:47 +01002495class HUnaryOperation : public HExpression<1> {
2496 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002497 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2498 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002499 SetRawInputAt(0, input);
2500 }
2501
2502 HInstruction* GetInput() const { return InputAt(0); }
2503 Primitive::Type GetResultType() const { return GetType(); }
2504
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002505 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002506 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002507 return true;
2508 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002509
Roland Levillain9240d6a2014-10-20 16:47:04 +01002510 // Try to statically evaluate `operation` and return a HConstant
2511 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002512 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002513 HConstant* TryStaticEvaluation() const;
2514
2515 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002516 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2517 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002518
Roland Levillain88cb1752014-10-20 16:36:47 +01002519 DECLARE_INSTRUCTION(UnaryOperation);
2520
2521 private:
2522 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2523};
2524
Dave Allison20dfc792014-06-16 20:44:29 -07002525class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002526 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002527 HBinaryOperation(Primitive::Type result_type,
2528 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002529 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002530 SideEffects side_effects = SideEffects::None(),
2531 uint32_t dex_pc = kNoDexPc)
2532 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002533 SetRawInputAt(0, left);
2534 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002535 }
2536
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002537 HInstruction* GetLeft() const { return InputAt(0); }
2538 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002539 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002540
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002541 virtual bool IsCommutative() const { return false; }
2542
2543 // Put constant on the right.
2544 // Returns whether order is changed.
2545 bool OrderInputsWithConstantOnTheRight() {
2546 HInstruction* left = InputAt(0);
2547 HInstruction* right = InputAt(1);
2548 if (left->IsConstant() && !right->IsConstant()) {
2549 ReplaceInput(right, 0);
2550 ReplaceInput(left, 1);
2551 return true;
2552 }
2553 return false;
2554 }
2555
2556 // Order inputs by instruction id, but favor constant on the right side.
2557 // This helps GVN for commutative ops.
2558 void OrderInputs() {
2559 DCHECK(IsCommutative());
2560 HInstruction* left = InputAt(0);
2561 HInstruction* right = InputAt(1);
2562 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2563 return;
2564 }
2565 if (OrderInputsWithConstantOnTheRight()) {
2566 return;
2567 }
2568 // Order according to instruction id.
2569 if (left->GetId() > right->GetId()) {
2570 ReplaceInput(right, 0);
2571 ReplaceInput(left, 1);
2572 }
2573 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002574
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002575 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002576 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002577 return true;
2578 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002579
Roland Levillain9240d6a2014-10-20 16:47:04 +01002580 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002581 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002582 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002583 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002584
2585 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002586 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2587 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2588 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2589 HLongConstant* y ATTRIBUTE_UNUSED) const {
2590 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2591 return nullptr;
2592 }
2593 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2594 HIntConstant* y ATTRIBUTE_UNUSED) const {
2595 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2596 return nullptr;
2597 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002598 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2599 HNullConstant* y ATTRIBUTE_UNUSED) const {
2600 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2601 return nullptr;
2602 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002603
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002604 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002605 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002606 HConstant* GetConstantRight() const;
2607
2608 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002609 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002610 HInstruction* GetLeastConstantLeft() const;
2611
Roland Levillainccc07a92014-09-16 14:48:16 +01002612 DECLARE_INSTRUCTION(BinaryOperation);
2613
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002614 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002615 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2616};
2617
Mark Mendellc4701932015-04-10 13:18:51 -04002618// The comparison bias applies for floating point operations and indicates how NaN
2619// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002620enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002621 kNoBias, // bias is not applicable (i.e. for long operation)
2622 kGtBias, // return 1 for NaN comparisons
2623 kLtBias, // return -1 for NaN comparisons
2624};
2625
Dave Allison20dfc792014-06-16 20:44:29 -07002626class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002627 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002628 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2629 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002630 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002631 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002632
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002633 bool NeedsMaterialization() const { return needs_materialization_; }
2634 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002635
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002636 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002637 // `instruction`, and disregard moves in between.
2638 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002639
Dave Allison20dfc792014-06-16 20:44:29 -07002640 DECLARE_INSTRUCTION(Condition);
2641
2642 virtual IfCondition GetCondition() const = 0;
2643
Mark Mendellc4701932015-04-10 13:18:51 -04002644 virtual IfCondition GetOppositeCondition() const = 0;
2645
Roland Levillain4fa13f62015-07-06 18:11:54 +01002646 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002647
2648 void SetBias(ComparisonBias bias) { bias_ = bias; }
2649
2650 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2651 return bias_ == other->AsCondition()->bias_;
2652 }
2653
Roland Levillain4fa13f62015-07-06 18:11:54 +01002654 bool IsFPConditionTrueIfNaN() const {
2655 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2656 IfCondition if_cond = GetCondition();
2657 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2658 }
2659
2660 bool IsFPConditionFalseIfNaN() const {
2661 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2662 IfCondition if_cond = GetCondition();
2663 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2664 }
2665
Dave Allison20dfc792014-06-16 20:44:29 -07002666 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002667 // For register allocation purposes, returns whether this instruction needs to be
2668 // materialized (that is, not just be in the processor flags).
2669 bool needs_materialization_;
2670
Mark Mendellc4701932015-04-10 13:18:51 -04002671 // Needed if we merge a HCompare into a HCondition.
2672 ComparisonBias bias_;
2673
Dave Allison20dfc792014-06-16 20:44:29 -07002674 DISALLOW_COPY_AND_ASSIGN(HCondition);
2675};
2676
2677// Instruction to check if two inputs are equal to each other.
2678class HEqual : public HCondition {
2679 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002680 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2681 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002682
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002683 bool IsCommutative() const OVERRIDE { return true; }
2684
Roland Levillain9867bc72015-08-05 10:21:34 +01002685 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002686 return GetBlock()->GetGraph()->GetIntConstant(
2687 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002688 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002689 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002690 return GetBlock()->GetGraph()->GetIntConstant(
2691 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002692 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002693 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2694 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
2695 return GetBlock()->GetGraph()->GetConstant(GetType(), 1);
2696 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002697
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002698 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002699
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002700 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002701 return kCondEQ;
2702 }
2703
Mark Mendellc4701932015-04-10 13:18:51 -04002704 IfCondition GetOppositeCondition() const OVERRIDE {
2705 return kCondNE;
2706 }
2707
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002708 private:
Aart Bike9f37602015-10-09 11:15:55 -07002709 template <typename T> bool Compute(T x, T y) const { return x == y; }
2710
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002711 DISALLOW_COPY_AND_ASSIGN(HEqual);
2712};
2713
Dave Allison20dfc792014-06-16 20:44:29 -07002714class HNotEqual : public HCondition {
2715 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002716 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2717 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002718
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002719 bool IsCommutative() const OVERRIDE { return true; }
2720
Roland Levillain9867bc72015-08-05 10:21:34 +01002721 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002722 return GetBlock()->GetGraph()->GetIntConstant(
2723 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002724 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002725 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002726 return GetBlock()->GetGraph()->GetIntConstant(
2727 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002728 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002729 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2730 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
2731 return GetBlock()->GetGraph()->GetConstant(GetType(), 0);
2732 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002733
Dave Allison20dfc792014-06-16 20:44:29 -07002734 DECLARE_INSTRUCTION(NotEqual);
2735
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002736 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002737 return kCondNE;
2738 }
2739
Mark Mendellc4701932015-04-10 13:18:51 -04002740 IfCondition GetOppositeCondition() const OVERRIDE {
2741 return kCondEQ;
2742 }
2743
Dave Allison20dfc792014-06-16 20:44:29 -07002744 private:
Aart Bike9f37602015-10-09 11:15:55 -07002745 template <typename T> bool Compute(T x, T y) const { return x != y; }
2746
Dave Allison20dfc792014-06-16 20:44:29 -07002747 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2748};
2749
2750class HLessThan : public HCondition {
2751 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002752 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2753 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002754
Roland Levillain9867bc72015-08-05 10:21:34 +01002755 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 return GetBlock()->GetGraph()->GetIntConstant(
2757 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002758 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002759 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002760 return GetBlock()->GetGraph()->GetIntConstant(
2761 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002762 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002763
Dave Allison20dfc792014-06-16 20:44:29 -07002764 DECLARE_INSTRUCTION(LessThan);
2765
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002766 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002767 return kCondLT;
2768 }
2769
Mark Mendellc4701932015-04-10 13:18:51 -04002770 IfCondition GetOppositeCondition() const OVERRIDE {
2771 return kCondGE;
2772 }
2773
Dave Allison20dfc792014-06-16 20:44:29 -07002774 private:
Aart Bike9f37602015-10-09 11:15:55 -07002775 template <typename T> bool Compute(T x, T y) const { return x < y; }
2776
Dave Allison20dfc792014-06-16 20:44:29 -07002777 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2778};
2779
2780class HLessThanOrEqual : public HCondition {
2781 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002782 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2783 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002784
Roland Levillain9867bc72015-08-05 10:21:34 +01002785 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002786 return GetBlock()->GetGraph()->GetIntConstant(
2787 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002788 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002789 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002790 return GetBlock()->GetGraph()->GetIntConstant(
2791 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002792 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002793
Dave Allison20dfc792014-06-16 20:44:29 -07002794 DECLARE_INSTRUCTION(LessThanOrEqual);
2795
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002796 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002797 return kCondLE;
2798 }
2799
Mark Mendellc4701932015-04-10 13:18:51 -04002800 IfCondition GetOppositeCondition() const OVERRIDE {
2801 return kCondGT;
2802 }
2803
Dave Allison20dfc792014-06-16 20:44:29 -07002804 private:
Aart Bike9f37602015-10-09 11:15:55 -07002805 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2806
Dave Allison20dfc792014-06-16 20:44:29 -07002807 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2808};
2809
2810class HGreaterThan : public HCondition {
2811 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002812 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2813 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002814
Roland Levillain9867bc72015-08-05 10:21:34 +01002815 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 return GetBlock()->GetGraph()->GetIntConstant(
2817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002818 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002819 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002820 return GetBlock()->GetGraph()->GetIntConstant(
2821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002822 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002823
Dave Allison20dfc792014-06-16 20:44:29 -07002824 DECLARE_INSTRUCTION(GreaterThan);
2825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002826 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002827 return kCondGT;
2828 }
2829
Mark Mendellc4701932015-04-10 13:18:51 -04002830 IfCondition GetOppositeCondition() const OVERRIDE {
2831 return kCondLE;
2832 }
2833
Dave Allison20dfc792014-06-16 20:44:29 -07002834 private:
Aart Bike9f37602015-10-09 11:15:55 -07002835 template <typename T> bool Compute(T x, T y) const { return x > y; }
2836
Dave Allison20dfc792014-06-16 20:44:29 -07002837 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2838};
2839
2840class HGreaterThanOrEqual : public HCondition {
2841 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002842 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2843 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002844
Roland Levillain9867bc72015-08-05 10:21:34 +01002845 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002846 return GetBlock()->GetGraph()->GetIntConstant(
2847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002848 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002849 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002850 return GetBlock()->GetGraph()->GetIntConstant(
2851 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002852 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002853
Dave Allison20dfc792014-06-16 20:44:29 -07002854 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2855
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002856 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002857 return kCondGE;
2858 }
2859
Mark Mendellc4701932015-04-10 13:18:51 -04002860 IfCondition GetOppositeCondition() const OVERRIDE {
2861 return kCondLT;
2862 }
2863
Dave Allison20dfc792014-06-16 20:44:29 -07002864 private:
Aart Bike9f37602015-10-09 11:15:55 -07002865 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2866
Dave Allison20dfc792014-06-16 20:44:29 -07002867 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2868};
2869
Aart Bike9f37602015-10-09 11:15:55 -07002870class HBelow : public HCondition {
2871 public:
2872 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2873 : HCondition(first, second, dex_pc) {}
2874
2875 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2876 return GetBlock()->GetGraph()->GetIntConstant(
2877 Compute(static_cast<uint32_t>(x->GetValue()),
2878 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2879 }
2880 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2881 return GetBlock()->GetGraph()->GetIntConstant(
2882 Compute(static_cast<uint64_t>(x->GetValue()),
2883 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2884 }
2885
2886 DECLARE_INSTRUCTION(Below);
2887
2888 IfCondition GetCondition() const OVERRIDE {
2889 return kCondB;
2890 }
2891
2892 IfCondition GetOppositeCondition() const OVERRIDE {
2893 return kCondAE;
2894 }
2895
2896 private:
2897 template <typename T> bool Compute(T x, T y) const { return x < y; }
2898
2899 DISALLOW_COPY_AND_ASSIGN(HBelow);
2900};
2901
2902class HBelowOrEqual : public HCondition {
2903 public:
2904 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2905 : HCondition(first, second, dex_pc) {}
2906
2907 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2908 return GetBlock()->GetGraph()->GetIntConstant(
2909 Compute(static_cast<uint32_t>(x->GetValue()),
2910 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2911 }
2912 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2913 return GetBlock()->GetGraph()->GetIntConstant(
2914 Compute(static_cast<uint64_t>(x->GetValue()),
2915 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2916 }
2917
2918 DECLARE_INSTRUCTION(BelowOrEqual);
2919
2920 IfCondition GetCondition() const OVERRIDE {
2921 return kCondBE;
2922 }
2923
2924 IfCondition GetOppositeCondition() const OVERRIDE {
2925 return kCondA;
2926 }
2927
2928 private:
2929 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2930
2931 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2932};
2933
2934class HAbove : public HCondition {
2935 public:
2936 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2937 : HCondition(first, second, dex_pc) {}
2938
2939 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2940 return GetBlock()->GetGraph()->GetIntConstant(
2941 Compute(static_cast<uint32_t>(x->GetValue()),
2942 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2943 }
2944 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2945 return GetBlock()->GetGraph()->GetIntConstant(
2946 Compute(static_cast<uint64_t>(x->GetValue()),
2947 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2948 }
2949
2950 DECLARE_INSTRUCTION(Above);
2951
2952 IfCondition GetCondition() const OVERRIDE {
2953 return kCondA;
2954 }
2955
2956 IfCondition GetOppositeCondition() const OVERRIDE {
2957 return kCondBE;
2958 }
2959
2960 private:
2961 template <typename T> bool Compute(T x, T y) const { return x > y; }
2962
2963 DISALLOW_COPY_AND_ASSIGN(HAbove);
2964};
2965
2966class HAboveOrEqual : public HCondition {
2967 public:
2968 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2969 : HCondition(first, second, dex_pc) {}
2970
2971 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2972 return GetBlock()->GetGraph()->GetIntConstant(
2973 Compute(static_cast<uint32_t>(x->GetValue()),
2974 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2975 }
2976 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2977 return GetBlock()->GetGraph()->GetIntConstant(
2978 Compute(static_cast<uint64_t>(x->GetValue()),
2979 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2980 }
2981
2982 DECLARE_INSTRUCTION(AboveOrEqual);
2983
2984 IfCondition GetCondition() const OVERRIDE {
2985 return kCondAE;
2986 }
2987
2988 IfCondition GetOppositeCondition() const OVERRIDE {
2989 return kCondB;
2990 }
2991
2992 private:
2993 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2994
2995 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
2996};
Dave Allison20dfc792014-06-16 20:44:29 -07002997
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002998// Instruction to check how two inputs compare to each other.
2999// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3000class HCompare : public HBinaryOperation {
3001 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003002 HCompare(Primitive::Type type,
3003 HInstruction* first,
3004 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003005 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003006 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003007 : HBinaryOperation(Primitive::kPrimInt,
3008 first,
3009 second,
3010 SideEffectsForArchRuntimeCalls(type),
3011 dex_pc),
3012 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003013 DCHECK_EQ(type, first->GetType());
3014 DCHECK_EQ(type, second->GetType());
3015 }
3016
Roland Levillain9867bc72015-08-05 10:21:34 +01003017 template <typename T>
3018 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003019
Roland Levillain9867bc72015-08-05 10:21:34 +01003020 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003021 return GetBlock()->GetGraph()->GetIntConstant(
3022 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003023 }
3024 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003025 return GetBlock()->GetGraph()->GetIntConstant(
3026 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003027 }
3028
Calin Juravleddb7df22014-11-25 20:56:51 +00003029 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3030 return bias_ == other->AsCompare()->bias_;
3031 }
3032
Mark Mendellc4701932015-04-10 13:18:51 -04003033 ComparisonBias GetBias() const { return bias_; }
3034
Roland Levillain4fa13f62015-07-06 18:11:54 +01003035 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003036
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003037
3038 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3039 // MIPS64 uses a runtime call for FP comparisons.
3040 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3041 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003042
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003043 DECLARE_INSTRUCTION(Compare);
3044
3045 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003046 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003047
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003048 DISALLOW_COPY_AND_ASSIGN(HCompare);
3049};
3050
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003051// A local in the graph. Corresponds to a Dex register.
3052class HLocal : public HTemplateInstruction<0> {
3053 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003054 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003055 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003056
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003057 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003058
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003059 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003060
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003061 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003062 // The Dex register number.
3063 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003064
3065 DISALLOW_COPY_AND_ASSIGN(HLocal);
3066};
3067
3068// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003069class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003070 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003071 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3072 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003073 SetRawInputAt(0, local);
3074 }
3075
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003076 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3077
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003078 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003079
3080 private:
3081 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3082};
3083
3084// Store a value in a given local. This instruction has two inputs: the value
3085// and the local.
3086class HStoreLocal : public HTemplateInstruction<2> {
3087 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003088 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3089 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003090 SetRawInputAt(0, local);
3091 SetRawInputAt(1, value);
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(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003097
3098 private:
3099 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3100};
3101
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003102class HFloatConstant : public HConstant {
3103 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003104 float GetValue() const { return value_; }
3105
David Brazdil77a48ae2015-09-15 12:34:04 +00003106 uint64_t GetValueAsUint64() const OVERRIDE {
3107 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3108 }
3109
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003110 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003111 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003112 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003113 }
3114
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003115 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003116
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003117 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003118 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003119 }
3120 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003121 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003122 }
3123 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003124 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3125 }
3126 bool IsNaN() const {
3127 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003128 }
3129
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003130 DECLARE_INSTRUCTION(FloatConstant);
3131
3132 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003133 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3134 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3135 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3136 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003137
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003138 const float value_;
3139
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003140 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003141 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003142 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003143 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3144};
3145
3146class HDoubleConstant : public HConstant {
3147 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003148 double GetValue() const { return value_; }
3149
David Brazdil77a48ae2015-09-15 12:34:04 +00003150 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3151
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003152 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003153 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003154 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003155 }
3156
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003157 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003158
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003159 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003160 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003161 }
3162 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003163 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003164 }
3165 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003166 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3167 }
3168 bool IsNaN() const {
3169 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003170 }
3171
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003172 DECLARE_INSTRUCTION(DoubleConstant);
3173
3174 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003175 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3176 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3177 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3178 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003179
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003180 const double value_;
3181
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003182 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003183 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003184 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003185 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3186};
3187
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003188enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003189#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003190#include "intrinsics_list.h"
3191 kNone,
3192 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3193#undef INTRINSICS_LIST
3194#undef OPTIMIZING_INTRINSICS
3195};
3196std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3197
Agi Csaki05f20562015-08-19 14:58:14 -07003198enum IntrinsicNeedsEnvironmentOrCache {
3199 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3200 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003201};
3202
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003203class HInvoke : public HInstruction {
3204 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003205 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003206
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003207 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003208
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003209 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003210 SetRawInputAt(index, argument);
3211 }
3212
Roland Levillain3e3d7332015-04-28 11:00:54 +01003213 // Return the number of arguments. This number can be lower than
3214 // the number of inputs returned by InputCount(), as some invoke
3215 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3216 // inputs at the end of their list of inputs.
3217 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3218
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003219 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003220
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003221
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003222 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003223 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003224
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003225 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3226
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003227 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003228 return intrinsic_;
3229 }
3230
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003231 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003232
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003233 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003234 return GetEnvironment()->GetParent() != nullptr;
3235 }
3236
3237 bool CanThrow() const OVERRIDE { return true; }
3238
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003239 uint32_t* GetIntrinsicOptimizations() {
3240 return &intrinsic_optimizations_;
3241 }
3242
3243 const uint32_t* GetIntrinsicOptimizations() const {
3244 return &intrinsic_optimizations_;
3245 }
3246
3247 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3248
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003249 DECLARE_INSTRUCTION(Invoke);
3250
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003251 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003252 HInvoke(ArenaAllocator* arena,
3253 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003254 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003255 Primitive::Type return_type,
3256 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003257 uint32_t dex_method_index,
3258 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003259 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003260 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003261 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003262 inputs_(number_of_arguments + number_of_other_inputs,
3263 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003264 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003265 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003266 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003267 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003268 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003269 }
3270
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003271 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003272 return inputs_[index];
3273 }
3274
David Brazdil1abb4192015-02-17 18:33:36 +00003275 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003276 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003277 }
3278
Roland Levillain3e3d7332015-04-28 11:00:54 +01003279 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003280 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003281 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003282 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003283 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003284 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003285
3286 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3287 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003288
3289 private:
3290 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3291};
3292
Calin Juravle175dc732015-08-25 15:42:32 +01003293class HInvokeUnresolved : public HInvoke {
3294 public:
3295 HInvokeUnresolved(ArenaAllocator* arena,
3296 uint32_t number_of_arguments,
3297 Primitive::Type return_type,
3298 uint32_t dex_pc,
3299 uint32_t dex_method_index,
3300 InvokeType invoke_type)
3301 : HInvoke(arena,
3302 number_of_arguments,
3303 0u /* number_of_other_inputs */,
3304 return_type,
3305 dex_pc,
3306 dex_method_index,
3307 invoke_type) {
3308 }
3309
3310 DECLARE_INSTRUCTION(InvokeUnresolved);
3311
3312 private:
3313 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3314};
3315
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003316class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003317 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003318 // Requirements of this method call regarding the class
3319 // initialization (clinit) check of its declaring class.
3320 enum class ClinitCheckRequirement {
3321 kNone, // Class already initialized.
3322 kExplicit, // Static call having explicit clinit check as last input.
3323 kImplicit, // Static call implicitly requiring a clinit check.
3324 };
3325
Vladimir Marko58155012015-08-19 12:49:41 +00003326 // Determines how to load the target ArtMethod*.
3327 enum class MethodLoadKind {
3328 // Use a String init ArtMethod* loaded from Thread entrypoints.
3329 kStringInit,
3330
3331 // Use the method's own ArtMethod* loaded by the register allocator.
3332 kRecursive,
3333
3334 // Use ArtMethod* at a known address, embed the direct address in the code.
3335 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3336 kDirectAddress,
3337
3338 // Use ArtMethod* at an address that will be known at link time, embed the direct
3339 // address in the code. If the image is relocatable, emit .patch_oat entry.
3340 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3341 // the image relocatable or not.
3342 kDirectAddressWithFixup,
3343
3344 // Load from resoved methods array in the dex cache using a PC-relative load.
3345 // Used when we need to use the dex cache, for example for invoke-static that
3346 // may cause class initialization (the entry may point to a resolution method),
3347 // and we know that we can access the dex cache arrays using a PC-relative load.
3348 kDexCachePcRelative,
3349
3350 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3351 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3352 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3353 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3354 kDexCacheViaMethod,
3355 };
3356
3357 // Determines the location of the code pointer.
3358 enum class CodePtrLocation {
3359 // Recursive call, use local PC-relative call instruction.
3360 kCallSelf,
3361
3362 // Use PC-relative call instruction patched at link time.
3363 // Used for calls within an oat file, boot->boot or app->app.
3364 kCallPCRelative,
3365
3366 // Call to a known target address, embed the direct address in code.
3367 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3368 kCallDirect,
3369
3370 // Call to a target address that will be known at link time, embed the direct
3371 // address in code. If the image is relocatable, emit .patch_oat entry.
3372 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3373 // the image relocatable or not.
3374 kCallDirectWithFixup,
3375
3376 // Use code pointer from the ArtMethod*.
3377 // Used when we don't know the target code. This is also the last-resort-kind used when
3378 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3379 kCallArtMethod,
3380 };
3381
3382 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003383 MethodLoadKind method_load_kind;
3384 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003385 // The method load data holds
3386 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3387 // Note that there are multiple string init methods, each having its own offset.
3388 // - the method address for kDirectAddress
3389 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003390 uint64_t method_load_data;
3391 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003392 };
3393
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003394 HInvokeStaticOrDirect(ArenaAllocator* arena,
3395 uint32_t number_of_arguments,
3396 Primitive::Type return_type,
3397 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003398 uint32_t method_index,
3399 MethodReference target_method,
3400 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003401 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003402 InvokeType invoke_type,
3403 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003404 : HInvoke(arena,
3405 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003406 // There is potentially one extra argument for the HCurrentMethod node, and
3407 // potentially one other if the clinit check is explicit, and potentially
3408 // one other if the method is a string factory.
3409 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3410 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3411 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003412 return_type,
3413 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003414 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003415 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003416 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003417 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003418 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003419 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003420
Vladimir Markodc151b22015-10-15 18:02:30 +01003421 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003422 bool had_current_method_input = HasCurrentMethodInput();
3423 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3424
3425 // Using the current method is the default and once we find a better
3426 // method load kind, we should not go back to using the current method.
3427 DCHECK(had_current_method_input || !needs_current_method_input);
3428
3429 if (had_current_method_input && !needs_current_method_input) {
3430 DCHECK_EQ(InputAt(GetCurrentMethodInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3431 RemoveInputAt(GetCurrentMethodInputIndex());
3432 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003433 dispatch_info_ = dispatch_info;
3434 }
3435
Vladimir Markob554b5a2015-11-06 12:57:55 +00003436 void RemoveInputAt(size_t index);
3437
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003438 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003439 // We access the method via the dex cache so we can't do an implicit null check.
3440 // TODO: for intrinsics we can generate implicit null checks.
3441 return false;
3442 }
3443
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003444 bool CanBeNull() const OVERRIDE {
3445 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3446 }
3447
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003448 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003449 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3450 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3451 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003452 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003453 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003454 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003455 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003456 bool HasPcRelDexCache() const {
3457 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3458 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003459 bool HasCurrentMethodInput() const {
3460 // This function can be called only after the invoke has been fully initialized by the builder.
3461 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
3462 DCHECK(InputAt(GetCurrentMethodInputIndex())->IsCurrentMethod());
3463 return true;
3464 } else {
3465 DCHECK(InputCount() == GetCurrentMethodInputIndex() ||
3466 !InputAt(GetCurrentMethodInputIndex())->IsCurrentMethod());
3467 return false;
3468 }
3469 }
Vladimir Marko58155012015-08-19 12:49:41 +00003470 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3471 MethodReference GetTargetMethod() const { return target_method_; }
3472
3473 int32_t GetStringInitOffset() const {
3474 DCHECK(IsStringInit());
3475 return dispatch_info_.method_load_data;
3476 }
3477
3478 uint64_t GetMethodAddress() const {
3479 DCHECK(HasMethodAddress());
3480 return dispatch_info_.method_load_data;
3481 }
3482
3483 uint32_t GetDexCacheArrayOffset() const {
3484 DCHECK(HasPcRelDexCache());
3485 return dispatch_info_.method_load_data;
3486 }
3487
3488 uint64_t GetDirectCodePtr() const {
3489 DCHECK(HasDirectCodePtr());
3490 return dispatch_info_.direct_code_ptr;
3491 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003492
Calin Juravle68ad6492015-08-18 17:08:12 +01003493 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3494
Roland Levillain4c0eb422015-04-24 16:43:49 +01003495 // Is this instruction a call to a static method?
3496 bool IsStatic() const {
3497 return GetInvokeType() == kStatic;
3498 }
3499
Roland Levillain3e3d7332015-04-28 11:00:54 +01003500 // Remove the art::HLoadClass instruction set as last input by
3501 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3502 // the initial art::HClinitCheck instruction (only relevant for
3503 // static calls with explicit clinit check).
3504 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003505 DCHECK(IsStaticWithExplicitClinitCheck());
3506 size_t last_input_index = InputCount() - 1;
3507 HInstruction* last_input = InputAt(last_input_index);
3508 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003509 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003510 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003511 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003512 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3513 DCHECK(IsStaticWithImplicitClinitCheck());
3514 }
3515
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003516 bool IsStringFactoryFor(HFakeString* str) const {
3517 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003518 DCHECK(!HasCurrentMethodInput());
3519 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003520 return InputAt(InputCount() - 1)->AsFakeString() == str;
3521 }
3522
3523 void RemoveFakeStringArgumentAsLastInput() {
3524 DCHECK(IsStringInit());
3525 size_t last_input_index = InputCount() - 1;
3526 HInstruction* last_input = InputAt(last_input_index);
3527 DCHECK(last_input != nullptr);
3528 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3529 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003530 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003531 }
3532
Roland Levillain4c0eb422015-04-24 16:43:49 +01003533 // Is this a call to a static method whose declaring class has an
3534 // explicit intialization check in the graph?
3535 bool IsStaticWithExplicitClinitCheck() const {
3536 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3537 }
3538
3539 // Is this a call to a static method whose declaring class has an
3540 // implicit intialization check requirement?
3541 bool IsStaticWithImplicitClinitCheck() const {
3542 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3543 }
3544
Vladimir Markob554b5a2015-11-06 12:57:55 +00003545 // Does this method load kind need the current method as an input?
3546 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3547 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3548 }
3549
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003550 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003551
Roland Levillain4c0eb422015-04-24 16:43:49 +01003552 protected:
3553 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3554 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3555 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3556 HInstruction* input = input_record.GetInstruction();
3557 // `input` is the last input of a static invoke marked as having
3558 // an explicit clinit check. It must either be:
3559 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3560 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3561 DCHECK(input != nullptr);
3562 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3563 }
3564 return input_record;
3565 }
3566
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003567 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003568 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003569 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003570 // The target method may refer to different dex file or method index than the original
3571 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3572 // in derived class to increase visibility.
3573 MethodReference target_method_;
3574 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003575
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003576 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003577};
3578
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003579class HInvokeVirtual : public HInvoke {
3580 public:
3581 HInvokeVirtual(ArenaAllocator* arena,
3582 uint32_t number_of_arguments,
3583 Primitive::Type return_type,
3584 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003585 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003586 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003587 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003588 vtable_index_(vtable_index) {}
3589
Calin Juravle641547a2015-04-21 22:08:51 +01003590 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003591 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003592 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003593 }
3594
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003595 uint32_t GetVTableIndex() const { return vtable_index_; }
3596
3597 DECLARE_INSTRUCTION(InvokeVirtual);
3598
3599 private:
3600 const uint32_t vtable_index_;
3601
3602 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3603};
3604
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003605class HInvokeInterface : public HInvoke {
3606 public:
3607 HInvokeInterface(ArenaAllocator* arena,
3608 uint32_t number_of_arguments,
3609 Primitive::Type return_type,
3610 uint32_t dex_pc,
3611 uint32_t dex_method_index,
3612 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003613 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003614 imt_index_(imt_index) {}
3615
Calin Juravle641547a2015-04-21 22:08:51 +01003616 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003617 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003618 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003619 }
3620
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003621 uint32_t GetImtIndex() const { return imt_index_; }
3622 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3623
3624 DECLARE_INSTRUCTION(InvokeInterface);
3625
3626 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003627 const uint32_t imt_index_;
3628
3629 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3630};
3631
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003632class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003633 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003634 HNewInstance(HCurrentMethod* current_method,
3635 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003636 uint16_t type_index,
3637 const DexFile& dex_file,
3638 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003639 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003640 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003641 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003642 entrypoint_(entrypoint) {
3643 SetRawInputAt(0, current_method);
3644 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003645
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003646 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003647 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003648
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003649 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003650 bool NeedsEnvironment() const OVERRIDE { return true; }
Andreas Gampe55d02cf2015-10-29 02:59:50 +00003651 // It may throw when called on:
3652 // - interfaces
3653 // - abstract/innaccessible/unknown classes
3654 // TODO: optimize when possible.
3655 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003656
Calin Juravle10e244f2015-01-26 18:54:32 +00003657 bool CanBeNull() const OVERRIDE { return false; }
3658
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003659 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3660
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003661 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003662
3663 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003664 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003665 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003666 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003667
3668 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3669};
3670
Roland Levillain88cb1752014-10-20 16:36:47 +01003671class HNeg : public HUnaryOperation {
3672 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003673 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3674 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003675
Roland Levillain9867bc72015-08-05 10:21:34 +01003676 template <typename T> T Compute(T x) const { return -x; }
3677
3678 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003679 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003680 }
3681 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003682 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003683 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003684
Roland Levillain88cb1752014-10-20 16:36:47 +01003685 DECLARE_INSTRUCTION(Neg);
3686
3687 private:
3688 DISALLOW_COPY_AND_ASSIGN(HNeg);
3689};
3690
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003691class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003692 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003693 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003694 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003695 uint32_t dex_pc,
3696 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003697 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003698 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003699 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003700 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003701 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003702 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003703 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003704 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003705 }
3706
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003707 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003708 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003709
3710 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003711 bool NeedsEnvironment() const OVERRIDE { return true; }
3712
Mingyao Yang0c365e62015-03-31 15:09:29 -07003713 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3714 bool CanThrow() const OVERRIDE { return true; }
3715
Calin Juravle10e244f2015-01-26 18:54:32 +00003716 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003717
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003718 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3719
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003720 DECLARE_INSTRUCTION(NewArray);
3721
3722 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003723 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003724 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003725 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003726
3727 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3728};
3729
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003730class HAdd : public HBinaryOperation {
3731 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003732 HAdd(Primitive::Type result_type,
3733 HInstruction* left,
3734 HInstruction* right,
3735 uint32_t dex_pc = kNoDexPc)
3736 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003737
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003738 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003739
Roland Levillain9867bc72015-08-05 10:21:34 +01003740 template <typename T> T Compute(T x, T y) const { return x + y; }
3741
3742 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003743 return GetBlock()->GetGraph()->GetIntConstant(
3744 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003745 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003746 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003747 return GetBlock()->GetGraph()->GetLongConstant(
3748 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003749 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003750
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003751 DECLARE_INSTRUCTION(Add);
3752
3753 private:
3754 DISALLOW_COPY_AND_ASSIGN(HAdd);
3755};
3756
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003757class HSub : public HBinaryOperation {
3758 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003759 HSub(Primitive::Type result_type,
3760 HInstruction* left,
3761 HInstruction* right,
3762 uint32_t dex_pc = kNoDexPc)
3763 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003764
Roland Levillain9867bc72015-08-05 10:21:34 +01003765 template <typename T> T Compute(T x, T y) const { return x - y; }
3766
3767 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003768 return GetBlock()->GetGraph()->GetIntConstant(
3769 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003770 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003771 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003772 return GetBlock()->GetGraph()->GetLongConstant(
3773 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003774 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003775
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003776 DECLARE_INSTRUCTION(Sub);
3777
3778 private:
3779 DISALLOW_COPY_AND_ASSIGN(HSub);
3780};
3781
Calin Juravle34bacdf2014-10-07 20:23:36 +01003782class HMul : public HBinaryOperation {
3783 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003784 HMul(Primitive::Type result_type,
3785 HInstruction* left,
3786 HInstruction* right,
3787 uint32_t dex_pc = kNoDexPc)
3788 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003789
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003790 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003791
Roland Levillain9867bc72015-08-05 10:21:34 +01003792 template <typename T> T Compute(T x, T y) const { return x * y; }
3793
3794 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003795 return GetBlock()->GetGraph()->GetIntConstant(
3796 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003797 }
3798 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003799 return GetBlock()->GetGraph()->GetLongConstant(
3800 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003801 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003802
3803 DECLARE_INSTRUCTION(Mul);
3804
3805 private:
3806 DISALLOW_COPY_AND_ASSIGN(HMul);
3807};
3808
Calin Juravle7c4954d2014-10-28 16:57:40 +00003809class HDiv : public HBinaryOperation {
3810 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003811 HDiv(Primitive::Type result_type,
3812 HInstruction* left,
3813 HInstruction* right,
3814 uint32_t dex_pc)
3815 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003816
Roland Levillain9867bc72015-08-05 10:21:34 +01003817 template <typename T>
3818 T Compute(T x, T y) const {
3819 // Our graph structure ensures we never have 0 for `y` during
3820 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003821 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003822 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003823 return (y == -1) ? -x : x / y;
3824 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003825
Roland Levillain9867bc72015-08-05 10:21:34 +01003826 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003827 return GetBlock()->GetGraph()->GetIntConstant(
3828 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003829 }
3830 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003831 return GetBlock()->GetGraph()->GetLongConstant(
3832 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003833 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003834
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003835 static SideEffects SideEffectsForArchRuntimeCalls() {
3836 // The generated code can use a runtime call.
3837 return SideEffects::CanTriggerGC();
3838 }
3839
Calin Juravle7c4954d2014-10-28 16:57:40 +00003840 DECLARE_INSTRUCTION(Div);
3841
3842 private:
3843 DISALLOW_COPY_AND_ASSIGN(HDiv);
3844};
3845
Calin Juravlebacfec32014-11-14 15:54:36 +00003846class HRem : public HBinaryOperation {
3847 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003848 HRem(Primitive::Type result_type,
3849 HInstruction* left,
3850 HInstruction* right,
3851 uint32_t dex_pc)
3852 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003853
Roland Levillain9867bc72015-08-05 10:21:34 +01003854 template <typename T>
3855 T Compute(T x, T y) const {
3856 // Our graph structure ensures we never have 0 for `y` during
3857 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003858 DCHECK_NE(y, 0);
3859 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3860 return (y == -1) ? 0 : x % y;
3861 }
3862
Roland Levillain9867bc72015-08-05 10:21:34 +01003863 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003864 return GetBlock()->GetGraph()->GetIntConstant(
3865 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003866 }
3867 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003868 return GetBlock()->GetGraph()->GetLongConstant(
3869 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003870 }
3871
Calin Juravlebacfec32014-11-14 15:54:36 +00003872
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003873 static SideEffects SideEffectsForArchRuntimeCalls() {
3874 return SideEffects::CanTriggerGC();
3875 }
3876
Calin Juravlebacfec32014-11-14 15:54:36 +00003877 DECLARE_INSTRUCTION(Rem);
3878
3879 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003880 DISALLOW_COPY_AND_ASSIGN(HRem);
3881};
3882
Calin Juravled0d48522014-11-04 16:40:20 +00003883class HDivZeroCheck : public HExpression<1> {
3884 public:
3885 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003886 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003887 SetRawInputAt(0, value);
3888 }
3889
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003890 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3891
Calin Juravled0d48522014-11-04 16:40:20 +00003892 bool CanBeMoved() const OVERRIDE { return true; }
3893
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003894 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003895 return true;
3896 }
3897
3898 bool NeedsEnvironment() const OVERRIDE { return true; }
3899 bool CanThrow() const OVERRIDE { return true; }
3900
Calin Juravled0d48522014-11-04 16:40:20 +00003901 DECLARE_INSTRUCTION(DivZeroCheck);
3902
3903 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003904 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3905};
3906
Calin Juravle9aec02f2014-11-18 23:06:35 +00003907class HShl : public HBinaryOperation {
3908 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003909 HShl(Primitive::Type result_type,
3910 HInstruction* left,
3911 HInstruction* right,
3912 uint32_t dex_pc = kNoDexPc)
3913 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003914
Roland Levillain9867bc72015-08-05 10:21:34 +01003915 template <typename T, typename U, typename V>
3916 T Compute(T x, U y, V max_shift_value) const {
3917 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3918 "V is not the unsigned integer type corresponding to T");
3919 return x << (y & max_shift_value);
3920 }
3921
3922 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3923 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003924 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003925 }
3926 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3927 // case is handled as `x << static_cast<int>(y)`.
3928 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3929 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003930 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003931 }
3932 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3933 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003934 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003935 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003936
3937 DECLARE_INSTRUCTION(Shl);
3938
3939 private:
3940 DISALLOW_COPY_AND_ASSIGN(HShl);
3941};
3942
3943class HShr : public HBinaryOperation {
3944 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003945 HShr(Primitive::Type result_type,
3946 HInstruction* left,
3947 HInstruction* right,
3948 uint32_t dex_pc = kNoDexPc)
3949 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003950
Roland Levillain9867bc72015-08-05 10:21:34 +01003951 template <typename T, typename U, typename V>
3952 T Compute(T x, U y, V max_shift_value) const {
3953 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3954 "V is not the unsigned integer type corresponding to T");
3955 return x >> (y & max_shift_value);
3956 }
3957
3958 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3959 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003960 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003961 }
3962 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3963 // case is handled as `x >> static_cast<int>(y)`.
3964 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3965 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003966 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003967 }
3968 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3969 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003970 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003971 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003972
3973 DECLARE_INSTRUCTION(Shr);
3974
3975 private:
3976 DISALLOW_COPY_AND_ASSIGN(HShr);
3977};
3978
3979class HUShr : public HBinaryOperation {
3980 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003981 HUShr(Primitive::Type result_type,
3982 HInstruction* left,
3983 HInstruction* right,
3984 uint32_t dex_pc = kNoDexPc)
3985 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003986
Roland Levillain9867bc72015-08-05 10:21:34 +01003987 template <typename T, typename U, typename V>
3988 T Compute(T x, U y, V max_shift_value) const {
3989 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3990 "V is not the unsigned integer type corresponding to T");
3991 V ux = static_cast<V>(x);
3992 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003993 }
3994
Roland Levillain9867bc72015-08-05 10:21:34 +01003995 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3996 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003997 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003998 }
3999 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4000 // case is handled as `x >>> static_cast<int>(y)`.
4001 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4002 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004003 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004004 }
4005 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4006 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004007 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004008 }
4009
4010 DECLARE_INSTRUCTION(UShr);
4011
4012 private:
4013 DISALLOW_COPY_AND_ASSIGN(HUShr);
4014};
4015
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004016class HAnd : public HBinaryOperation {
4017 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004018 HAnd(Primitive::Type result_type,
4019 HInstruction* left,
4020 HInstruction* right,
4021 uint32_t dex_pc = kNoDexPc)
4022 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004023
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004024 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004025
Roland Levillain9867bc72015-08-05 10:21:34 +01004026 template <typename T, typename U>
4027 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4028
4029 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004030 return GetBlock()->GetGraph()->GetIntConstant(
4031 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004032 }
4033 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004034 return GetBlock()->GetGraph()->GetLongConstant(
4035 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004036 }
4037 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004038 return GetBlock()->GetGraph()->GetLongConstant(
4039 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004040 }
4041 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004042 return GetBlock()->GetGraph()->GetLongConstant(
4043 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004044 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004045
4046 DECLARE_INSTRUCTION(And);
4047
4048 private:
4049 DISALLOW_COPY_AND_ASSIGN(HAnd);
4050};
4051
4052class HOr : public HBinaryOperation {
4053 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004054 HOr(Primitive::Type result_type,
4055 HInstruction* left,
4056 HInstruction* right,
4057 uint32_t dex_pc = kNoDexPc)
4058 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004059
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004060 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004061
Roland Levillain9867bc72015-08-05 10:21:34 +01004062 template <typename T, typename U>
4063 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4064
4065 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004066 return GetBlock()->GetGraph()->GetIntConstant(
4067 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004068 }
4069 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 return GetBlock()->GetGraph()->GetLongConstant(
4071 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004072 }
4073 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004074 return GetBlock()->GetGraph()->GetLongConstant(
4075 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004076 }
4077 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004078 return GetBlock()->GetGraph()->GetLongConstant(
4079 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004080 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004081
4082 DECLARE_INSTRUCTION(Or);
4083
4084 private:
4085 DISALLOW_COPY_AND_ASSIGN(HOr);
4086};
4087
4088class HXor : public HBinaryOperation {
4089 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004090 HXor(Primitive::Type result_type,
4091 HInstruction* left,
4092 HInstruction* right,
4093 uint32_t dex_pc = kNoDexPc)
4094 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004095
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004096 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004097
Roland Levillain9867bc72015-08-05 10:21:34 +01004098 template <typename T, typename U>
4099 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4100
4101 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004102 return GetBlock()->GetGraph()->GetIntConstant(
4103 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004104 }
4105 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004106 return GetBlock()->GetGraph()->GetLongConstant(
4107 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004108 }
4109 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004110 return GetBlock()->GetGraph()->GetLongConstant(
4111 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004112 }
4113 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004114 return GetBlock()->GetGraph()->GetLongConstant(
4115 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004116 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004117
4118 DECLARE_INSTRUCTION(Xor);
4119
4120 private:
4121 DISALLOW_COPY_AND_ASSIGN(HXor);
4122};
4123
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004124// The value of a parameter in this method. Its location depends on
4125// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004126class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004127 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004128 HParameterValue(const DexFile& dex_file,
4129 uint16_t type_index,
4130 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004131 Primitive::Type parameter_type,
4132 bool is_this = false)
4133 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004134 dex_file_(dex_file),
4135 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004136 index_(index),
4137 is_this_(is_this),
4138 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004139
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004140 const DexFile& GetDexFile() const { return dex_file_; }
4141 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004142 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004143 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004144
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004145 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4146 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004147
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004148 DECLARE_INSTRUCTION(ParameterValue);
4149
4150 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004151 const DexFile& dex_file_;
4152 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004153 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004154 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004155 const uint8_t index_;
4156
Calin Juravle10e244f2015-01-26 18:54:32 +00004157 // Whether or not the parameter value corresponds to 'this' argument.
4158 const bool is_this_;
4159
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004160 bool can_be_null_;
4161
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004162 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4163};
4164
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004165class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004166 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004167 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4168 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004169
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004170 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004171 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004172 return true;
4173 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004174
Roland Levillain9867bc72015-08-05 10:21:34 +01004175 template <typename T> T Compute(T x) const { return ~x; }
4176
4177 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004178 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004179 }
4180 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004181 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004182 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004183
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004184 DECLARE_INSTRUCTION(Not);
4185
4186 private:
4187 DISALLOW_COPY_AND_ASSIGN(HNot);
4188};
4189
David Brazdil66d126e2015-04-03 16:02:44 +01004190class HBooleanNot : public HUnaryOperation {
4191 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004192 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4193 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004194
4195 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004196 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004197 return true;
4198 }
4199
Roland Levillain9867bc72015-08-05 10:21:34 +01004200 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004201 DCHECK(IsUint<1>(x));
4202 return !x;
4203 }
4204
Roland Levillain9867bc72015-08-05 10:21:34 +01004205 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004206 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004207 }
4208 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4209 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004210 UNREACHABLE();
4211 }
4212
4213 DECLARE_INSTRUCTION(BooleanNot);
4214
4215 private:
4216 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4217};
4218
Roland Levillaindff1f282014-11-05 14:15:05 +00004219class HTypeConversion : public HExpression<1> {
4220 public:
4221 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004222 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004223 : HExpression(result_type,
4224 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4225 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004226 SetRawInputAt(0, input);
4227 DCHECK_NE(input->GetType(), result_type);
4228 }
4229
4230 HInstruction* GetInput() const { return InputAt(0); }
4231 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4232 Primitive::Type GetResultType() const { return GetType(); }
4233
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004234 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004235 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004236
Roland Levillaindff1f282014-11-05 14:15:05 +00004237 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004238 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004239
Mark Mendelle82549b2015-05-06 10:55:34 -04004240 // Try to statically evaluate the conversion and return a HConstant
4241 // containing the result. If the input cannot be converted, return nullptr.
4242 HConstant* TryStaticEvaluation() const;
4243
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004244 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4245 Primitive::Type result_type) {
4246 // Some architectures may not require the 'GC' side effects, but at this point
4247 // in the compilation process we do not know what architecture we will
4248 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004249 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4250 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004251 return SideEffects::CanTriggerGC();
4252 }
4253 return SideEffects::None();
4254 }
4255
Roland Levillaindff1f282014-11-05 14:15:05 +00004256 DECLARE_INSTRUCTION(TypeConversion);
4257
4258 private:
4259 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4260};
4261
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004262static constexpr uint32_t kNoRegNumber = -1;
4263
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004264class HPhi : public HInstruction {
4265 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004266 HPhi(ArenaAllocator* arena,
4267 uint32_t reg_number,
4268 size_t number_of_inputs,
4269 Primitive::Type type,
4270 uint32_t dex_pc = kNoDexPc)
4271 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004272 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004273 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004274 type_(type),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004275 is_live_(false),
Calin Juravle10e244f2015-01-26 18:54:32 +00004276 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004277 }
4278
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004279 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4280 static Primitive::Type ToPhiType(Primitive::Type type) {
4281 switch (type) {
4282 case Primitive::kPrimBoolean:
4283 case Primitive::kPrimByte:
4284 case Primitive::kPrimShort:
4285 case Primitive::kPrimChar:
4286 return Primitive::kPrimInt;
4287 default:
4288 return type;
4289 }
4290 }
4291
David Brazdilffee3d32015-07-06 11:48:53 +01004292 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4293
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004294 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004295
4296 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004297 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004298
Calin Juravle10e244f2015-01-26 18:54:32 +00004299 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004300 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004301
Calin Juravle10e244f2015-01-26 18:54:32 +00004302 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4303 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4304
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004305 uint32_t GetRegNumber() const { return reg_number_; }
4306
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004307 void SetDead() { is_live_ = false; }
4308 void SetLive() { is_live_ = true; }
4309 bool IsDead() const { return !is_live_; }
4310 bool IsLive() const { return is_live_; }
4311
David Brazdil77a48ae2015-09-15 12:34:04 +00004312 bool IsVRegEquivalentOf(HInstruction* other) const {
4313 return other != nullptr
4314 && other->IsPhi()
4315 && other->AsPhi()->GetBlock() == GetBlock()
4316 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4317 }
4318
Calin Juravlea4f88312015-04-16 12:57:19 +01004319 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4320 // An equivalent phi is a phi having the same dex register and type.
4321 // It assumes that phis with the same dex register are adjacent.
4322 HPhi* GetNextEquivalentPhiWithSameType() {
4323 HInstruction* next = GetNext();
4324 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4325 if (next->GetType() == GetType()) {
4326 return next->AsPhi();
4327 }
4328 next = next->GetNext();
4329 }
4330 return nullptr;
4331 }
4332
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004333 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004334
David Brazdil1abb4192015-02-17 18:33:36 +00004335 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004336 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004337 return inputs_[index];
4338 }
David Brazdil1abb4192015-02-17 18:33:36 +00004339
4340 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004341 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004342 }
4343
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004344 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004345 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004346 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004347 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004348 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004349 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004350
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004351 DISALLOW_COPY_AND_ASSIGN(HPhi);
4352};
4353
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004354class HNullCheck : public HExpression<1> {
4355 public:
4356 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004357 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004358 SetRawInputAt(0, value);
4359 }
4360
Calin Juravle10e244f2015-01-26 18:54:32 +00004361 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004362 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004363 return true;
4364 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004365
Calin Juravle10e244f2015-01-26 18:54:32 +00004366 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004367
Calin Juravle10e244f2015-01-26 18:54:32 +00004368 bool CanThrow() const OVERRIDE { return true; }
4369
4370 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004371
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004372
4373 DECLARE_INSTRUCTION(NullCheck);
4374
4375 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004376 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4377};
4378
4379class FieldInfo : public ValueObject {
4380 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004381 FieldInfo(MemberOffset field_offset,
4382 Primitive::Type field_type,
4383 bool is_volatile,
4384 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004385 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004386 const DexFile& dex_file,
4387 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004388 : field_offset_(field_offset),
4389 field_type_(field_type),
4390 is_volatile_(is_volatile),
4391 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004392 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004393 dex_file_(dex_file),
4394 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004395
4396 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004397 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004398 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004399 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004400 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004401 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004402 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004403
4404 private:
4405 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004406 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004407 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004408 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004409 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004410 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004411 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004412};
4413
4414class HInstanceFieldGet : public HExpression<1> {
4415 public:
4416 HInstanceFieldGet(HInstruction* value,
4417 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004418 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004419 bool is_volatile,
4420 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004421 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004422 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004423 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004424 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004425 : HExpression(field_type,
4426 SideEffects::FieldReadOfType(field_type, is_volatile),
4427 dex_pc),
4428 field_info_(field_offset,
4429 field_type,
4430 is_volatile,
4431 field_idx,
4432 declaring_class_def_index,
4433 dex_file,
4434 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004435 SetRawInputAt(0, value);
4436 }
4437
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004438 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004439
4440 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4441 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4442 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004443 }
4444
Calin Juravle641547a2015-04-21 22:08:51 +01004445 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4446 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004447 }
4448
4449 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004450 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4451 }
4452
Calin Juravle52c48962014-12-16 17:02:57 +00004453 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004454 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004455 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004456 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004457
4458 DECLARE_INSTRUCTION(InstanceFieldGet);
4459
4460 private:
4461 const FieldInfo field_info_;
4462
4463 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4464};
4465
4466class HInstanceFieldSet : public HTemplateInstruction<2> {
4467 public:
4468 HInstanceFieldSet(HInstruction* object,
4469 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004470 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004471 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004472 bool is_volatile,
4473 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004474 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004475 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004476 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004477 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004478 : HTemplateInstruction(SideEffects::FieldWriteOfType(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 Geoffray07276db2015-05-18 14:22:09 +01004487 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004488 SetRawInputAt(0, object);
4489 SetRawInputAt(1, value);
4490 }
4491
Calin Juravle641547a2015-04-21 22:08:51 +01004492 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4493 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004494 }
4495
Calin Juravle52c48962014-12-16 17:02:57 +00004496 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004497 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004498 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004499 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004500 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004501 bool GetValueCanBeNull() const { return value_can_be_null_; }
4502 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004503
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004504 DECLARE_INSTRUCTION(InstanceFieldSet);
4505
4506 private:
4507 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004508 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004509
4510 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4511};
4512
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004513class HArrayGet : public HExpression<2> {
4514 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004515 HArrayGet(HInstruction* array,
4516 HInstruction* index,
4517 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004518 uint32_t dex_pc,
4519 SideEffects additional_side_effects = SideEffects::None())
4520 : HExpression(type,
4521 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004522 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004523 SetRawInputAt(0, array);
4524 SetRawInputAt(1, index);
4525 }
4526
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004527 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004528 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004529 return true;
4530 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004531 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004532 // TODO: We can be smarter here.
4533 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4534 // which generates the implicit null check. There are cases when these can be removed
4535 // to produce better code. If we ever add optimizations to do so we should allow an
4536 // implicit check here (as long as the address falls in the first page).
4537 return false;
4538 }
4539
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004540 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004541
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004542 HInstruction* GetArray() const { return InputAt(0); }
4543 HInstruction* GetIndex() const { return InputAt(1); }
4544
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004545 DECLARE_INSTRUCTION(ArrayGet);
4546
4547 private:
4548 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4549};
4550
4551class HArraySet : public HTemplateInstruction<3> {
4552 public:
4553 HArraySet(HInstruction* array,
4554 HInstruction* index,
4555 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004556 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004557 uint32_t dex_pc,
4558 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004559 : HTemplateInstruction(
4560 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004561 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4562 additional_side_effects),
4563 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004564 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004565 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004566 value_can_be_null_(true),
4567 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004568 SetRawInputAt(0, array);
4569 SetRawInputAt(1, index);
4570 SetRawInputAt(2, value);
4571 }
4572
Calin Juravle77520bc2015-01-12 18:45:46 +00004573 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004574 // We currently always call a runtime method to catch array store
4575 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004576 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004577 }
4578
Mingyao Yang81014cb2015-06-02 03:16:27 -07004579 // Can throw ArrayStoreException.
4580 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4581
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004582 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004583 // TODO: Same as for ArrayGet.
4584 return false;
4585 }
4586
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004587 void ClearNeedsTypeCheck() {
4588 needs_type_check_ = false;
4589 }
4590
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004591 void ClearValueCanBeNull() {
4592 value_can_be_null_ = false;
4593 }
4594
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004595 void SetStaticTypeOfArrayIsObjectArray() {
4596 static_type_of_array_is_object_array_ = true;
4597 }
4598
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004599 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004600 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004601 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004602
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004603 HInstruction* GetArray() const { return InputAt(0); }
4604 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004605 HInstruction* GetValue() const { return InputAt(2); }
4606
4607 Primitive::Type GetComponentType() const {
4608 // The Dex format does not type floating point index operations. Since the
4609 // `expected_component_type_` is set during building and can therefore not
4610 // be correct, we also check what is the value type. If it is a floating
4611 // point type, we must use that type.
4612 Primitive::Type value_type = GetValue()->GetType();
4613 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4614 ? value_type
4615 : expected_component_type_;
4616 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004617
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004618 Primitive::Type GetRawExpectedComponentType() const {
4619 return expected_component_type_;
4620 }
4621
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004622 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4623 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4624 }
4625
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004626 DECLARE_INSTRUCTION(ArraySet);
4627
4628 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004629 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004630 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004631 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004632 // Cached information for the reference_type_info_ so that codegen
4633 // does not need to inspect the static type.
4634 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004635
4636 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4637};
4638
4639class HArrayLength : public HExpression<1> {
4640 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004641 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004642 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004643 // Note that arrays do not change length, so the instruction does not
4644 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004645 SetRawInputAt(0, array);
4646 }
4647
Calin Juravle77520bc2015-01-12 18:45:46 +00004648 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004649 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004650 return true;
4651 }
Calin Juravle641547a2015-04-21 22:08:51 +01004652 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4653 return obj == InputAt(0);
4654 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004655
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004656 DECLARE_INSTRUCTION(ArrayLength);
4657
4658 private:
4659 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4660};
4661
4662class HBoundsCheck : public HExpression<2> {
4663 public:
4664 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004665 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004666 DCHECK(index->GetType() == Primitive::kPrimInt);
4667 SetRawInputAt(0, index);
4668 SetRawInputAt(1, length);
4669 }
4670
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004671 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004672 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004673 return true;
4674 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004675
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004676 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004677
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004678 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004679
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004680 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004681
4682 DECLARE_INSTRUCTION(BoundsCheck);
4683
4684 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004685 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4686};
4687
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004688/**
4689 * Some DEX instructions are folded into multiple HInstructions that need
4690 * to stay live until the last HInstruction. This class
4691 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004692 * HInstruction stays live. `index` represents the stack location index of the
4693 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004694 */
4695class HTemporary : public HTemplateInstruction<0> {
4696 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004697 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4698 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004699
4700 size_t GetIndex() const { return index_; }
4701
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004702 Primitive::Type GetType() const OVERRIDE {
4703 // The previous instruction is the one that will be stored in the temporary location.
4704 DCHECK(GetPrevious() != nullptr);
4705 return GetPrevious()->GetType();
4706 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004707
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004708 DECLARE_INSTRUCTION(Temporary);
4709
4710 private:
4711 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004712 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4713};
4714
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004715class HSuspendCheck : public HTemplateInstruction<0> {
4716 public:
4717 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004718 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004719
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004720 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004721 return true;
4722 }
4723
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004724 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4725 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004726
4727 DECLARE_INSTRUCTION(SuspendCheck);
4728
4729 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004730 // Only used for code generation, in order to share the same slow path between back edges
4731 // of a same loop.
4732 SlowPathCode* slow_path_;
4733
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004734 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4735};
4736
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004737/**
4738 * Instruction to load a Class object.
4739 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004740class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004741 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004742 HLoadClass(HCurrentMethod* current_method,
4743 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004744 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004745 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004746 uint32_t dex_pc,
4747 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004748 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004749 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004750 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004751 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004752 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004753 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004754 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004755 // Referrers class should not need access check. We never inline unverified
4756 // methods so we can't possibly end up in this situation.
4757 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004758 SetRawInputAt(0, current_method);
4759 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004760
4761 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004762
4763 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004764 // Note that we don't need to test for generate_clinit_check_.
4765 // Whether or not we need to generate the clinit check is processed in
4766 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004767 return other->AsLoadClass()->type_index_ == type_index_ &&
4768 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004769 }
4770
4771 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4772
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004773 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004774 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004775 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004776
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004777 bool NeedsEnvironment() const OVERRIDE {
4778 // Will call runtime and load the class if the class is not loaded yet.
4779 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004780 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004781 }
4782
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004783 bool MustGenerateClinitCheck() const {
4784 return generate_clinit_check_;
4785 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004786 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004787 // The entrypoint the code generator is going to call does not do
4788 // clinit of the class.
4789 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004790 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004791 }
4792
4793 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004794 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4795 }
4796
4797 bool NeedsAccessCheck() const {
4798 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004799 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004800
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004801 bool CanThrow() const OVERRIDE {
4802 // May call runtime and and therefore can throw.
4803 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004804 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004805 }
4806
Calin Juravleacf735c2015-02-12 15:25:22 +00004807 ReferenceTypeInfo GetLoadedClassRTI() {
4808 return loaded_class_rti_;
4809 }
4810
4811 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4812 // Make sure we only set exact types (the loaded class should never be merged).
4813 DCHECK(rti.IsExact());
4814 loaded_class_rti_ = rti;
4815 }
4816
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004817 const DexFile& GetDexFile() { return dex_file_; }
4818
Vladimir Markodc151b22015-10-15 18:02:30 +01004819 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004820
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004821 static SideEffects SideEffectsForArchRuntimeCalls() {
4822 return SideEffects::CanTriggerGC();
4823 }
4824
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004825 DECLARE_INSTRUCTION(LoadClass);
4826
4827 private:
4828 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004829 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004830 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004831 // Whether this instruction must generate the initialization check.
4832 // Used for code generation.
4833 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004834 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004835
Calin Juravleacf735c2015-02-12 15:25:22 +00004836 ReferenceTypeInfo loaded_class_rti_;
4837
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004838 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4839};
4840
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004841class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004842 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004843 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004844 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4845 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004846 SetRawInputAt(0, current_method);
4847 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004848
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004849 bool CanBeMoved() const OVERRIDE { return true; }
4850
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004851 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4852 return other->AsLoadString()->string_index_ == string_index_;
4853 }
4854
4855 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4856
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004857 uint32_t GetStringIndex() const { return string_index_; }
4858
4859 // TODO: Can we deopt or debug when we resolve a string?
4860 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004861 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004862 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004863
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004864 static SideEffects SideEffectsForArchRuntimeCalls() {
4865 return SideEffects::CanTriggerGC();
4866 }
4867
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004868 DECLARE_INSTRUCTION(LoadString);
4869
4870 private:
4871 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004872
4873 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4874};
4875
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004876/**
4877 * Performs an initialization check on its Class object input.
4878 */
4879class HClinitCheck : public HExpression<1> {
4880 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004881 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004882 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004883 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004884 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4885 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004886 SetRawInputAt(0, constant);
4887 }
4888
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004889 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004890 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004891 return true;
4892 }
4893
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004894 bool NeedsEnvironment() const OVERRIDE {
4895 // May call runtime to initialize the class.
4896 return true;
4897 }
4898
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004899
4900 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4901
4902 DECLARE_INSTRUCTION(ClinitCheck);
4903
4904 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004905 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4906};
4907
4908class HStaticFieldGet : public HExpression<1> {
4909 public:
4910 HStaticFieldGet(HInstruction* cls,
4911 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004912 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004913 bool is_volatile,
4914 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004915 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004916 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004917 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004918 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004919 : HExpression(field_type,
4920 SideEffects::FieldReadOfType(field_type, is_volatile),
4921 dex_pc),
4922 field_info_(field_offset,
4923 field_type,
4924 is_volatile,
4925 field_idx,
4926 declaring_class_def_index,
4927 dex_file,
4928 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004929 SetRawInputAt(0, cls);
4930 }
4931
Calin Juravle52c48962014-12-16 17:02:57 +00004932
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004933 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004934
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004935 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004936 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4937 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004938 }
4939
4940 size_t ComputeHashCode() const OVERRIDE {
4941 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4942 }
4943
Calin Juravle52c48962014-12-16 17:02:57 +00004944 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004945 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4946 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004947 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004948
4949 DECLARE_INSTRUCTION(StaticFieldGet);
4950
4951 private:
4952 const FieldInfo field_info_;
4953
4954 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4955};
4956
4957class HStaticFieldSet : public HTemplateInstruction<2> {
4958 public:
4959 HStaticFieldSet(HInstruction* cls,
4960 HInstruction* value,
4961 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004962 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004963 bool is_volatile,
4964 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004965 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004966 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004967 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004968 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004969 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4970 dex_pc),
4971 field_info_(field_offset,
4972 field_type,
4973 is_volatile,
4974 field_idx,
4975 declaring_class_def_index,
4976 dex_file,
4977 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004978 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004979 SetRawInputAt(0, cls);
4980 SetRawInputAt(1, value);
4981 }
4982
Calin Juravle52c48962014-12-16 17:02:57 +00004983 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004984 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4985 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004986 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004987
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004988 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004989 bool GetValueCanBeNull() const { return value_can_be_null_; }
4990 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004991
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004992 DECLARE_INSTRUCTION(StaticFieldSet);
4993
4994 private:
4995 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004996 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004997
4998 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4999};
5000
Calin Juravlee460d1d2015-09-29 04:52:17 +01005001class HUnresolvedInstanceFieldGet : public HExpression<1> {
5002 public:
5003 HUnresolvedInstanceFieldGet(HInstruction* obj,
5004 Primitive::Type field_type,
5005 uint32_t field_index,
5006 uint32_t dex_pc)
5007 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5008 field_index_(field_index) {
5009 SetRawInputAt(0, obj);
5010 }
5011
5012 bool NeedsEnvironment() const OVERRIDE { return true; }
5013 bool CanThrow() const OVERRIDE { return true; }
5014
5015 Primitive::Type GetFieldType() const { return GetType(); }
5016 uint32_t GetFieldIndex() const { return field_index_; }
5017
5018 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5019
5020 private:
5021 const uint32_t field_index_;
5022
5023 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5024};
5025
5026class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5027 public:
5028 HUnresolvedInstanceFieldSet(HInstruction* obj,
5029 HInstruction* value,
5030 Primitive::Type field_type,
5031 uint32_t field_index,
5032 uint32_t dex_pc)
5033 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5034 field_type_(field_type),
5035 field_index_(field_index) {
5036 DCHECK_EQ(field_type, value->GetType());
5037 SetRawInputAt(0, obj);
5038 SetRawInputAt(1, value);
5039 }
5040
5041 bool NeedsEnvironment() const OVERRIDE { return true; }
5042 bool CanThrow() const OVERRIDE { return true; }
5043
5044 Primitive::Type GetFieldType() const { return field_type_; }
5045 uint32_t GetFieldIndex() const { return field_index_; }
5046
5047 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5048
5049 private:
5050 const Primitive::Type field_type_;
5051 const uint32_t field_index_;
5052
5053 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5054};
5055
5056class HUnresolvedStaticFieldGet : public HExpression<0> {
5057 public:
5058 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5059 uint32_t field_index,
5060 uint32_t dex_pc)
5061 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5062 field_index_(field_index) {
5063 }
5064
5065 bool NeedsEnvironment() const OVERRIDE { return true; }
5066 bool CanThrow() const OVERRIDE { return true; }
5067
5068 Primitive::Type GetFieldType() const { return GetType(); }
5069 uint32_t GetFieldIndex() const { return field_index_; }
5070
5071 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5072
5073 private:
5074 const uint32_t field_index_;
5075
5076 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5077};
5078
5079class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5080 public:
5081 HUnresolvedStaticFieldSet(HInstruction* value,
5082 Primitive::Type field_type,
5083 uint32_t field_index,
5084 uint32_t dex_pc)
5085 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5086 field_type_(field_type),
5087 field_index_(field_index) {
5088 DCHECK_EQ(field_type, value->GetType());
5089 SetRawInputAt(0, value);
5090 }
5091
5092 bool NeedsEnvironment() const OVERRIDE { return true; }
5093 bool CanThrow() const OVERRIDE { return true; }
5094
5095 Primitive::Type GetFieldType() const { return field_type_; }
5096 uint32_t GetFieldIndex() const { return field_index_; }
5097
5098 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5099
5100 private:
5101 const Primitive::Type field_type_;
5102 const uint32_t field_index_;
5103
5104 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5105};
5106
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005107// Implement the move-exception DEX instruction.
5108class HLoadException : public HExpression<0> {
5109 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005110 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5111 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005112
David Brazdilbbd733e2015-08-18 17:48:17 +01005113 bool CanBeNull() const OVERRIDE { return false; }
5114
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005115 DECLARE_INSTRUCTION(LoadException);
5116
5117 private:
5118 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5119};
5120
David Brazdilcb1c0552015-08-04 16:22:25 +01005121// Implicit part of move-exception which clears thread-local exception storage.
5122// Must not be removed because the runtime expects the TLS to get cleared.
5123class HClearException : public HTemplateInstruction<0> {
5124 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005125 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5126 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005127
5128 DECLARE_INSTRUCTION(ClearException);
5129
5130 private:
5131 DISALLOW_COPY_AND_ASSIGN(HClearException);
5132};
5133
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005134class HThrow : public HTemplateInstruction<1> {
5135 public:
5136 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005137 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005138 SetRawInputAt(0, exception);
5139 }
5140
5141 bool IsControlFlow() const OVERRIDE { return true; }
5142
5143 bool NeedsEnvironment() const OVERRIDE { return true; }
5144
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005145 bool CanThrow() const OVERRIDE { return true; }
5146
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005147
5148 DECLARE_INSTRUCTION(Throw);
5149
5150 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005151 DISALLOW_COPY_AND_ASSIGN(HThrow);
5152};
5153
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005154/**
5155 * Implementation strategies for the code generator of a HInstanceOf
5156 * or `HCheckCast`.
5157 */
5158enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005159 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005160 kExactCheck, // Can do a single class compare.
5161 kClassHierarchyCheck, // Can just walk the super class chain.
5162 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5163 kInterfaceCheck, // No optimization yet when checking against an interface.
5164 kArrayObjectCheck, // Can just check if the array is not primitive.
5165 kArrayCheck // No optimization yet when checking against a generic array.
5166};
5167
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005168class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005169 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005170 HInstanceOf(HInstruction* object,
5171 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005172 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005173 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005174 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005175 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005176 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005177 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005178 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005179 SetRawInputAt(0, object);
5180 SetRawInputAt(1, constant);
5181 }
5182
5183 bool CanBeMoved() const OVERRIDE { return true; }
5184
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005185 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005186 return true;
5187 }
5188
5189 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005190 return false;
5191 }
5192
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005193 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5194
5195 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005196
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005197 // Used only in code generation.
5198 bool MustDoNullCheck() const { return must_do_null_check_; }
5199 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5200
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005201 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5202 return (check_kind == TypeCheckKind::kExactCheck)
5203 ? SideEffects::None()
5204 // Mips currently does runtime calls for any other checks.
5205 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005206 }
5207
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005208 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005209
5210 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005211 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005212 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005213
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005214 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5215};
5216
Calin Juravleb1498f62015-02-16 13:13:29 +00005217class HBoundType : public HExpression<1> {
5218 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005219 // Constructs an HBoundType with the given upper_bound.
5220 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005221 HBoundType(HInstruction* input,
5222 ReferenceTypeInfo upper_bound,
5223 bool upper_can_be_null,
5224 uint32_t dex_pc = kNoDexPc)
5225 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005226 upper_bound_(upper_bound),
5227 upper_can_be_null_(upper_can_be_null),
5228 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005229 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005230 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005231 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005232 }
5233
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005234 // GetUpper* should only be used in reference type propagation.
5235 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5236 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005237
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005238 void SetCanBeNull(bool can_be_null) {
5239 DCHECK(upper_can_be_null_ || !can_be_null);
5240 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005241 }
5242
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005243 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5244
Calin Juravleb1498f62015-02-16 13:13:29 +00005245 DECLARE_INSTRUCTION(BoundType);
5246
5247 private:
5248 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005249 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5250 // It is used to bound the type in cases like:
5251 // if (x instanceof ClassX) {
5252 // // uper_bound_ will be ClassX
5253 // }
5254 const ReferenceTypeInfo upper_bound_;
5255 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5256 // is false then can_be_null_ cannot be true).
5257 const bool upper_can_be_null_;
5258 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005259
5260 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5261};
5262
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005263class HCheckCast : public HTemplateInstruction<2> {
5264 public:
5265 HCheckCast(HInstruction* object,
5266 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005267 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005268 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005269 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005270 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005271 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005272 SetRawInputAt(0, object);
5273 SetRawInputAt(1, constant);
5274 }
5275
5276 bool CanBeMoved() const OVERRIDE { return true; }
5277
5278 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5279 return true;
5280 }
5281
5282 bool NeedsEnvironment() const OVERRIDE {
5283 // Instruction may throw a CheckCastError.
5284 return true;
5285 }
5286
5287 bool CanThrow() const OVERRIDE { return true; }
5288
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005289 bool MustDoNullCheck() const { return must_do_null_check_; }
5290 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005291 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005292
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005293 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005294
5295 DECLARE_INSTRUCTION(CheckCast);
5296
5297 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005298 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005299 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005300
5301 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005302};
5303
Calin Juravle27df7582015-04-17 19:12:31 +01005304class HMemoryBarrier : public HTemplateInstruction<0> {
5305 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005306 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005307 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005308 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005309 barrier_kind_(barrier_kind) {}
5310
5311 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5312
5313 DECLARE_INSTRUCTION(MemoryBarrier);
5314
5315 private:
5316 const MemBarrierKind barrier_kind_;
5317
5318 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5319};
5320
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005321class HMonitorOperation : public HTemplateInstruction<1> {
5322 public:
5323 enum OperationKind {
5324 kEnter,
5325 kExit,
5326 };
5327
5328 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005329 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005330 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5331 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005332 SetRawInputAt(0, object);
5333 }
5334
5335 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005336 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5337
5338 bool CanThrow() const OVERRIDE {
5339 // Verifier guarantees that monitor-exit cannot throw.
5340 // This is important because it allows the HGraphBuilder to remove
5341 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5342 return IsEnter();
5343 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005344
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005345
5346 bool IsEnter() const { return kind_ == kEnter; }
5347
5348 DECLARE_INSTRUCTION(MonitorOperation);
5349
Calin Juravle52c48962014-12-16 17:02:57 +00005350 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005351 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005352
5353 private:
5354 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5355};
5356
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005357/**
5358 * A HInstruction used as a marker for the replacement of new + <init>
5359 * of a String to a call to a StringFactory. Only baseline will see
5360 * the node at code generation, where it will be be treated as null.
5361 * When compiling non-baseline, `HFakeString` instructions are being removed
5362 * in the instruction simplifier.
5363 */
5364class HFakeString : public HTemplateInstruction<0> {
5365 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005366 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5367 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005368
5369 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5370
5371 DECLARE_INSTRUCTION(FakeString);
5372
5373 private:
5374 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5375};
5376
Vladimir Markof9f64412015-09-02 14:05:49 +01005377class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005378 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005379 MoveOperands(Location source,
5380 Location destination,
5381 Primitive::Type type,
5382 HInstruction* instruction)
5383 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005384
5385 Location GetSource() const { return source_; }
5386 Location GetDestination() const { return destination_; }
5387
5388 void SetSource(Location value) { source_ = value; }
5389 void SetDestination(Location value) { destination_ = value; }
5390
5391 // The parallel move resolver marks moves as "in-progress" by clearing the
5392 // destination (but not the source).
5393 Location MarkPending() {
5394 DCHECK(!IsPending());
5395 Location dest = destination_;
5396 destination_ = Location::NoLocation();
5397 return dest;
5398 }
5399
5400 void ClearPending(Location dest) {
5401 DCHECK(IsPending());
5402 destination_ = dest;
5403 }
5404
5405 bool IsPending() const {
5406 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5407 return destination_.IsInvalid() && !source_.IsInvalid();
5408 }
5409
5410 // True if this blocks a move from the given location.
5411 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005412 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005413 }
5414
5415 // A move is redundant if it's been eliminated, if its source and
5416 // destination are the same, or if its destination is unneeded.
5417 bool IsRedundant() const {
5418 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5419 }
5420
5421 // We clear both operands to indicate move that's been eliminated.
5422 void Eliminate() {
5423 source_ = destination_ = Location::NoLocation();
5424 }
5425
5426 bool IsEliminated() const {
5427 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5428 return source_.IsInvalid();
5429 }
5430
Alexey Frunze4dda3372015-06-01 18:31:49 -07005431 Primitive::Type GetType() const { return type_; }
5432
Nicolas Geoffray90218252015-04-15 11:56:51 +01005433 bool Is64BitMove() const {
5434 return Primitive::Is64BitType(type_);
5435 }
5436
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005437 HInstruction* GetInstruction() const { return instruction_; }
5438
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005439 private:
5440 Location source_;
5441 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005442 // The type this move is for.
5443 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005444 // The instruction this move is assocatied with. Null when this move is
5445 // for moving an input in the expected locations of user (including a phi user).
5446 // This is only used in debug mode, to ensure we do not connect interval siblings
5447 // in the same parallel move.
5448 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005449};
5450
5451static constexpr size_t kDefaultNumberOfMoves = 4;
5452
5453class HParallelMove : public HTemplateInstruction<0> {
5454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005455 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005456 : HTemplateInstruction(SideEffects::None(), dex_pc),
5457 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5458 moves_.reserve(kDefaultNumberOfMoves);
5459 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005460
Nicolas Geoffray90218252015-04-15 11:56:51 +01005461 void AddMove(Location source,
5462 Location destination,
5463 Primitive::Type type,
5464 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005465 DCHECK(source.IsValid());
5466 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005467 if (kIsDebugBuild) {
5468 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005469 for (const MoveOperands& move : moves_) {
5470 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005471 // Special case the situation where the move is for the spill slot
5472 // of the instruction.
5473 if ((GetPrevious() == instruction)
5474 || ((GetPrevious() == nullptr)
5475 && instruction->IsPhi()
5476 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005477 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005478 << "Doing parallel moves for the same instruction.";
5479 } else {
5480 DCHECK(false) << "Doing parallel moves for the same instruction.";
5481 }
5482 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005483 }
5484 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005485 for (const MoveOperands& move : moves_) {
5486 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005487 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005488 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005489 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005490 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005491 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005492 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005493 }
5494
Vladimir Marko225b6462015-09-28 12:17:40 +01005495 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005496 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005497 }
5498
Vladimir Marko225b6462015-09-28 12:17:40 +01005499 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005500
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005501 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005502
5503 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005504 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005505
5506 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5507};
5508
Mark Mendell0616ae02015-04-17 12:49:27 -04005509} // namespace art
5510
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005511#ifdef ART_ENABLE_CODEGEN_arm64
5512#include "nodes_arm64.h"
5513#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005514#ifdef ART_ENABLE_CODEGEN_x86
5515#include "nodes_x86.h"
5516#endif
5517
5518namespace art {
5519
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005520class HGraphVisitor : public ValueObject {
5521 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005522 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5523 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005524
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005525 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005526 virtual void VisitBasicBlock(HBasicBlock* block);
5527
Roland Levillain633021e2014-10-01 14:12:25 +01005528 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005529 void VisitInsertionOrder();
5530
Roland Levillain633021e2014-10-01 14:12:25 +01005531 // Visit the graph following dominator tree reverse post-order.
5532 void VisitReversePostOrder();
5533
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005534 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005535
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005536 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005537#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005538 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5539
5540 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5541
5542#undef DECLARE_VISIT_INSTRUCTION
5543
5544 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005545 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005546
5547 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5548};
5549
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005550class HGraphDelegateVisitor : public HGraphVisitor {
5551 public:
5552 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5553 virtual ~HGraphDelegateVisitor() {}
5554
5555 // Visit functions that delegate to to super class.
5556#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005557 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005558
5559 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5560
5561#undef DECLARE_VISIT_INSTRUCTION
5562
5563 private:
5564 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5565};
5566
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005567class HInsertionOrderIterator : public ValueObject {
5568 public:
5569 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5570
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005571 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005572 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005573 void Advance() { ++index_; }
5574
5575 private:
5576 const HGraph& graph_;
5577 size_t index_;
5578
5579 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5580};
5581
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005582class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005583 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005584 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5585 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005586 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005587 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005588
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005589 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5590 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005591 void Advance() { ++index_; }
5592
5593 private:
5594 const HGraph& graph_;
5595 size_t index_;
5596
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005597 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005598};
5599
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005600class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005601 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005602 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005603 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005604 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005605 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005606 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005607
5608 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005609 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005610 void Advance() { --index_; }
5611
5612 private:
5613 const HGraph& graph_;
5614 size_t index_;
5615
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005616 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005617};
5618
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005619class HLinearPostOrderIterator : public ValueObject {
5620 public:
5621 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005622 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005623
5624 bool Done() const { return index_ == 0; }
5625
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005626 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005627
5628 void Advance() {
5629 --index_;
5630 DCHECK_GE(index_, 0U);
5631 }
5632
5633 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005634 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005635 size_t index_;
5636
5637 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5638};
5639
5640class HLinearOrderIterator : public ValueObject {
5641 public:
5642 explicit HLinearOrderIterator(const HGraph& graph)
5643 : order_(graph.GetLinearOrder()), index_(0) {}
5644
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005645 bool Done() const { return index_ == order_.size(); }
5646 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005647 void Advance() { ++index_; }
5648
5649 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005650 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005651 size_t index_;
5652
5653 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5654};
5655
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005656// Iterator over the blocks that art part of the loop. Includes blocks part
5657// of an inner loop. The order in which the blocks are iterated is on their
5658// block id.
5659class HBlocksInLoopIterator : public ValueObject {
5660 public:
5661 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5662 : blocks_in_loop_(info.GetBlocks()),
5663 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5664 index_(0) {
5665 if (!blocks_in_loop_.IsBitSet(index_)) {
5666 Advance();
5667 }
5668 }
5669
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005670 bool Done() const { return index_ == blocks_.size(); }
5671 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005672 void Advance() {
5673 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005674 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005675 if (blocks_in_loop_.IsBitSet(index_)) {
5676 break;
5677 }
5678 }
5679 }
5680
5681 private:
5682 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005683 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005684 size_t index_;
5685
5686 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5687};
5688
Mingyao Yang3584bce2015-05-19 16:01:59 -07005689// Iterator over the blocks that art part of the loop. Includes blocks part
5690// of an inner loop. The order in which the blocks are iterated is reverse
5691// post order.
5692class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5693 public:
5694 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5695 : blocks_in_loop_(info.GetBlocks()),
5696 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5697 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005698 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005699 Advance();
5700 }
5701 }
5702
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005703 bool Done() const { return index_ == blocks_.size(); }
5704 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005705 void Advance() {
5706 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005707 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5708 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005709 break;
5710 }
5711 }
5712 }
5713
5714 private:
5715 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005716 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005717 size_t index_;
5718
5719 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5720};
5721
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005722inline int64_t Int64FromConstant(HConstant* constant) {
5723 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5724 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5725 : constant->AsLongConstant()->GetValue();
5726}
5727
Vladimir Marko58155012015-08-19 12:49:41 +00005728inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5729 // For the purposes of the compiler, the dex files must actually be the same object
5730 // if we want to safely treat them as the same. This is especially important for JIT
5731 // as custom class loaders can open the same underlying file (or memory) multiple
5732 // times and provide different class resolution but no two class loaders should ever
5733 // use the same DexFile object - doing so is an unsupported hack that can lead to
5734 // all sorts of weird failures.
5735 return &lhs == &rhs;
5736}
5737
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005738} // namespace art
5739
5740#endif // ART_COMPILER_OPTIMIZING_NODES_H_