blob: f36e0f5b2ce7ada43878611578614a5b998a8800 [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) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001099 M(Arm64IntermediateAddress, Instruction) \
1100 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001101#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001102
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001103#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1104
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001105#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1106
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001107#ifndef ART_ENABLE_CODEGEN_x86
1108#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1109#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001110#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1111 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001112 M(X86LoadFromConstantTable, Instruction) \
1113 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001114#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001115
1116#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1117
1118#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1119 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1120 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1121 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001122 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001123 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001124 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1125 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1126
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001127#define FOR_EACH_INSTRUCTION(M) \
1128 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1129 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001130 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001131 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001132 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001133
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001134#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001135FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1136#undef FORWARD_DECLARATION
1137
Roland Levillainccc07a92014-09-16 14:48:16 +01001138#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001139 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1140 const char* DebugName() const OVERRIDE { return #type; } \
1141 const H##type* As##type() const OVERRIDE { return this; } \
1142 H##type* As##type() OVERRIDE { return this; } \
1143 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001144 return other->Is##type(); \
1145 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001146 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001147
David Brazdiled596192015-01-23 10:39:45 +00001148template <typename T> class HUseList;
1149
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001150template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001151class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001152 public:
David Brazdiled596192015-01-23 10:39:45 +00001153 HUseListNode* GetPrevious() const { return prev_; }
1154 HUseListNode* GetNext() const { return next_; }
1155 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001156 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001157 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001158
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001159 private:
David Brazdiled596192015-01-23 10:39:45 +00001160 HUseListNode(T user, size_t index)
1161 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1162
1163 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001164 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001165 HUseListNode<T>* prev_;
1166 HUseListNode<T>* next_;
1167
1168 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001169
1170 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1171};
1172
David Brazdiled596192015-01-23 10:39:45 +00001173template <typename T>
1174class HUseList : public ValueObject {
1175 public:
1176 HUseList() : first_(nullptr) {}
1177
1178 void Clear() {
1179 first_ = nullptr;
1180 }
1181
1182 // Adds a new entry at the beginning of the use list and returns
1183 // the newly created node.
1184 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001185 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001186 if (IsEmpty()) {
1187 first_ = new_node;
1188 } else {
1189 first_->prev_ = new_node;
1190 new_node->next_ = first_;
1191 first_ = new_node;
1192 }
1193 return new_node;
1194 }
1195
1196 HUseListNode<T>* GetFirst() const {
1197 return first_;
1198 }
1199
1200 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001201 DCHECK(node != nullptr);
1202 DCHECK(Contains(node));
1203
David Brazdiled596192015-01-23 10:39:45 +00001204 if (node->prev_ != nullptr) {
1205 node->prev_->next_ = node->next_;
1206 }
1207 if (node->next_ != nullptr) {
1208 node->next_->prev_ = node->prev_;
1209 }
1210 if (node == first_) {
1211 first_ = node->next_;
1212 }
1213 }
1214
David Brazdil1abb4192015-02-17 18:33:36 +00001215 bool Contains(const HUseListNode<T>* node) const {
1216 if (node == nullptr) {
1217 return false;
1218 }
1219 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1220 if (current == node) {
1221 return true;
1222 }
1223 }
1224 return false;
1225 }
1226
David Brazdiled596192015-01-23 10:39:45 +00001227 bool IsEmpty() const {
1228 return first_ == nullptr;
1229 }
1230
1231 bool HasOnlyOneUse() const {
1232 return first_ != nullptr && first_->next_ == nullptr;
1233 }
1234
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001235 size_t SizeSlow() const {
1236 size_t count = 0;
1237 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1238 ++count;
1239 }
1240 return count;
1241 }
1242
David Brazdiled596192015-01-23 10:39:45 +00001243 private:
1244 HUseListNode<T>* first_;
1245};
1246
1247template<typename T>
1248class HUseIterator : public ValueObject {
1249 public:
1250 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1251
1252 bool Done() const { return current_ == nullptr; }
1253
1254 void Advance() {
1255 DCHECK(!Done());
1256 current_ = current_->GetNext();
1257 }
1258
1259 HUseListNode<T>* Current() const {
1260 DCHECK(!Done());
1261 return current_;
1262 }
1263
1264 private:
1265 HUseListNode<T>* current_;
1266
1267 friend class HValue;
1268};
1269
David Brazdil1abb4192015-02-17 18:33:36 +00001270// This class is used by HEnvironment and HInstruction classes to record the
1271// instructions they use and pointers to the corresponding HUseListNodes kept
1272// by the used instructions.
1273template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001274class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001275 public:
1276 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1277 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1278
1279 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1280 : instruction_(old_record.instruction_), use_node_(use_node) {
1281 DCHECK(instruction_ != nullptr);
1282 DCHECK(use_node_ != nullptr);
1283 DCHECK(old_record.use_node_ == nullptr);
1284 }
1285
1286 HInstruction* GetInstruction() const { return instruction_; }
1287 HUseListNode<T>* GetUseNode() const { return use_node_; }
1288
1289 private:
1290 // Instruction used by the user.
1291 HInstruction* instruction_;
1292
1293 // Corresponding entry in the use list kept by 'instruction_'.
1294 HUseListNode<T>* use_node_;
1295};
1296
Aart Bik854a02b2015-07-14 16:07:00 -07001297/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001298 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001299 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001300 * For write/read dependences on fields/arrays, the dependence analysis uses
1301 * type disambiguation (e.g. a float field write cannot modify the value of an
1302 * integer field read) and the access type (e.g. a reference array write cannot
1303 * modify the value of a reference field read [although it may modify the
1304 * reference fetch prior to reading the field, which is represented by its own
1305 * write/read dependence]). The analysis makes conservative points-to
1306 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1307 * the same, and any reference read depends on any reference read without
1308 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001309 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001310 * The internal representation uses 38-bit and is described in the table below.
1311 * The first line indicates the side effect, and for field/array accesses the
1312 * second line indicates the type of the access (in the order of the
1313 * Primitive::Type enum).
1314 * The two numbered lines below indicate the bit position in the bitfield (read
1315 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001316 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001317 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1318 * +-------------+---------+---------+--------------+---------+---------+
1319 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1320 * | 3 |333333322|222222221| 1 |111111110|000000000|
1321 * | 7 |654321098|765432109| 8 |765432109|876543210|
1322 *
1323 * Note that, to ease the implementation, 'changes' bits are least significant
1324 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001325 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001326class SideEffects : public ValueObject {
1327 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001328 SideEffects() : flags_(0) {}
1329
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001330 static SideEffects None() {
1331 return SideEffects(0);
1332 }
1333
1334 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001335 return SideEffects(kAllChangeBits | kAllDependOnBits);
1336 }
1337
1338 static SideEffects AllChanges() {
1339 return SideEffects(kAllChangeBits);
1340 }
1341
1342 static SideEffects AllDependencies() {
1343 return SideEffects(kAllDependOnBits);
1344 }
1345
1346 static SideEffects AllExceptGCDependency() {
1347 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1348 }
1349
1350 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001351 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001352 }
1353
Aart Bik34c3ba92015-07-20 14:08:59 -07001354 static SideEffects AllWrites() {
1355 return SideEffects(kAllWrites);
1356 }
1357
1358 static SideEffects AllReads() {
1359 return SideEffects(kAllReads);
1360 }
1361
1362 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1363 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001364 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001365 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001366 }
1367
Aart Bik854a02b2015-07-14 16:07:00 -07001368 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1369 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001370 }
1371
Aart Bik34c3ba92015-07-20 14:08:59 -07001372 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1373 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001374 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001375 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001376 }
1377
1378 static SideEffects ArrayReadOfType(Primitive::Type type) {
1379 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1380 }
1381
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001382 static SideEffects CanTriggerGC() {
1383 return SideEffects(1ULL << kCanTriggerGCBit);
1384 }
1385
1386 static SideEffects DependsOnGC() {
1387 return SideEffects(1ULL << kDependsOnGCBit);
1388 }
1389
Aart Bik854a02b2015-07-14 16:07:00 -07001390 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001391 SideEffects Union(SideEffects other) const {
1392 return SideEffects(flags_ | other.flags_);
1393 }
1394
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001395 SideEffects Exclusion(SideEffects other) const {
1396 return SideEffects(flags_ & ~other.flags_);
1397 }
1398
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001399 void Add(SideEffects other) {
1400 flags_ |= other.flags_;
1401 }
1402
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001403 bool Includes(SideEffects other) const {
1404 return (other.flags_ & flags_) == other.flags_;
1405 }
1406
1407 bool HasSideEffects() const {
1408 return (flags_ & kAllChangeBits);
1409 }
1410
1411 bool HasDependencies() const {
1412 return (flags_ & kAllDependOnBits);
1413 }
1414
1415 // Returns true if there are no side effects or dependencies.
1416 bool DoesNothing() const {
1417 return flags_ == 0;
1418 }
1419
Aart Bik854a02b2015-07-14 16:07:00 -07001420 // Returns true if something is written.
1421 bool DoesAnyWrite() const {
1422 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001423 }
1424
Aart Bik854a02b2015-07-14 16:07:00 -07001425 // Returns true if something is read.
1426 bool DoesAnyRead() const {
1427 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001428 }
1429
Aart Bik854a02b2015-07-14 16:07:00 -07001430 // Returns true if potentially everything is written and read
1431 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001432 bool DoesAllReadWrite() const {
1433 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1434 }
1435
Aart Bik854a02b2015-07-14 16:07:00 -07001436 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001437 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001438 }
1439
Roland Levillain0d5a2812015-11-13 10:07:31 +00001440 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001441 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001442 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1443 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001444 }
1445
1446 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001448 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001449 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001450 for (int s = kLastBit; s >= 0; s--) {
1451 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1452 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1453 // This is a bit for the GC side effect.
1454 if (current_bit_is_set) {
1455 flags += "GC";
1456 }
Aart Bik854a02b2015-07-14 16:07:00 -07001457 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001458 } else {
1459 // This is a bit for the array/field analysis.
1460 // The underscore character stands for the 'can trigger GC' bit.
1461 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1462 if (current_bit_is_set) {
1463 flags += kDebug[s];
1464 }
1465 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1466 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1467 flags += "|";
1468 }
1469 }
Aart Bik854a02b2015-07-14 16:07:00 -07001470 }
1471 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001472 }
1473
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001474 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001476 private:
1477 static constexpr int kFieldArrayAnalysisBits = 9;
1478
1479 static constexpr int kFieldWriteOffset = 0;
1480 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1481 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1482 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1483
1484 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1485
1486 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1487 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1488 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1489 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1490
1491 static constexpr int kLastBit = kDependsOnGCBit;
1492 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1493
1494 // Aliases.
1495
1496 static_assert(kChangeBits == kDependOnBits,
1497 "the 'change' bits should match the 'depend on' bits.");
1498
1499 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1500 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1501 static constexpr uint64_t kAllWrites =
1502 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1503 static constexpr uint64_t kAllReads =
1504 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001505
Aart Bik854a02b2015-07-14 16:07:00 -07001506 // Work around the fact that HIR aliases I/F and J/D.
1507 // TODO: remove this interceptor once HIR types are clean
1508 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1509 switch (type) {
1510 case Primitive::kPrimInt:
1511 case Primitive::kPrimFloat:
1512 return TypeFlag(Primitive::kPrimInt, offset) |
1513 TypeFlag(Primitive::kPrimFloat, offset);
1514 case Primitive::kPrimLong:
1515 case Primitive::kPrimDouble:
1516 return TypeFlag(Primitive::kPrimLong, offset) |
1517 TypeFlag(Primitive::kPrimDouble, offset);
1518 default:
1519 return TypeFlag(type, offset);
1520 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001521 }
1522
Aart Bik854a02b2015-07-14 16:07:00 -07001523 // Translates type to bit flag.
1524 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1525 CHECK_NE(type, Primitive::kPrimVoid);
1526 const uint64_t one = 1;
1527 const int shift = type; // 0-based consecutive enum
1528 DCHECK_LE(kFieldWriteOffset, shift);
1529 DCHECK_LT(shift, kArrayWriteOffset);
1530 return one << (type + offset);
1531 }
1532
1533 // Private constructor on direct flags value.
1534 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1535
1536 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001537};
1538
David Brazdiled596192015-01-23 10:39:45 +00001539// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001540class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001541 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001542 HEnvironment(ArenaAllocator* arena,
1543 size_t number_of_vregs,
1544 const DexFile& dex_file,
1545 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001546 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001547 InvokeType invoke_type,
1548 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001549 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1550 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001551 parent_(nullptr),
1552 dex_file_(dex_file),
1553 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001554 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001555 invoke_type_(invoke_type),
1556 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001557 }
1558
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001559 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001560 : HEnvironment(arena,
1561 to_copy.Size(),
1562 to_copy.GetDexFile(),
1563 to_copy.GetMethodIdx(),
1564 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001565 to_copy.GetInvokeType(),
1566 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001567
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001568 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001569 if (parent_ != nullptr) {
1570 parent_->SetAndCopyParentChain(allocator, parent);
1571 } else {
1572 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1573 parent_->CopyFrom(parent);
1574 if (parent->GetParent() != nullptr) {
1575 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1576 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001577 }
David Brazdiled596192015-01-23 10:39:45 +00001578 }
1579
Vladimir Marko71bf8092015-09-15 15:33:14 +01001580 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001581 void CopyFrom(HEnvironment* environment);
1582
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001583 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1584 // input to the loop phi instead. This is for inserting instructions that
1585 // require an environment (like HDeoptimization) in the loop pre-header.
1586 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001587
1588 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001589 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001590 }
1591
1592 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001593 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001594 }
1595
David Brazdil1abb4192015-02-17 18:33:36 +00001596 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001597
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001598 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001599
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001600 HEnvironment* GetParent() const { return parent_; }
1601
1602 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001603 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001604 }
1605
1606 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001607 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001608 }
1609
1610 uint32_t GetDexPc() const {
1611 return dex_pc_;
1612 }
1613
1614 uint32_t GetMethodIdx() const {
1615 return method_idx_;
1616 }
1617
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001618 InvokeType GetInvokeType() const {
1619 return invoke_type_;
1620 }
1621
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001622 const DexFile& GetDexFile() const {
1623 return dex_file_;
1624 }
1625
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001626 HInstruction* GetHolder() const {
1627 return holder_;
1628 }
1629
David Brazdiled596192015-01-23 10:39:45 +00001630 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001631 // Record instructions' use entries of this environment for constant-time removal.
1632 // It should only be called by HInstruction when a new environment use is added.
1633 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1634 DCHECK(env_use->GetUser() == this);
1635 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001636 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001637 }
David Brazdiled596192015-01-23 10:39:45 +00001638
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001639 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1640 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001641 HEnvironment* parent_;
1642 const DexFile& dex_file_;
1643 const uint32_t method_idx_;
1644 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001645 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001646
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001647 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001648 HInstruction* const holder_;
1649
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001650 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001651
1652 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1653};
1654
Calin Juravleacf735c2015-02-12 15:25:22 +00001655class ReferenceTypeInfo : ValueObject {
1656 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001657 typedef Handle<mirror::Class> TypeHandle;
1658
Calin Juravle2e768302015-07-28 14:41:11 +00001659 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1660 // The constructor will check that the type_handle is valid.
1661 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001662 }
1663
Calin Juravle2e768302015-07-28 14:41:11 +00001664 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1665
1666 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1667 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001668 }
1669
Calin Juravle2e768302015-07-28 14:41:11 +00001670 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1671 return IsValidHandle(type_handle_);
1672 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001673
Calin Juravleacf735c2015-02-12 15:25:22 +00001674 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001675
1676 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1677 DCHECK(IsValid());
1678 return GetTypeHandle()->IsObjectClass();
1679 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001680
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001681 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001682 DCHECK(IsValid());
1683 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001684 }
1685
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001686 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1687 DCHECK(IsValid());
1688 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1689 }
1690
Mathieu Chartier90443472015-07-16 20:32:27 -07001691 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001692 DCHECK(IsValid());
1693 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001694 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001695
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001696 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001697 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001698 return GetTypeHandle()->IsArrayClass();
1699 }
1700
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001701 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1702 DCHECK(IsValid());
1703 return GetTypeHandle()->IsPrimitiveArray();
1704 }
1705
1706 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1707 DCHECK(IsValid());
1708 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1709 }
1710
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001711 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001712 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001713 if (!IsExact()) return false;
1714 if (!IsArrayClass()) return false;
1715 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1716 }
1717
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001718 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1719 DCHECK(IsValid());
1720 if (!IsExact()) return false;
1721 if (!IsArrayClass()) return false;
1722 if (!rti.IsArrayClass()) return false;
1723 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1724 rti.GetTypeHandle()->GetComponentType());
1725 }
1726
Calin Juravleacf735c2015-02-12 15:25:22 +00001727 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1728
Mathieu Chartier90443472015-07-16 20:32:27 -07001729 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001730 DCHECK(IsValid());
1731 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001732 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1733 }
1734
Calin Juravlecdfed3d2015-10-26 14:05:01 +00001735 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1736 DCHECK(IsValid());
1737 DCHECK(rti.IsValid());
1738 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
1739 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1740 }
1741
Calin Juravleacf735c2015-02-12 15:25:22 +00001742 // Returns true if the type information provide the same amount of details.
1743 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001744 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001745 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001746 if (!IsValid() && !rti.IsValid()) {
1747 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001748 return true;
1749 }
Calin Juravle2e768302015-07-28 14:41:11 +00001750 if (!IsValid() || !rti.IsValid()) {
1751 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001752 return false;
1753 }
Calin Juravle2e768302015-07-28 14:41:11 +00001754 return IsExact() == rti.IsExact()
1755 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001756 }
1757
1758 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001759 ReferenceTypeInfo();
1760 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001761
Calin Juravleacf735c2015-02-12 15:25:22 +00001762 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001763 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001764 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001765 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001766 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001767};
1768
1769std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1770
Vladimir Markof9f64412015-09-02 14:05:49 +01001771class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001772 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001773 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001774 : previous_(nullptr),
1775 next_(nullptr),
1776 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001777 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001778 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001779 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001780 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001781 locations_(nullptr),
1782 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001783 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001784 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001785 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001786
Dave Allison20dfc792014-06-16 20:44:29 -07001787 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001788
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001789#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001790 enum InstructionKind {
1791 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1792 };
1793#undef DECLARE_KIND
1794
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001795 HInstruction* GetNext() const { return next_; }
1796 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001797
Calin Juravle77520bc2015-01-12 18:45:46 +00001798 HInstruction* GetNextDisregardingMoves() const;
1799 HInstruction* GetPreviousDisregardingMoves() const;
1800
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001801 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001802 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001803 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001804 bool IsInBlock() const { return block_ != nullptr; }
1805 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001806 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001807
Roland Levillain6b879dd2014-09-22 17:13:44 +01001808 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001809 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001810
1811 virtual void Accept(HGraphVisitor* visitor) = 0;
1812 virtual const char* DebugName() const = 0;
1813
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001814 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001815 void SetRawInputAt(size_t index, HInstruction* input) {
1816 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1817 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001818
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001819 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001820
1821 uint32_t GetDexPc() const { return dex_pc_; }
1822
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001823 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001824
Roland Levillaine161a2a2014-10-03 12:45:18 +01001825 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001826 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001827
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001828 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001829 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001830
Calin Juravle10e244f2015-01-26 18:54:32 +00001831 // Does not apply for all instructions, but having this at top level greatly
1832 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001833 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001834 virtual bool CanBeNull() const {
1835 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1836 return true;
1837 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001838
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001839 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001840 return false;
1841 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001842
Calin Juravle2e768302015-07-28 14:41:11 +00001843 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001844
Calin Juravle61d544b2015-02-23 16:46:57 +00001845 ReferenceTypeInfo GetReferenceTypeInfo() const {
1846 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1847 return reference_type_info_;
1848 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001849
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001850 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001851 DCHECK(user != nullptr);
1852 HUseListNode<HInstruction*>* use =
1853 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1854 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001855 }
1856
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001857 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001858 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001859 HUseListNode<HEnvironment*>* env_use =
1860 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1861 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001862 }
1863
David Brazdil1abb4192015-02-17 18:33:36 +00001864 void RemoveAsUserOfInput(size_t input) {
1865 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1866 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1867 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001868
David Brazdil1abb4192015-02-17 18:33:36 +00001869 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1870 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001871
David Brazdiled596192015-01-23 10:39:45 +00001872 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1873 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001874 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001875 bool HasOnlyOneNonEnvironmentUse() const {
1876 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1877 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001878
Roland Levillain6c82d402014-10-13 16:10:27 +01001879 // Does this instruction strictly dominate `other_instruction`?
1880 // Returns false if this instruction and `other_instruction` are the same.
1881 // Aborts if this instruction and `other_instruction` are both phis.
1882 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001883
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001884 int GetId() const { return id_; }
1885 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001886
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001887 int GetSsaIndex() const { return ssa_index_; }
1888 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1889 bool HasSsaIndex() const { return ssa_index_ != -1; }
1890
1891 bool HasEnvironment() const { return environment_ != nullptr; }
1892 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001893 // Set the `environment_` field. Raw because this method does not
1894 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001895 void SetRawEnvironment(HEnvironment* environment) {
1896 DCHECK(environment_ == nullptr);
1897 DCHECK_EQ(environment->GetHolder(), this);
1898 environment_ = environment;
1899 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001900
1901 // Set the environment of this instruction, copying it from `environment`. While
1902 // copying, the uses lists are being updated.
1903 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001904 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001905 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001906 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001907 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001908 if (environment->GetParent() != nullptr) {
1909 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1910 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001911 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001912
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001913 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1914 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001915 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001916 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001917 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001918 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001919 if (environment->GetParent() != nullptr) {
1920 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1921 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001922 }
1923
Nicolas Geoffray39468442014-09-02 15:17:15 +01001924 // Returns the number of entries in the environment. Typically, that is the
1925 // number of dex registers in a method. It could be more in case of inlining.
1926 size_t EnvironmentSize() const;
1927
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001928 LocationSummary* GetLocations() const { return locations_; }
1929 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001930
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001931 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001932 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001933
Alexandre Rames188d4312015-04-09 18:30:21 +01001934 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1935 // uses of this instruction by `other` are *not* updated.
1936 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1937 ReplaceWith(other);
1938 other->ReplaceInput(this, use_index);
1939 }
1940
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001941 // Move `this` instruction before `cursor`.
1942 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001943
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001944#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001945 bool Is##type() const { return (As##type() != nullptr); } \
1946 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001947 virtual H##type* As##type() { return nullptr; }
1948
1949 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1950#undef INSTRUCTION_TYPE_CHECK
1951
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001952 // Returns whether the instruction can be moved within the graph.
1953 virtual bool CanBeMoved() const { return false; }
1954
1955 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001956 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001957 return false;
1958 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001959
1960 // Returns whether any data encoded in the two instructions is equal.
1961 // This method does not look at the inputs. Both instructions must be
1962 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001963 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001964 return false;
1965 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001966
1967 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001968 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001969 // 2) Their inputs are identical.
1970 bool Equals(HInstruction* other) const;
1971
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001972 virtual InstructionKind GetKind() const = 0;
1973
1974 virtual size_t ComputeHashCode() const {
1975 size_t result = GetKind();
1976 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1977 result = (result * 31) + InputAt(i)->GetId();
1978 }
1979 return result;
1980 }
1981
1982 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001983 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001984
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001985 size_t GetLifetimePosition() const { return lifetime_position_; }
1986 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1987 LiveInterval* GetLiveInterval() const { return live_interval_; }
1988 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1989 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1990
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001991 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1992
1993 // Returns whether the code generation of the instruction will require to have access
1994 // to the current method. Such instructions are:
1995 // (1): Instructions that require an environment, as calling the runtime requires
1996 // to walk the stack and have the current method stored at a specific stack address.
1997 // (2): Object literals like classes and strings, that are loaded from the dex cache
1998 // fields of the current method.
1999 bool NeedsCurrentMethod() const {
2000 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2001 }
2002
Vladimir Markodc151b22015-10-15 18:02:30 +01002003 // Returns whether the code generation of the instruction will require to have access
2004 // to the dex cache of the current method's declaring class via the current method.
2005 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002006
Mark Mendellc4701932015-04-10 13:18:51 -04002007 // Does this instruction have any use in an environment before
2008 // control flow hits 'other'?
2009 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2010
2011 // Remove all references to environment uses of this instruction.
2012 // The caller must ensure that this is safe to do.
2013 void RemoveEnvironmentUsers();
2014
David Brazdil1abb4192015-02-17 18:33:36 +00002015 protected:
2016 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2017 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2018
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002019 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002020 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2021
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002022 HInstruction* previous_;
2023 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002024 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002025 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002026
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002027 // An instruction gets an id when it is added to the graph.
2028 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002029 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002030 int id_;
2031
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002032 // When doing liveness analysis, instructions that have uses get an SSA index.
2033 int ssa_index_;
2034
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002035 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002036 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002037
2038 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002039 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002040
Nicolas Geoffray39468442014-09-02 15:17:15 +01002041 // The environment associated with this instruction. Not null if the instruction
2042 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002043 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002044
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002045 // Set by the code generator.
2046 LocationSummary* locations_;
2047
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002048 // Set by the liveness analysis.
2049 LiveInterval* live_interval_;
2050
2051 // Set by the liveness analysis, this is the position in a linear
2052 // order of blocks where this instruction's live interval start.
2053 size_t lifetime_position_;
2054
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002055 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002056
Calin Juravleacf735c2015-02-12 15:25:22 +00002057 // TODO: for primitive types this should be marked as invalid.
2058 ReferenceTypeInfo reference_type_info_;
2059
David Brazdil1abb4192015-02-17 18:33:36 +00002060 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002061 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002062 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002063 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002064 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002065
2066 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2067};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002068std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002069
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002070class HInputIterator : public ValueObject {
2071 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002072 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002073
2074 bool Done() const { return index_ == instruction_->InputCount(); }
2075 HInstruction* Current() const { return instruction_->InputAt(index_); }
2076 void Advance() { index_++; }
2077
2078 private:
2079 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002080 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002081
2082 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2083};
2084
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002085class HInstructionIterator : public ValueObject {
2086 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002087 explicit HInstructionIterator(const HInstructionList& instructions)
2088 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002089 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002090 }
2091
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002092 bool Done() const { return instruction_ == nullptr; }
2093 HInstruction* Current() const { return instruction_; }
2094 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002095 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002096 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002097 }
2098
2099 private:
2100 HInstruction* instruction_;
2101 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002102
2103 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002104};
2105
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002106class HBackwardInstructionIterator : public ValueObject {
2107 public:
2108 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2109 : instruction_(instructions.last_instruction_) {
2110 next_ = Done() ? nullptr : instruction_->GetPrevious();
2111 }
2112
2113 bool Done() const { return instruction_ == nullptr; }
2114 HInstruction* Current() const { return instruction_; }
2115 void Advance() {
2116 instruction_ = next_;
2117 next_ = Done() ? nullptr : instruction_->GetPrevious();
2118 }
2119
2120 private:
2121 HInstruction* instruction_;
2122 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002123
2124 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002125};
2126
Vladimir Markof9f64412015-09-02 14:05:49 +01002127template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002128class HTemplateInstruction: public HInstruction {
2129 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002130 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002131 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002132 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002133
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002134 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002135
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002136 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002137 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2138 DCHECK_LT(i, N);
2139 return inputs_[i];
2140 }
David Brazdil1abb4192015-02-17 18:33:36 +00002141
2142 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002143 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002144 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002145 }
2146
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002147 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002148 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002149
2150 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002151};
2152
Vladimir Markof9f64412015-09-02 14:05:49 +01002153// HTemplateInstruction specialization for N=0.
2154template<>
2155class HTemplateInstruction<0>: public HInstruction {
2156 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002157 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002158 : HInstruction(side_effects, dex_pc) {}
2159
Vladimir Markof9f64412015-09-02 14:05:49 +01002160 virtual ~HTemplateInstruction() {}
2161
2162 size_t InputCount() const OVERRIDE { return 0; }
2163
2164 protected:
2165 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2166 LOG(FATAL) << "Unreachable";
2167 UNREACHABLE();
2168 }
2169
2170 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2171 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2172 LOG(FATAL) << "Unreachable";
2173 UNREACHABLE();
2174 }
2175
2176 private:
2177 friend class SsaBuilder;
2178};
2179
Dave Allison20dfc792014-06-16 20:44:29 -07002180template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002181class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002182 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002183 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002184 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002185 virtual ~HExpression() {}
2186
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002187 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002188
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002189 protected:
2190 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002191};
2192
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002193// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2194// instruction that branches to the exit block.
2195class HReturnVoid : public HTemplateInstruction<0> {
2196 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002197 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2198 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002199
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002200 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002201
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002202 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002203
2204 private:
2205 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2206};
2207
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002208// Represents dex's RETURN opcodes. A HReturn is a control flow
2209// instruction that branches to the exit block.
2210class HReturn : public HTemplateInstruction<1> {
2211 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002212 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2213 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002214 SetRawInputAt(0, value);
2215 }
2216
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002217 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002218
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002219 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002220
2221 private:
2222 DISALLOW_COPY_AND_ASSIGN(HReturn);
2223};
2224
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002226// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002227// exit block.
2228class HExit : public HTemplateInstruction<0> {
2229 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002230 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002231
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002232 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002233
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002234 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002235
2236 private:
2237 DISALLOW_COPY_AND_ASSIGN(HExit);
2238};
2239
2240// Jumps from one block to another.
2241class HGoto : public HTemplateInstruction<0> {
2242 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002243 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002244
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002245 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002247 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002248 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002249 }
2250
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002251 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002252
2253 private:
2254 DISALLOW_COPY_AND_ASSIGN(HGoto);
2255};
2256
Roland Levillain9867bc72015-08-05 10:21:34 +01002257class HConstant : public HExpression<0> {
2258 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002259 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2260 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002261
2262 bool CanBeMoved() const OVERRIDE { return true; }
2263
2264 virtual bool IsMinusOne() const { return false; }
2265 virtual bool IsZero() const { return false; }
2266 virtual bool IsOne() const { return false; }
2267
David Brazdil77a48ae2015-09-15 12:34:04 +00002268 virtual uint64_t GetValueAsUint64() const = 0;
2269
Roland Levillain9867bc72015-08-05 10:21:34 +01002270 DECLARE_INSTRUCTION(Constant);
2271
2272 private:
2273 DISALLOW_COPY_AND_ASSIGN(HConstant);
2274};
2275
2276class HNullConstant : public HConstant {
2277 public:
2278 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2279 return true;
2280 }
2281
David Brazdil77a48ae2015-09-15 12:34:04 +00002282 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2283
Roland Levillain9867bc72015-08-05 10:21:34 +01002284 size_t ComputeHashCode() const OVERRIDE { return 0; }
2285
2286 DECLARE_INSTRUCTION(NullConstant);
2287
2288 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002289 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002290
2291 friend class HGraph;
2292 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2293};
2294
2295// Constants of the type int. Those can be from Dex instructions, or
2296// synthesized (for example with the if-eqz instruction).
2297class HIntConstant : public HConstant {
2298 public:
2299 int32_t GetValue() const { return value_; }
2300
David Brazdil9f389d42015-10-01 14:32:56 +01002301 uint64_t GetValueAsUint64() const OVERRIDE {
2302 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2303 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002304
Roland Levillain9867bc72015-08-05 10:21:34 +01002305 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2306 DCHECK(other->IsIntConstant());
2307 return other->AsIntConstant()->value_ == value_;
2308 }
2309
2310 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2311
2312 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2313 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2314 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2315
2316 DECLARE_INSTRUCTION(IntConstant);
2317
2318 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002319 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2320 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2321 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2322 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002323
2324 const int32_t value_;
2325
2326 friend class HGraph;
2327 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2328 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2329 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2330};
2331
2332class HLongConstant : public HConstant {
2333 public:
2334 int64_t GetValue() const { return value_; }
2335
David Brazdil77a48ae2015-09-15 12:34:04 +00002336 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2337
Roland Levillain9867bc72015-08-05 10:21:34 +01002338 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2339 DCHECK(other->IsLongConstant());
2340 return other->AsLongConstant()->value_ == value_;
2341 }
2342
2343 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2344
2345 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2346 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2347 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2348
2349 DECLARE_INSTRUCTION(LongConstant);
2350
2351 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002352 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2353 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002354
2355 const int64_t value_;
2356
2357 friend class HGraph;
2358 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2359};
Dave Allison20dfc792014-06-16 20:44:29 -07002360
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002361// Conditional branch. A block ending with an HIf instruction must have
2362// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002363class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002364 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002365 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2366 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002367 SetRawInputAt(0, input);
2368 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002369
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002370 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002371
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002372 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002373 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002374 }
2375
2376 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002377 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002378 }
2379
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002380 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002381
2382 private:
2383 DISALLOW_COPY_AND_ASSIGN(HIf);
2384};
2385
David Brazdilfc6a86a2015-06-26 10:33:45 +00002386
2387// Abstract instruction which marks the beginning and/or end of a try block and
2388// links it to the respective exception handlers. Behaves the same as a Goto in
2389// non-exceptional control flow.
2390// Normal-flow successor is stored at index zero, exception handlers under
2391// higher indices in no particular order.
2392class HTryBoundary : public HTemplateInstruction<0> {
2393 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002394 enum BoundaryKind {
2395 kEntry,
2396 kExit,
2397 };
2398
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002399 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2400 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002401
2402 bool IsControlFlow() const OVERRIDE { return true; }
2403
2404 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002405 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002406
David Brazdild26a4112015-11-10 11:07:31 +00002407 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2408 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2409 }
2410
David Brazdilfc6a86a2015-06-26 10:33:45 +00002411 // Returns whether `handler` is among its exception handlers (non-zero index
2412 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002413 bool HasExceptionHandler(const HBasicBlock& handler) const {
2414 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002415 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002416 }
2417
2418 // If not present already, adds `handler` to its block's list of exception
2419 // handlers.
2420 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002421 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002422 GetBlock()->AddSuccessor(handler);
2423 }
2424 }
2425
David Brazdil56e1acc2015-06-30 15:41:36 +01002426 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002427
David Brazdilffee3d32015-07-06 11:48:53 +01002428 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2429
David Brazdilfc6a86a2015-06-26 10:33:45 +00002430 DECLARE_INSTRUCTION(TryBoundary);
2431
2432 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002433 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002434
2435 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2436};
2437
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002438// Deoptimize to interpreter, upon checking a condition.
2439class HDeoptimize : public HTemplateInstruction<1> {
2440 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002441 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2442 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002443 SetRawInputAt(0, cond);
2444 }
2445
2446 bool NeedsEnvironment() const OVERRIDE { return true; }
2447 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002448
2449 DECLARE_INSTRUCTION(Deoptimize);
2450
2451 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002452 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2453};
2454
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002455// Represents the ArtMethod that was passed as a first argument to
2456// the method. It is used by instructions that depend on it, like
2457// instructions that work with the dex cache.
2458class HCurrentMethod : public HExpression<0> {
2459 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002460 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2461 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002462
2463 DECLARE_INSTRUCTION(CurrentMethod);
2464
2465 private:
2466 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2467};
2468
Mark Mendellfe57faa2015-09-18 09:26:15 -04002469// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2470// have one successor for each entry in the switch table, and the final successor
2471// will be the block containing the next Dex opcode.
2472class HPackedSwitch : public HTemplateInstruction<1> {
2473 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002474 HPackedSwitch(int32_t start_value,
2475 uint32_t num_entries,
2476 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002477 uint32_t dex_pc = kNoDexPc)
2478 : HTemplateInstruction(SideEffects::None(), dex_pc),
2479 start_value_(start_value),
2480 num_entries_(num_entries) {
2481 SetRawInputAt(0, input);
2482 }
2483
2484 bool IsControlFlow() const OVERRIDE { return true; }
2485
2486 int32_t GetStartValue() const { return start_value_; }
2487
Vladimir Marko211c2112015-09-24 16:52:33 +01002488 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002489
2490 HBasicBlock* GetDefaultBlock() const {
2491 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002492 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002493 }
2494 DECLARE_INSTRUCTION(PackedSwitch);
2495
2496 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002497 const int32_t start_value_;
2498 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002499
2500 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2501};
2502
Roland Levillain88cb1752014-10-20 16:36:47 +01002503class HUnaryOperation : public HExpression<1> {
2504 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002505 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2506 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002507 SetRawInputAt(0, input);
2508 }
2509
2510 HInstruction* GetInput() const { return InputAt(0); }
2511 Primitive::Type GetResultType() const { return GetType(); }
2512
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002513 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002514 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002515 return true;
2516 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002517
Roland Levillain9240d6a2014-10-20 16:47:04 +01002518 // Try to statically evaluate `operation` and return a HConstant
2519 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002520 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002521 HConstant* TryStaticEvaluation() const;
2522
2523 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002524 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2525 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002526
Roland Levillain88cb1752014-10-20 16:36:47 +01002527 DECLARE_INSTRUCTION(UnaryOperation);
2528
2529 private:
2530 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2531};
2532
Dave Allison20dfc792014-06-16 20:44:29 -07002533class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002534 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002535 HBinaryOperation(Primitive::Type result_type,
2536 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002537 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002538 SideEffects side_effects = SideEffects::None(),
2539 uint32_t dex_pc = kNoDexPc)
2540 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002541 SetRawInputAt(0, left);
2542 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002543 }
2544
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002545 HInstruction* GetLeft() const { return InputAt(0); }
2546 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002547 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002548
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002549 virtual bool IsCommutative() const { return false; }
2550
2551 // Put constant on the right.
2552 // Returns whether order is changed.
2553 bool OrderInputsWithConstantOnTheRight() {
2554 HInstruction* left = InputAt(0);
2555 HInstruction* right = InputAt(1);
2556 if (left->IsConstant() && !right->IsConstant()) {
2557 ReplaceInput(right, 0);
2558 ReplaceInput(left, 1);
2559 return true;
2560 }
2561 return false;
2562 }
2563
2564 // Order inputs by instruction id, but favor constant on the right side.
2565 // This helps GVN for commutative ops.
2566 void OrderInputs() {
2567 DCHECK(IsCommutative());
2568 HInstruction* left = InputAt(0);
2569 HInstruction* right = InputAt(1);
2570 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2571 return;
2572 }
2573 if (OrderInputsWithConstantOnTheRight()) {
2574 return;
2575 }
2576 // Order according to instruction id.
2577 if (left->GetId() > right->GetId()) {
2578 ReplaceInput(right, 0);
2579 ReplaceInput(left, 1);
2580 }
2581 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002582
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002583 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002584 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002585 return true;
2586 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002587
Roland Levillain9240d6a2014-10-20 16:47:04 +01002588 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002589 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002590 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002591 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002592
2593 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002594 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2595 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2596 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2597 HLongConstant* y ATTRIBUTE_UNUSED) const {
2598 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2599 return nullptr;
2600 }
2601 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2602 HIntConstant* y ATTRIBUTE_UNUSED) const {
2603 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2604 return nullptr;
2605 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002606 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2607 HNullConstant* y ATTRIBUTE_UNUSED) const {
2608 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2609 return nullptr;
2610 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002611
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002612 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002613 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002614 HConstant* GetConstantRight() const;
2615
2616 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002617 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002618 HInstruction* GetLeastConstantLeft() const;
2619
Roland Levillainccc07a92014-09-16 14:48:16 +01002620 DECLARE_INSTRUCTION(BinaryOperation);
2621
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002622 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002623 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2624};
2625
Mark Mendellc4701932015-04-10 13:18:51 -04002626// The comparison bias applies for floating point operations and indicates how NaN
2627// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002628enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002629 kNoBias, // bias is not applicable (i.e. for long operation)
2630 kGtBias, // return 1 for NaN comparisons
2631 kLtBias, // return -1 for NaN comparisons
2632};
2633
Dave Allison20dfc792014-06-16 20:44:29 -07002634class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002635 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002636 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2637 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002638 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002639 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002640
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002641 bool NeedsMaterialization() const { return needs_materialization_; }
2642 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002643
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002644 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002645 // `instruction`, and disregard moves in between.
2646 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002647
Dave Allison20dfc792014-06-16 20:44:29 -07002648 DECLARE_INSTRUCTION(Condition);
2649
2650 virtual IfCondition GetCondition() const = 0;
2651
Mark Mendellc4701932015-04-10 13:18:51 -04002652 virtual IfCondition GetOppositeCondition() const = 0;
2653
Roland Levillain4fa13f62015-07-06 18:11:54 +01002654 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002655
2656 void SetBias(ComparisonBias bias) { bias_ = bias; }
2657
2658 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2659 return bias_ == other->AsCondition()->bias_;
2660 }
2661
Roland Levillain4fa13f62015-07-06 18:11:54 +01002662 bool IsFPConditionTrueIfNaN() const {
2663 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2664 IfCondition if_cond = GetCondition();
2665 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2666 }
2667
2668 bool IsFPConditionFalseIfNaN() const {
2669 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2670 IfCondition if_cond = GetCondition();
2671 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2672 }
2673
Dave Allison20dfc792014-06-16 20:44:29 -07002674 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002675 // For register allocation purposes, returns whether this instruction needs to be
2676 // materialized (that is, not just be in the processor flags).
2677 bool needs_materialization_;
2678
Mark Mendellc4701932015-04-10 13:18:51 -04002679 // Needed if we merge a HCompare into a HCondition.
2680 ComparisonBias bias_;
2681
Dave Allison20dfc792014-06-16 20:44:29 -07002682 DISALLOW_COPY_AND_ASSIGN(HCondition);
2683};
2684
2685// Instruction to check if two inputs are equal to each other.
2686class HEqual : public HCondition {
2687 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002688 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2689 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002690
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002691 bool IsCommutative() const OVERRIDE { return true; }
2692
Roland Levillain9867bc72015-08-05 10:21:34 +01002693 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002694 return GetBlock()->GetGraph()->GetIntConstant(
2695 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002696 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002697 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002698 return GetBlock()->GetGraph()->GetIntConstant(
2699 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002700 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002701 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2702 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002703 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002704 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002705
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002706 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002707
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002708 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002709 return kCondEQ;
2710 }
2711
Mark Mendellc4701932015-04-10 13:18:51 -04002712 IfCondition GetOppositeCondition() const OVERRIDE {
2713 return kCondNE;
2714 }
2715
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002716 private:
Aart Bike9f37602015-10-09 11:15:55 -07002717 template <typename T> bool Compute(T x, T y) const { return x == y; }
2718
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002719 DISALLOW_COPY_AND_ASSIGN(HEqual);
2720};
2721
Dave Allison20dfc792014-06-16 20:44:29 -07002722class HNotEqual : public HCondition {
2723 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002724 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2725 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002726
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002727 bool IsCommutative() const OVERRIDE { return true; }
2728
Roland Levillain9867bc72015-08-05 10:21:34 +01002729 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002730 return GetBlock()->GetGraph()->GetIntConstant(
2731 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002732 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002733 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002734 return GetBlock()->GetGraph()->GetIntConstant(
2735 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002736 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002737 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2738 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002739 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002740 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002741
Dave Allison20dfc792014-06-16 20:44:29 -07002742 DECLARE_INSTRUCTION(NotEqual);
2743
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002744 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002745 return kCondNE;
2746 }
2747
Mark Mendellc4701932015-04-10 13:18:51 -04002748 IfCondition GetOppositeCondition() const OVERRIDE {
2749 return kCondEQ;
2750 }
2751
Dave Allison20dfc792014-06-16 20:44:29 -07002752 private:
Aart Bike9f37602015-10-09 11:15:55 -07002753 template <typename T> bool Compute(T x, T y) const { return x != y; }
2754
Dave Allison20dfc792014-06-16 20:44:29 -07002755 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2756};
2757
2758class HLessThan : public HCondition {
2759 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002760 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2761 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002762
Roland Levillain9867bc72015-08-05 10:21:34 +01002763 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002764 return GetBlock()->GetGraph()->GetIntConstant(
2765 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002766 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002767 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002768 return GetBlock()->GetGraph()->GetIntConstant(
2769 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002770 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002771
Dave Allison20dfc792014-06-16 20:44:29 -07002772 DECLARE_INSTRUCTION(LessThan);
2773
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002774 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002775 return kCondLT;
2776 }
2777
Mark Mendellc4701932015-04-10 13:18:51 -04002778 IfCondition GetOppositeCondition() const OVERRIDE {
2779 return kCondGE;
2780 }
2781
Dave Allison20dfc792014-06-16 20:44:29 -07002782 private:
Aart Bike9f37602015-10-09 11:15:55 -07002783 template <typename T> bool Compute(T x, T y) const { return x < y; }
2784
Dave Allison20dfc792014-06-16 20:44:29 -07002785 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2786};
2787
2788class HLessThanOrEqual : public HCondition {
2789 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002790 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2791 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002792
Roland Levillain9867bc72015-08-05 10:21:34 +01002793 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002794 return GetBlock()->GetGraph()->GetIntConstant(
2795 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002796 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002797 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002798 return GetBlock()->GetGraph()->GetIntConstant(
2799 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002800 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002801
Dave Allison20dfc792014-06-16 20:44:29 -07002802 DECLARE_INSTRUCTION(LessThanOrEqual);
2803
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002804 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002805 return kCondLE;
2806 }
2807
Mark Mendellc4701932015-04-10 13:18:51 -04002808 IfCondition GetOppositeCondition() const OVERRIDE {
2809 return kCondGT;
2810 }
2811
Dave Allison20dfc792014-06-16 20:44:29 -07002812 private:
Aart Bike9f37602015-10-09 11:15:55 -07002813 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2814
Dave Allison20dfc792014-06-16 20:44:29 -07002815 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2816};
2817
2818class HGreaterThan : public HCondition {
2819 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002820 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2821 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002822
Roland Levillain9867bc72015-08-05 10:21:34 +01002823 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002824 return GetBlock()->GetGraph()->GetIntConstant(
2825 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002826 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002827 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002828 return GetBlock()->GetGraph()->GetIntConstant(
2829 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002830 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002831
Dave Allison20dfc792014-06-16 20:44:29 -07002832 DECLARE_INSTRUCTION(GreaterThan);
2833
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002834 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002835 return kCondGT;
2836 }
2837
Mark Mendellc4701932015-04-10 13:18:51 -04002838 IfCondition GetOppositeCondition() const OVERRIDE {
2839 return kCondLE;
2840 }
2841
Dave Allison20dfc792014-06-16 20:44:29 -07002842 private:
Aart Bike9f37602015-10-09 11:15:55 -07002843 template <typename T> bool Compute(T x, T y) const { return x > y; }
2844
Dave Allison20dfc792014-06-16 20:44:29 -07002845 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2846};
2847
2848class HGreaterThanOrEqual : public HCondition {
2849 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002850 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2851 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002852
Roland Levillain9867bc72015-08-05 10:21:34 +01002853 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002854 return GetBlock()->GetGraph()->GetIntConstant(
2855 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002856 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002857 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002858 return GetBlock()->GetGraph()->GetIntConstant(
2859 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002860 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002861
Dave Allison20dfc792014-06-16 20:44:29 -07002862 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2863
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002864 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002865 return kCondGE;
2866 }
2867
Mark Mendellc4701932015-04-10 13:18:51 -04002868 IfCondition GetOppositeCondition() const OVERRIDE {
2869 return kCondLT;
2870 }
2871
Dave Allison20dfc792014-06-16 20:44:29 -07002872 private:
Aart Bike9f37602015-10-09 11:15:55 -07002873 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2874
Dave Allison20dfc792014-06-16 20:44:29 -07002875 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2876};
2877
Aart Bike9f37602015-10-09 11:15:55 -07002878class HBelow : public HCondition {
2879 public:
2880 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2881 : HCondition(first, second, dex_pc) {}
2882
2883 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2884 return GetBlock()->GetGraph()->GetIntConstant(
2885 Compute(static_cast<uint32_t>(x->GetValue()),
2886 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2887 }
2888 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2889 return GetBlock()->GetGraph()->GetIntConstant(
2890 Compute(static_cast<uint64_t>(x->GetValue()),
2891 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2892 }
2893
2894 DECLARE_INSTRUCTION(Below);
2895
2896 IfCondition GetCondition() const OVERRIDE {
2897 return kCondB;
2898 }
2899
2900 IfCondition GetOppositeCondition() const OVERRIDE {
2901 return kCondAE;
2902 }
2903
2904 private:
2905 template <typename T> bool Compute(T x, T y) const { return x < y; }
2906
2907 DISALLOW_COPY_AND_ASSIGN(HBelow);
2908};
2909
2910class HBelowOrEqual : public HCondition {
2911 public:
2912 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2913 : HCondition(first, second, dex_pc) {}
2914
2915 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2916 return GetBlock()->GetGraph()->GetIntConstant(
2917 Compute(static_cast<uint32_t>(x->GetValue()),
2918 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2919 }
2920 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2921 return GetBlock()->GetGraph()->GetIntConstant(
2922 Compute(static_cast<uint64_t>(x->GetValue()),
2923 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2924 }
2925
2926 DECLARE_INSTRUCTION(BelowOrEqual);
2927
2928 IfCondition GetCondition() const OVERRIDE {
2929 return kCondBE;
2930 }
2931
2932 IfCondition GetOppositeCondition() const OVERRIDE {
2933 return kCondA;
2934 }
2935
2936 private:
2937 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2938
2939 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2940};
2941
2942class HAbove : public HCondition {
2943 public:
2944 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2945 : HCondition(first, second, dex_pc) {}
2946
2947 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2948 return GetBlock()->GetGraph()->GetIntConstant(
2949 Compute(static_cast<uint32_t>(x->GetValue()),
2950 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2951 }
2952 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2953 return GetBlock()->GetGraph()->GetIntConstant(
2954 Compute(static_cast<uint64_t>(x->GetValue()),
2955 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2956 }
2957
2958 DECLARE_INSTRUCTION(Above);
2959
2960 IfCondition GetCondition() const OVERRIDE {
2961 return kCondA;
2962 }
2963
2964 IfCondition GetOppositeCondition() const OVERRIDE {
2965 return kCondBE;
2966 }
2967
2968 private:
2969 template <typename T> bool Compute(T x, T y) const { return x > y; }
2970
2971 DISALLOW_COPY_AND_ASSIGN(HAbove);
2972};
2973
2974class HAboveOrEqual : public HCondition {
2975 public:
2976 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2977 : HCondition(first, second, dex_pc) {}
2978
2979 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2980 return GetBlock()->GetGraph()->GetIntConstant(
2981 Compute(static_cast<uint32_t>(x->GetValue()),
2982 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2983 }
2984 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2985 return GetBlock()->GetGraph()->GetIntConstant(
2986 Compute(static_cast<uint64_t>(x->GetValue()),
2987 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2988 }
2989
2990 DECLARE_INSTRUCTION(AboveOrEqual);
2991
2992 IfCondition GetCondition() const OVERRIDE {
2993 return kCondAE;
2994 }
2995
2996 IfCondition GetOppositeCondition() const OVERRIDE {
2997 return kCondB;
2998 }
2999
3000 private:
3001 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3002
3003 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3004};
Dave Allison20dfc792014-06-16 20:44:29 -07003005
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003006// Instruction to check how two inputs compare to each other.
3007// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3008class HCompare : public HBinaryOperation {
3009 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003010 HCompare(Primitive::Type type,
3011 HInstruction* first,
3012 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003013 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003014 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003015 : HBinaryOperation(Primitive::kPrimInt,
3016 first,
3017 second,
3018 SideEffectsForArchRuntimeCalls(type),
3019 dex_pc),
3020 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003021 DCHECK_EQ(type, first->GetType());
3022 DCHECK_EQ(type, second->GetType());
3023 }
3024
Roland Levillain9867bc72015-08-05 10:21:34 +01003025 template <typename T>
3026 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003027
Roland Levillain9867bc72015-08-05 10:21:34 +01003028 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003029 return GetBlock()->GetGraph()->GetIntConstant(
3030 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003031 }
3032 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003033 return GetBlock()->GetGraph()->GetIntConstant(
3034 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003035 }
3036
Calin Juravleddb7df22014-11-25 20:56:51 +00003037 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3038 return bias_ == other->AsCompare()->bias_;
3039 }
3040
Mark Mendellc4701932015-04-10 13:18:51 -04003041 ComparisonBias GetBias() const { return bias_; }
3042
Roland Levillain4fa13f62015-07-06 18:11:54 +01003043 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003044
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003045
3046 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3047 // MIPS64 uses a runtime call for FP comparisons.
3048 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3049 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003050
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003051 DECLARE_INSTRUCTION(Compare);
3052
3053 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003054 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003055
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003056 DISALLOW_COPY_AND_ASSIGN(HCompare);
3057};
3058
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003059// A local in the graph. Corresponds to a Dex register.
3060class HLocal : public HTemplateInstruction<0> {
3061 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003062 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003063 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003064
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003065 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003066
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003067 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003068
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003069 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003070 // The Dex register number.
3071 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003072
3073 DISALLOW_COPY_AND_ASSIGN(HLocal);
3074};
3075
3076// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003077class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003078 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003079 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3080 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003081 SetRawInputAt(0, local);
3082 }
3083
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003084 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3085
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003086 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003087
3088 private:
3089 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3090};
3091
3092// Store a value in a given local. This instruction has two inputs: the value
3093// and the local.
3094class HStoreLocal : public HTemplateInstruction<2> {
3095 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003096 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3097 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003098 SetRawInputAt(0, local);
3099 SetRawInputAt(1, value);
3100 }
3101
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003102 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3103
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003104 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003105
3106 private:
3107 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3108};
3109
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003110class HFloatConstant : public HConstant {
3111 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003112 float GetValue() const { return value_; }
3113
David Brazdil77a48ae2015-09-15 12:34:04 +00003114 uint64_t GetValueAsUint64() const OVERRIDE {
3115 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3116 }
3117
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003118 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003119 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003120 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003121 }
3122
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003123 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003124
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003125 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003126 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003127 }
3128 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003129 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003130 }
3131 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003132 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3133 }
3134 bool IsNaN() const {
3135 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003136 }
3137
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003138 DECLARE_INSTRUCTION(FloatConstant);
3139
3140 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003141 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3142 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3143 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3144 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003145
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003146 const float value_;
3147
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003148 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003149 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003150 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003151 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3152};
3153
3154class HDoubleConstant : public HConstant {
3155 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003156 double GetValue() const { return value_; }
3157
David Brazdil77a48ae2015-09-15 12:34:04 +00003158 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3159
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003160 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003161 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003162 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003163 }
3164
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003165 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003166
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003167 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003168 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003169 }
3170 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003171 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003172 }
3173 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003174 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3175 }
3176 bool IsNaN() const {
3177 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003178 }
3179
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003180 DECLARE_INSTRUCTION(DoubleConstant);
3181
3182 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003183 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3184 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3185 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3186 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003187
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003188 const double value_;
3189
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003190 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003191 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003192 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003193 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3194};
3195
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003196enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003197#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003198#include "intrinsics_list.h"
3199 kNone,
3200 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3201#undef INTRINSICS_LIST
3202#undef OPTIMIZING_INTRINSICS
3203};
3204std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3205
Agi Csaki05f20562015-08-19 14:58:14 -07003206enum IntrinsicNeedsEnvironmentOrCache {
3207 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3208 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003209};
3210
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003211class HInvoke : public HInstruction {
3212 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003213 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003214
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003215 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003216
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003217 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003218 SetRawInputAt(index, argument);
3219 }
3220
Roland Levillain3e3d7332015-04-28 11:00:54 +01003221 // Return the number of arguments. This number can be lower than
3222 // the number of inputs returned by InputCount(), as some invoke
3223 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3224 // inputs at the end of their list of inputs.
3225 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3226
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003227 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003228
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003229
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003230 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003231 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003232
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003233 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3234
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003235 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003236 return intrinsic_;
3237 }
3238
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003239 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003240
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003241 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003242 return GetEnvironment()->GetParent() != nullptr;
3243 }
3244
3245 bool CanThrow() const OVERRIDE { return true; }
3246
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003247 uint32_t* GetIntrinsicOptimizations() {
3248 return &intrinsic_optimizations_;
3249 }
3250
3251 const uint32_t* GetIntrinsicOptimizations() const {
3252 return &intrinsic_optimizations_;
3253 }
3254
3255 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3256
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003257 DECLARE_INSTRUCTION(Invoke);
3258
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003259 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003260 HInvoke(ArenaAllocator* arena,
3261 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003262 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003263 Primitive::Type return_type,
3264 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003265 uint32_t dex_method_index,
3266 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003267 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003268 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003269 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003270 inputs_(number_of_arguments + number_of_other_inputs,
3271 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003272 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003273 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003274 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003275 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003276 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003277 }
3278
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003279 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003280 return inputs_[index];
3281 }
3282
David Brazdil1abb4192015-02-17 18:33:36 +00003283 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003284 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003285 }
3286
Roland Levillain3e3d7332015-04-28 11:00:54 +01003287 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003288 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003289 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003290 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003291 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003292 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003293
3294 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3295 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003296
3297 private:
3298 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3299};
3300
Calin Juravle175dc732015-08-25 15:42:32 +01003301class HInvokeUnresolved : public HInvoke {
3302 public:
3303 HInvokeUnresolved(ArenaAllocator* arena,
3304 uint32_t number_of_arguments,
3305 Primitive::Type return_type,
3306 uint32_t dex_pc,
3307 uint32_t dex_method_index,
3308 InvokeType invoke_type)
3309 : HInvoke(arena,
3310 number_of_arguments,
3311 0u /* number_of_other_inputs */,
3312 return_type,
3313 dex_pc,
3314 dex_method_index,
3315 invoke_type) {
3316 }
3317
3318 DECLARE_INSTRUCTION(InvokeUnresolved);
3319
3320 private:
3321 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3322};
3323
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003324class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003325 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003326 // Requirements of this method call regarding the class
3327 // initialization (clinit) check of its declaring class.
3328 enum class ClinitCheckRequirement {
3329 kNone, // Class already initialized.
3330 kExplicit, // Static call having explicit clinit check as last input.
3331 kImplicit, // Static call implicitly requiring a clinit check.
3332 };
3333
Vladimir Marko58155012015-08-19 12:49:41 +00003334 // Determines how to load the target ArtMethod*.
3335 enum class MethodLoadKind {
3336 // Use a String init ArtMethod* loaded from Thread entrypoints.
3337 kStringInit,
3338
3339 // Use the method's own ArtMethod* loaded by the register allocator.
3340 kRecursive,
3341
3342 // Use ArtMethod* at a known address, embed the direct address in the code.
3343 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3344 kDirectAddress,
3345
3346 // Use ArtMethod* at an address that will be known at link time, embed the direct
3347 // address in the code. If the image is relocatable, emit .patch_oat entry.
3348 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3349 // the image relocatable or not.
3350 kDirectAddressWithFixup,
3351
3352 // Load from resoved methods array in the dex cache using a PC-relative load.
3353 // Used when we need to use the dex cache, for example for invoke-static that
3354 // may cause class initialization (the entry may point to a resolution method),
3355 // and we know that we can access the dex cache arrays using a PC-relative load.
3356 kDexCachePcRelative,
3357
3358 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3359 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3360 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3361 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3362 kDexCacheViaMethod,
3363 };
3364
3365 // Determines the location of the code pointer.
3366 enum class CodePtrLocation {
3367 // Recursive call, use local PC-relative call instruction.
3368 kCallSelf,
3369
3370 // Use PC-relative call instruction patched at link time.
3371 // Used for calls within an oat file, boot->boot or app->app.
3372 kCallPCRelative,
3373
3374 // Call to a known target address, embed the direct address in code.
3375 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3376 kCallDirect,
3377
3378 // Call to a target address that will be known at link time, embed the direct
3379 // address in code. If the image is relocatable, emit .patch_oat entry.
3380 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3381 // the image relocatable or not.
3382 kCallDirectWithFixup,
3383
3384 // Use code pointer from the ArtMethod*.
3385 // Used when we don't know the target code. This is also the last-resort-kind used when
3386 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3387 kCallArtMethod,
3388 };
3389
3390 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003391 MethodLoadKind method_load_kind;
3392 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003393 // The method load data holds
3394 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3395 // Note that there are multiple string init methods, each having its own offset.
3396 // - the method address for kDirectAddress
3397 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003398 uint64_t method_load_data;
3399 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003400 };
3401
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003402 HInvokeStaticOrDirect(ArenaAllocator* arena,
3403 uint32_t number_of_arguments,
3404 Primitive::Type return_type,
3405 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003406 uint32_t method_index,
3407 MethodReference target_method,
3408 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003409 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003410 InvokeType invoke_type,
3411 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003412 : HInvoke(arena,
3413 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003414 // There is potentially one extra argument for the HCurrentMethod node, and
3415 // potentially one other if the clinit check is explicit, and potentially
3416 // one other if the method is a string factory.
3417 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3418 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3419 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003420 return_type,
3421 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003422 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003423 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003424 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003425 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003426 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003427 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003428
Vladimir Markodc151b22015-10-15 18:02:30 +01003429 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003430 bool had_current_method_input = HasCurrentMethodInput();
3431 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3432
3433 // Using the current method is the default and once we find a better
3434 // method load kind, we should not go back to using the current method.
3435 DCHECK(had_current_method_input || !needs_current_method_input);
3436
3437 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003438 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3439 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003440 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003441 dispatch_info_ = dispatch_info;
3442 }
3443
Vladimir Markoc53c0792015-11-19 15:48:33 +00003444 void AddSpecialInput(HInstruction* input) {
3445 // We allow only one special input.
3446 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3447 DCHECK(InputCount() == GetSpecialInputIndex() ||
3448 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3449 InsertInputAt(GetSpecialInputIndex(), input);
3450 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003451
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003452 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003453 // We access the method via the dex cache so we can't do an implicit null check.
3454 // TODO: for intrinsics we can generate implicit null checks.
3455 return false;
3456 }
3457
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003458 bool CanBeNull() const OVERRIDE {
3459 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3460 }
3461
Vladimir Markoc53c0792015-11-19 15:48:33 +00003462 // Get the index of the special input, if any.
3463 //
3464 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3465 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3466 // the "special input" is the current method pointer; otherwise there may be one
3467 // platform-specific special input, such as PC-relative addressing base.
3468 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3469
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003470 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003471 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3472 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3473 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003474 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003475 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003476 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003477 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003478 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3479 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003480 bool HasCurrentMethodInput() const {
3481 // This function can be called only after the invoke has been fully initialized by the builder.
3482 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003483 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003484 return true;
3485 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003486 DCHECK(InputCount() == GetSpecialInputIndex() ||
3487 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003488 return false;
3489 }
3490 }
Vladimir Marko58155012015-08-19 12:49:41 +00003491 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3492 MethodReference GetTargetMethod() const { return target_method_; }
3493
3494 int32_t GetStringInitOffset() const {
3495 DCHECK(IsStringInit());
3496 return dispatch_info_.method_load_data;
3497 }
3498
3499 uint64_t GetMethodAddress() const {
3500 DCHECK(HasMethodAddress());
3501 return dispatch_info_.method_load_data;
3502 }
3503
3504 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003505 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003506 return dispatch_info_.method_load_data;
3507 }
3508
3509 uint64_t GetDirectCodePtr() const {
3510 DCHECK(HasDirectCodePtr());
3511 return dispatch_info_.direct_code_ptr;
3512 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003513
Calin Juravle68ad6492015-08-18 17:08:12 +01003514 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3515
Roland Levillain4c0eb422015-04-24 16:43:49 +01003516 // Is this instruction a call to a static method?
3517 bool IsStatic() const {
3518 return GetInvokeType() == kStatic;
3519 }
3520
Vladimir Markofbb184a2015-11-13 14:47:00 +00003521 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3522 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3523 // instruction; only relevant for static calls with explicit clinit check.
3524 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003525 DCHECK(IsStaticWithExplicitClinitCheck());
3526 size_t last_input_index = InputCount() - 1;
3527 HInstruction* last_input = InputAt(last_input_index);
3528 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003529 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003530 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003531 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003532 clinit_check_requirement_ = new_requirement;
3533 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003534 }
3535
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003536 bool IsStringFactoryFor(HFakeString* str) const {
3537 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003538 DCHECK(!HasCurrentMethodInput());
3539 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003540 return InputAt(InputCount() - 1)->AsFakeString() == str;
3541 }
3542
3543 void RemoveFakeStringArgumentAsLastInput() {
3544 DCHECK(IsStringInit());
3545 size_t last_input_index = InputCount() - 1;
3546 HInstruction* last_input = InputAt(last_input_index);
3547 DCHECK(last_input != nullptr);
3548 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3549 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003550 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003551 }
3552
Roland Levillain4c0eb422015-04-24 16:43:49 +01003553 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003554 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003555 bool IsStaticWithExplicitClinitCheck() const {
3556 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3557 }
3558
3559 // Is this a call to a static method whose declaring class has an
3560 // implicit intialization check requirement?
3561 bool IsStaticWithImplicitClinitCheck() const {
3562 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3563 }
3564
Vladimir Markob554b5a2015-11-06 12:57:55 +00003565 // Does this method load kind need the current method as an input?
3566 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3567 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3568 }
3569
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003570 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003571
Roland Levillain4c0eb422015-04-24 16:43:49 +01003572 protected:
3573 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3574 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3575 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3576 HInstruction* input = input_record.GetInstruction();
3577 // `input` is the last input of a static invoke marked as having
3578 // an explicit clinit check. It must either be:
3579 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3580 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3581 DCHECK(input != nullptr);
3582 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3583 }
3584 return input_record;
3585 }
3586
Vladimir Markoc53c0792015-11-19 15:48:33 +00003587 void InsertInputAt(size_t index, HInstruction* input);
3588 void RemoveInputAt(size_t index);
3589
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003590 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003591 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003592 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003593 // The target method may refer to different dex file or method index than the original
3594 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3595 // in derived class to increase visibility.
3596 MethodReference target_method_;
3597 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003598
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003599 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003600};
Vladimir Markofbb184a2015-11-13 14:47:00 +00003601std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003602
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003603class HInvokeVirtual : public HInvoke {
3604 public:
3605 HInvokeVirtual(ArenaAllocator* arena,
3606 uint32_t number_of_arguments,
3607 Primitive::Type return_type,
3608 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003609 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003610 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003611 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003612 vtable_index_(vtable_index) {}
3613
Calin Juravle641547a2015-04-21 22:08:51 +01003614 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003615 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003616 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003617 }
3618
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003619 uint32_t GetVTableIndex() const { return vtable_index_; }
3620
3621 DECLARE_INSTRUCTION(InvokeVirtual);
3622
3623 private:
3624 const uint32_t vtable_index_;
3625
3626 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3627};
3628
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003629class HInvokeInterface : public HInvoke {
3630 public:
3631 HInvokeInterface(ArenaAllocator* arena,
3632 uint32_t number_of_arguments,
3633 Primitive::Type return_type,
3634 uint32_t dex_pc,
3635 uint32_t dex_method_index,
3636 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003637 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003638 imt_index_(imt_index) {}
3639
Calin Juravle641547a2015-04-21 22:08:51 +01003640 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003641 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003642 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003643 }
3644
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003645 uint32_t GetImtIndex() const { return imt_index_; }
3646 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3647
3648 DECLARE_INSTRUCTION(InvokeInterface);
3649
3650 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003651 const uint32_t imt_index_;
3652
3653 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3654};
3655
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003656class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003657 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003658 HNewInstance(HCurrentMethod* current_method,
3659 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003660 uint16_t type_index,
3661 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003662 bool can_throw,
3663 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003664 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003665 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003666 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003667 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003668 can_throw_(can_throw),
3669 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003670 entrypoint_(entrypoint) {
3671 SetRawInputAt(0, current_method);
3672 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003673
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003674 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003675 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003676
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003677 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003678 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003679
3680 // It may throw when called on type that's not instantiable/accessible.
3681 // It can throw OOME.
3682 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3683 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3684
3685 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003686
Calin Juravle10e244f2015-01-26 18:54:32 +00003687 bool CanBeNull() const OVERRIDE { return false; }
3688
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003689 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3690
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003691 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003692
3693 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003694 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003695 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003696 const bool can_throw_;
3697 const bool finalizable_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003698 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003699
3700 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3701};
3702
Roland Levillain88cb1752014-10-20 16:36:47 +01003703class HNeg : public HUnaryOperation {
3704 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003705 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3706 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003707
Roland Levillain9867bc72015-08-05 10:21:34 +01003708 template <typename T> T Compute(T x) const { return -x; }
3709
3710 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003711 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003712 }
3713 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003714 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003715 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003716
Roland Levillain88cb1752014-10-20 16:36:47 +01003717 DECLARE_INSTRUCTION(Neg);
3718
3719 private:
3720 DISALLOW_COPY_AND_ASSIGN(HNeg);
3721};
3722
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003723class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003724 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003725 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003726 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003727 uint32_t dex_pc,
3728 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003729 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003730 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003731 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003732 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003733 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003734 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003735 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003736 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003737 }
3738
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003739 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003740 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003741
3742 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003743 bool NeedsEnvironment() const OVERRIDE { return true; }
3744
Mingyao Yang0c365e62015-03-31 15:09:29 -07003745 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3746 bool CanThrow() const OVERRIDE { return true; }
3747
Calin Juravle10e244f2015-01-26 18:54:32 +00003748 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003749
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003750 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3751
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003752 DECLARE_INSTRUCTION(NewArray);
3753
3754 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003755 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003756 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003757 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003758
3759 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3760};
3761
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003762class HAdd : public HBinaryOperation {
3763 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003764 HAdd(Primitive::Type result_type,
3765 HInstruction* left,
3766 HInstruction* right,
3767 uint32_t dex_pc = kNoDexPc)
3768 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003769
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003770 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003771
Roland Levillain9867bc72015-08-05 10:21:34 +01003772 template <typename T> T Compute(T x, T y) const { return x + y; }
3773
3774 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003775 return GetBlock()->GetGraph()->GetIntConstant(
3776 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003777 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003778 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003779 return GetBlock()->GetGraph()->GetLongConstant(
3780 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003781 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003782
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003783 DECLARE_INSTRUCTION(Add);
3784
3785 private:
3786 DISALLOW_COPY_AND_ASSIGN(HAdd);
3787};
3788
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003789class HSub : public HBinaryOperation {
3790 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003791 HSub(Primitive::Type result_type,
3792 HInstruction* left,
3793 HInstruction* right,
3794 uint32_t dex_pc = kNoDexPc)
3795 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003796
Roland Levillain9867bc72015-08-05 10:21:34 +01003797 template <typename T> T Compute(T x, T y) const { return x - y; }
3798
3799 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003800 return GetBlock()->GetGraph()->GetIntConstant(
3801 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003802 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003803 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003804 return GetBlock()->GetGraph()->GetLongConstant(
3805 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003806 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003807
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003808 DECLARE_INSTRUCTION(Sub);
3809
3810 private:
3811 DISALLOW_COPY_AND_ASSIGN(HSub);
3812};
3813
Calin Juravle34bacdf2014-10-07 20:23:36 +01003814class HMul : public HBinaryOperation {
3815 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003816 HMul(Primitive::Type result_type,
3817 HInstruction* left,
3818 HInstruction* right,
3819 uint32_t dex_pc = kNoDexPc)
3820 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003822 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003823
Roland Levillain9867bc72015-08-05 10:21:34 +01003824 template <typename T> T Compute(T x, T y) const { return x * y; }
3825
3826 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());
Roland Levillain9867bc72015-08-05 10:21:34 +01003833 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003834
3835 DECLARE_INSTRUCTION(Mul);
3836
3837 private:
3838 DISALLOW_COPY_AND_ASSIGN(HMul);
3839};
3840
Calin Juravle7c4954d2014-10-28 16:57:40 +00003841class HDiv : public HBinaryOperation {
3842 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003843 HDiv(Primitive::Type result_type,
3844 HInstruction* left,
3845 HInstruction* right,
3846 uint32_t dex_pc)
3847 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003848
Roland Levillain9867bc72015-08-05 10:21:34 +01003849 template <typename T>
3850 T Compute(T x, T y) const {
3851 // Our graph structure ensures we never have 0 for `y` during
3852 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003853 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003854 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003855 return (y == -1) ? -x : x / y;
3856 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003857
Roland Levillain9867bc72015-08-05 10:21:34 +01003858 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003859 return GetBlock()->GetGraph()->GetIntConstant(
3860 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003861 }
3862 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003863 return GetBlock()->GetGraph()->GetLongConstant(
3864 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003865 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003866
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003867 static SideEffects SideEffectsForArchRuntimeCalls() {
3868 // The generated code can use a runtime call.
3869 return SideEffects::CanTriggerGC();
3870 }
3871
Calin Juravle7c4954d2014-10-28 16:57:40 +00003872 DECLARE_INSTRUCTION(Div);
3873
3874 private:
3875 DISALLOW_COPY_AND_ASSIGN(HDiv);
3876};
3877
Calin Juravlebacfec32014-11-14 15:54:36 +00003878class HRem : public HBinaryOperation {
3879 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003880 HRem(Primitive::Type result_type,
3881 HInstruction* left,
3882 HInstruction* right,
3883 uint32_t dex_pc)
3884 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003885
Roland Levillain9867bc72015-08-05 10:21:34 +01003886 template <typename T>
3887 T Compute(T x, T y) const {
3888 // Our graph structure ensures we never have 0 for `y` during
3889 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003890 DCHECK_NE(y, 0);
3891 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3892 return (y == -1) ? 0 : x % y;
3893 }
3894
Roland Levillain9867bc72015-08-05 10:21:34 +01003895 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003896 return GetBlock()->GetGraph()->GetIntConstant(
3897 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003898 }
3899 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003900 return GetBlock()->GetGraph()->GetLongConstant(
3901 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003902 }
3903
Calin Juravlebacfec32014-11-14 15:54:36 +00003904
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003905 static SideEffects SideEffectsForArchRuntimeCalls() {
3906 return SideEffects::CanTriggerGC();
3907 }
3908
Calin Juravlebacfec32014-11-14 15:54:36 +00003909 DECLARE_INSTRUCTION(Rem);
3910
3911 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003912 DISALLOW_COPY_AND_ASSIGN(HRem);
3913};
3914
Calin Juravled0d48522014-11-04 16:40:20 +00003915class HDivZeroCheck : public HExpression<1> {
3916 public:
3917 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003918 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003919 SetRawInputAt(0, value);
3920 }
3921
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003922 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3923
Calin Juravled0d48522014-11-04 16:40:20 +00003924 bool CanBeMoved() const OVERRIDE { return true; }
3925
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003926 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003927 return true;
3928 }
3929
3930 bool NeedsEnvironment() const OVERRIDE { return true; }
3931 bool CanThrow() const OVERRIDE { return true; }
3932
Calin Juravled0d48522014-11-04 16:40:20 +00003933 DECLARE_INSTRUCTION(DivZeroCheck);
3934
3935 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003936 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3937};
3938
Calin Juravle9aec02f2014-11-18 23:06:35 +00003939class HShl : public HBinaryOperation {
3940 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003941 HShl(Primitive::Type result_type,
3942 HInstruction* left,
3943 HInstruction* right,
3944 uint32_t dex_pc = kNoDexPc)
3945 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003946
Roland Levillain9867bc72015-08-05 10:21:34 +01003947 template <typename T, typename U, typename V>
3948 T Compute(T x, U y, V max_shift_value) const {
3949 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3950 "V is not the unsigned integer type corresponding to T");
3951 return x << (y & max_shift_value);
3952 }
3953
3954 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3955 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003956 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003957 }
3958 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3959 // case is handled as `x << static_cast<int>(y)`.
3960 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3961 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003962 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003963 }
3964 HConstant* Evaluate(HLongConstant* x, HLongConstant* 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 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003968
3969 DECLARE_INSTRUCTION(Shl);
3970
3971 private:
3972 DISALLOW_COPY_AND_ASSIGN(HShl);
3973};
3974
3975class HShr : public HBinaryOperation {
3976 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003977 HShr(Primitive::Type result_type,
3978 HInstruction* left,
3979 HInstruction* right,
3980 uint32_t dex_pc = kNoDexPc)
3981 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003982
Roland Levillain9867bc72015-08-05 10:21:34 +01003983 template <typename T, typename U, typename V>
3984 T Compute(T x, U y, V max_shift_value) const {
3985 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3986 "V is not the unsigned integer type corresponding to T");
3987 return x >> (y & max_shift_value);
3988 }
3989
3990 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3991 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003992 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003993 }
3994 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3995 // case is handled as `x >> static_cast<int>(y)`.
3996 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3997 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003998 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003999 }
4000 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4001 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004002 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004003 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004004
4005 DECLARE_INSTRUCTION(Shr);
4006
4007 private:
4008 DISALLOW_COPY_AND_ASSIGN(HShr);
4009};
4010
4011class HUShr : public HBinaryOperation {
4012 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004013 HUShr(Primitive::Type result_type,
4014 HInstruction* left,
4015 HInstruction* right,
4016 uint32_t dex_pc = kNoDexPc)
4017 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004018
Roland Levillain9867bc72015-08-05 10:21:34 +01004019 template <typename T, typename U, typename V>
4020 T Compute(T x, U y, V max_shift_value) const {
4021 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4022 "V is not the unsigned integer type corresponding to T");
4023 V ux = static_cast<V>(x);
4024 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004025 }
4026
Roland Levillain9867bc72015-08-05 10:21:34 +01004027 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4028 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004029 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004030 }
4031 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4032 // case is handled as `x >>> static_cast<int>(y)`.
4033 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4034 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004035 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004036 }
4037 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4038 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004039 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004040 }
4041
4042 DECLARE_INSTRUCTION(UShr);
4043
4044 private:
4045 DISALLOW_COPY_AND_ASSIGN(HUShr);
4046};
4047
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004048class HAnd : public HBinaryOperation {
4049 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004050 HAnd(Primitive::Type result_type,
4051 HInstruction* left,
4052 HInstruction* right,
4053 uint32_t dex_pc = kNoDexPc)
4054 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004055
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004056 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004057
Roland Levillain9867bc72015-08-05 10:21:34 +01004058 template <typename T, typename U>
4059 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4060
4061 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004062 return GetBlock()->GetGraph()->GetIntConstant(
4063 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004064 }
4065 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004066 return GetBlock()->GetGraph()->GetLongConstant(
4067 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004068 }
4069 HConstant* Evaluate(HLongConstant* x, HIntConstant* 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, HLongConstant* 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 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004077
4078 DECLARE_INSTRUCTION(And);
4079
4080 private:
4081 DISALLOW_COPY_AND_ASSIGN(HAnd);
4082};
4083
4084class HOr : public HBinaryOperation {
4085 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004086 HOr(Primitive::Type result_type,
4087 HInstruction* left,
4088 HInstruction* right,
4089 uint32_t dex_pc = kNoDexPc)
4090 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004091
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004092 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004093
Roland Levillain9867bc72015-08-05 10:21:34 +01004094 template <typename T, typename U>
4095 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4096
4097 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004098 return GetBlock()->GetGraph()->GetIntConstant(
4099 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004100 }
4101 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004102 return GetBlock()->GetGraph()->GetLongConstant(
4103 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004104 }
4105 HConstant* Evaluate(HLongConstant* x, HIntConstant* 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, HLongConstant* 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 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004113
4114 DECLARE_INSTRUCTION(Or);
4115
4116 private:
4117 DISALLOW_COPY_AND_ASSIGN(HOr);
4118};
4119
4120class HXor : public HBinaryOperation {
4121 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004122 HXor(Primitive::Type result_type,
4123 HInstruction* left,
4124 HInstruction* right,
4125 uint32_t dex_pc = kNoDexPc)
4126 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004127
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004128 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004129
Roland Levillain9867bc72015-08-05 10:21:34 +01004130 template <typename T, typename U>
4131 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4132
4133 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004134 return GetBlock()->GetGraph()->GetIntConstant(
4135 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004136 }
4137 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004138 return GetBlock()->GetGraph()->GetLongConstant(
4139 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004140 }
4141 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004142 return GetBlock()->GetGraph()->GetLongConstant(
4143 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004144 }
4145 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004146 return GetBlock()->GetGraph()->GetLongConstant(
4147 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004148 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004149
4150 DECLARE_INSTRUCTION(Xor);
4151
4152 private:
4153 DISALLOW_COPY_AND_ASSIGN(HXor);
4154};
4155
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004156// The value of a parameter in this method. Its location depends on
4157// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004158class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004159 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004160 HParameterValue(const DexFile& dex_file,
4161 uint16_t type_index,
4162 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004163 Primitive::Type parameter_type,
4164 bool is_this = false)
4165 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004166 dex_file_(dex_file),
4167 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004168 index_(index),
4169 is_this_(is_this),
4170 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004171
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004172 const DexFile& GetDexFile() const { return dex_file_; }
4173 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004174 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004175 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004176
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004177 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4178 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004179
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004180 DECLARE_INSTRUCTION(ParameterValue);
4181
4182 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004183 const DexFile& dex_file_;
4184 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004185 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004186 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004187 const uint8_t index_;
4188
Calin Juravle10e244f2015-01-26 18:54:32 +00004189 // Whether or not the parameter value corresponds to 'this' argument.
4190 const bool is_this_;
4191
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004192 bool can_be_null_;
4193
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004194 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4195};
4196
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004197class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004198 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004199 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4200 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004201
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004202 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004203 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004204 return true;
4205 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004206
Roland Levillain9867bc72015-08-05 10:21:34 +01004207 template <typename T> T Compute(T x) const { return ~x; }
4208
4209 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004211 }
4212 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004213 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004214 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004215
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004216 DECLARE_INSTRUCTION(Not);
4217
4218 private:
4219 DISALLOW_COPY_AND_ASSIGN(HNot);
4220};
4221
David Brazdil66d126e2015-04-03 16:02:44 +01004222class HBooleanNot : public HUnaryOperation {
4223 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004224 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4225 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004226
4227 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004228 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004229 return true;
4230 }
4231
Roland Levillain9867bc72015-08-05 10:21:34 +01004232 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004233 DCHECK(IsUint<1>(x));
4234 return !x;
4235 }
4236
Roland Levillain9867bc72015-08-05 10:21:34 +01004237 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004238 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004239 }
4240 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4241 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004242 UNREACHABLE();
4243 }
4244
4245 DECLARE_INSTRUCTION(BooleanNot);
4246
4247 private:
4248 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4249};
4250
Roland Levillaindff1f282014-11-05 14:15:05 +00004251class HTypeConversion : public HExpression<1> {
4252 public:
4253 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004254 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004255 : HExpression(result_type,
4256 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4257 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004258 SetRawInputAt(0, input);
4259 DCHECK_NE(input->GetType(), result_type);
4260 }
4261
4262 HInstruction* GetInput() const { return InputAt(0); }
4263 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4264 Primitive::Type GetResultType() const { return GetType(); }
4265
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004266 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004267 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004268
Roland Levillaindff1f282014-11-05 14:15:05 +00004269 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004270 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004271
Mark Mendelle82549b2015-05-06 10:55:34 -04004272 // Try to statically evaluate the conversion and return a HConstant
4273 // containing the result. If the input cannot be converted, return nullptr.
4274 HConstant* TryStaticEvaluation() const;
4275
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004276 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4277 Primitive::Type result_type) {
4278 // Some architectures may not require the 'GC' side effects, but at this point
4279 // in the compilation process we do not know what architecture we will
4280 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004281 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4282 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004283 return SideEffects::CanTriggerGC();
4284 }
4285 return SideEffects::None();
4286 }
4287
Roland Levillaindff1f282014-11-05 14:15:05 +00004288 DECLARE_INSTRUCTION(TypeConversion);
4289
4290 private:
4291 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4292};
4293
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004294static constexpr uint32_t kNoRegNumber = -1;
4295
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004296class HPhi : public HInstruction {
4297 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004298 HPhi(ArenaAllocator* arena,
4299 uint32_t reg_number,
4300 size_t number_of_inputs,
4301 Primitive::Type type,
4302 uint32_t dex_pc = kNoDexPc)
4303 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004304 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004305 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004306 type_(type),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004307 is_live_(false),
Calin Juravle10e244f2015-01-26 18:54:32 +00004308 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004309 }
4310
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004311 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4312 static Primitive::Type ToPhiType(Primitive::Type type) {
4313 switch (type) {
4314 case Primitive::kPrimBoolean:
4315 case Primitive::kPrimByte:
4316 case Primitive::kPrimShort:
4317 case Primitive::kPrimChar:
4318 return Primitive::kPrimInt;
4319 default:
4320 return type;
4321 }
4322 }
4323
David Brazdilffee3d32015-07-06 11:48:53 +01004324 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4325
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004326 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004327
4328 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004329 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004330
Calin Juravle10e244f2015-01-26 18:54:32 +00004331 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004332 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004333
Calin Juravle10e244f2015-01-26 18:54:32 +00004334 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4335 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4336
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004337 uint32_t GetRegNumber() const { return reg_number_; }
4338
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004339 void SetDead() { is_live_ = false; }
4340 void SetLive() { is_live_ = true; }
4341 bool IsDead() const { return !is_live_; }
4342 bool IsLive() const { return is_live_; }
4343
David Brazdil77a48ae2015-09-15 12:34:04 +00004344 bool IsVRegEquivalentOf(HInstruction* other) const {
4345 return other != nullptr
4346 && other->IsPhi()
4347 && other->AsPhi()->GetBlock() == GetBlock()
4348 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4349 }
4350
Calin Juravlea4f88312015-04-16 12:57:19 +01004351 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4352 // An equivalent phi is a phi having the same dex register and type.
4353 // It assumes that phis with the same dex register are adjacent.
4354 HPhi* GetNextEquivalentPhiWithSameType() {
4355 HInstruction* next = GetNext();
4356 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4357 if (next->GetType() == GetType()) {
4358 return next->AsPhi();
4359 }
4360 next = next->GetNext();
4361 }
4362 return nullptr;
4363 }
4364
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004365 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004366
David Brazdil1abb4192015-02-17 18:33:36 +00004367 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004368 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004369 return inputs_[index];
4370 }
David Brazdil1abb4192015-02-17 18:33:36 +00004371
4372 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004373 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004374 }
4375
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004376 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004377 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004378 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004379 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004380 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004381 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004382
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004383 DISALLOW_COPY_AND_ASSIGN(HPhi);
4384};
4385
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004386class HNullCheck : public HExpression<1> {
4387 public:
4388 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004389 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004390 SetRawInputAt(0, value);
4391 }
4392
Calin Juravle10e244f2015-01-26 18:54:32 +00004393 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004394 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004395 return true;
4396 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004397
Calin Juravle10e244f2015-01-26 18:54:32 +00004398 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004399
Calin Juravle10e244f2015-01-26 18:54:32 +00004400 bool CanThrow() const OVERRIDE { return true; }
4401
4402 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004403
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004404
4405 DECLARE_INSTRUCTION(NullCheck);
4406
4407 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004408 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4409};
4410
4411class FieldInfo : public ValueObject {
4412 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004413 FieldInfo(MemberOffset field_offset,
4414 Primitive::Type field_type,
4415 bool is_volatile,
4416 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004417 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004418 const DexFile& dex_file,
4419 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004420 : field_offset_(field_offset),
4421 field_type_(field_type),
4422 is_volatile_(is_volatile),
4423 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004424 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004425 dex_file_(dex_file),
4426 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004427
4428 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004429 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004430 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004431 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004432 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004433 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004434 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004435
4436 private:
4437 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004438 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004439 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004440 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004441 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004442 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004443 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004444};
4445
4446class HInstanceFieldGet : public HExpression<1> {
4447 public:
4448 HInstanceFieldGet(HInstruction* value,
4449 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004450 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004451 bool is_volatile,
4452 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004453 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004454 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004455 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004456 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004457 : HExpression(field_type,
4458 SideEffects::FieldReadOfType(field_type, is_volatile),
4459 dex_pc),
4460 field_info_(field_offset,
4461 field_type,
4462 is_volatile,
4463 field_idx,
4464 declaring_class_def_index,
4465 dex_file,
4466 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004467 SetRawInputAt(0, value);
4468 }
4469
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004470 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004471
4472 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4473 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4474 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004475 }
4476
Calin Juravle641547a2015-04-21 22:08:51 +01004477 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4478 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004479 }
4480
4481 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004482 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4483 }
4484
Calin Juravle52c48962014-12-16 17:02:57 +00004485 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004486 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004487 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004488 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004489
4490 DECLARE_INSTRUCTION(InstanceFieldGet);
4491
4492 private:
4493 const FieldInfo field_info_;
4494
4495 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4496};
4497
4498class HInstanceFieldSet : public HTemplateInstruction<2> {
4499 public:
4500 HInstanceFieldSet(HInstruction* object,
4501 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004502 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004503 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004504 bool is_volatile,
4505 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004506 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004507 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004508 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004509 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004510 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4511 dex_pc),
4512 field_info_(field_offset,
4513 field_type,
4514 is_volatile,
4515 field_idx,
4516 declaring_class_def_index,
4517 dex_file,
4518 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004519 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004520 SetRawInputAt(0, object);
4521 SetRawInputAt(1, value);
4522 }
4523
Calin Juravle641547a2015-04-21 22:08:51 +01004524 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4525 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004526 }
4527
Calin Juravle52c48962014-12-16 17:02:57 +00004528 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004529 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004530 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004531 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004532 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004533 bool GetValueCanBeNull() const { return value_can_be_null_; }
4534 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004535
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004536 DECLARE_INSTRUCTION(InstanceFieldSet);
4537
4538 private:
4539 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004540 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004541
4542 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4543};
4544
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004545class HArrayGet : public HExpression<2> {
4546 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004547 HArrayGet(HInstruction* array,
4548 HInstruction* index,
4549 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004550 uint32_t dex_pc,
4551 SideEffects additional_side_effects = SideEffects::None())
4552 : HExpression(type,
4553 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004554 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004555 SetRawInputAt(0, array);
4556 SetRawInputAt(1, index);
4557 }
4558
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004559 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004560 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004561 return true;
4562 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004563 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004564 // TODO: We can be smarter here.
4565 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4566 // which generates the implicit null check. There are cases when these can be removed
4567 // to produce better code. If we ever add optimizations to do so we should allow an
4568 // implicit check here (as long as the address falls in the first page).
4569 return false;
4570 }
4571
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004572 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004573
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004574 HInstruction* GetArray() const { return InputAt(0); }
4575 HInstruction* GetIndex() const { return InputAt(1); }
4576
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004577 DECLARE_INSTRUCTION(ArrayGet);
4578
4579 private:
4580 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4581};
4582
4583class HArraySet : public HTemplateInstruction<3> {
4584 public:
4585 HArraySet(HInstruction* array,
4586 HInstruction* index,
4587 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004588 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004589 uint32_t dex_pc,
4590 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004591 : HTemplateInstruction(
4592 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004593 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4594 additional_side_effects),
4595 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004596 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004597 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004598 value_can_be_null_(true),
4599 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600 SetRawInputAt(0, array);
4601 SetRawInputAt(1, index);
4602 SetRawInputAt(2, value);
4603 }
4604
Calin Juravle77520bc2015-01-12 18:45:46 +00004605 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004606 // We currently always call a runtime method to catch array store
4607 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004608 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004609 }
4610
Mingyao Yang81014cb2015-06-02 03:16:27 -07004611 // Can throw ArrayStoreException.
4612 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4613
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004614 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004615 // TODO: Same as for ArrayGet.
4616 return false;
4617 }
4618
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004619 void ClearNeedsTypeCheck() {
4620 needs_type_check_ = false;
4621 }
4622
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004623 void ClearValueCanBeNull() {
4624 value_can_be_null_ = false;
4625 }
4626
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004627 void SetStaticTypeOfArrayIsObjectArray() {
4628 static_type_of_array_is_object_array_ = true;
4629 }
4630
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004631 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004632 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004633 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004634
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004635 HInstruction* GetArray() const { return InputAt(0); }
4636 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004637 HInstruction* GetValue() const { return InputAt(2); }
4638
4639 Primitive::Type GetComponentType() const {
4640 // The Dex format does not type floating point index operations. Since the
4641 // `expected_component_type_` is set during building and can therefore not
4642 // be correct, we also check what is the value type. If it is a floating
4643 // point type, we must use that type.
4644 Primitive::Type value_type = GetValue()->GetType();
4645 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4646 ? value_type
4647 : expected_component_type_;
4648 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004649
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004650 Primitive::Type GetRawExpectedComponentType() const {
4651 return expected_component_type_;
4652 }
4653
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004654 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4655 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4656 }
4657
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004658 DECLARE_INSTRUCTION(ArraySet);
4659
4660 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004661 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004662 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004663 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004664 // Cached information for the reference_type_info_ so that codegen
4665 // does not need to inspect the static type.
4666 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004667
4668 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4669};
4670
4671class HArrayLength : public HExpression<1> {
4672 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004673 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004674 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004675 // Note that arrays do not change length, so the instruction does not
4676 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004677 SetRawInputAt(0, array);
4678 }
4679
Calin Juravle77520bc2015-01-12 18:45:46 +00004680 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004681 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004682 return true;
4683 }
Calin Juravle641547a2015-04-21 22:08:51 +01004684 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4685 return obj == InputAt(0);
4686 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004687
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004688 DECLARE_INSTRUCTION(ArrayLength);
4689
4690 private:
4691 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4692};
4693
4694class HBoundsCheck : public HExpression<2> {
4695 public:
4696 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004697 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004698 DCHECK(index->GetType() == Primitive::kPrimInt);
4699 SetRawInputAt(0, index);
4700 SetRawInputAt(1, length);
4701 }
4702
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004703 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004704 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004705 return true;
4706 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004707
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004708 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004709
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004710 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004711
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004712 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004713
4714 DECLARE_INSTRUCTION(BoundsCheck);
4715
4716 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004717 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4718};
4719
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004720/**
4721 * Some DEX instructions are folded into multiple HInstructions that need
4722 * to stay live until the last HInstruction. This class
4723 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004724 * HInstruction stays live. `index` represents the stack location index of the
4725 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004726 */
4727class HTemporary : public HTemplateInstruction<0> {
4728 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004729 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4730 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004731
4732 size_t GetIndex() const { return index_; }
4733
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004734 Primitive::Type GetType() const OVERRIDE {
4735 // The previous instruction is the one that will be stored in the temporary location.
4736 DCHECK(GetPrevious() != nullptr);
4737 return GetPrevious()->GetType();
4738 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004739
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004740 DECLARE_INSTRUCTION(Temporary);
4741
4742 private:
4743 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004744 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4745};
4746
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004747class HSuspendCheck : public HTemplateInstruction<0> {
4748 public:
4749 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004750 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004751
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004752 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004753 return true;
4754 }
4755
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004756 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4757 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004758
4759 DECLARE_INSTRUCTION(SuspendCheck);
4760
4761 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004762 // Only used for code generation, in order to share the same slow path between back edges
4763 // of a same loop.
4764 SlowPathCode* slow_path_;
4765
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004766 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4767};
4768
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004769/**
4770 * Instruction to load a Class object.
4771 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004772class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004773 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004774 HLoadClass(HCurrentMethod* current_method,
4775 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004776 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004777 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004778 uint32_t dex_pc,
4779 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004780 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004781 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004782 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004783 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004784 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004785 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004786 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004787 // Referrers class should not need access check. We never inline unverified
4788 // methods so we can't possibly end up in this situation.
4789 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004790 SetRawInputAt(0, current_method);
4791 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004792
4793 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004794
4795 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004796 // Note that we don't need to test for generate_clinit_check_.
4797 // Whether or not we need to generate the clinit check is processed in
4798 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004799 return other->AsLoadClass()->type_index_ == type_index_ &&
4800 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004801 }
4802
4803 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4804
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004805 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004806 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004807 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004808
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004809 bool NeedsEnvironment() const OVERRIDE {
4810 // Will call runtime and load the class if the class is not loaded yet.
4811 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004812 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004813 }
4814
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004815 bool MustGenerateClinitCheck() const {
4816 return generate_clinit_check_;
4817 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004818 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004819 // The entrypoint the code generator is going to call does not do
4820 // clinit of the class.
4821 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004822 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004823 }
4824
4825 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004826 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4827 }
4828
4829 bool NeedsAccessCheck() const {
4830 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004831 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004832
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004833 bool CanThrow() const OVERRIDE {
4834 // May call runtime and and therefore can throw.
4835 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004836 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004837 }
4838
Calin Juravleacf735c2015-02-12 15:25:22 +00004839 ReferenceTypeInfo GetLoadedClassRTI() {
4840 return loaded_class_rti_;
4841 }
4842
4843 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4844 // Make sure we only set exact types (the loaded class should never be merged).
4845 DCHECK(rti.IsExact());
4846 loaded_class_rti_ = rti;
4847 }
4848
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004849 const DexFile& GetDexFile() { return dex_file_; }
4850
Vladimir Markodc151b22015-10-15 18:02:30 +01004851 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004852
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004853 static SideEffects SideEffectsForArchRuntimeCalls() {
4854 return SideEffects::CanTriggerGC();
4855 }
4856
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004857 DECLARE_INSTRUCTION(LoadClass);
4858
4859 private:
4860 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004861 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004862 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004863 // Whether this instruction must generate the initialization check.
4864 // Used for code generation.
4865 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004866 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004867
Calin Juravleacf735c2015-02-12 15:25:22 +00004868 ReferenceTypeInfo loaded_class_rti_;
4869
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004870 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4871};
4872
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004873class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004874 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004875 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004876 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4877 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004878 SetRawInputAt(0, current_method);
4879 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004880
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004881 bool CanBeMoved() const OVERRIDE { return true; }
4882
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004883 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4884 return other->AsLoadString()->string_index_ == string_index_;
4885 }
4886
4887 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4888
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004889 uint32_t GetStringIndex() const { return string_index_; }
4890
4891 // TODO: Can we deopt or debug when we resolve a string?
4892 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004893 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004894 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004895
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004896 static SideEffects SideEffectsForArchRuntimeCalls() {
4897 return SideEffects::CanTriggerGC();
4898 }
4899
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004900 DECLARE_INSTRUCTION(LoadString);
4901
4902 private:
4903 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004904
4905 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4906};
4907
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004908/**
4909 * Performs an initialization check on its Class object input.
4910 */
4911class HClinitCheck : public HExpression<1> {
4912 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004913 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004914 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004915 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004916 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4917 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004918 SetRawInputAt(0, constant);
4919 }
4920
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004921 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004922 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004923 return true;
4924 }
4925
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004926 bool NeedsEnvironment() const OVERRIDE {
4927 // May call runtime to initialize the class.
4928 return true;
4929 }
4930
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004931
4932 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4933
4934 DECLARE_INSTRUCTION(ClinitCheck);
4935
4936 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004937 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4938};
4939
4940class HStaticFieldGet : public HExpression<1> {
4941 public:
4942 HStaticFieldGet(HInstruction* cls,
4943 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004944 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004945 bool is_volatile,
4946 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004947 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004948 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004949 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004950 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004951 : HExpression(field_type,
4952 SideEffects::FieldReadOfType(field_type, is_volatile),
4953 dex_pc),
4954 field_info_(field_offset,
4955 field_type,
4956 is_volatile,
4957 field_idx,
4958 declaring_class_def_index,
4959 dex_file,
4960 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004961 SetRawInputAt(0, cls);
4962 }
4963
Calin Juravle52c48962014-12-16 17:02:57 +00004964
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004965 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004966
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004967 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004968 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4969 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004970 }
4971
4972 size_t ComputeHashCode() const OVERRIDE {
4973 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4974 }
4975
Calin Juravle52c48962014-12-16 17:02:57 +00004976 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004977 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4978 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004979 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004980
4981 DECLARE_INSTRUCTION(StaticFieldGet);
4982
4983 private:
4984 const FieldInfo field_info_;
4985
4986 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4987};
4988
4989class HStaticFieldSet : public HTemplateInstruction<2> {
4990 public:
4991 HStaticFieldSet(HInstruction* cls,
4992 HInstruction* value,
4993 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004994 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004995 bool is_volatile,
4996 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004997 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004998 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004999 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005000 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005001 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5002 dex_pc),
5003 field_info_(field_offset,
5004 field_type,
5005 is_volatile,
5006 field_idx,
5007 declaring_class_def_index,
5008 dex_file,
5009 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005010 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005011 SetRawInputAt(0, cls);
5012 SetRawInputAt(1, value);
5013 }
5014
Calin Juravle52c48962014-12-16 17:02:57 +00005015 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005016 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5017 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005018 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005019
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005020 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005021 bool GetValueCanBeNull() const { return value_can_be_null_; }
5022 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005023
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005024 DECLARE_INSTRUCTION(StaticFieldSet);
5025
5026 private:
5027 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005028 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005029
5030 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5031};
5032
Calin Juravlee460d1d2015-09-29 04:52:17 +01005033class HUnresolvedInstanceFieldGet : public HExpression<1> {
5034 public:
5035 HUnresolvedInstanceFieldGet(HInstruction* obj,
5036 Primitive::Type field_type,
5037 uint32_t field_index,
5038 uint32_t dex_pc)
5039 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5040 field_index_(field_index) {
5041 SetRawInputAt(0, obj);
5042 }
5043
5044 bool NeedsEnvironment() const OVERRIDE { return true; }
5045 bool CanThrow() const OVERRIDE { return true; }
5046
5047 Primitive::Type GetFieldType() const { return GetType(); }
5048 uint32_t GetFieldIndex() const { return field_index_; }
5049
5050 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5051
5052 private:
5053 const uint32_t field_index_;
5054
5055 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5056};
5057
5058class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5059 public:
5060 HUnresolvedInstanceFieldSet(HInstruction* obj,
5061 HInstruction* value,
5062 Primitive::Type field_type,
5063 uint32_t field_index,
5064 uint32_t dex_pc)
5065 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5066 field_type_(field_type),
5067 field_index_(field_index) {
5068 DCHECK_EQ(field_type, value->GetType());
5069 SetRawInputAt(0, obj);
5070 SetRawInputAt(1, value);
5071 }
5072
5073 bool NeedsEnvironment() const OVERRIDE { return true; }
5074 bool CanThrow() const OVERRIDE { return true; }
5075
5076 Primitive::Type GetFieldType() const { return field_type_; }
5077 uint32_t GetFieldIndex() const { return field_index_; }
5078
5079 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5080
5081 private:
5082 const Primitive::Type field_type_;
5083 const uint32_t field_index_;
5084
5085 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5086};
5087
5088class HUnresolvedStaticFieldGet : public HExpression<0> {
5089 public:
5090 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5091 uint32_t field_index,
5092 uint32_t dex_pc)
5093 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5094 field_index_(field_index) {
5095 }
5096
5097 bool NeedsEnvironment() const OVERRIDE { return true; }
5098 bool CanThrow() const OVERRIDE { return true; }
5099
5100 Primitive::Type GetFieldType() const { return GetType(); }
5101 uint32_t GetFieldIndex() const { return field_index_; }
5102
5103 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5104
5105 private:
5106 const uint32_t field_index_;
5107
5108 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5109};
5110
5111class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5112 public:
5113 HUnresolvedStaticFieldSet(HInstruction* value,
5114 Primitive::Type field_type,
5115 uint32_t field_index,
5116 uint32_t dex_pc)
5117 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5118 field_type_(field_type),
5119 field_index_(field_index) {
5120 DCHECK_EQ(field_type, value->GetType());
5121 SetRawInputAt(0, value);
5122 }
5123
5124 bool NeedsEnvironment() const OVERRIDE { return true; }
5125 bool CanThrow() const OVERRIDE { return true; }
5126
5127 Primitive::Type GetFieldType() const { return field_type_; }
5128 uint32_t GetFieldIndex() const { return field_index_; }
5129
5130 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5131
5132 private:
5133 const Primitive::Type field_type_;
5134 const uint32_t field_index_;
5135
5136 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5137};
5138
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005139// Implement the move-exception DEX instruction.
5140class HLoadException : public HExpression<0> {
5141 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005142 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5143 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005144
David Brazdilbbd733e2015-08-18 17:48:17 +01005145 bool CanBeNull() const OVERRIDE { return false; }
5146
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005147 DECLARE_INSTRUCTION(LoadException);
5148
5149 private:
5150 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5151};
5152
David Brazdilcb1c0552015-08-04 16:22:25 +01005153// Implicit part of move-exception which clears thread-local exception storage.
5154// Must not be removed because the runtime expects the TLS to get cleared.
5155class HClearException : public HTemplateInstruction<0> {
5156 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005157 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5158 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005159
5160 DECLARE_INSTRUCTION(ClearException);
5161
5162 private:
5163 DISALLOW_COPY_AND_ASSIGN(HClearException);
5164};
5165
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005166class HThrow : public HTemplateInstruction<1> {
5167 public:
5168 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005169 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005170 SetRawInputAt(0, exception);
5171 }
5172
5173 bool IsControlFlow() const OVERRIDE { return true; }
5174
5175 bool NeedsEnvironment() const OVERRIDE { return true; }
5176
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005177 bool CanThrow() const OVERRIDE { return true; }
5178
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005179
5180 DECLARE_INSTRUCTION(Throw);
5181
5182 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005183 DISALLOW_COPY_AND_ASSIGN(HThrow);
5184};
5185
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005186/**
5187 * Implementation strategies for the code generator of a HInstanceOf
5188 * or `HCheckCast`.
5189 */
5190enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005191 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005192 kExactCheck, // Can do a single class compare.
5193 kClassHierarchyCheck, // Can just walk the super class chain.
5194 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5195 kInterfaceCheck, // No optimization yet when checking against an interface.
5196 kArrayObjectCheck, // Can just check if the array is not primitive.
5197 kArrayCheck // No optimization yet when checking against a generic array.
5198};
5199
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005200class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005201 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005202 HInstanceOf(HInstruction* object,
5203 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005204 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005205 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005206 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005207 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005208 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005209 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005210 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005211 SetRawInputAt(0, object);
5212 SetRawInputAt(1, constant);
5213 }
5214
5215 bool CanBeMoved() const OVERRIDE { return true; }
5216
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005217 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005218 return true;
5219 }
5220
5221 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005222 return false;
5223 }
5224
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005225 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5226
5227 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005228
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005229 // Used only in code generation.
5230 bool MustDoNullCheck() const { return must_do_null_check_; }
5231 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5232
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005233 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5234 return (check_kind == TypeCheckKind::kExactCheck)
5235 ? SideEffects::None()
5236 // Mips currently does runtime calls for any other checks.
5237 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005238 }
5239
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005240 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005241
5242 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005243 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005244 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005245
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005246 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5247};
5248
Calin Juravleb1498f62015-02-16 13:13:29 +00005249class HBoundType : public HExpression<1> {
5250 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005251 // Constructs an HBoundType with the given upper_bound.
5252 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005253 HBoundType(HInstruction* input,
5254 ReferenceTypeInfo upper_bound,
5255 bool upper_can_be_null,
5256 uint32_t dex_pc = kNoDexPc)
5257 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005258 upper_bound_(upper_bound),
5259 upper_can_be_null_(upper_can_be_null),
5260 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005261 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005262 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005263 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005264 }
5265
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005266 // GetUpper* should only be used in reference type propagation.
5267 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5268 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005269
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005270 void SetCanBeNull(bool can_be_null) {
5271 DCHECK(upper_can_be_null_ || !can_be_null);
5272 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005273 }
5274
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005275 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5276
Calin Juravleb1498f62015-02-16 13:13:29 +00005277 DECLARE_INSTRUCTION(BoundType);
5278
5279 private:
5280 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005281 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5282 // It is used to bound the type in cases like:
5283 // if (x instanceof ClassX) {
5284 // // uper_bound_ will be ClassX
5285 // }
5286 const ReferenceTypeInfo upper_bound_;
5287 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5288 // is false then can_be_null_ cannot be true).
5289 const bool upper_can_be_null_;
5290 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005291
5292 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5293};
5294
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005295class HCheckCast : public HTemplateInstruction<2> {
5296 public:
5297 HCheckCast(HInstruction* object,
5298 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005299 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005300 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005301 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005302 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005303 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005304 SetRawInputAt(0, object);
5305 SetRawInputAt(1, constant);
5306 }
5307
5308 bool CanBeMoved() const OVERRIDE { return true; }
5309
5310 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5311 return true;
5312 }
5313
5314 bool NeedsEnvironment() const OVERRIDE {
5315 // Instruction may throw a CheckCastError.
5316 return true;
5317 }
5318
5319 bool CanThrow() const OVERRIDE { return true; }
5320
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005321 bool MustDoNullCheck() const { return must_do_null_check_; }
5322 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005323 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005324
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005325 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005326
5327 DECLARE_INSTRUCTION(CheckCast);
5328
5329 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005330 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005331 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005332
5333 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005334};
5335
Calin Juravle27df7582015-04-17 19:12:31 +01005336class HMemoryBarrier : public HTemplateInstruction<0> {
5337 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005338 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005339 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005340 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005341 barrier_kind_(barrier_kind) {}
5342
5343 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5344
5345 DECLARE_INSTRUCTION(MemoryBarrier);
5346
5347 private:
5348 const MemBarrierKind barrier_kind_;
5349
5350 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5351};
5352
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005353class HMonitorOperation : public HTemplateInstruction<1> {
5354 public:
5355 enum OperationKind {
5356 kEnter,
5357 kExit,
5358 };
5359
5360 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005361 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005362 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5363 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005364 SetRawInputAt(0, object);
5365 }
5366
5367 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005368 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5369
5370 bool CanThrow() const OVERRIDE {
5371 // Verifier guarantees that monitor-exit cannot throw.
5372 // This is important because it allows the HGraphBuilder to remove
5373 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5374 return IsEnter();
5375 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005376
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005377
5378 bool IsEnter() const { return kind_ == kEnter; }
5379
5380 DECLARE_INSTRUCTION(MonitorOperation);
5381
Calin Juravle52c48962014-12-16 17:02:57 +00005382 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005383 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005384
5385 private:
5386 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5387};
5388
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005389/**
5390 * A HInstruction used as a marker for the replacement of new + <init>
5391 * of a String to a call to a StringFactory. Only baseline will see
5392 * the node at code generation, where it will be be treated as null.
5393 * When compiling non-baseline, `HFakeString` instructions are being removed
5394 * in the instruction simplifier.
5395 */
5396class HFakeString : public HTemplateInstruction<0> {
5397 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005398 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5399 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005400
5401 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5402
5403 DECLARE_INSTRUCTION(FakeString);
5404
5405 private:
5406 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5407};
5408
Vladimir Markof9f64412015-09-02 14:05:49 +01005409class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005410 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005411 MoveOperands(Location source,
5412 Location destination,
5413 Primitive::Type type,
5414 HInstruction* instruction)
5415 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005416
5417 Location GetSource() const { return source_; }
5418 Location GetDestination() const { return destination_; }
5419
5420 void SetSource(Location value) { source_ = value; }
5421 void SetDestination(Location value) { destination_ = value; }
5422
5423 // The parallel move resolver marks moves as "in-progress" by clearing the
5424 // destination (but not the source).
5425 Location MarkPending() {
5426 DCHECK(!IsPending());
5427 Location dest = destination_;
5428 destination_ = Location::NoLocation();
5429 return dest;
5430 }
5431
5432 void ClearPending(Location dest) {
5433 DCHECK(IsPending());
5434 destination_ = dest;
5435 }
5436
5437 bool IsPending() const {
5438 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5439 return destination_.IsInvalid() && !source_.IsInvalid();
5440 }
5441
5442 // True if this blocks a move from the given location.
5443 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005444 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005445 }
5446
5447 // A move is redundant if it's been eliminated, if its source and
5448 // destination are the same, or if its destination is unneeded.
5449 bool IsRedundant() const {
5450 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5451 }
5452
5453 // We clear both operands to indicate move that's been eliminated.
5454 void Eliminate() {
5455 source_ = destination_ = Location::NoLocation();
5456 }
5457
5458 bool IsEliminated() const {
5459 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5460 return source_.IsInvalid();
5461 }
5462
Alexey Frunze4dda3372015-06-01 18:31:49 -07005463 Primitive::Type GetType() const { return type_; }
5464
Nicolas Geoffray90218252015-04-15 11:56:51 +01005465 bool Is64BitMove() const {
5466 return Primitive::Is64BitType(type_);
5467 }
5468
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005469 HInstruction* GetInstruction() const { return instruction_; }
5470
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005471 private:
5472 Location source_;
5473 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005474 // The type this move is for.
5475 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005476 // The instruction this move is assocatied with. Null when this move is
5477 // for moving an input in the expected locations of user (including a phi user).
5478 // This is only used in debug mode, to ensure we do not connect interval siblings
5479 // in the same parallel move.
5480 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005481};
5482
5483static constexpr size_t kDefaultNumberOfMoves = 4;
5484
5485class HParallelMove : public HTemplateInstruction<0> {
5486 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005487 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005488 : HTemplateInstruction(SideEffects::None(), dex_pc),
5489 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5490 moves_.reserve(kDefaultNumberOfMoves);
5491 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005492
Nicolas Geoffray90218252015-04-15 11:56:51 +01005493 void AddMove(Location source,
5494 Location destination,
5495 Primitive::Type type,
5496 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005497 DCHECK(source.IsValid());
5498 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005499 if (kIsDebugBuild) {
5500 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005501 for (const MoveOperands& move : moves_) {
5502 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005503 // Special case the situation where the move is for the spill slot
5504 // of the instruction.
5505 if ((GetPrevious() == instruction)
5506 || ((GetPrevious() == nullptr)
5507 && instruction->IsPhi()
5508 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005509 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005510 << "Doing parallel moves for the same instruction.";
5511 } else {
5512 DCHECK(false) << "Doing parallel moves for the same instruction.";
5513 }
5514 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005515 }
5516 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005517 for (const MoveOperands& move : moves_) {
5518 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005519 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005520 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005521 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005522 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005523 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005524 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005525 }
5526
Vladimir Marko225b6462015-09-28 12:17:40 +01005527 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005528 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005529 }
5530
Vladimir Marko225b6462015-09-28 12:17:40 +01005531 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005532
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005533 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005534
5535 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005536 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005537
5538 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5539};
5540
Mark Mendell0616ae02015-04-17 12:49:27 -04005541} // namespace art
5542
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005543#ifdef ART_ENABLE_CODEGEN_arm64
5544#include "nodes_arm64.h"
5545#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005546#ifdef ART_ENABLE_CODEGEN_x86
5547#include "nodes_x86.h"
5548#endif
5549
5550namespace art {
5551
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005552class HGraphVisitor : public ValueObject {
5553 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005554 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5555 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005556
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005557 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005558 virtual void VisitBasicBlock(HBasicBlock* block);
5559
Roland Levillain633021e2014-10-01 14:12:25 +01005560 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005561 void VisitInsertionOrder();
5562
Roland Levillain633021e2014-10-01 14:12:25 +01005563 // Visit the graph following dominator tree reverse post-order.
5564 void VisitReversePostOrder();
5565
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005566 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005567
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005568 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005569#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005570 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5571
5572 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5573
5574#undef DECLARE_VISIT_INSTRUCTION
5575
5576 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005577 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005578
5579 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5580};
5581
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005582class HGraphDelegateVisitor : public HGraphVisitor {
5583 public:
5584 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5585 virtual ~HGraphDelegateVisitor() {}
5586
5587 // Visit functions that delegate to to super class.
5588#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005589 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005590
5591 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5592
5593#undef DECLARE_VISIT_INSTRUCTION
5594
5595 private:
5596 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5597};
5598
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005599class HInsertionOrderIterator : public ValueObject {
5600 public:
5601 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5602
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005603 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005604 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005605 void Advance() { ++index_; }
5606
5607 private:
5608 const HGraph& graph_;
5609 size_t index_;
5610
5611 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5612};
5613
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005614class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005615 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005616 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5617 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005618 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005619 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005620
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005621 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5622 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005623 void Advance() { ++index_; }
5624
5625 private:
5626 const HGraph& graph_;
5627 size_t index_;
5628
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005629 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005630};
5631
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005632class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005633 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005634 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005635 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005636 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005637 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005638 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005639
5640 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005641 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005642 void Advance() { --index_; }
5643
5644 private:
5645 const HGraph& graph_;
5646 size_t index_;
5647
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005648 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005649};
5650
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005651class HLinearPostOrderIterator : public ValueObject {
5652 public:
5653 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005654 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005655
5656 bool Done() const { return index_ == 0; }
5657
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005658 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005659
5660 void Advance() {
5661 --index_;
5662 DCHECK_GE(index_, 0U);
5663 }
5664
5665 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005666 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005667 size_t index_;
5668
5669 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5670};
5671
5672class HLinearOrderIterator : public ValueObject {
5673 public:
5674 explicit HLinearOrderIterator(const HGraph& graph)
5675 : order_(graph.GetLinearOrder()), index_(0) {}
5676
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005677 bool Done() const { return index_ == order_.size(); }
5678 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005679 void Advance() { ++index_; }
5680
5681 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005682 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005683 size_t index_;
5684
5685 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5686};
5687
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005688// Iterator over the blocks that art part of the loop. Includes blocks part
5689// of an inner loop. The order in which the blocks are iterated is on their
5690// block id.
5691class HBlocksInLoopIterator : public ValueObject {
5692 public:
5693 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5694 : blocks_in_loop_(info.GetBlocks()),
5695 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5696 index_(0) {
5697 if (!blocks_in_loop_.IsBitSet(index_)) {
5698 Advance();
5699 }
5700 }
5701
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005702 bool Done() const { return index_ == blocks_.size(); }
5703 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005704 void Advance() {
5705 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005706 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005707 if (blocks_in_loop_.IsBitSet(index_)) {
5708 break;
5709 }
5710 }
5711 }
5712
5713 private:
5714 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005715 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005716 size_t index_;
5717
5718 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5719};
5720
Mingyao Yang3584bce2015-05-19 16:01:59 -07005721// Iterator over the blocks that art part of the loop. Includes blocks part
5722// of an inner loop. The order in which the blocks are iterated is reverse
5723// post order.
5724class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5725 public:
5726 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5727 : blocks_in_loop_(info.GetBlocks()),
5728 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5729 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005730 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005731 Advance();
5732 }
5733 }
5734
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005735 bool Done() const { return index_ == blocks_.size(); }
5736 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005737 void Advance() {
5738 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005739 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5740 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005741 break;
5742 }
5743 }
5744 }
5745
5746 private:
5747 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005748 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005749 size_t index_;
5750
5751 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5752};
5753
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005754inline int64_t Int64FromConstant(HConstant* constant) {
5755 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5756 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5757 : constant->AsLongConstant()->GetValue();
5758}
5759
Vladimir Marko58155012015-08-19 12:49:41 +00005760inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5761 // For the purposes of the compiler, the dex files must actually be the same object
5762 // if we want to safely treat them as the same. This is especially important for JIT
5763 // as custom class loaders can open the same underlying file (or memory) multiple
5764 // times and provide different class resolution but no two class loaders should ever
5765 // use the same DexFile object - doing so is an unsupported hack that can lead to
5766 // all sorts of weird failures.
5767 return &lhs == &rhs;
5768}
5769
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005770} // namespace art
5771
5772#endif // ART_COMPILER_OPTIMIZING_NODES_H_