blob: 3b5c384c842939d90a3a67b3892ae5cc18cac383 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010047class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Calin Juravle9aec02f2014-11-18 23:06:35 +000075static constexpr uint32_t kMaxIntShiftValue = 0x1f;
76static constexpr uint64_t kMaxLongShiftValue = 0x3f;
77
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010078static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070079static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010080
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010081static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
82
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060083static constexpr uint32_t kNoDexPc = -1;
84
Dave Allison20dfc792014-06-16 20:44:29 -070085enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070086 // All types.
87 kCondEQ, // ==
88 kCondNE, // !=
89 // Signed integers and floating-point numbers.
90 kCondLT, // <
91 kCondLE, // <=
92 kCondGT, // >
93 kCondGE, // >=
94 // Unsigned integers.
95 kCondB, // <
96 kCondBE, // <=
97 kCondA, // >
98 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070099};
100
Vladimir Markof9f64412015-09-02 14:05:49 +0100101class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100102 public:
103 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
104
105 void AddInstruction(HInstruction* instruction);
106 void RemoveInstruction(HInstruction* instruction);
107
David Brazdilc3d743f2015-04-22 13:40:50 +0100108 // Insert `instruction` before/after an existing instruction `cursor`.
109 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
110 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
111
Roland Levillain6b469232014-09-25 10:10:38 +0100112 // Return true if this list contains `instruction`.
113 bool Contains(HInstruction* instruction) const;
114
Roland Levillainccc07a92014-09-16 14:48:16 +0100115 // Return true if `instruction1` is found before `instruction2` in
116 // this instruction list and false otherwise. Abort if none
117 // of these instructions is found.
118 bool FoundBefore(const HInstruction* instruction1,
119 const HInstruction* instruction2) const;
120
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000121 bool IsEmpty() const { return first_instruction_ == nullptr; }
122 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
123
124 // Update the block of all instructions to be `block`.
125 void SetBlockOfInstructions(HBasicBlock* block) const;
126
127 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
128 void Add(const HInstructionList& instruction_list);
129
David Brazdil2d7352b2015-04-20 14:52:42 +0100130 // Return the number of instructions in the list. This is an expensive operation.
131 size_t CountSize() const;
132
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100133 private:
134 HInstruction* first_instruction_;
135 HInstruction* last_instruction_;
136
137 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000138 friend class HGraph;
139 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100141 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100142
143 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
144};
145
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000146// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100147class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000148 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100149 HGraph(ArenaAllocator* arena,
150 const DexFile& dex_file,
151 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100152 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100154 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100155 bool debuggable = false,
156 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000157 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100158 blocks_(arena->Adapter(kArenaAllocBlockList)),
159 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
160 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700161 entry_block_(nullptr),
162 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100163 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100164 number_of_vregs_(0),
165 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000166 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400167 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000168 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000169 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000170 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100171 dex_file_(dex_file),
172 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100173 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100174 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100175 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000177 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100178 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
179 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
180 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
181 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
182 cached_current_method_(nullptr) {
183 blocks_.reserve(kDefaultNumberOfBlocks);
184 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000185
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000186 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100187 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
188
David Brazdil69ba7b72015-06-23 18:27:30 +0100189 bool IsInSsaForm() const { return in_ssa_form_; }
190
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000191 HBasicBlock* GetEntryBlock() const { return entry_block_; }
192 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100193 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000194
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000195 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
196 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000197
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000198 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000200 // Try building the SSA form of this graph, with dominance computation and loop
201 // recognition. Returns whether it was successful in doing all these steps.
202 bool TryBuildingSsa() {
203 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000204 // The SSA builder requires loops to all be natural. Specifically, the dead phi
205 // elimination phase checks the consistency of the graph when doing a post-order
206 // visit for eliminating dead phis: a dead phi can only have loop header phi
207 // users remaining when being visited.
208 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100209 // Precompute per-block try membership before entering the SSA builder,
210 // which needs the information to build catch block phis from values of
211 // locals at throwing instructions inside try blocks.
212 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100214 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000215 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000216 }
217
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100218 void ComputeDominanceInformation();
219 void ClearDominanceInformation();
220
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000222 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100223 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100224 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000225
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000226 // Analyze all natural loops in this graph. Returns false if one
227 // loop is not natural, that is the header does not dominate the
228 // back edge.
229 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100230
David Brazdilffee3d32015-07-06 11:48:53 +0100231 // Iterate over blocks to compute try block membership. Needs reverse post
232 // order and loop information.
233 void ComputeTryBlockInformation();
234
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000235 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000236 // Returns the instruction used to replace the invoke expression or null if the
237 // invoke is for a void method.
238 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000239
Mingyao Yang3584bce2015-05-19 16:01:59 -0700240 // Need to add a couple of blocks to test if the loop body is entered and
241 // put deoptimization instructions, etc.
242 void TransformLoopHeaderForBCE(HBasicBlock* header);
243
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000244 // Removes `block` from the graph. Assumes `block` has been disconnected from
245 // other blocks and has no instructions or phis.
246 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000247
David Brazdilfc6a86a2015-06-26 10:33:45 +0000248 // Splits the edge between `block` and `successor` while preserving the
249 // indices in the predecessor/successor lists. If there are multiple edges
250 // between the blocks, the lowest indices are used.
251 // Returns the new block which is empty and has the same dex pc as `successor`.
252 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
253
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100254 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
255 void SimplifyLoop(HBasicBlock* header);
256
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000257 int32_t GetNextInstructionId() {
258 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000259 return current_instruction_id_++;
260 }
261
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000262 int32_t GetCurrentInstructionId() const {
263 return current_instruction_id_;
264 }
265
266 void SetCurrentInstructionId(int32_t id) {
267 current_instruction_id_ = id;
268 }
269
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100270 uint16_t GetMaximumNumberOfOutVRegs() const {
271 return maximum_number_of_out_vregs_;
272 }
273
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000274 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
275 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100276 }
277
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100278 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
279 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
280 }
281
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000282 void UpdateTemporariesVRegSlots(size_t slots) {
283 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100284 }
285
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000286 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100287 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000288 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100289 }
290
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100291 void SetNumberOfVRegs(uint16_t number_of_vregs) {
292 number_of_vregs_ = number_of_vregs;
293 }
294
295 uint16_t GetNumberOfVRegs() const {
296 return number_of_vregs_;
297 }
298
299 void SetNumberOfInVRegs(uint16_t value) {
300 number_of_in_vregs_ = value;
301 }
302
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100303 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100304 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100305 return number_of_vregs_ - number_of_in_vregs_;
306 }
307
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100308 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100309 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100310 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100311
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100312 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100313 return linear_order_;
314 }
315
Mark Mendell1152c922015-04-24 17:06:35 -0400316 bool HasBoundsChecks() const {
317 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800318 }
319
Mark Mendell1152c922015-04-24 17:06:35 -0400320 void SetHasBoundsChecks(bool value) {
321 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800322 }
323
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100324 bool ShouldGenerateConstructorBarrier() const {
325 return should_generate_constructor_barrier_;
326 }
327
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000328 bool IsDebuggable() const { return debuggable_; }
329
David Brazdil8d5b8b22015-03-24 10:51:52 +0000330 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000331 // already, it is created and inserted into the graph. This method is only for
332 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600333 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000334
335 // TODO: This is problematic for the consistency of reference type propagation
336 // because it can be created anytime after the pass and thus it will be left
337 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600338 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000339
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600340 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
341 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600343 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
344 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000345 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600346 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
347 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000348 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600349 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
350 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000351 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000352
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100353 HCurrentMethod* GetCurrentMethod();
354
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100355 const DexFile& GetDexFile() const {
356 return dex_file_;
357 }
358
359 uint32_t GetMethodIdx() const {
360 return method_idx_;
361 }
362
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100363 InvokeType GetInvokeType() const {
364 return invoke_type_;
365 }
366
Mark Mendellc4701932015-04-10 13:18:51 -0400367 InstructionSet GetInstructionSet() const {
368 return instruction_set_;
369 }
370
David Brazdil77a48ae2015-09-15 12:34:04 +0000371 bool HasTryCatch() const { return has_try_catch_; }
372 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100373
Mark Mendellf6529172015-11-17 11:16:56 -0500374 // Returns an instruction with the opposite boolean value from 'cond'.
375 // The instruction has been inserted into the graph, either as a constant, or
376 // before cursor.
377 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
378
David Brazdil2d7352b2015-04-20 14:52:42 +0100379 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100380 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000381 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100382 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000383
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000384 template <class InstructionType, typename ValueType>
385 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600386 ArenaSafeMap<ValueType, InstructionType*>* cache,
387 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000388 // Try to find an existing constant of the given value.
389 InstructionType* constant = nullptr;
390 auto cached_constant = cache->find(value);
391 if (cached_constant != cache->end()) {
392 constant = cached_constant->second;
393 }
394
395 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100396 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000397 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600398 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000399 cache->Overwrite(value, constant);
400 InsertConstant(constant);
401 }
402 return constant;
403 }
404
David Brazdil8d5b8b22015-03-24 10:51:52 +0000405 void InsertConstant(HConstant* instruction);
406
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000407 // Cache a float constant into the graph. This method should only be
408 // called by the SsaBuilder when creating "equivalent" instructions.
409 void CacheFloatConstant(HFloatConstant* constant);
410
411 // See CacheFloatConstant comment.
412 void CacheDoubleConstant(HDoubleConstant* constant);
413
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000414 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000415
416 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100417 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100420 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000421
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100422 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100423 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100424
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000425 HBasicBlock* entry_block_;
426 HBasicBlock* exit_block_;
427
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100428 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100429 uint16_t maximum_number_of_out_vregs_;
430
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100431 // The number of virtual registers in this method. Contains the parameters.
432 uint16_t number_of_vregs_;
433
434 // The number of virtual registers used by parameters of this method.
435 uint16_t number_of_in_vregs_;
436
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000437 // Number of vreg size slots that the temporaries use (used in baseline compiler).
438 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100439
Mark Mendell1152c922015-04-24 17:06:35 -0400440 // Has bounds checks. We can totally skip BCE if it's false.
441 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800442
David Brazdil77a48ae2015-09-15 12:34:04 +0000443 // Flag whether there are any try/catch blocks in the graph. We will skip
444 // try/catch-related passes if false.
445 bool has_try_catch_;
446
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000447 // Indicates whether the graph should be compiled in a way that
448 // ensures full debuggability. If false, we can apply more
449 // aggressive optimizations that may limit the level of debugging.
450 const bool debuggable_;
451
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000452 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000453 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000454
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100455 // The dex file from which the method is from.
456 const DexFile& dex_file_;
457
458 // The method index in the dex file.
459 const uint32_t method_idx_;
460
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100461 // If inlined, this encodes how the callee is being invoked.
462 const InvokeType invoke_type_;
463
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100464 // Whether the graph has been transformed to SSA form. Only used
465 // in debug mode to ensure we are not using properties only valid
466 // for non-SSA form (like the number of temporaries).
467 bool in_ssa_form_;
468
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100469 const bool should_generate_constructor_barrier_;
470
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471 const InstructionSet instruction_set_;
472
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000473 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000474 HNullConstant* cached_null_constant_;
475 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000476 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000477 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000478 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000479
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100480 HCurrentMethod* cached_current_method_;
481
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000482 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100483 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000484 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000485 DISALLOW_COPY_AND_ASSIGN(HGraph);
486};
487
Vladimir Markof9f64412015-09-02 14:05:49 +0100488class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489 public:
490 HLoopInformation(HBasicBlock* header, HGraph* graph)
491 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100492 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100493 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100494 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100495 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
496 back_edges_.reserve(kDefaultNumberOfBackEdges);
497 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100498
499 HBasicBlock* GetHeader() const {
500 return header_;
501 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000502
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000503 void SetHeader(HBasicBlock* block) {
504 header_ = block;
505 }
506
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100507 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
508 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
509 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
510
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000511 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100512 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000513 }
514
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100515 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100516 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100517 }
518
David Brazdil46e2a392015-03-16 17:31:52 +0000519 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100520 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100521 }
522
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000523 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100524 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000525 }
526
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100527 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100528
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100529 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100530 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100531 }
532
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100533 // Returns the lifetime position of the back edge that has the
534 // greatest lifetime position.
535 size_t GetLifetimeEnd() const;
536
537 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100538 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100539 }
540
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100541 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 // that is the header dominates the back edge.
543 bool Populate();
544
David Brazdila4b8c212015-05-07 09:59:30 +0100545 // Reanalyzes the loop by removing loop info from its blocks and re-running
546 // Populate(). If there are no back edges left, the loop info is completely
547 // removed as well as its SuspendCheck instruction. It must be run on nested
548 // inner loops first.
549 void Update();
550
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100551 // Returns whether this loop information contains `block`.
552 // Note that this loop information *must* be populated before entering this function.
553 bool Contains(const HBasicBlock& block) const;
554
555 // Returns whether this loop information is an inner loop of `other`.
556 // Note that `other` *must* be populated before entering this function.
557 bool IsIn(const HLoopInformation& other) const;
558
Aart Bik73f1f3b2015-10-28 15:28:08 -0700559 // Returns true if instruction is not defined within this loop or any loop nested inside
560 // this loop. If must_dominate is set, only definitions that actually dominate the loop
561 // header can be invariant. Otherwise, any definition outside the loop, including
562 // definitions that appear after the loop, is invariant.
563 bool IsLoopInvariant(HInstruction* instruction, bool must_dominate) const;
564
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565 const ArenaBitVector& GetBlocks() const { return blocks_; }
566
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000567 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000568 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000569
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000570 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100571 // Internal recursive implementation of `Populate`.
572 void PopulateRecursive(HBasicBlock* block);
573
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000574 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100575 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100576 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100577 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000578
579 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
580};
581
David Brazdilec16f792015-08-19 15:04:01 +0100582// Stores try/catch information for basic blocks.
583// Note that HGraph is constructed so that catch blocks cannot simultaneously
584// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100585class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100586 public:
587 // Try block information constructor.
588 explicit TryCatchInformation(const HTryBoundary& try_entry)
589 : try_entry_(&try_entry),
590 catch_dex_file_(nullptr),
591 catch_type_index_(DexFile::kDexNoIndex16) {
592 DCHECK(try_entry_ != nullptr);
593 }
594
595 // Catch block information constructor.
596 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
597 : try_entry_(nullptr),
598 catch_dex_file_(&dex_file),
599 catch_type_index_(catch_type_index) {}
600
601 bool IsTryBlock() const { return try_entry_ != nullptr; }
602
603 const HTryBoundary& GetTryEntry() const {
604 DCHECK(IsTryBlock());
605 return *try_entry_;
606 }
607
608 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
609
610 bool IsCatchAllTypeIndex() const {
611 DCHECK(IsCatchBlock());
612 return catch_type_index_ == DexFile::kDexNoIndex16;
613 }
614
615 uint16_t GetCatchTypeIndex() const {
616 DCHECK(IsCatchBlock());
617 return catch_type_index_;
618 }
619
620 const DexFile& GetCatchDexFile() const {
621 DCHECK(IsCatchBlock());
622 return *catch_dex_file_;
623 }
624
625 private:
626 // One of possibly several TryBoundary instructions entering the block's try.
627 // Only set for try blocks.
628 const HTryBoundary* try_entry_;
629
630 // Exception type information. Only set for catch blocks.
631 const DexFile* catch_dex_file_;
632 const uint16_t catch_type_index_;
633};
634
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100635static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100636static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100637
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638// A block in a method. Contains the list of instructions represented
639// as a double linked list. Each block knows its predecessors and
640// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100641
Vladimir Markof9f64412015-09-02 14:05:49 +0100642class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600644 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000645 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000646 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
647 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000648 loop_information_(nullptr),
649 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000650 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100651 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100652 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100653 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000654 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000655 try_catch_information_(nullptr) {
656 predecessors_.reserve(kDefaultNumberOfPredecessors);
657 successors_.reserve(kDefaultNumberOfSuccessors);
658 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
659 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660
Vladimir Marko60584552015-09-03 13:35:12 +0000661 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100662 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000663 }
664
Vladimir Marko60584552015-09-03 13:35:12 +0000665 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100666 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000667 }
668
David Brazdild26a4112015-11-10 11:07:31 +0000669 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
670 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
671
Vladimir Marko60584552015-09-03 13:35:12 +0000672 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
673 return ContainsElement(successors_, block, start_from);
674 }
675
676 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100677 return dominated_blocks_;
678 }
679
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100680 bool IsEntryBlock() const {
681 return graph_->GetEntryBlock() == this;
682 }
683
684 bool IsExitBlock() const {
685 return graph_->GetExitBlock() == this;
686 }
687
David Brazdil46e2a392015-03-16 17:31:52 +0000688 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000689 bool IsSingleTryBoundary() const;
690
691 // Returns true if this block emits nothing but a jump.
692 bool IsSingleJump() const {
693 HLoopInformation* loop_info = GetLoopInformation();
694 return (IsSingleGoto() || IsSingleTryBoundary())
695 // Back edges generate a suspend check.
696 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
697 }
David Brazdil46e2a392015-03-16 17:31:52 +0000698
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000699 void AddBackEdge(HBasicBlock* back_edge) {
700 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000701 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000702 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100703 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000704 loop_information_->AddBackEdge(back_edge);
705 }
706
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000707 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000708 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000709
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100710 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000711 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600712 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000713
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000714 HBasicBlock* GetDominator() const { return dominator_; }
715 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000716 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
717
718 void RemoveDominatedBlock(HBasicBlock* block) {
719 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100720 }
Vladimir Marko60584552015-09-03 13:35:12 +0000721
722 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
723 ReplaceElement(dominated_blocks_, existing, new_block);
724 }
725
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100726 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000727
728 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100729 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000730 }
731
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100732 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
733 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100734 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100735 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100736 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
737 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000738
739 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000740 successors_.push_back(block);
741 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000742 }
743
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100744 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
745 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100746 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000747 new_block->predecessors_.push_back(this);
748 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000749 }
750
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000751 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
752 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000753 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000754 new_block->successors_.push_back(this);
755 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 }
757
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100758 // Insert `this` between `predecessor` and `successor. This method
759 // preserves the indicies, and will update the first edge found between
760 // `predecessor` and `successor`.
761 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
762 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100763 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000764 successor->predecessors_[predecessor_index] = this;
765 predecessor->successors_[successor_index] = this;
766 successors_.push_back(successor);
767 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100768 }
769
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100770 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000771 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100772 }
773
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000774 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000775 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000776 }
777
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000779 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780 }
781
782 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000783 predecessors_.push_back(block);
784 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 }
786
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100787 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 DCHECK_EQ(predecessors_.size(), 2u);
789 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100790 }
791
David Brazdil769c9e52015-04-27 13:54:09 +0100792 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000793 DCHECK_EQ(successors_.size(), 2u);
794 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100795 }
796
David Brazdilfc6a86a2015-06-26 10:33:45 +0000797 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000798 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100799 }
800
David Brazdilfc6a86a2015-06-26 10:33:45 +0000801 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000802 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100803 }
804
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000806 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100807 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000808 }
809
810 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000811 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100812 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 }
814
815 // Returns whether the first occurrence of `predecessor` in the list of
816 // predecessors is at index `idx`.
817 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100818 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000819 return GetPredecessorIndexOf(predecessor) == idx;
820 }
821
David Brazdild7558da2015-09-22 13:04:14 +0100822 // Create a new block between this block and its predecessors. The new block
823 // is added to the graph, all predecessor edges are relinked to it and an edge
824 // is created to `this`. Returns the new empty block. Reverse post order or
825 // loop and try/catch information are not updated.
826 HBasicBlock* CreateImmediateDominator();
827
David Brazdilfc6a86a2015-06-26 10:33:45 +0000828 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100829 // created, latter block. Note that this method will add the block to the
830 // graph, create a Goto at the end of the former block and will create an edge
831 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100832 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000833 HBasicBlock* SplitBefore(HInstruction* cursor);
834
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000835 // Split the block into two blocks just after `cursor`. Returns the newly
836 // created block. Note that this method just updates raw block information,
837 // like predecessors, successors, dominators, and instruction list. It does not
838 // update the graph, reverse post order, loop information, nor make sure the
839 // blocks are consistent (for example ending with a control flow instruction).
840 HBasicBlock* SplitAfter(HInstruction* cursor);
841
David Brazdil9bc43612015-11-05 21:25:24 +0000842 // Split catch block into two blocks after the original move-exception bytecode
843 // instruction, or at the beginning if not present. Returns the newly created,
844 // latter block, or nullptr if such block could not be created (must be dead
845 // in that case). Note that this method just updates raw block information,
846 // like predecessors, successors, dominators, and instruction list. It does not
847 // update the graph, reverse post order, loop information, nor make sure the
848 // blocks are consistent (for example ending with a control flow instruction).
849 HBasicBlock* SplitCatchBlockAfterMoveException();
850
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000851 // Merge `other` at the end of `this`. Successors and dominated blocks of
852 // `other` are changed to be successors and dominated blocks of `this`. Note
853 // that this method does not update the graph, reverse post order, loop
854 // information, nor make sure the blocks are consistent (for example ending
855 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100856 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000857
858 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
859 // of `this` are moved to `other`.
860 // Note that this method does not update the graph, reverse post order, loop
861 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000862 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000863 void ReplaceWith(HBasicBlock* other);
864
David Brazdil2d7352b2015-04-20 14:52:42 +0100865 // Merge `other` at the end of `this`. This method updates loops, reverse post
866 // order, links to predecessors, successors, dominators and deletes the block
867 // from the graph. The two blocks must be successive, i.e. `this` the only
868 // predecessor of `other` and vice versa.
869 void MergeWith(HBasicBlock* other);
870
871 // Disconnects `this` from all its predecessors, successors and dominator,
872 // removes it from all loops it is included in and eventually from the graph.
873 // The block must not dominate any other block. Predecessors and successors
874 // are safely updated.
875 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000876
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000877 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100878 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100879 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100880 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100881 // Replace instruction `initial` with `replacement` within this block.
882 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
883 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100884 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100885 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000886 // RemoveInstruction and RemovePhi delete a given instruction from the respective
887 // instruction list. With 'ensure_safety' set to true, it verifies that the
888 // instruction is not in use and removes it from the use lists of its inputs.
889 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
890 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100891 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892
893 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100894 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100895 }
896
Roland Levillain6b879dd2014-09-22 17:13:44 +0100897 bool IsLoopPreHeaderFirstPredecessor() const {
898 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100899 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100900 }
901
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100902 HLoopInformation* GetLoopInformation() const {
903 return loop_information_;
904 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000905
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100907 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000908 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100909 void SetInLoop(HLoopInformation* info) {
910 if (IsLoopHeader()) {
911 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100912 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100913 loop_information_ = info;
914 } else if (loop_information_->Contains(*info->GetHeader())) {
915 // Block is currently part of an outer loop. Make it part of this inner loop.
916 // Note that a non loop header having a loop information means this loop information
917 // has already been populated
918 loop_information_ = info;
919 } else {
920 // Block is part of an inner loop. Do not update the loop information.
921 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
922 // at this point, because this method is being called while populating `info`.
923 }
924 }
925
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000926 // Raw update of the loop information.
927 void SetLoopInformation(HLoopInformation* info) {
928 loop_information_ = info;
929 }
930
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100931 bool IsInLoop() const { return loop_information_ != nullptr; }
932
David Brazdilec16f792015-08-19 15:04:01 +0100933 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
934
935 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
936 try_catch_information_ = try_catch_information;
937 }
938
939 bool IsTryBlock() const {
940 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
941 }
942
943 bool IsCatchBlock() const {
944 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
945 }
David Brazdilffee3d32015-07-06 11:48:53 +0100946
947 // Returns the try entry that this block's successors should have. They will
948 // be in the same try, unless the block ends in a try boundary. In that case,
949 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100950 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100951
David Brazdild7558da2015-09-22 13:04:14 +0100952 bool HasThrowingInstructions() const;
953
David Brazdila4b8c212015-05-07 09:59:30 +0100954 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100955 bool Dominates(HBasicBlock* block) const;
956
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100957 size_t GetLifetimeStart() const { return lifetime_start_; }
958 size_t GetLifetimeEnd() const { return lifetime_end_; }
959
960 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
961 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
962
David Brazdil8d5b8b22015-03-24 10:51:52 +0000963 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000964 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100965 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000966 bool HasSinglePhi() const;
967
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000968 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000969 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000970 ArenaVector<HBasicBlock*> predecessors_;
971 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100972 HInstructionList instructions_;
973 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000974 HLoopInformation* loop_information_;
975 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000976 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100977 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100978 // The dex program counter of the first instruction of this block.
979 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100980 size_t lifetime_start_;
981 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100982 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100983
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000984 friend class HGraph;
985 friend class HInstruction;
986
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
988};
989
David Brazdilb2bd1c52015-03-25 11:17:37 +0000990// Iterates over the LoopInformation of all loops which contain 'block'
991// from the innermost to the outermost.
992class HLoopInformationOutwardIterator : public ValueObject {
993 public:
994 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
995 : current_(block.GetLoopInformation()) {}
996
997 bool Done() const { return current_ == nullptr; }
998
999 void Advance() {
1000 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001001 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001002 }
1003
1004 HLoopInformation* Current() const {
1005 DCHECK(!Done());
1006 return current_;
1007 }
1008
1009 private:
1010 HLoopInformation* current_;
1011
1012 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1013};
1014
Alexandre Ramesef20f712015-06-09 10:29:30 +01001015#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001016 M(Above, Condition) \
1017 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001018 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001019 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001020 M(ArrayGet, Instruction) \
1021 M(ArrayLength, Instruction) \
1022 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001023 M(Below, Condition) \
1024 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001025 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001026 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001027 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001028 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001029 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001030 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001031 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001033 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001034 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001035 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001036 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001037 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001039 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001040 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001041 M(FloatConstant, Constant) \
1042 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001043 M(GreaterThan, Condition) \
1044 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001045 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001046 M(InstanceFieldGet, Instruction) \
1047 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001048 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001050 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001051 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001052 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001054 M(LessThan, Condition) \
1055 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001056 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001057 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001058 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001059 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001060 M(Local, Instruction) \
1061 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001062 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001063 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001064 M(Mul, BinaryOperation) \
1065 M(Neg, UnaryOperation) \
1066 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001067 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001068 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001069 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001070 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001071 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001072 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001073 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001074 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001075 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001076 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001077 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001078 M(Return, Instruction) \
1079 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001080 M(Shl, BinaryOperation) \
1081 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001082 M(StaticFieldGet, Instruction) \
1083 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001084 M(UnresolvedInstanceFieldGet, Instruction) \
1085 M(UnresolvedInstanceFieldSet, Instruction) \
1086 M(UnresolvedStaticFieldGet, Instruction) \
1087 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001088 M(StoreLocal, Instruction) \
1089 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001090 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001091 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001092 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001094 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001095 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001096 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001097
Alexandre Ramesef20f712015-06-09 10:29:30 +01001098#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1099
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001100#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001101#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001102#else
1103#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1104 M(Arm64IntermediateAddress, Instruction)
1105#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001106
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001107#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1108
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001109#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1110
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001111#ifndef ART_ENABLE_CODEGEN_x86
1112#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1113#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001114#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1115 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001116 M(X86LoadFromConstantTable, Instruction) \
1117 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001118#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001119
1120#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1121
1122#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1123 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1124 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1125 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001126 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001127 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001128 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1129 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1130
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001131#define FOR_EACH_INSTRUCTION(M) \
1132 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1133 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001134 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001135 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001136 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001137
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001138#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001139FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1140#undef FORWARD_DECLARATION
1141
Roland Levillainccc07a92014-09-16 14:48:16 +01001142#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001143 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1144 const char* DebugName() const OVERRIDE { return #type; } \
1145 const H##type* As##type() const OVERRIDE { return this; } \
1146 H##type* As##type() OVERRIDE { return this; } \
1147 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001148 return other->Is##type(); \
1149 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001150 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001151
David Brazdiled596192015-01-23 10:39:45 +00001152template <typename T> class HUseList;
1153
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001154template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001155class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001156 public:
David Brazdiled596192015-01-23 10:39:45 +00001157 HUseListNode* GetPrevious() const { return prev_; }
1158 HUseListNode* GetNext() const { return next_; }
1159 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001160 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001161 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001162
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001163 private:
David Brazdiled596192015-01-23 10:39:45 +00001164 HUseListNode(T user, size_t index)
1165 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1166
1167 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001168 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001169 HUseListNode<T>* prev_;
1170 HUseListNode<T>* next_;
1171
1172 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001173
1174 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1175};
1176
David Brazdiled596192015-01-23 10:39:45 +00001177template <typename T>
1178class HUseList : public ValueObject {
1179 public:
1180 HUseList() : first_(nullptr) {}
1181
1182 void Clear() {
1183 first_ = nullptr;
1184 }
1185
1186 // Adds a new entry at the beginning of the use list and returns
1187 // the newly created node.
1188 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001189 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001190 if (IsEmpty()) {
1191 first_ = new_node;
1192 } else {
1193 first_->prev_ = new_node;
1194 new_node->next_ = first_;
1195 first_ = new_node;
1196 }
1197 return new_node;
1198 }
1199
1200 HUseListNode<T>* GetFirst() const {
1201 return first_;
1202 }
1203
1204 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001205 DCHECK(node != nullptr);
1206 DCHECK(Contains(node));
1207
David Brazdiled596192015-01-23 10:39:45 +00001208 if (node->prev_ != nullptr) {
1209 node->prev_->next_ = node->next_;
1210 }
1211 if (node->next_ != nullptr) {
1212 node->next_->prev_ = node->prev_;
1213 }
1214 if (node == first_) {
1215 first_ = node->next_;
1216 }
1217 }
1218
David Brazdil1abb4192015-02-17 18:33:36 +00001219 bool Contains(const HUseListNode<T>* node) const {
1220 if (node == nullptr) {
1221 return false;
1222 }
1223 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1224 if (current == node) {
1225 return true;
1226 }
1227 }
1228 return false;
1229 }
1230
David Brazdiled596192015-01-23 10:39:45 +00001231 bool IsEmpty() const {
1232 return first_ == nullptr;
1233 }
1234
1235 bool HasOnlyOneUse() const {
1236 return first_ != nullptr && first_->next_ == nullptr;
1237 }
1238
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001239 size_t SizeSlow() const {
1240 size_t count = 0;
1241 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1242 ++count;
1243 }
1244 return count;
1245 }
1246
David Brazdiled596192015-01-23 10:39:45 +00001247 private:
1248 HUseListNode<T>* first_;
1249};
1250
1251template<typename T>
1252class HUseIterator : public ValueObject {
1253 public:
1254 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1255
1256 bool Done() const { return current_ == nullptr; }
1257
1258 void Advance() {
1259 DCHECK(!Done());
1260 current_ = current_->GetNext();
1261 }
1262
1263 HUseListNode<T>* Current() const {
1264 DCHECK(!Done());
1265 return current_;
1266 }
1267
1268 private:
1269 HUseListNode<T>* current_;
1270
1271 friend class HValue;
1272};
1273
David Brazdil1abb4192015-02-17 18:33:36 +00001274// This class is used by HEnvironment and HInstruction classes to record the
1275// instructions they use and pointers to the corresponding HUseListNodes kept
1276// by the used instructions.
1277template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001278class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001279 public:
1280 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1281 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1282
1283 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1284 : instruction_(old_record.instruction_), use_node_(use_node) {
1285 DCHECK(instruction_ != nullptr);
1286 DCHECK(use_node_ != nullptr);
1287 DCHECK(old_record.use_node_ == nullptr);
1288 }
1289
1290 HInstruction* GetInstruction() const { return instruction_; }
1291 HUseListNode<T>* GetUseNode() const { return use_node_; }
1292
1293 private:
1294 // Instruction used by the user.
1295 HInstruction* instruction_;
1296
1297 // Corresponding entry in the use list kept by 'instruction_'.
1298 HUseListNode<T>* use_node_;
1299};
1300
Aart Bik854a02b2015-07-14 16:07:00 -07001301/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001302 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001303 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001304 * For write/read dependences on fields/arrays, the dependence analysis uses
1305 * type disambiguation (e.g. a float field write cannot modify the value of an
1306 * integer field read) and the access type (e.g. a reference array write cannot
1307 * modify the value of a reference field read [although it may modify the
1308 * reference fetch prior to reading the field, which is represented by its own
1309 * write/read dependence]). The analysis makes conservative points-to
1310 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1311 * the same, and any reference read depends on any reference read without
1312 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001313 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001314 * The internal representation uses 38-bit and is described in the table below.
1315 * The first line indicates the side effect, and for field/array accesses the
1316 * second line indicates the type of the access (in the order of the
1317 * Primitive::Type enum).
1318 * The two numbered lines below indicate the bit position in the bitfield (read
1319 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001320 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001321 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1322 * +-------------+---------+---------+--------------+---------+---------+
1323 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1324 * | 3 |333333322|222222221| 1 |111111110|000000000|
1325 * | 7 |654321098|765432109| 8 |765432109|876543210|
1326 *
1327 * Note that, to ease the implementation, 'changes' bits are least significant
1328 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001329 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001330class SideEffects : public ValueObject {
1331 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001332 SideEffects() : flags_(0) {}
1333
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001334 static SideEffects None() {
1335 return SideEffects(0);
1336 }
1337
1338 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001339 return SideEffects(kAllChangeBits | kAllDependOnBits);
1340 }
1341
1342 static SideEffects AllChanges() {
1343 return SideEffects(kAllChangeBits);
1344 }
1345
1346 static SideEffects AllDependencies() {
1347 return SideEffects(kAllDependOnBits);
1348 }
1349
1350 static SideEffects AllExceptGCDependency() {
1351 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1352 }
1353
1354 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001355 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001356 }
1357
Aart Bik34c3ba92015-07-20 14:08:59 -07001358 static SideEffects AllWrites() {
1359 return SideEffects(kAllWrites);
1360 }
1361
1362 static SideEffects AllReads() {
1363 return SideEffects(kAllReads);
1364 }
1365
1366 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1367 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001368 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001369 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001370 }
1371
Aart Bik854a02b2015-07-14 16:07:00 -07001372 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1373 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001374 }
1375
Aart Bik34c3ba92015-07-20 14:08:59 -07001376 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1377 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001378 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001379 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001380 }
1381
1382 static SideEffects ArrayReadOfType(Primitive::Type type) {
1383 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1384 }
1385
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001386 static SideEffects CanTriggerGC() {
1387 return SideEffects(1ULL << kCanTriggerGCBit);
1388 }
1389
1390 static SideEffects DependsOnGC() {
1391 return SideEffects(1ULL << kDependsOnGCBit);
1392 }
1393
Aart Bik854a02b2015-07-14 16:07:00 -07001394 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001395 SideEffects Union(SideEffects other) const {
1396 return SideEffects(flags_ | other.flags_);
1397 }
1398
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001399 SideEffects Exclusion(SideEffects other) const {
1400 return SideEffects(flags_ & ~other.flags_);
1401 }
1402
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001403 void Add(SideEffects other) {
1404 flags_ |= other.flags_;
1405 }
1406
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001407 bool Includes(SideEffects other) const {
1408 return (other.flags_ & flags_) == other.flags_;
1409 }
1410
1411 bool HasSideEffects() const {
1412 return (flags_ & kAllChangeBits);
1413 }
1414
1415 bool HasDependencies() const {
1416 return (flags_ & kAllDependOnBits);
1417 }
1418
1419 // Returns true if there are no side effects or dependencies.
1420 bool DoesNothing() const {
1421 return flags_ == 0;
1422 }
1423
Aart Bik854a02b2015-07-14 16:07:00 -07001424 // Returns true if something is written.
1425 bool DoesAnyWrite() const {
1426 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001427 }
1428
Aart Bik854a02b2015-07-14 16:07:00 -07001429 // Returns true if something is read.
1430 bool DoesAnyRead() const {
1431 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001432 }
1433
Aart Bik854a02b2015-07-14 16:07:00 -07001434 // Returns true if potentially everything is written and read
1435 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001436 bool DoesAllReadWrite() const {
1437 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1438 }
1439
Aart Bik854a02b2015-07-14 16:07:00 -07001440 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001441 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001442 }
1443
Roland Levillain0d5a2812015-11-13 10:07:31 +00001444 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001445 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001446 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1447 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001448 }
1449
1450 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001451 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001452 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001453 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001454 for (int s = kLastBit; s >= 0; s--) {
1455 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1456 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1457 // This is a bit for the GC side effect.
1458 if (current_bit_is_set) {
1459 flags += "GC";
1460 }
Aart Bik854a02b2015-07-14 16:07:00 -07001461 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001462 } else {
1463 // This is a bit for the array/field analysis.
1464 // The underscore character stands for the 'can trigger GC' bit.
1465 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1466 if (current_bit_is_set) {
1467 flags += kDebug[s];
1468 }
1469 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1470 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1471 flags += "|";
1472 }
1473 }
Aart Bik854a02b2015-07-14 16:07:00 -07001474 }
1475 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001476 }
1477
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001478 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001479
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001480 private:
1481 static constexpr int kFieldArrayAnalysisBits = 9;
1482
1483 static constexpr int kFieldWriteOffset = 0;
1484 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1485 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1486 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1487
1488 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1489
1490 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1491 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1492 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1493 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1494
1495 static constexpr int kLastBit = kDependsOnGCBit;
1496 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1497
1498 // Aliases.
1499
1500 static_assert(kChangeBits == kDependOnBits,
1501 "the 'change' bits should match the 'depend on' bits.");
1502
1503 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1504 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1505 static constexpr uint64_t kAllWrites =
1506 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1507 static constexpr uint64_t kAllReads =
1508 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001509
Aart Bik854a02b2015-07-14 16:07:00 -07001510 // Work around the fact that HIR aliases I/F and J/D.
1511 // TODO: remove this interceptor once HIR types are clean
1512 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1513 switch (type) {
1514 case Primitive::kPrimInt:
1515 case Primitive::kPrimFloat:
1516 return TypeFlag(Primitive::kPrimInt, offset) |
1517 TypeFlag(Primitive::kPrimFloat, offset);
1518 case Primitive::kPrimLong:
1519 case Primitive::kPrimDouble:
1520 return TypeFlag(Primitive::kPrimLong, offset) |
1521 TypeFlag(Primitive::kPrimDouble, offset);
1522 default:
1523 return TypeFlag(type, offset);
1524 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001525 }
1526
Aart Bik854a02b2015-07-14 16:07:00 -07001527 // Translates type to bit flag.
1528 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1529 CHECK_NE(type, Primitive::kPrimVoid);
1530 const uint64_t one = 1;
1531 const int shift = type; // 0-based consecutive enum
1532 DCHECK_LE(kFieldWriteOffset, shift);
1533 DCHECK_LT(shift, kArrayWriteOffset);
1534 return one << (type + offset);
1535 }
1536
1537 // Private constructor on direct flags value.
1538 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1539
1540 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001541};
1542
David Brazdiled596192015-01-23 10:39:45 +00001543// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001544class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001545 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001546 HEnvironment(ArenaAllocator* arena,
1547 size_t number_of_vregs,
1548 const DexFile& dex_file,
1549 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001550 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001551 InvokeType invoke_type,
1552 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001553 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1554 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001555 parent_(nullptr),
1556 dex_file_(dex_file),
1557 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001558 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001559 invoke_type_(invoke_type),
1560 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001561 }
1562
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001563 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001564 : HEnvironment(arena,
1565 to_copy.Size(),
1566 to_copy.GetDexFile(),
1567 to_copy.GetMethodIdx(),
1568 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001569 to_copy.GetInvokeType(),
1570 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001571
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001572 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001573 if (parent_ != nullptr) {
1574 parent_->SetAndCopyParentChain(allocator, parent);
1575 } else {
1576 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1577 parent_->CopyFrom(parent);
1578 if (parent->GetParent() != nullptr) {
1579 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1580 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001581 }
David Brazdiled596192015-01-23 10:39:45 +00001582 }
1583
Vladimir Marko71bf8092015-09-15 15:33:14 +01001584 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001585 void CopyFrom(HEnvironment* environment);
1586
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001587 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1588 // input to the loop phi instead. This is for inserting instructions that
1589 // require an environment (like HDeoptimization) in the loop pre-header.
1590 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001591
1592 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001593 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001594 }
1595
1596 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001597 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001598 }
1599
David Brazdil1abb4192015-02-17 18:33:36 +00001600 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001601
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001602 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001603
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001604 HEnvironment* GetParent() const { return parent_; }
1605
1606 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001607 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001608 }
1609
1610 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001611 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001612 }
1613
1614 uint32_t GetDexPc() const {
1615 return dex_pc_;
1616 }
1617
1618 uint32_t GetMethodIdx() const {
1619 return method_idx_;
1620 }
1621
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001622 InvokeType GetInvokeType() const {
1623 return invoke_type_;
1624 }
1625
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001626 const DexFile& GetDexFile() const {
1627 return dex_file_;
1628 }
1629
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001630 HInstruction* GetHolder() const {
1631 return holder_;
1632 }
1633
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001634
1635 bool IsFromInlinedInvoke() const {
1636 return GetParent() != nullptr;
1637 }
1638
David Brazdiled596192015-01-23 10:39:45 +00001639 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001640 // Record instructions' use entries of this environment for constant-time removal.
1641 // It should only be called by HInstruction when a new environment use is added.
1642 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1643 DCHECK(env_use->GetUser() == this);
1644 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001645 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001646 }
David Brazdiled596192015-01-23 10:39:45 +00001647
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001648 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1649 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001650 HEnvironment* parent_;
1651 const DexFile& dex_file_;
1652 const uint32_t method_idx_;
1653 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001654 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001655
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001656 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001657 HInstruction* const holder_;
1658
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001659 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001660
1661 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1662};
1663
Calin Juravleacf735c2015-02-12 15:25:22 +00001664class ReferenceTypeInfo : ValueObject {
1665 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001666 typedef Handle<mirror::Class> TypeHandle;
1667
Calin Juravle2e768302015-07-28 14:41:11 +00001668 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1669 // The constructor will check that the type_handle is valid.
1670 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001671 }
1672
Calin Juravle2e768302015-07-28 14:41:11 +00001673 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1674
1675 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1676 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001677 }
1678
Calin Juravle2e768302015-07-28 14:41:11 +00001679 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1680 return IsValidHandle(type_handle_);
1681 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001682
Calin Juravleacf735c2015-02-12 15:25:22 +00001683 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001684
1685 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1686 DCHECK(IsValid());
1687 return GetTypeHandle()->IsObjectClass();
1688 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001689
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001690 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001691 DCHECK(IsValid());
1692 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001693 }
1694
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001695 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1696 DCHECK(IsValid());
1697 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1698 }
1699
Mathieu Chartier90443472015-07-16 20:32:27 -07001700 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001701 DCHECK(IsValid());
1702 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001703 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001704
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001705 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001706 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001707 return GetTypeHandle()->IsArrayClass();
1708 }
1709
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001710 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1711 DCHECK(IsValid());
1712 return GetTypeHandle()->IsPrimitiveArray();
1713 }
1714
1715 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1716 DCHECK(IsValid());
1717 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1718 }
1719
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001720 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001721 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001722 if (!IsExact()) return false;
1723 if (!IsArrayClass()) return false;
1724 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1725 }
1726
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001727 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1728 DCHECK(IsValid());
1729 if (!IsExact()) return false;
1730 if (!IsArrayClass()) return false;
1731 if (!rti.IsArrayClass()) return false;
1732 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1733 rti.GetTypeHandle()->GetComponentType());
1734 }
1735
Calin Juravleacf735c2015-02-12 15:25:22 +00001736 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1737
Mathieu Chartier90443472015-07-16 20:32:27 -07001738 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001739 DCHECK(IsValid());
1740 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001741 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1742 }
1743
Calin Juravlecdfed3d2015-10-26 14:05:01 +00001744 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1745 DCHECK(IsValid());
1746 DCHECK(rti.IsValid());
1747 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
1748 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1749 }
1750
Calin Juravleacf735c2015-02-12 15:25:22 +00001751 // Returns true if the type information provide the same amount of details.
1752 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001753 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001754 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001755 if (!IsValid() && !rti.IsValid()) {
1756 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001757 return true;
1758 }
Calin Juravle2e768302015-07-28 14:41:11 +00001759 if (!IsValid() || !rti.IsValid()) {
1760 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001761 return false;
1762 }
Calin Juravle2e768302015-07-28 14:41:11 +00001763 return IsExact() == rti.IsExact()
1764 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001765 }
1766
1767 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001768 ReferenceTypeInfo();
1769 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001770
Calin Juravleacf735c2015-02-12 15:25:22 +00001771 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001772 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001773 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001774 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001775 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001776};
1777
1778std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1779
Vladimir Markof9f64412015-09-02 14:05:49 +01001780class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001781 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001782 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001783 : previous_(nullptr),
1784 next_(nullptr),
1785 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001786 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001787 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001788 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001789 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001790 locations_(nullptr),
1791 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001792 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001793 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001794 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795
Dave Allison20dfc792014-06-16 20:44:29 -07001796 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001797
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001798#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001799 enum InstructionKind {
1800 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1801 };
1802#undef DECLARE_KIND
1803
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001804 HInstruction* GetNext() const { return next_; }
1805 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001806
Calin Juravle77520bc2015-01-12 18:45:46 +00001807 HInstruction* GetNextDisregardingMoves() const;
1808 HInstruction* GetPreviousDisregardingMoves() const;
1809
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001810 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001811 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001812 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001813 bool IsInBlock() const { return block_ != nullptr; }
1814 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001815 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001816
Roland Levillain6b879dd2014-09-22 17:13:44 +01001817 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001818 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001819
1820 virtual void Accept(HGraphVisitor* visitor) = 0;
1821 virtual const char* DebugName() const = 0;
1822
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001823 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001824 void SetRawInputAt(size_t index, HInstruction* input) {
1825 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1826 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001827
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001828 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001829
1830 uint32_t GetDexPc() const { return dex_pc_; }
1831
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001832 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001833
Roland Levillaine161a2a2014-10-03 12:45:18 +01001834 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001835 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001836
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001837 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001838 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001839
Calin Juravle10e244f2015-01-26 18:54:32 +00001840 // Does not apply for all instructions, but having this at top level greatly
1841 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001842 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001843 virtual bool CanBeNull() const {
1844 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1845 return true;
1846 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001847
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001848 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001849 return false;
1850 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001851
Calin Juravle2e768302015-07-28 14:41:11 +00001852 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001853
Calin Juravle61d544b2015-02-23 16:46:57 +00001854 ReferenceTypeInfo GetReferenceTypeInfo() const {
1855 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1856 return reference_type_info_;
1857 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001858
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001859 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001860 DCHECK(user != nullptr);
1861 HUseListNode<HInstruction*>* use =
1862 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1863 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001864 }
1865
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001866 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001867 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001868 HUseListNode<HEnvironment*>* env_use =
1869 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1870 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001871 }
1872
David Brazdil1abb4192015-02-17 18:33:36 +00001873 void RemoveAsUserOfInput(size_t input) {
1874 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1875 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1876 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001877
David Brazdil1abb4192015-02-17 18:33:36 +00001878 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1879 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001880
David Brazdiled596192015-01-23 10:39:45 +00001881 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1882 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001883 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001884 bool HasOnlyOneNonEnvironmentUse() const {
1885 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1886 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001887
Roland Levillain6c82d402014-10-13 16:10:27 +01001888 // Does this instruction strictly dominate `other_instruction`?
1889 // Returns false if this instruction and `other_instruction` are the same.
1890 // Aborts if this instruction and `other_instruction` are both phis.
1891 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001892
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001893 int GetId() const { return id_; }
1894 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001895
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001896 int GetSsaIndex() const { return ssa_index_; }
1897 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1898 bool HasSsaIndex() const { return ssa_index_ != -1; }
1899
1900 bool HasEnvironment() const { return environment_ != nullptr; }
1901 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001902 // Set the `environment_` field. Raw because this method does not
1903 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001904 void SetRawEnvironment(HEnvironment* environment) {
1905 DCHECK(environment_ == nullptr);
1906 DCHECK_EQ(environment->GetHolder(), this);
1907 environment_ = environment;
1908 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001909
1910 // Set the environment of this instruction, copying it from `environment`. While
1911 // copying, the uses lists are being updated.
1912 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001913 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001914 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001915 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001916 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001917 if (environment->GetParent() != nullptr) {
1918 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1919 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001920 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001921
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001922 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1923 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001924 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001925 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001926 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001927 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001928 if (environment->GetParent() != nullptr) {
1929 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1930 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001931 }
1932
Nicolas Geoffray39468442014-09-02 15:17:15 +01001933 // Returns the number of entries in the environment. Typically, that is the
1934 // number of dex registers in a method. It could be more in case of inlining.
1935 size_t EnvironmentSize() const;
1936
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001937 LocationSummary* GetLocations() const { return locations_; }
1938 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001939
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001940 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001941 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001942
Alexandre Rames188d4312015-04-09 18:30:21 +01001943 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1944 // uses of this instruction by `other` are *not* updated.
1945 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1946 ReplaceWith(other);
1947 other->ReplaceInput(this, use_index);
1948 }
1949
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001950 // Move `this` instruction before `cursor`.
1951 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001952
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001953#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001954 bool Is##type() const { return (As##type() != nullptr); } \
1955 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001956 virtual H##type* As##type() { return nullptr; }
1957
1958 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1959#undef INSTRUCTION_TYPE_CHECK
1960
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001961 // Returns whether the instruction can be moved within the graph.
1962 virtual bool CanBeMoved() const { return false; }
1963
1964 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001965 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001966 return false;
1967 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001968
1969 // Returns whether any data encoded in the two instructions is equal.
1970 // This method does not look at the inputs. Both instructions must be
1971 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001972 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001973 return false;
1974 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001975
1976 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001977 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001978 // 2) Their inputs are identical.
1979 bool Equals(HInstruction* other) const;
1980
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001981 virtual InstructionKind GetKind() const = 0;
1982
1983 virtual size_t ComputeHashCode() const {
1984 size_t result = GetKind();
1985 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1986 result = (result * 31) + InputAt(i)->GetId();
1987 }
1988 return result;
1989 }
1990
1991 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001992 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001993
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001994 size_t GetLifetimePosition() const { return lifetime_position_; }
1995 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1996 LiveInterval* GetLiveInterval() const { return live_interval_; }
1997 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1998 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1999
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002000 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2001
2002 // Returns whether the code generation of the instruction will require to have access
2003 // to the current method. Such instructions are:
2004 // (1): Instructions that require an environment, as calling the runtime requires
2005 // to walk the stack and have the current method stored at a specific stack address.
2006 // (2): Object literals like classes and strings, that are loaded from the dex cache
2007 // fields of the current method.
2008 bool NeedsCurrentMethod() const {
2009 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2010 }
2011
Vladimir Markodc151b22015-10-15 18:02:30 +01002012 // Returns whether the code generation of the instruction will require to have access
2013 // to the dex cache of the current method's declaring class via the current method.
2014 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002015
Mark Mendellc4701932015-04-10 13:18:51 -04002016 // Does this instruction have any use in an environment before
2017 // control flow hits 'other'?
2018 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2019
2020 // Remove all references to environment uses of this instruction.
2021 // The caller must ensure that this is safe to do.
2022 void RemoveEnvironmentUsers();
2023
David Brazdil1abb4192015-02-17 18:33:36 +00002024 protected:
2025 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2026 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2027
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002028 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002029 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2030
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002031 HInstruction* previous_;
2032 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002033 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002034 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002035
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002036 // An instruction gets an id when it is added to the graph.
2037 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002038 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002039 int id_;
2040
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002041 // When doing liveness analysis, instructions that have uses get an SSA index.
2042 int ssa_index_;
2043
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002044 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002045 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002046
2047 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002048 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002049
Nicolas Geoffray39468442014-09-02 15:17:15 +01002050 // The environment associated with this instruction. Not null if the instruction
2051 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002052 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002053
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002054 // Set by the code generator.
2055 LocationSummary* locations_;
2056
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002057 // Set by the liveness analysis.
2058 LiveInterval* live_interval_;
2059
2060 // Set by the liveness analysis, this is the position in a linear
2061 // order of blocks where this instruction's live interval start.
2062 size_t lifetime_position_;
2063
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002064 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002065
Calin Juravleacf735c2015-02-12 15:25:22 +00002066 // TODO: for primitive types this should be marked as invalid.
2067 ReferenceTypeInfo reference_type_info_;
2068
David Brazdil1abb4192015-02-17 18:33:36 +00002069 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002070 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002071 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002072 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002073 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002074
2075 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2076};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002077std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002078
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002079class HInputIterator : public ValueObject {
2080 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002081 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002082
2083 bool Done() const { return index_ == instruction_->InputCount(); }
2084 HInstruction* Current() const { return instruction_->InputAt(index_); }
2085 void Advance() { index_++; }
2086
2087 private:
2088 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002089 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002090
2091 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2092};
2093
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002094class HInstructionIterator : public ValueObject {
2095 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002096 explicit HInstructionIterator(const HInstructionList& instructions)
2097 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002098 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002099 }
2100
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002101 bool Done() const { return instruction_ == nullptr; }
2102 HInstruction* Current() const { return instruction_; }
2103 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002104 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002105 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002106 }
2107
2108 private:
2109 HInstruction* instruction_;
2110 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002111
2112 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113};
2114
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002115class HBackwardInstructionIterator : public ValueObject {
2116 public:
2117 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2118 : instruction_(instructions.last_instruction_) {
2119 next_ = Done() ? nullptr : instruction_->GetPrevious();
2120 }
2121
2122 bool Done() const { return instruction_ == nullptr; }
2123 HInstruction* Current() const { return instruction_; }
2124 void Advance() {
2125 instruction_ = next_;
2126 next_ = Done() ? nullptr : instruction_->GetPrevious();
2127 }
2128
2129 private:
2130 HInstruction* instruction_;
2131 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002132
2133 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002134};
2135
Vladimir Markof9f64412015-09-02 14:05:49 +01002136template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002137class HTemplateInstruction: public HInstruction {
2138 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002139 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002140 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002141 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002142
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002143 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002144
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002145 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002146 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2147 DCHECK_LT(i, N);
2148 return inputs_[i];
2149 }
David Brazdil1abb4192015-02-17 18:33:36 +00002150
2151 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002152 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002153 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002154 }
2155
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002157 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002158
2159 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002160};
2161
Vladimir Markof9f64412015-09-02 14:05:49 +01002162// HTemplateInstruction specialization for N=0.
2163template<>
2164class HTemplateInstruction<0>: public HInstruction {
2165 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002166 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002167 : HInstruction(side_effects, dex_pc) {}
2168
Vladimir Markof9f64412015-09-02 14:05:49 +01002169 virtual ~HTemplateInstruction() {}
2170
2171 size_t InputCount() const OVERRIDE { return 0; }
2172
2173 protected:
2174 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2175 LOG(FATAL) << "Unreachable";
2176 UNREACHABLE();
2177 }
2178
2179 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2180 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2181 LOG(FATAL) << "Unreachable";
2182 UNREACHABLE();
2183 }
2184
2185 private:
2186 friend class SsaBuilder;
2187};
2188
Dave Allison20dfc792014-06-16 20:44:29 -07002189template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002190class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002191 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002192 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002193 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002194 virtual ~HExpression() {}
2195
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002196 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002197
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002198 protected:
2199 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002200};
2201
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002202// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2203// instruction that branches to the exit block.
2204class HReturnVoid : public HTemplateInstruction<0> {
2205 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002206 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2207 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002208
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002209 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002210
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002211 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002212
2213 private:
2214 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2215};
2216
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002217// Represents dex's RETURN opcodes. A HReturn is a control flow
2218// instruction that branches to the exit block.
2219class HReturn : public HTemplateInstruction<1> {
2220 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002221 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2222 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002223 SetRawInputAt(0, value);
2224 }
2225
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002226 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002227
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002228 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002229
2230 private:
2231 DISALLOW_COPY_AND_ASSIGN(HReturn);
2232};
2233
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002234// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002235// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002236// exit block.
2237class HExit : public HTemplateInstruction<0> {
2238 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002239 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002240
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002241 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002242
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002243 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002244
2245 private:
2246 DISALLOW_COPY_AND_ASSIGN(HExit);
2247};
2248
2249// Jumps from one block to another.
2250class HGoto : public HTemplateInstruction<0> {
2251 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002252 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002253
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002254 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002255
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002256 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002257 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002258 }
2259
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002260 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002261
2262 private:
2263 DISALLOW_COPY_AND_ASSIGN(HGoto);
2264};
2265
Roland Levillain9867bc72015-08-05 10:21:34 +01002266class HConstant : public HExpression<0> {
2267 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002268 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2269 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002270
2271 bool CanBeMoved() const OVERRIDE { return true; }
2272
2273 virtual bool IsMinusOne() const { return false; }
2274 virtual bool IsZero() const { return false; }
2275 virtual bool IsOne() const { return false; }
2276
David Brazdil77a48ae2015-09-15 12:34:04 +00002277 virtual uint64_t GetValueAsUint64() const = 0;
2278
Roland Levillain9867bc72015-08-05 10:21:34 +01002279 DECLARE_INSTRUCTION(Constant);
2280
2281 private:
2282 DISALLOW_COPY_AND_ASSIGN(HConstant);
2283};
2284
2285class HNullConstant : public HConstant {
2286 public:
2287 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2288 return true;
2289 }
2290
David Brazdil77a48ae2015-09-15 12:34:04 +00002291 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2292
Roland Levillain9867bc72015-08-05 10:21:34 +01002293 size_t ComputeHashCode() const OVERRIDE { return 0; }
2294
2295 DECLARE_INSTRUCTION(NullConstant);
2296
2297 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002298 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002299
2300 friend class HGraph;
2301 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2302};
2303
2304// Constants of the type int. Those can be from Dex instructions, or
2305// synthesized (for example with the if-eqz instruction).
2306class HIntConstant : public HConstant {
2307 public:
2308 int32_t GetValue() const { return value_; }
2309
David Brazdil9f389d42015-10-01 14:32:56 +01002310 uint64_t GetValueAsUint64() const OVERRIDE {
2311 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2312 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002313
Roland Levillain9867bc72015-08-05 10:21:34 +01002314 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2315 DCHECK(other->IsIntConstant());
2316 return other->AsIntConstant()->value_ == value_;
2317 }
2318
2319 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2320
2321 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2322 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2323 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2324
2325 DECLARE_INSTRUCTION(IntConstant);
2326
2327 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002328 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2329 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2330 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2331 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002332
2333 const int32_t value_;
2334
2335 friend class HGraph;
2336 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2337 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2338 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2339};
2340
2341class HLongConstant : public HConstant {
2342 public:
2343 int64_t GetValue() const { return value_; }
2344
David Brazdil77a48ae2015-09-15 12:34:04 +00002345 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2346
Roland Levillain9867bc72015-08-05 10:21:34 +01002347 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2348 DCHECK(other->IsLongConstant());
2349 return other->AsLongConstant()->value_ == value_;
2350 }
2351
2352 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2353
2354 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2355 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2356 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2357
2358 DECLARE_INSTRUCTION(LongConstant);
2359
2360 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002361 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2362 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002363
2364 const int64_t value_;
2365
2366 friend class HGraph;
2367 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2368};
Dave Allison20dfc792014-06-16 20:44:29 -07002369
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002370// Conditional branch. A block ending with an HIf instruction must have
2371// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002372class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002373 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002374 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2375 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002376 SetRawInputAt(0, input);
2377 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002378
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002379 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002380
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002381 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002382 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002383 }
2384
2385 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002386 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002387 }
2388
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002389 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002390
2391 private:
2392 DISALLOW_COPY_AND_ASSIGN(HIf);
2393};
2394
David Brazdilfc6a86a2015-06-26 10:33:45 +00002395
2396// Abstract instruction which marks the beginning and/or end of a try block and
2397// links it to the respective exception handlers. Behaves the same as a Goto in
2398// non-exceptional control flow.
2399// Normal-flow successor is stored at index zero, exception handlers under
2400// higher indices in no particular order.
2401class HTryBoundary : public HTemplateInstruction<0> {
2402 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002403 enum BoundaryKind {
2404 kEntry,
2405 kExit,
2406 };
2407
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002408 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2409 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002410
2411 bool IsControlFlow() const OVERRIDE { return true; }
2412
2413 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002414 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002415
David Brazdild26a4112015-11-10 11:07:31 +00002416 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2417 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2418 }
2419
David Brazdilfc6a86a2015-06-26 10:33:45 +00002420 // Returns whether `handler` is among its exception handlers (non-zero index
2421 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002422 bool HasExceptionHandler(const HBasicBlock& handler) const {
2423 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002424 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002425 }
2426
2427 // If not present already, adds `handler` to its block's list of exception
2428 // handlers.
2429 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002430 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002431 GetBlock()->AddSuccessor(handler);
2432 }
2433 }
2434
David Brazdil56e1acc2015-06-30 15:41:36 +01002435 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002436
David Brazdilffee3d32015-07-06 11:48:53 +01002437 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2438
David Brazdilfc6a86a2015-06-26 10:33:45 +00002439 DECLARE_INSTRUCTION(TryBoundary);
2440
2441 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002442 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002443
2444 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2445};
2446
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002447// Deoptimize to interpreter, upon checking a condition.
2448class HDeoptimize : public HTemplateInstruction<1> {
2449 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002450 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2451 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002452 SetRawInputAt(0, cond);
2453 }
2454
2455 bool NeedsEnvironment() const OVERRIDE { return true; }
2456 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002457
2458 DECLARE_INSTRUCTION(Deoptimize);
2459
2460 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002461 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2462};
2463
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002464// Represents the ArtMethod that was passed as a first argument to
2465// the method. It is used by instructions that depend on it, like
2466// instructions that work with the dex cache.
2467class HCurrentMethod : public HExpression<0> {
2468 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002469 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2470 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002471
2472 DECLARE_INSTRUCTION(CurrentMethod);
2473
2474 private:
2475 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2476};
2477
Mark Mendellfe57faa2015-09-18 09:26:15 -04002478// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2479// have one successor for each entry in the switch table, and the final successor
2480// will be the block containing the next Dex opcode.
2481class HPackedSwitch : public HTemplateInstruction<1> {
2482 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002483 HPackedSwitch(int32_t start_value,
2484 uint32_t num_entries,
2485 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002486 uint32_t dex_pc = kNoDexPc)
2487 : HTemplateInstruction(SideEffects::None(), dex_pc),
2488 start_value_(start_value),
2489 num_entries_(num_entries) {
2490 SetRawInputAt(0, input);
2491 }
2492
2493 bool IsControlFlow() const OVERRIDE { return true; }
2494
2495 int32_t GetStartValue() const { return start_value_; }
2496
Vladimir Marko211c2112015-09-24 16:52:33 +01002497 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002498
2499 HBasicBlock* GetDefaultBlock() const {
2500 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002501 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002502 }
2503 DECLARE_INSTRUCTION(PackedSwitch);
2504
2505 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002506 const int32_t start_value_;
2507 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002508
2509 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2510};
2511
Roland Levillain88cb1752014-10-20 16:36:47 +01002512class HUnaryOperation : public HExpression<1> {
2513 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002514 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2515 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002516 SetRawInputAt(0, input);
2517 }
2518
2519 HInstruction* GetInput() const { return InputAt(0); }
2520 Primitive::Type GetResultType() const { return GetType(); }
2521
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002522 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002523 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002524 return true;
2525 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002526
Roland Levillain9240d6a2014-10-20 16:47:04 +01002527 // Try to statically evaluate `operation` and return a HConstant
2528 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002529 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002530 HConstant* TryStaticEvaluation() const;
2531
2532 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002533 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2534 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002535
Roland Levillain88cb1752014-10-20 16:36:47 +01002536 DECLARE_INSTRUCTION(UnaryOperation);
2537
2538 private:
2539 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2540};
2541
Dave Allison20dfc792014-06-16 20:44:29 -07002542class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002543 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002544 HBinaryOperation(Primitive::Type result_type,
2545 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002546 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002547 SideEffects side_effects = SideEffects::None(),
2548 uint32_t dex_pc = kNoDexPc)
2549 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002550 SetRawInputAt(0, left);
2551 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002552 }
2553
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002554 HInstruction* GetLeft() const { return InputAt(0); }
2555 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002556 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002557
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002558 virtual bool IsCommutative() const { return false; }
2559
2560 // Put constant on the right.
2561 // Returns whether order is changed.
2562 bool OrderInputsWithConstantOnTheRight() {
2563 HInstruction* left = InputAt(0);
2564 HInstruction* right = InputAt(1);
2565 if (left->IsConstant() && !right->IsConstant()) {
2566 ReplaceInput(right, 0);
2567 ReplaceInput(left, 1);
2568 return true;
2569 }
2570 return false;
2571 }
2572
2573 // Order inputs by instruction id, but favor constant on the right side.
2574 // This helps GVN for commutative ops.
2575 void OrderInputs() {
2576 DCHECK(IsCommutative());
2577 HInstruction* left = InputAt(0);
2578 HInstruction* right = InputAt(1);
2579 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2580 return;
2581 }
2582 if (OrderInputsWithConstantOnTheRight()) {
2583 return;
2584 }
2585 // Order according to instruction id.
2586 if (left->GetId() > right->GetId()) {
2587 ReplaceInput(right, 0);
2588 ReplaceInput(left, 1);
2589 }
2590 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002591
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002592 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002593 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002594 return true;
2595 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002596
Roland Levillain9240d6a2014-10-20 16:47:04 +01002597 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002598 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002599 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002600 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002601
2602 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002603 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2604 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2605 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2606 HLongConstant* y ATTRIBUTE_UNUSED) const {
2607 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2608 return nullptr;
2609 }
2610 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2611 HIntConstant* y ATTRIBUTE_UNUSED) const {
2612 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2613 return nullptr;
2614 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002615 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2616 HNullConstant* y ATTRIBUTE_UNUSED) const {
2617 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2618 return nullptr;
2619 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002620
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002621 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002622 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002623 HConstant* GetConstantRight() const;
2624
2625 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002626 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002627 HInstruction* GetLeastConstantLeft() const;
2628
Roland Levillainccc07a92014-09-16 14:48:16 +01002629 DECLARE_INSTRUCTION(BinaryOperation);
2630
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002631 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002632 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2633};
2634
Mark Mendellc4701932015-04-10 13:18:51 -04002635// The comparison bias applies for floating point operations and indicates how NaN
2636// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002637enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002638 kNoBias, // bias is not applicable (i.e. for long operation)
2639 kGtBias, // return 1 for NaN comparisons
2640 kLtBias, // return -1 for NaN comparisons
2641};
2642
Dave Allison20dfc792014-06-16 20:44:29 -07002643class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002644 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002645 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2646 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002647 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002648 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002649
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002650 bool NeedsMaterialization() const { return needs_materialization_; }
2651 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002652
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002653 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002654 // `instruction`, and disregard moves in between.
2655 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002656
Dave Allison20dfc792014-06-16 20:44:29 -07002657 DECLARE_INSTRUCTION(Condition);
2658
2659 virtual IfCondition GetCondition() const = 0;
2660
Mark Mendellc4701932015-04-10 13:18:51 -04002661 virtual IfCondition GetOppositeCondition() const = 0;
2662
Roland Levillain4fa13f62015-07-06 18:11:54 +01002663 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002664
2665 void SetBias(ComparisonBias bias) { bias_ = bias; }
2666
2667 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2668 return bias_ == other->AsCondition()->bias_;
2669 }
2670
Roland Levillain4fa13f62015-07-06 18:11:54 +01002671 bool IsFPConditionTrueIfNaN() const {
2672 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2673 IfCondition if_cond = GetCondition();
2674 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2675 }
2676
2677 bool IsFPConditionFalseIfNaN() const {
2678 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2679 IfCondition if_cond = GetCondition();
2680 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2681 }
2682
Dave Allison20dfc792014-06-16 20:44:29 -07002683 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002684 // For register allocation purposes, returns whether this instruction needs to be
2685 // materialized (that is, not just be in the processor flags).
2686 bool needs_materialization_;
2687
Mark Mendellc4701932015-04-10 13:18:51 -04002688 // Needed if we merge a HCompare into a HCondition.
2689 ComparisonBias bias_;
2690
Dave Allison20dfc792014-06-16 20:44:29 -07002691 DISALLOW_COPY_AND_ASSIGN(HCondition);
2692};
2693
2694// Instruction to check if two inputs are equal to each other.
2695class HEqual : public HCondition {
2696 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002697 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2698 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002699
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002700 bool IsCommutative() const OVERRIDE { return true; }
2701
Roland Levillain9867bc72015-08-05 10:21:34 +01002702 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002703 return GetBlock()->GetGraph()->GetIntConstant(
2704 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002705 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002706 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002707 return GetBlock()->GetGraph()->GetIntConstant(
2708 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002709 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002710 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2711 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002712 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002713 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002714
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002715 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002716
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002717 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002718 return kCondEQ;
2719 }
2720
Mark Mendellc4701932015-04-10 13:18:51 -04002721 IfCondition GetOppositeCondition() const OVERRIDE {
2722 return kCondNE;
2723 }
2724
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002725 private:
Aart Bike9f37602015-10-09 11:15:55 -07002726 template <typename T> bool Compute(T x, T y) const { return x == y; }
2727
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002728 DISALLOW_COPY_AND_ASSIGN(HEqual);
2729};
2730
Dave Allison20dfc792014-06-16 20:44:29 -07002731class HNotEqual : public HCondition {
2732 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002733 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2734 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002735
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002736 bool IsCommutative() const OVERRIDE { return true; }
2737
Roland Levillain9867bc72015-08-05 10:21:34 +01002738 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002739 return GetBlock()->GetGraph()->GetIntConstant(
2740 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002741 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002742 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002743 return GetBlock()->GetGraph()->GetIntConstant(
2744 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002745 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002746 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2747 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002748 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002749 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002750
Dave Allison20dfc792014-06-16 20:44:29 -07002751 DECLARE_INSTRUCTION(NotEqual);
2752
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002753 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002754 return kCondNE;
2755 }
2756
Mark Mendellc4701932015-04-10 13:18:51 -04002757 IfCondition GetOppositeCondition() const OVERRIDE {
2758 return kCondEQ;
2759 }
2760
Dave Allison20dfc792014-06-16 20:44:29 -07002761 private:
Aart Bike9f37602015-10-09 11:15:55 -07002762 template <typename T> bool Compute(T x, T y) const { return x != y; }
2763
Dave Allison20dfc792014-06-16 20:44:29 -07002764 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2765};
2766
2767class HLessThan : public HCondition {
2768 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002769 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2770 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002771
Roland Levillain9867bc72015-08-05 10:21:34 +01002772 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002773 return GetBlock()->GetGraph()->GetIntConstant(
2774 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002775 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002776 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002777 return GetBlock()->GetGraph()->GetIntConstant(
2778 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002779 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002780
Dave Allison20dfc792014-06-16 20:44:29 -07002781 DECLARE_INSTRUCTION(LessThan);
2782
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002783 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002784 return kCondLT;
2785 }
2786
Mark Mendellc4701932015-04-10 13:18:51 -04002787 IfCondition GetOppositeCondition() const OVERRIDE {
2788 return kCondGE;
2789 }
2790
Dave Allison20dfc792014-06-16 20:44:29 -07002791 private:
Aart Bike9f37602015-10-09 11:15:55 -07002792 template <typename T> bool Compute(T x, T y) const { return x < y; }
2793
Dave Allison20dfc792014-06-16 20:44:29 -07002794 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2795};
2796
2797class HLessThanOrEqual : public HCondition {
2798 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002799 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2800 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002801
Roland Levillain9867bc72015-08-05 10:21:34 +01002802 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002803 return GetBlock()->GetGraph()->GetIntConstant(
2804 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002805 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002806 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002807 return GetBlock()->GetGraph()->GetIntConstant(
2808 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002809 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002810
Dave Allison20dfc792014-06-16 20:44:29 -07002811 DECLARE_INSTRUCTION(LessThanOrEqual);
2812
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002813 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002814 return kCondLE;
2815 }
2816
Mark Mendellc4701932015-04-10 13:18:51 -04002817 IfCondition GetOppositeCondition() const OVERRIDE {
2818 return kCondGT;
2819 }
2820
Dave Allison20dfc792014-06-16 20:44:29 -07002821 private:
Aart Bike9f37602015-10-09 11:15:55 -07002822 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2823
Dave Allison20dfc792014-06-16 20:44:29 -07002824 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2825};
2826
2827class HGreaterThan : public HCondition {
2828 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002829 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2830 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002831
Roland Levillain9867bc72015-08-05 10:21:34 +01002832 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002833 return GetBlock()->GetGraph()->GetIntConstant(
2834 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002835 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002836 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002837 return GetBlock()->GetGraph()->GetIntConstant(
2838 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002839 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002840
Dave Allison20dfc792014-06-16 20:44:29 -07002841 DECLARE_INSTRUCTION(GreaterThan);
2842
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002843 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002844 return kCondGT;
2845 }
2846
Mark Mendellc4701932015-04-10 13:18:51 -04002847 IfCondition GetOppositeCondition() const OVERRIDE {
2848 return kCondLE;
2849 }
2850
Dave Allison20dfc792014-06-16 20:44:29 -07002851 private:
Aart Bike9f37602015-10-09 11:15:55 -07002852 template <typename T> bool Compute(T x, T y) const { return x > y; }
2853
Dave Allison20dfc792014-06-16 20:44:29 -07002854 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2855};
2856
2857class HGreaterThanOrEqual : public HCondition {
2858 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002859 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2860 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002861
Roland Levillain9867bc72015-08-05 10:21:34 +01002862 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002863 return GetBlock()->GetGraph()->GetIntConstant(
2864 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002865 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002866 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002867 return GetBlock()->GetGraph()->GetIntConstant(
2868 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002869 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002870
Dave Allison20dfc792014-06-16 20:44:29 -07002871 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2872
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002873 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002874 return kCondGE;
2875 }
2876
Mark Mendellc4701932015-04-10 13:18:51 -04002877 IfCondition GetOppositeCondition() const OVERRIDE {
2878 return kCondLT;
2879 }
2880
Dave Allison20dfc792014-06-16 20:44:29 -07002881 private:
Aart Bike9f37602015-10-09 11:15:55 -07002882 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2883
Dave Allison20dfc792014-06-16 20:44:29 -07002884 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2885};
2886
Aart Bike9f37602015-10-09 11:15:55 -07002887class HBelow : public HCondition {
2888 public:
2889 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2890 : HCondition(first, second, dex_pc) {}
2891
2892 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2893 return GetBlock()->GetGraph()->GetIntConstant(
2894 Compute(static_cast<uint32_t>(x->GetValue()),
2895 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2896 }
2897 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2898 return GetBlock()->GetGraph()->GetIntConstant(
2899 Compute(static_cast<uint64_t>(x->GetValue()),
2900 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2901 }
2902
2903 DECLARE_INSTRUCTION(Below);
2904
2905 IfCondition GetCondition() const OVERRIDE {
2906 return kCondB;
2907 }
2908
2909 IfCondition GetOppositeCondition() const OVERRIDE {
2910 return kCondAE;
2911 }
2912
2913 private:
2914 template <typename T> bool Compute(T x, T y) const { return x < y; }
2915
2916 DISALLOW_COPY_AND_ASSIGN(HBelow);
2917};
2918
2919class HBelowOrEqual : public HCondition {
2920 public:
2921 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2922 : HCondition(first, second, dex_pc) {}
2923
2924 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2925 return GetBlock()->GetGraph()->GetIntConstant(
2926 Compute(static_cast<uint32_t>(x->GetValue()),
2927 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2928 }
2929 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2930 return GetBlock()->GetGraph()->GetIntConstant(
2931 Compute(static_cast<uint64_t>(x->GetValue()),
2932 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2933 }
2934
2935 DECLARE_INSTRUCTION(BelowOrEqual);
2936
2937 IfCondition GetCondition() const OVERRIDE {
2938 return kCondBE;
2939 }
2940
2941 IfCondition GetOppositeCondition() const OVERRIDE {
2942 return kCondA;
2943 }
2944
2945 private:
2946 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2947
2948 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2949};
2950
2951class HAbove : public HCondition {
2952 public:
2953 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2954 : HCondition(first, second, dex_pc) {}
2955
2956 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2957 return GetBlock()->GetGraph()->GetIntConstant(
2958 Compute(static_cast<uint32_t>(x->GetValue()),
2959 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2960 }
2961 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2962 return GetBlock()->GetGraph()->GetIntConstant(
2963 Compute(static_cast<uint64_t>(x->GetValue()),
2964 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2965 }
2966
2967 DECLARE_INSTRUCTION(Above);
2968
2969 IfCondition GetCondition() const OVERRIDE {
2970 return kCondA;
2971 }
2972
2973 IfCondition GetOppositeCondition() const OVERRIDE {
2974 return kCondBE;
2975 }
2976
2977 private:
2978 template <typename T> bool Compute(T x, T y) const { return x > y; }
2979
2980 DISALLOW_COPY_AND_ASSIGN(HAbove);
2981};
2982
2983class HAboveOrEqual : public HCondition {
2984 public:
2985 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2986 : HCondition(first, second, dex_pc) {}
2987
2988 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2989 return GetBlock()->GetGraph()->GetIntConstant(
2990 Compute(static_cast<uint32_t>(x->GetValue()),
2991 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2992 }
2993 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2994 return GetBlock()->GetGraph()->GetIntConstant(
2995 Compute(static_cast<uint64_t>(x->GetValue()),
2996 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2997 }
2998
2999 DECLARE_INSTRUCTION(AboveOrEqual);
3000
3001 IfCondition GetCondition() const OVERRIDE {
3002 return kCondAE;
3003 }
3004
3005 IfCondition GetOppositeCondition() const OVERRIDE {
3006 return kCondB;
3007 }
3008
3009 private:
3010 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3011
3012 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3013};
Dave Allison20dfc792014-06-16 20:44:29 -07003014
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003015// Instruction to check how two inputs compare to each other.
3016// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3017class HCompare : public HBinaryOperation {
3018 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003019 HCompare(Primitive::Type type,
3020 HInstruction* first,
3021 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003022 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003023 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003024 : HBinaryOperation(Primitive::kPrimInt,
3025 first,
3026 second,
3027 SideEffectsForArchRuntimeCalls(type),
3028 dex_pc),
3029 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003030 DCHECK_EQ(type, first->GetType());
3031 DCHECK_EQ(type, second->GetType());
3032 }
3033
Roland Levillain9867bc72015-08-05 10:21:34 +01003034 template <typename T>
3035 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003036
Roland Levillain9867bc72015-08-05 10:21:34 +01003037 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003038 return GetBlock()->GetGraph()->GetIntConstant(
3039 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003040 }
3041 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003042 return GetBlock()->GetGraph()->GetIntConstant(
3043 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003044 }
3045
Calin Juravleddb7df22014-11-25 20:56:51 +00003046 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3047 return bias_ == other->AsCompare()->bias_;
3048 }
3049
Mark Mendellc4701932015-04-10 13:18:51 -04003050 ComparisonBias GetBias() const { return bias_; }
3051
Roland Levillain4fa13f62015-07-06 18:11:54 +01003052 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003053
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003054
3055 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3056 // MIPS64 uses a runtime call for FP comparisons.
3057 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3058 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003059
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003060 DECLARE_INSTRUCTION(Compare);
3061
3062 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003063 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003064
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003065 DISALLOW_COPY_AND_ASSIGN(HCompare);
3066};
3067
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003068// A local in the graph. Corresponds to a Dex register.
3069class HLocal : public HTemplateInstruction<0> {
3070 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003071 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003072 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003073
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003074 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003075
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003076 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003077
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003078 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003079 // The Dex register number.
3080 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003081
3082 DISALLOW_COPY_AND_ASSIGN(HLocal);
3083};
3084
3085// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003086class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003087 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003088 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3089 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003090 SetRawInputAt(0, local);
3091 }
3092
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003093 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3094
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003095 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003096
3097 private:
3098 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3099};
3100
3101// Store a value in a given local. This instruction has two inputs: the value
3102// and the local.
3103class HStoreLocal : public HTemplateInstruction<2> {
3104 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003105 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3106 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003107 SetRawInputAt(0, local);
3108 SetRawInputAt(1, value);
3109 }
3110
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003111 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3112
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003113 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003114
3115 private:
3116 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3117};
3118
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003119class HFloatConstant : public HConstant {
3120 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003121 float GetValue() const { return value_; }
3122
David Brazdil77a48ae2015-09-15 12:34:04 +00003123 uint64_t GetValueAsUint64() const OVERRIDE {
3124 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3125 }
3126
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003127 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003128 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003129 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003130 }
3131
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003132 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003133
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003134 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003135 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003136 }
3137 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003138 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003139 }
3140 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003141 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3142 }
3143 bool IsNaN() const {
3144 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003145 }
3146
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003147 DECLARE_INSTRUCTION(FloatConstant);
3148
3149 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003150 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3151 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3152 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3153 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003154
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003155 const float value_;
3156
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003157 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003158 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003159 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003160 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3161};
3162
3163class HDoubleConstant : public HConstant {
3164 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003165 double GetValue() const { return value_; }
3166
David Brazdil77a48ae2015-09-15 12:34:04 +00003167 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3168
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003169 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003170 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003171 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003172 }
3173
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003174 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003175
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003176 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003177 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003178 }
3179 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003180 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003181 }
3182 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003183 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3184 }
3185 bool IsNaN() const {
3186 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003187 }
3188
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003189 DECLARE_INSTRUCTION(DoubleConstant);
3190
3191 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003192 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3193 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3194 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3195 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003196
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003197 const double value_;
3198
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003199 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003200 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003201 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003202 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3203};
3204
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003205enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003206#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003207#include "intrinsics_list.h"
3208 kNone,
3209 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3210#undef INTRINSICS_LIST
3211#undef OPTIMIZING_INTRINSICS
3212};
3213std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3214
Agi Csaki05f20562015-08-19 14:58:14 -07003215enum IntrinsicNeedsEnvironmentOrCache {
3216 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3217 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003218};
3219
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003220class HInvoke : public HInstruction {
3221 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003222 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003223
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003224 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003225
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003226 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003227 SetRawInputAt(index, argument);
3228 }
3229
Roland Levillain3e3d7332015-04-28 11:00:54 +01003230 // Return the number of arguments. This number can be lower than
3231 // the number of inputs returned by InputCount(), as some invoke
3232 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3233 // inputs at the end of their list of inputs.
3234 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3235
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003236 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003237
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003238
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003239 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003240 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003241
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003242 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3243
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003244 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003245 return intrinsic_;
3246 }
3247
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003248 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003249
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003250 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003251 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003252 }
3253
3254 bool CanThrow() const OVERRIDE { return true; }
3255
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003256 uint32_t* GetIntrinsicOptimizations() {
3257 return &intrinsic_optimizations_;
3258 }
3259
3260 const uint32_t* GetIntrinsicOptimizations() const {
3261 return &intrinsic_optimizations_;
3262 }
3263
3264 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3265
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003266 DECLARE_INSTRUCTION(Invoke);
3267
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003268 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003269 HInvoke(ArenaAllocator* arena,
3270 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003271 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003272 Primitive::Type return_type,
3273 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003274 uint32_t dex_method_index,
3275 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003276 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003277 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003278 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003279 inputs_(number_of_arguments + number_of_other_inputs,
3280 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003281 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003282 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003283 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003284 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003285 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003286 }
3287
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003288 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003289 return inputs_[index];
3290 }
3291
David Brazdil1abb4192015-02-17 18:33:36 +00003292 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003293 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003294 }
3295
Roland Levillain3e3d7332015-04-28 11:00:54 +01003296 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003297 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003298 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003299 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003300 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003301 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003302
3303 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3304 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003305
3306 private:
3307 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3308};
3309
Calin Juravle175dc732015-08-25 15:42:32 +01003310class HInvokeUnresolved : public HInvoke {
3311 public:
3312 HInvokeUnresolved(ArenaAllocator* arena,
3313 uint32_t number_of_arguments,
3314 Primitive::Type return_type,
3315 uint32_t dex_pc,
3316 uint32_t dex_method_index,
3317 InvokeType invoke_type)
3318 : HInvoke(arena,
3319 number_of_arguments,
3320 0u /* number_of_other_inputs */,
3321 return_type,
3322 dex_pc,
3323 dex_method_index,
3324 invoke_type) {
3325 }
3326
3327 DECLARE_INSTRUCTION(InvokeUnresolved);
3328
3329 private:
3330 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3331};
3332
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003333class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003334 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003335 // Requirements of this method call regarding the class
3336 // initialization (clinit) check of its declaring class.
3337 enum class ClinitCheckRequirement {
3338 kNone, // Class already initialized.
3339 kExplicit, // Static call having explicit clinit check as last input.
3340 kImplicit, // Static call implicitly requiring a clinit check.
3341 };
3342
Vladimir Marko58155012015-08-19 12:49:41 +00003343 // Determines how to load the target ArtMethod*.
3344 enum class MethodLoadKind {
3345 // Use a String init ArtMethod* loaded from Thread entrypoints.
3346 kStringInit,
3347
3348 // Use the method's own ArtMethod* loaded by the register allocator.
3349 kRecursive,
3350
3351 // Use ArtMethod* at a known address, embed the direct address in the code.
3352 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3353 kDirectAddress,
3354
3355 // Use ArtMethod* at an address that will be known at link time, embed the direct
3356 // address in the code. If the image is relocatable, emit .patch_oat entry.
3357 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3358 // the image relocatable or not.
3359 kDirectAddressWithFixup,
3360
3361 // Load from resoved methods array in the dex cache using a PC-relative load.
3362 // Used when we need to use the dex cache, for example for invoke-static that
3363 // may cause class initialization (the entry may point to a resolution method),
3364 // and we know that we can access the dex cache arrays using a PC-relative load.
3365 kDexCachePcRelative,
3366
3367 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3368 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3369 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3370 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3371 kDexCacheViaMethod,
3372 };
3373
3374 // Determines the location of the code pointer.
3375 enum class CodePtrLocation {
3376 // Recursive call, use local PC-relative call instruction.
3377 kCallSelf,
3378
3379 // Use PC-relative call instruction patched at link time.
3380 // Used for calls within an oat file, boot->boot or app->app.
3381 kCallPCRelative,
3382
3383 // Call to a known target address, embed the direct address in code.
3384 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3385 kCallDirect,
3386
3387 // Call to a target address that will be known at link time, embed the direct
3388 // address in code. If the image is relocatable, emit .patch_oat entry.
3389 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3390 // the image relocatable or not.
3391 kCallDirectWithFixup,
3392
3393 // Use code pointer from the ArtMethod*.
3394 // Used when we don't know the target code. This is also the last-resort-kind used when
3395 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3396 kCallArtMethod,
3397 };
3398
3399 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003400 MethodLoadKind method_load_kind;
3401 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003402 // The method load data holds
3403 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3404 // Note that there are multiple string init methods, each having its own offset.
3405 // - the method address for kDirectAddress
3406 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003407 uint64_t method_load_data;
3408 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003409 };
3410
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003411 HInvokeStaticOrDirect(ArenaAllocator* arena,
3412 uint32_t number_of_arguments,
3413 Primitive::Type return_type,
3414 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003415 uint32_t method_index,
3416 MethodReference target_method,
3417 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003418 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003419 InvokeType invoke_type,
3420 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003421 : HInvoke(arena,
3422 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003423 // There is potentially one extra argument for the HCurrentMethod node, and
3424 // potentially one other if the clinit check is explicit, and potentially
3425 // one other if the method is a string factory.
3426 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3427 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3428 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003429 return_type,
3430 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003431 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003432 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003433 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003434 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003435 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003436 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003437
Vladimir Markodc151b22015-10-15 18:02:30 +01003438 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003439 bool had_current_method_input = HasCurrentMethodInput();
3440 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3441
3442 // Using the current method is the default and once we find a better
3443 // method load kind, we should not go back to using the current method.
3444 DCHECK(had_current_method_input || !needs_current_method_input);
3445
3446 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003447 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3448 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003449 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003450 dispatch_info_ = dispatch_info;
3451 }
3452
Vladimir Markoc53c0792015-11-19 15:48:33 +00003453 void AddSpecialInput(HInstruction* input) {
3454 // We allow only one special input.
3455 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3456 DCHECK(InputCount() == GetSpecialInputIndex() ||
3457 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3458 InsertInputAt(GetSpecialInputIndex(), input);
3459 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003460
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003461 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003462 // We access the method via the dex cache so we can't do an implicit null check.
3463 // TODO: for intrinsics we can generate implicit null checks.
3464 return false;
3465 }
3466
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003467 bool CanBeNull() const OVERRIDE {
3468 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3469 }
3470
Vladimir Markoc53c0792015-11-19 15:48:33 +00003471 // Get the index of the special input, if any.
3472 //
3473 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3474 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3475 // the "special input" is the current method pointer; otherwise there may be one
3476 // platform-specific special input, such as PC-relative addressing base.
3477 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3478
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003479 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003480 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3481 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3482 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003483 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003484 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003485 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003486 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003487 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3488 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003489 bool HasCurrentMethodInput() const {
3490 // This function can be called only after the invoke has been fully initialized by the builder.
3491 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003492 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003493 return true;
3494 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003495 DCHECK(InputCount() == GetSpecialInputIndex() ||
3496 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003497 return false;
3498 }
3499 }
Vladimir Marko58155012015-08-19 12:49:41 +00003500 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3501 MethodReference GetTargetMethod() const { return target_method_; }
3502
3503 int32_t GetStringInitOffset() const {
3504 DCHECK(IsStringInit());
3505 return dispatch_info_.method_load_data;
3506 }
3507
3508 uint64_t GetMethodAddress() const {
3509 DCHECK(HasMethodAddress());
3510 return dispatch_info_.method_load_data;
3511 }
3512
3513 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003514 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003515 return dispatch_info_.method_load_data;
3516 }
3517
3518 uint64_t GetDirectCodePtr() const {
3519 DCHECK(HasDirectCodePtr());
3520 return dispatch_info_.direct_code_ptr;
3521 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003522
Calin Juravle68ad6492015-08-18 17:08:12 +01003523 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3524
Roland Levillain4c0eb422015-04-24 16:43:49 +01003525 // Is this instruction a call to a static method?
3526 bool IsStatic() const {
3527 return GetInvokeType() == kStatic;
3528 }
3529
Vladimir Markofbb184a2015-11-13 14:47:00 +00003530 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3531 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3532 // instruction; only relevant for static calls with explicit clinit check.
3533 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003534 DCHECK(IsStaticWithExplicitClinitCheck());
3535 size_t last_input_index = InputCount() - 1;
3536 HInstruction* last_input = InputAt(last_input_index);
3537 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003538 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003539 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003540 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003541 clinit_check_requirement_ = new_requirement;
3542 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003543 }
3544
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003545 bool IsStringFactoryFor(HFakeString* str) const {
3546 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003547 DCHECK(!HasCurrentMethodInput());
3548 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003549 return InputAt(InputCount() - 1)->AsFakeString() == str;
3550 }
3551
3552 void RemoveFakeStringArgumentAsLastInput() {
3553 DCHECK(IsStringInit());
3554 size_t last_input_index = InputCount() - 1;
3555 HInstruction* last_input = InputAt(last_input_index);
3556 DCHECK(last_input != nullptr);
3557 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3558 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003559 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003560 }
3561
Roland Levillain4c0eb422015-04-24 16:43:49 +01003562 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003563 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003564 bool IsStaticWithExplicitClinitCheck() const {
3565 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3566 }
3567
3568 // Is this a call to a static method whose declaring class has an
3569 // implicit intialization check requirement?
3570 bool IsStaticWithImplicitClinitCheck() const {
3571 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3572 }
3573
Vladimir Markob554b5a2015-11-06 12:57:55 +00003574 // Does this method load kind need the current method as an input?
3575 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3576 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3577 }
3578
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003579 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003580
Roland Levillain4c0eb422015-04-24 16:43:49 +01003581 protected:
3582 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3583 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3584 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3585 HInstruction* input = input_record.GetInstruction();
3586 // `input` is the last input of a static invoke marked as having
3587 // an explicit clinit check. It must either be:
3588 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3589 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3590 DCHECK(input != nullptr);
3591 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3592 }
3593 return input_record;
3594 }
3595
Vladimir Markoc53c0792015-11-19 15:48:33 +00003596 void InsertInputAt(size_t index, HInstruction* input);
3597 void RemoveInputAt(size_t index);
3598
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003599 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003600 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003601 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003602 // The target method may refer to different dex file or method index than the original
3603 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3604 // in derived class to increase visibility.
3605 MethodReference target_method_;
3606 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003607
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003608 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003609};
Vladimir Markofbb184a2015-11-13 14:47:00 +00003610std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003611
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003612class HInvokeVirtual : public HInvoke {
3613 public:
3614 HInvokeVirtual(ArenaAllocator* arena,
3615 uint32_t number_of_arguments,
3616 Primitive::Type return_type,
3617 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003618 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003619 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003620 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003621 vtable_index_(vtable_index) {}
3622
Calin Juravle641547a2015-04-21 22:08:51 +01003623 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003624 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003625 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003626 }
3627
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003628 uint32_t GetVTableIndex() const { return vtable_index_; }
3629
3630 DECLARE_INSTRUCTION(InvokeVirtual);
3631
3632 private:
3633 const uint32_t vtable_index_;
3634
3635 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3636};
3637
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003638class HInvokeInterface : public HInvoke {
3639 public:
3640 HInvokeInterface(ArenaAllocator* arena,
3641 uint32_t number_of_arguments,
3642 Primitive::Type return_type,
3643 uint32_t dex_pc,
3644 uint32_t dex_method_index,
3645 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003646 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003647 imt_index_(imt_index) {}
3648
Calin Juravle641547a2015-04-21 22:08:51 +01003649 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003650 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003651 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003652 }
3653
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003654 uint32_t GetImtIndex() const { return imt_index_; }
3655 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3656
3657 DECLARE_INSTRUCTION(InvokeInterface);
3658
3659 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003660 const uint32_t imt_index_;
3661
3662 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3663};
3664
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003665class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003666 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003667 HNewInstance(HInstruction* cls,
3668 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003669 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003670 uint16_t type_index,
3671 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003672 bool can_throw,
3673 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003674 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003675 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003676 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003677 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003678 can_throw_(can_throw),
3679 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003680 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003681 SetRawInputAt(0, cls);
3682 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003683 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003684
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003685 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003686 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003687
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003688 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003689 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003690
3691 // It may throw when called on type that's not instantiable/accessible.
3692 // It can throw OOME.
3693 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3694 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3695
3696 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003697
Calin Juravle10e244f2015-01-26 18:54:32 +00003698 bool CanBeNull() const OVERRIDE { return false; }
3699
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003700 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3701
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003702 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3703 entrypoint_ = entrypoint;
3704 }
3705
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003706 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003707
3708 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003709 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003710 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003711 const bool can_throw_;
3712 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003713 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003714
3715 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3716};
3717
Roland Levillain88cb1752014-10-20 16:36:47 +01003718class HNeg : public HUnaryOperation {
3719 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003720 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3721 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003722
Roland Levillain9867bc72015-08-05 10:21:34 +01003723 template <typename T> T Compute(T x) const { return -x; }
3724
3725 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003726 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003727 }
3728 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003729 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003730 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003731
Roland Levillain88cb1752014-10-20 16:36:47 +01003732 DECLARE_INSTRUCTION(Neg);
3733
3734 private:
3735 DISALLOW_COPY_AND_ASSIGN(HNeg);
3736};
3737
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003738class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003739 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003740 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003741 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003742 uint32_t dex_pc,
3743 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003744 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003745 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003746 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003747 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003748 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003749 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003750 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003751 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003752 }
3753
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003754 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003755 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003756
3757 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003758 bool NeedsEnvironment() const OVERRIDE { return true; }
3759
Mingyao Yang0c365e62015-03-31 15:09:29 -07003760 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3761 bool CanThrow() const OVERRIDE { return true; }
3762
Calin Juravle10e244f2015-01-26 18:54:32 +00003763 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003764
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003765 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3766
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003767 DECLARE_INSTRUCTION(NewArray);
3768
3769 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003770 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003771 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003772 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003773
3774 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3775};
3776
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003777class HAdd : public HBinaryOperation {
3778 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003779 HAdd(Primitive::Type result_type,
3780 HInstruction* left,
3781 HInstruction* right,
3782 uint32_t dex_pc = kNoDexPc)
3783 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003784
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003785 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003786
Roland Levillain9867bc72015-08-05 10:21:34 +01003787 template <typename T> T Compute(T x, T y) const { return x + y; }
3788
3789 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003790 return GetBlock()->GetGraph()->GetIntConstant(
3791 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003792 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003793 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003794 return GetBlock()->GetGraph()->GetLongConstant(
3795 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003796 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003797
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003798 DECLARE_INSTRUCTION(Add);
3799
3800 private:
3801 DISALLOW_COPY_AND_ASSIGN(HAdd);
3802};
3803
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003804class HSub : public HBinaryOperation {
3805 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003806 HSub(Primitive::Type result_type,
3807 HInstruction* left,
3808 HInstruction* right,
3809 uint32_t dex_pc = kNoDexPc)
3810 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003811
Roland Levillain9867bc72015-08-05 10:21:34 +01003812 template <typename T> T Compute(T x, T y) const { return x - y; }
3813
3814 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003815 return GetBlock()->GetGraph()->GetIntConstant(
3816 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003817 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003818 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003819 return GetBlock()->GetGraph()->GetLongConstant(
3820 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003821 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003822
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003823 DECLARE_INSTRUCTION(Sub);
3824
3825 private:
3826 DISALLOW_COPY_AND_ASSIGN(HSub);
3827};
3828
Calin Juravle34bacdf2014-10-07 20:23:36 +01003829class HMul : public HBinaryOperation {
3830 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003831 HMul(Primitive::Type result_type,
3832 HInstruction* left,
3833 HInstruction* right,
3834 uint32_t dex_pc = kNoDexPc)
3835 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003836
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003837 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003838
Roland Levillain9867bc72015-08-05 10:21:34 +01003839 template <typename T> T Compute(T x, T y) const { return x * y; }
3840
3841 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003842 return GetBlock()->GetGraph()->GetIntConstant(
3843 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003844 }
3845 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003846 return GetBlock()->GetGraph()->GetLongConstant(
3847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003848 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003849
3850 DECLARE_INSTRUCTION(Mul);
3851
3852 private:
3853 DISALLOW_COPY_AND_ASSIGN(HMul);
3854};
3855
Calin Juravle7c4954d2014-10-28 16:57:40 +00003856class HDiv : public HBinaryOperation {
3857 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003858 HDiv(Primitive::Type result_type,
3859 HInstruction* left,
3860 HInstruction* right,
3861 uint32_t dex_pc)
3862 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003863
Roland Levillain9867bc72015-08-05 10:21:34 +01003864 template <typename T>
3865 T Compute(T x, T y) const {
3866 // Our graph structure ensures we never have 0 for `y` during
3867 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003868 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003869 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003870 return (y == -1) ? -x : x / y;
3871 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003872
Roland Levillain9867bc72015-08-05 10:21:34 +01003873 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003874 return GetBlock()->GetGraph()->GetIntConstant(
3875 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003876 }
3877 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003878 return GetBlock()->GetGraph()->GetLongConstant(
3879 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003880 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003881
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003882 static SideEffects SideEffectsForArchRuntimeCalls() {
3883 // The generated code can use a runtime call.
3884 return SideEffects::CanTriggerGC();
3885 }
3886
Calin Juravle7c4954d2014-10-28 16:57:40 +00003887 DECLARE_INSTRUCTION(Div);
3888
3889 private:
3890 DISALLOW_COPY_AND_ASSIGN(HDiv);
3891};
3892
Calin Juravlebacfec32014-11-14 15:54:36 +00003893class HRem : public HBinaryOperation {
3894 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003895 HRem(Primitive::Type result_type,
3896 HInstruction* left,
3897 HInstruction* right,
3898 uint32_t dex_pc)
3899 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003900
Roland Levillain9867bc72015-08-05 10:21:34 +01003901 template <typename T>
3902 T Compute(T x, T y) const {
3903 // Our graph structure ensures we never have 0 for `y` during
3904 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003905 DCHECK_NE(y, 0);
3906 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3907 return (y == -1) ? 0 : x % y;
3908 }
3909
Roland Levillain9867bc72015-08-05 10:21:34 +01003910 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003911 return GetBlock()->GetGraph()->GetIntConstant(
3912 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003913 }
3914 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003915 return GetBlock()->GetGraph()->GetLongConstant(
3916 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003917 }
3918
Calin Juravlebacfec32014-11-14 15:54:36 +00003919
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003920 static SideEffects SideEffectsForArchRuntimeCalls() {
3921 return SideEffects::CanTriggerGC();
3922 }
3923
Calin Juravlebacfec32014-11-14 15:54:36 +00003924 DECLARE_INSTRUCTION(Rem);
3925
3926 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003927 DISALLOW_COPY_AND_ASSIGN(HRem);
3928};
3929
Calin Juravled0d48522014-11-04 16:40:20 +00003930class HDivZeroCheck : public HExpression<1> {
3931 public:
3932 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003933 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003934 SetRawInputAt(0, value);
3935 }
3936
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003937 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3938
Calin Juravled0d48522014-11-04 16:40:20 +00003939 bool CanBeMoved() const OVERRIDE { return true; }
3940
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003941 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003942 return true;
3943 }
3944
3945 bool NeedsEnvironment() const OVERRIDE { return true; }
3946 bool CanThrow() const OVERRIDE { return true; }
3947
Calin Juravled0d48522014-11-04 16:40:20 +00003948 DECLARE_INSTRUCTION(DivZeroCheck);
3949
3950 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003951 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3952};
3953
Calin Juravle9aec02f2014-11-18 23:06:35 +00003954class HShl : public HBinaryOperation {
3955 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003956 HShl(Primitive::Type result_type,
3957 HInstruction* left,
3958 HInstruction* right,
3959 uint32_t dex_pc = kNoDexPc)
3960 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003961
Roland Levillain9867bc72015-08-05 10:21:34 +01003962 template <typename T, typename U, typename V>
3963 T Compute(T x, U y, V max_shift_value) const {
3964 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3965 "V is not the unsigned integer type corresponding to T");
3966 return x << (y & max_shift_value);
3967 }
3968
3969 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3970 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003971 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003972 }
3973 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3974 // case is handled as `x << static_cast<int>(y)`.
3975 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3976 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003977 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003978 }
3979 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3980 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003981 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003982 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003983
3984 DECLARE_INSTRUCTION(Shl);
3985
3986 private:
3987 DISALLOW_COPY_AND_ASSIGN(HShl);
3988};
3989
3990class HShr : public HBinaryOperation {
3991 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003992 HShr(Primitive::Type result_type,
3993 HInstruction* left,
3994 HInstruction* right,
3995 uint32_t dex_pc = kNoDexPc)
3996 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003997
Roland Levillain9867bc72015-08-05 10:21:34 +01003998 template <typename T, typename U, typename V>
3999 T Compute(T x, U y, V max_shift_value) const {
4000 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4001 "V is not the unsigned integer type corresponding to T");
4002 return x >> (y & max_shift_value);
4003 }
4004
4005 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4006 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004007 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004008 }
4009 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4010 // case is handled as `x >> static_cast<int>(y)`.
4011 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4012 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004013 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004014 }
4015 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4016 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004017 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004018 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004019
4020 DECLARE_INSTRUCTION(Shr);
4021
4022 private:
4023 DISALLOW_COPY_AND_ASSIGN(HShr);
4024};
4025
4026class HUShr : public HBinaryOperation {
4027 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004028 HUShr(Primitive::Type result_type,
4029 HInstruction* left,
4030 HInstruction* right,
4031 uint32_t dex_pc = kNoDexPc)
4032 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004033
Roland Levillain9867bc72015-08-05 10:21:34 +01004034 template <typename T, typename U, typename V>
4035 T Compute(T x, U y, V max_shift_value) const {
4036 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4037 "V is not the unsigned integer type corresponding to T");
4038 V ux = static_cast<V>(x);
4039 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004040 }
4041
Roland Levillain9867bc72015-08-05 10:21:34 +01004042 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4043 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004044 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004045 }
4046 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4047 // case is handled as `x >>> static_cast<int>(y)`.
4048 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4049 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004050 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004051 }
4052 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4053 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004054 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004055 }
4056
4057 DECLARE_INSTRUCTION(UShr);
4058
4059 private:
4060 DISALLOW_COPY_AND_ASSIGN(HUShr);
4061};
4062
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004063class HAnd : public HBinaryOperation {
4064 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004065 HAnd(Primitive::Type result_type,
4066 HInstruction* left,
4067 HInstruction* right,
4068 uint32_t dex_pc = kNoDexPc)
4069 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004070
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004071 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004072
Roland Levillain9867bc72015-08-05 10:21:34 +01004073 template <typename T, typename U>
4074 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4075
4076 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004077 return GetBlock()->GetGraph()->GetIntConstant(
4078 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004079 }
4080 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004081 return GetBlock()->GetGraph()->GetLongConstant(
4082 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004083 }
4084 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004085 return GetBlock()->GetGraph()->GetLongConstant(
4086 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004087 }
4088 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004089 return GetBlock()->GetGraph()->GetLongConstant(
4090 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004091 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004092
4093 DECLARE_INSTRUCTION(And);
4094
4095 private:
4096 DISALLOW_COPY_AND_ASSIGN(HAnd);
4097};
4098
4099class HOr : public HBinaryOperation {
4100 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004101 HOr(Primitive::Type result_type,
4102 HInstruction* left,
4103 HInstruction* right,
4104 uint32_t dex_pc = kNoDexPc)
4105 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004106
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004107 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004108
Roland Levillain9867bc72015-08-05 10:21:34 +01004109 template <typename T, typename U>
4110 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4111
4112 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004113 return GetBlock()->GetGraph()->GetIntConstant(
4114 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004115 }
4116 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004117 return GetBlock()->GetGraph()->GetLongConstant(
4118 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004119 }
4120 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004121 return GetBlock()->GetGraph()->GetLongConstant(
4122 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004123 }
4124 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004125 return GetBlock()->GetGraph()->GetLongConstant(
4126 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004127 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004128
4129 DECLARE_INSTRUCTION(Or);
4130
4131 private:
4132 DISALLOW_COPY_AND_ASSIGN(HOr);
4133};
4134
4135class HXor : public HBinaryOperation {
4136 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004137 HXor(Primitive::Type result_type,
4138 HInstruction* left,
4139 HInstruction* right,
4140 uint32_t dex_pc = kNoDexPc)
4141 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004142
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004143 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004144
Roland Levillain9867bc72015-08-05 10:21:34 +01004145 template <typename T, typename U>
4146 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4147
4148 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004149 return GetBlock()->GetGraph()->GetIntConstant(
4150 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004151 }
4152 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004153 return GetBlock()->GetGraph()->GetLongConstant(
4154 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004155 }
4156 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004157 return GetBlock()->GetGraph()->GetLongConstant(
4158 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004159 }
4160 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004161 return GetBlock()->GetGraph()->GetLongConstant(
4162 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004163 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004164
4165 DECLARE_INSTRUCTION(Xor);
4166
4167 private:
4168 DISALLOW_COPY_AND_ASSIGN(HXor);
4169};
4170
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004171// The value of a parameter in this method. Its location depends on
4172// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004173class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004174 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004175 HParameterValue(const DexFile& dex_file,
4176 uint16_t type_index,
4177 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004178 Primitive::Type parameter_type,
4179 bool is_this = false)
4180 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004181 dex_file_(dex_file),
4182 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004183 index_(index),
4184 is_this_(is_this),
4185 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004186
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004187 const DexFile& GetDexFile() const { return dex_file_; }
4188 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004189 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004190 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004191
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004192 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4193 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004194
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004195 DECLARE_INSTRUCTION(ParameterValue);
4196
4197 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004198 const DexFile& dex_file_;
4199 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004200 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004201 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004202 const uint8_t index_;
4203
Calin Juravle10e244f2015-01-26 18:54:32 +00004204 // Whether or not the parameter value corresponds to 'this' argument.
4205 const bool is_this_;
4206
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004207 bool can_be_null_;
4208
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004209 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4210};
4211
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004212class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004213 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004214 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4215 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004216
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004217 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004218 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004219 return true;
4220 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004221
Roland Levillain9867bc72015-08-05 10:21:34 +01004222 template <typename T> T Compute(T x) const { return ~x; }
4223
4224 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004225 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004226 }
4227 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004228 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004229 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004230
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004231 DECLARE_INSTRUCTION(Not);
4232
4233 private:
4234 DISALLOW_COPY_AND_ASSIGN(HNot);
4235};
4236
David Brazdil66d126e2015-04-03 16:02:44 +01004237class HBooleanNot : public HUnaryOperation {
4238 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004239 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4240 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004241
4242 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004243 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004244 return true;
4245 }
4246
Roland Levillain9867bc72015-08-05 10:21:34 +01004247 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004248 DCHECK(IsUint<1>(x));
4249 return !x;
4250 }
4251
Roland Levillain9867bc72015-08-05 10:21:34 +01004252 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004253 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004254 }
4255 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4256 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004257 UNREACHABLE();
4258 }
4259
4260 DECLARE_INSTRUCTION(BooleanNot);
4261
4262 private:
4263 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4264};
4265
Roland Levillaindff1f282014-11-05 14:15:05 +00004266class HTypeConversion : public HExpression<1> {
4267 public:
4268 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004269 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004270 : HExpression(result_type,
4271 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4272 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004273 SetRawInputAt(0, input);
4274 DCHECK_NE(input->GetType(), result_type);
4275 }
4276
4277 HInstruction* GetInput() const { return InputAt(0); }
4278 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4279 Primitive::Type GetResultType() const { return GetType(); }
4280
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004281 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004282 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004283
Roland Levillaindff1f282014-11-05 14:15:05 +00004284 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004285 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004286
Mark Mendelle82549b2015-05-06 10:55:34 -04004287 // Try to statically evaluate the conversion and return a HConstant
4288 // containing the result. If the input cannot be converted, return nullptr.
4289 HConstant* TryStaticEvaluation() const;
4290
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004291 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4292 Primitive::Type result_type) {
4293 // Some architectures may not require the 'GC' side effects, but at this point
4294 // in the compilation process we do not know what architecture we will
4295 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004296 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4297 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004298 return SideEffects::CanTriggerGC();
4299 }
4300 return SideEffects::None();
4301 }
4302
Roland Levillaindff1f282014-11-05 14:15:05 +00004303 DECLARE_INSTRUCTION(TypeConversion);
4304
4305 private:
4306 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4307};
4308
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004309static constexpr uint32_t kNoRegNumber = -1;
4310
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004311class HPhi : public HInstruction {
4312 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004313 HPhi(ArenaAllocator* arena,
4314 uint32_t reg_number,
4315 size_t number_of_inputs,
4316 Primitive::Type type,
4317 uint32_t dex_pc = kNoDexPc)
4318 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004319 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004320 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004321 type_(type),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004322 is_live_(false),
Calin Juravle10e244f2015-01-26 18:54:32 +00004323 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004324 }
4325
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004326 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4327 static Primitive::Type ToPhiType(Primitive::Type type) {
4328 switch (type) {
4329 case Primitive::kPrimBoolean:
4330 case Primitive::kPrimByte:
4331 case Primitive::kPrimShort:
4332 case Primitive::kPrimChar:
4333 return Primitive::kPrimInt;
4334 default:
4335 return type;
4336 }
4337 }
4338
David Brazdilffee3d32015-07-06 11:48:53 +01004339 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4340
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004341 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004342
4343 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004344 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004345
Calin Juravle10e244f2015-01-26 18:54:32 +00004346 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004347 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004348
Calin Juravle10e244f2015-01-26 18:54:32 +00004349 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4350 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4351
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004352 uint32_t GetRegNumber() const { return reg_number_; }
4353
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004354 void SetDead() { is_live_ = false; }
4355 void SetLive() { is_live_ = true; }
4356 bool IsDead() const { return !is_live_; }
4357 bool IsLive() const { return is_live_; }
4358
David Brazdil77a48ae2015-09-15 12:34:04 +00004359 bool IsVRegEquivalentOf(HInstruction* other) const {
4360 return other != nullptr
4361 && other->IsPhi()
4362 && other->AsPhi()->GetBlock() == GetBlock()
4363 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4364 }
4365
Calin Juravlea4f88312015-04-16 12:57:19 +01004366 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4367 // An equivalent phi is a phi having the same dex register and type.
4368 // It assumes that phis with the same dex register are adjacent.
4369 HPhi* GetNextEquivalentPhiWithSameType() {
4370 HInstruction* next = GetNext();
4371 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4372 if (next->GetType() == GetType()) {
4373 return next->AsPhi();
4374 }
4375 next = next->GetNext();
4376 }
4377 return nullptr;
4378 }
4379
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004380 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004381
David Brazdil1abb4192015-02-17 18:33:36 +00004382 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004383 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004384 return inputs_[index];
4385 }
David Brazdil1abb4192015-02-17 18:33:36 +00004386
4387 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004388 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004389 }
4390
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004391 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004392 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004393 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004394 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004395 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004396 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004397
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004398 DISALLOW_COPY_AND_ASSIGN(HPhi);
4399};
4400
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004401class HNullCheck : public HExpression<1> {
4402 public:
4403 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004404 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004405 SetRawInputAt(0, value);
4406 }
4407
Calin Juravle10e244f2015-01-26 18:54:32 +00004408 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004409 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004410 return true;
4411 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004412
Calin Juravle10e244f2015-01-26 18:54:32 +00004413 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004414
Calin Juravle10e244f2015-01-26 18:54:32 +00004415 bool CanThrow() const OVERRIDE { return true; }
4416
4417 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004418
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004419
4420 DECLARE_INSTRUCTION(NullCheck);
4421
4422 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004423 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4424};
4425
4426class FieldInfo : public ValueObject {
4427 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004428 FieldInfo(MemberOffset field_offset,
4429 Primitive::Type field_type,
4430 bool is_volatile,
4431 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004432 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004433 const DexFile& dex_file,
4434 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004435 : field_offset_(field_offset),
4436 field_type_(field_type),
4437 is_volatile_(is_volatile),
4438 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004439 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004440 dex_file_(dex_file),
4441 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004442
4443 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004444 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004445 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004446 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004447 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004448 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004449 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004450
4451 private:
4452 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004453 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004454 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004455 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004456 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004457 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004458 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004459};
4460
4461class HInstanceFieldGet : public HExpression<1> {
4462 public:
4463 HInstanceFieldGet(HInstruction* value,
4464 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004465 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004466 bool is_volatile,
4467 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004468 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004469 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004470 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004471 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004472 : HExpression(field_type,
4473 SideEffects::FieldReadOfType(field_type, is_volatile),
4474 dex_pc),
4475 field_info_(field_offset,
4476 field_type,
4477 is_volatile,
4478 field_idx,
4479 declaring_class_def_index,
4480 dex_file,
4481 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004482 SetRawInputAt(0, value);
4483 }
4484
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004485 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004486
4487 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4488 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4489 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004490 }
4491
Calin Juravle641547a2015-04-21 22:08:51 +01004492 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4493 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004494 }
4495
4496 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004497 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4498 }
4499
Calin Juravle52c48962014-12-16 17:02:57 +00004500 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004501 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004502 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004503 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004504
4505 DECLARE_INSTRUCTION(InstanceFieldGet);
4506
4507 private:
4508 const FieldInfo field_info_;
4509
4510 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4511};
4512
4513class HInstanceFieldSet : public HTemplateInstruction<2> {
4514 public:
4515 HInstanceFieldSet(HInstruction* object,
4516 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004517 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004518 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004519 bool is_volatile,
4520 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004521 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004522 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004523 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004524 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004525 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4526 dex_pc),
4527 field_info_(field_offset,
4528 field_type,
4529 is_volatile,
4530 field_idx,
4531 declaring_class_def_index,
4532 dex_file,
4533 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004534 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004535 SetRawInputAt(0, object);
4536 SetRawInputAt(1, value);
4537 }
4538
Calin Juravle641547a2015-04-21 22:08:51 +01004539 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4540 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004541 }
4542
Calin Juravle52c48962014-12-16 17:02:57 +00004543 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004544 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004545 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004546 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004547 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004548 bool GetValueCanBeNull() const { return value_can_be_null_; }
4549 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004550
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004551 DECLARE_INSTRUCTION(InstanceFieldSet);
4552
4553 private:
4554 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004555 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004556
4557 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4558};
4559
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004560class HArrayGet : public HExpression<2> {
4561 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004562 HArrayGet(HInstruction* array,
4563 HInstruction* index,
4564 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004565 uint32_t dex_pc,
4566 SideEffects additional_side_effects = SideEffects::None())
4567 : HExpression(type,
4568 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004569 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004570 SetRawInputAt(0, array);
4571 SetRawInputAt(1, index);
4572 }
4573
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004574 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004575 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004576 return true;
4577 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004578 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004579 // TODO: We can be smarter here.
4580 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4581 // which generates the implicit null check. There are cases when these can be removed
4582 // to produce better code. If we ever add optimizations to do so we should allow an
4583 // implicit check here (as long as the address falls in the first page).
4584 return false;
4585 }
4586
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004587 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004588
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004589 HInstruction* GetArray() const { return InputAt(0); }
4590 HInstruction* GetIndex() const { return InputAt(1); }
4591
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004592 DECLARE_INSTRUCTION(ArrayGet);
4593
4594 private:
4595 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4596};
4597
4598class HArraySet : public HTemplateInstruction<3> {
4599 public:
4600 HArraySet(HInstruction* array,
4601 HInstruction* index,
4602 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004603 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004604 uint32_t dex_pc,
4605 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004606 : HTemplateInstruction(
4607 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004608 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4609 additional_side_effects),
4610 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004611 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004612 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004613 value_can_be_null_(true),
4614 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004615 SetRawInputAt(0, array);
4616 SetRawInputAt(1, index);
4617 SetRawInputAt(2, value);
4618 }
4619
Calin Juravle77520bc2015-01-12 18:45:46 +00004620 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004621 // We currently always call a runtime method to catch array store
4622 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004623 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004624 }
4625
Mingyao Yang81014cb2015-06-02 03:16:27 -07004626 // Can throw ArrayStoreException.
4627 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4628
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004629 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004630 // TODO: Same as for ArrayGet.
4631 return false;
4632 }
4633
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004634 void ClearNeedsTypeCheck() {
4635 needs_type_check_ = false;
4636 }
4637
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004638 void ClearValueCanBeNull() {
4639 value_can_be_null_ = false;
4640 }
4641
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004642 void SetStaticTypeOfArrayIsObjectArray() {
4643 static_type_of_array_is_object_array_ = true;
4644 }
4645
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004646 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004647 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004648 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004649
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004650 HInstruction* GetArray() const { return InputAt(0); }
4651 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004652 HInstruction* GetValue() const { return InputAt(2); }
4653
4654 Primitive::Type GetComponentType() const {
4655 // The Dex format does not type floating point index operations. Since the
4656 // `expected_component_type_` is set during building and can therefore not
4657 // be correct, we also check what is the value type. If it is a floating
4658 // point type, we must use that type.
4659 Primitive::Type value_type = GetValue()->GetType();
4660 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4661 ? value_type
4662 : expected_component_type_;
4663 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004664
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004665 Primitive::Type GetRawExpectedComponentType() const {
4666 return expected_component_type_;
4667 }
4668
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004669 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4670 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4671 }
4672
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004673 DECLARE_INSTRUCTION(ArraySet);
4674
4675 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004676 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004677 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004678 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004679 // Cached information for the reference_type_info_ so that codegen
4680 // does not need to inspect the static type.
4681 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004682
4683 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4684};
4685
4686class HArrayLength : public HExpression<1> {
4687 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004688 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004689 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004690 // Note that arrays do not change length, so the instruction does not
4691 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004692 SetRawInputAt(0, array);
4693 }
4694
Calin Juravle77520bc2015-01-12 18:45:46 +00004695 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004696 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004697 return true;
4698 }
Calin Juravle641547a2015-04-21 22:08:51 +01004699 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4700 return obj == InputAt(0);
4701 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004702
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004703 DECLARE_INSTRUCTION(ArrayLength);
4704
4705 private:
4706 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4707};
4708
4709class HBoundsCheck : public HExpression<2> {
4710 public:
4711 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004712 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004713 DCHECK(index->GetType() == Primitive::kPrimInt);
4714 SetRawInputAt(0, index);
4715 SetRawInputAt(1, length);
4716 }
4717
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004718 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004719 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004720 return true;
4721 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004722
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004723 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004724
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004725 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004726
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004727 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004728
4729 DECLARE_INSTRUCTION(BoundsCheck);
4730
4731 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004732 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4733};
4734
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004735/**
4736 * Some DEX instructions are folded into multiple HInstructions that need
4737 * to stay live until the last HInstruction. This class
4738 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004739 * HInstruction stays live. `index` represents the stack location index of the
4740 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004741 */
4742class HTemporary : public HTemplateInstruction<0> {
4743 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004744 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4745 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004746
4747 size_t GetIndex() const { return index_; }
4748
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004749 Primitive::Type GetType() const OVERRIDE {
4750 // The previous instruction is the one that will be stored in the temporary location.
4751 DCHECK(GetPrevious() != nullptr);
4752 return GetPrevious()->GetType();
4753 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004754
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004755 DECLARE_INSTRUCTION(Temporary);
4756
4757 private:
4758 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004759 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4760};
4761
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004762class HSuspendCheck : public HTemplateInstruction<0> {
4763 public:
4764 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004765 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004766
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004767 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004768 return true;
4769 }
4770
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004771 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4772 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004773
4774 DECLARE_INSTRUCTION(SuspendCheck);
4775
4776 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004777 // Only used for code generation, in order to share the same slow path between back edges
4778 // of a same loop.
4779 SlowPathCode* slow_path_;
4780
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004781 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4782};
4783
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004784/**
4785 * Instruction to load a Class object.
4786 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004787class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004788 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004789 HLoadClass(HCurrentMethod* current_method,
4790 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004791 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004792 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004793 uint32_t dex_pc,
4794 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004795 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004796 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004797 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004798 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004799 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004800 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004801 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004802 // Referrers class should not need access check. We never inline unverified
4803 // methods so we can't possibly end up in this situation.
4804 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004805 SetRawInputAt(0, current_method);
4806 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004807
4808 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004809
4810 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004811 // Note that we don't need to test for generate_clinit_check_.
4812 // Whether or not we need to generate the clinit check is processed in
4813 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004814 return other->AsLoadClass()->type_index_ == type_index_ &&
4815 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004816 }
4817
4818 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4819
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004820 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004821 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004822 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004823
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004824 bool NeedsEnvironment() const OVERRIDE {
4825 // Will call runtime and load the class if the class is not loaded yet.
4826 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004827 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004828 }
4829
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004830 bool MustGenerateClinitCheck() const {
4831 return generate_clinit_check_;
4832 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004833 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004834 // The entrypoint the code generator is going to call does not do
4835 // clinit of the class.
4836 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004837 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004838 }
4839
4840 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004841 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4842 }
4843
4844 bool NeedsAccessCheck() const {
4845 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004846 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004847
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004848 bool CanThrow() const OVERRIDE {
4849 // May call runtime and and therefore can throw.
4850 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004851 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004852 }
4853
Calin Juravleacf735c2015-02-12 15:25:22 +00004854 ReferenceTypeInfo GetLoadedClassRTI() {
4855 return loaded_class_rti_;
4856 }
4857
4858 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4859 // Make sure we only set exact types (the loaded class should never be merged).
4860 DCHECK(rti.IsExact());
4861 loaded_class_rti_ = rti;
4862 }
4863
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004864 const DexFile& GetDexFile() { return dex_file_; }
4865
Vladimir Markodc151b22015-10-15 18:02:30 +01004866 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004867
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004868 static SideEffects SideEffectsForArchRuntimeCalls() {
4869 return SideEffects::CanTriggerGC();
4870 }
4871
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004872 DECLARE_INSTRUCTION(LoadClass);
4873
4874 private:
4875 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004876 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004877 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004878 // Whether this instruction must generate the initialization check.
4879 // Used for code generation.
4880 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004881 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004882
Calin Juravleacf735c2015-02-12 15:25:22 +00004883 ReferenceTypeInfo loaded_class_rti_;
4884
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004885 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4886};
4887
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004888class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004889 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004890 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004891 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4892 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004893 SetRawInputAt(0, current_method);
4894 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004895
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004896 bool CanBeMoved() const OVERRIDE { return true; }
4897
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004898 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4899 return other->AsLoadString()->string_index_ == string_index_;
4900 }
4901
4902 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4903
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004904 uint32_t GetStringIndex() const { return string_index_; }
4905
4906 // TODO: Can we deopt or debug when we resolve a string?
4907 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004908 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004909 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004910
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004911 static SideEffects SideEffectsForArchRuntimeCalls() {
4912 return SideEffects::CanTriggerGC();
4913 }
4914
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004915 DECLARE_INSTRUCTION(LoadString);
4916
4917 private:
4918 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004919
4920 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4921};
4922
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004923/**
4924 * Performs an initialization check on its Class object input.
4925 */
4926class HClinitCheck : public HExpression<1> {
4927 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004928 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004929 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004930 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004931 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4932 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004933 SetRawInputAt(0, constant);
4934 }
4935
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004936 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004937 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004938 return true;
4939 }
4940
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004941 bool NeedsEnvironment() const OVERRIDE {
4942 // May call runtime to initialize the class.
4943 return true;
4944 }
4945
Nicolas Geoffray729645a2015-11-19 13:29:02 +00004946 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004947
4948 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4949
4950 DECLARE_INSTRUCTION(ClinitCheck);
4951
4952 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004953 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4954};
4955
4956class HStaticFieldGet : public HExpression<1> {
4957 public:
4958 HStaticFieldGet(HInstruction* cls,
4959 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004960 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004961 bool is_volatile,
4962 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004963 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004964 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004965 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004966 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004967 : HExpression(field_type,
4968 SideEffects::FieldReadOfType(field_type, is_volatile),
4969 dex_pc),
4970 field_info_(field_offset,
4971 field_type,
4972 is_volatile,
4973 field_idx,
4974 declaring_class_def_index,
4975 dex_file,
4976 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004977 SetRawInputAt(0, cls);
4978 }
4979
Calin Juravle52c48962014-12-16 17:02:57 +00004980
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004981 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004982
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004983 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004984 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4985 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004986 }
4987
4988 size_t ComputeHashCode() const OVERRIDE {
4989 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4990 }
4991
Calin Juravle52c48962014-12-16 17:02:57 +00004992 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004993 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4994 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004995 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004996
4997 DECLARE_INSTRUCTION(StaticFieldGet);
4998
4999 private:
5000 const FieldInfo field_info_;
5001
5002 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5003};
5004
5005class HStaticFieldSet : public HTemplateInstruction<2> {
5006 public:
5007 HStaticFieldSet(HInstruction* cls,
5008 HInstruction* value,
5009 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005010 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005011 bool is_volatile,
5012 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005013 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005014 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005015 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005016 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005017 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5018 dex_pc),
5019 field_info_(field_offset,
5020 field_type,
5021 is_volatile,
5022 field_idx,
5023 declaring_class_def_index,
5024 dex_file,
5025 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005026 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005027 SetRawInputAt(0, cls);
5028 SetRawInputAt(1, value);
5029 }
5030
Calin Juravle52c48962014-12-16 17:02:57 +00005031 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005032 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5033 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005034 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005035
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005036 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005037 bool GetValueCanBeNull() const { return value_can_be_null_; }
5038 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005039
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005040 DECLARE_INSTRUCTION(StaticFieldSet);
5041
5042 private:
5043 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005044 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005045
5046 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5047};
5048
Calin Juravlee460d1d2015-09-29 04:52:17 +01005049class HUnresolvedInstanceFieldGet : public HExpression<1> {
5050 public:
5051 HUnresolvedInstanceFieldGet(HInstruction* obj,
5052 Primitive::Type field_type,
5053 uint32_t field_index,
5054 uint32_t dex_pc)
5055 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5056 field_index_(field_index) {
5057 SetRawInputAt(0, obj);
5058 }
5059
5060 bool NeedsEnvironment() const OVERRIDE { return true; }
5061 bool CanThrow() const OVERRIDE { return true; }
5062
5063 Primitive::Type GetFieldType() const { return GetType(); }
5064 uint32_t GetFieldIndex() const { return field_index_; }
5065
5066 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5067
5068 private:
5069 const uint32_t field_index_;
5070
5071 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5072};
5073
5074class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5075 public:
5076 HUnresolvedInstanceFieldSet(HInstruction* obj,
5077 HInstruction* value,
5078 Primitive::Type field_type,
5079 uint32_t field_index,
5080 uint32_t dex_pc)
5081 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5082 field_type_(field_type),
5083 field_index_(field_index) {
5084 DCHECK_EQ(field_type, value->GetType());
5085 SetRawInputAt(0, obj);
5086 SetRawInputAt(1, value);
5087 }
5088
5089 bool NeedsEnvironment() const OVERRIDE { return true; }
5090 bool CanThrow() const OVERRIDE { return true; }
5091
5092 Primitive::Type GetFieldType() const { return field_type_; }
5093 uint32_t GetFieldIndex() const { return field_index_; }
5094
5095 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5096
5097 private:
5098 const Primitive::Type field_type_;
5099 const uint32_t field_index_;
5100
5101 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5102};
5103
5104class HUnresolvedStaticFieldGet : public HExpression<0> {
5105 public:
5106 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5107 uint32_t field_index,
5108 uint32_t dex_pc)
5109 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5110 field_index_(field_index) {
5111 }
5112
5113 bool NeedsEnvironment() const OVERRIDE { return true; }
5114 bool CanThrow() const OVERRIDE { return true; }
5115
5116 Primitive::Type GetFieldType() const { return GetType(); }
5117 uint32_t GetFieldIndex() const { return field_index_; }
5118
5119 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5120
5121 private:
5122 const uint32_t field_index_;
5123
5124 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5125};
5126
5127class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5128 public:
5129 HUnresolvedStaticFieldSet(HInstruction* value,
5130 Primitive::Type field_type,
5131 uint32_t field_index,
5132 uint32_t dex_pc)
5133 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5134 field_type_(field_type),
5135 field_index_(field_index) {
5136 DCHECK_EQ(field_type, value->GetType());
5137 SetRawInputAt(0, value);
5138 }
5139
5140 bool NeedsEnvironment() const OVERRIDE { return true; }
5141 bool CanThrow() const OVERRIDE { return true; }
5142
5143 Primitive::Type GetFieldType() const { return field_type_; }
5144 uint32_t GetFieldIndex() const { return field_index_; }
5145
5146 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5147
5148 private:
5149 const Primitive::Type field_type_;
5150 const uint32_t field_index_;
5151
5152 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5153};
5154
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005155// Implement the move-exception DEX instruction.
5156class HLoadException : public HExpression<0> {
5157 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005158 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5159 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005160
David Brazdilbbd733e2015-08-18 17:48:17 +01005161 bool CanBeNull() const OVERRIDE { return false; }
5162
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005163 DECLARE_INSTRUCTION(LoadException);
5164
5165 private:
5166 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5167};
5168
David Brazdilcb1c0552015-08-04 16:22:25 +01005169// Implicit part of move-exception which clears thread-local exception storage.
5170// Must not be removed because the runtime expects the TLS to get cleared.
5171class HClearException : public HTemplateInstruction<0> {
5172 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005173 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5174 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005175
5176 DECLARE_INSTRUCTION(ClearException);
5177
5178 private:
5179 DISALLOW_COPY_AND_ASSIGN(HClearException);
5180};
5181
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005182class HThrow : public HTemplateInstruction<1> {
5183 public:
5184 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005185 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005186 SetRawInputAt(0, exception);
5187 }
5188
5189 bool IsControlFlow() const OVERRIDE { return true; }
5190
5191 bool NeedsEnvironment() const OVERRIDE { return true; }
5192
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005193 bool CanThrow() const OVERRIDE { return true; }
5194
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005195
5196 DECLARE_INSTRUCTION(Throw);
5197
5198 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005199 DISALLOW_COPY_AND_ASSIGN(HThrow);
5200};
5201
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005202/**
5203 * Implementation strategies for the code generator of a HInstanceOf
5204 * or `HCheckCast`.
5205 */
5206enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005207 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005208 kExactCheck, // Can do a single class compare.
5209 kClassHierarchyCheck, // Can just walk the super class chain.
5210 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5211 kInterfaceCheck, // No optimization yet when checking against an interface.
5212 kArrayObjectCheck, // Can just check if the array is not primitive.
5213 kArrayCheck // No optimization yet when checking against a generic array.
5214};
5215
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005216class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005217 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005218 HInstanceOf(HInstruction* object,
5219 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005220 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005221 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005222 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005223 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005224 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005225 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005226 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005227 SetRawInputAt(0, object);
5228 SetRawInputAt(1, constant);
5229 }
5230
5231 bool CanBeMoved() const OVERRIDE { return true; }
5232
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005233 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005234 return true;
5235 }
5236
5237 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005238 return false;
5239 }
5240
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005241 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5242
5243 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005244
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005245 // Used only in code generation.
5246 bool MustDoNullCheck() const { return must_do_null_check_; }
5247 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5248
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005249 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5250 return (check_kind == TypeCheckKind::kExactCheck)
5251 ? SideEffects::None()
5252 // Mips currently does runtime calls for any other checks.
5253 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005254 }
5255
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005256 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005257
5258 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005259 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005260 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005261
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005262 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5263};
5264
Calin Juravleb1498f62015-02-16 13:13:29 +00005265class HBoundType : public HExpression<1> {
5266 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005267 // Constructs an HBoundType with the given upper_bound.
5268 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005269 HBoundType(HInstruction* input,
5270 ReferenceTypeInfo upper_bound,
5271 bool upper_can_be_null,
5272 uint32_t dex_pc = kNoDexPc)
5273 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005274 upper_bound_(upper_bound),
5275 upper_can_be_null_(upper_can_be_null),
5276 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005277 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005278 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005279 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005280 }
5281
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005282 // GetUpper* should only be used in reference type propagation.
5283 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5284 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005285
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005286 void SetCanBeNull(bool can_be_null) {
5287 DCHECK(upper_can_be_null_ || !can_be_null);
5288 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005289 }
5290
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005291 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5292
Calin Juravleb1498f62015-02-16 13:13:29 +00005293 DECLARE_INSTRUCTION(BoundType);
5294
5295 private:
5296 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005297 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5298 // It is used to bound the type in cases like:
5299 // if (x instanceof ClassX) {
5300 // // uper_bound_ will be ClassX
5301 // }
5302 const ReferenceTypeInfo upper_bound_;
5303 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5304 // is false then can_be_null_ cannot be true).
5305 const bool upper_can_be_null_;
5306 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005307
5308 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5309};
5310
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005311class HCheckCast : public HTemplateInstruction<2> {
5312 public:
5313 HCheckCast(HInstruction* object,
5314 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005315 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005316 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005317 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005318 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005319 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005320 SetRawInputAt(0, object);
5321 SetRawInputAt(1, constant);
5322 }
5323
5324 bool CanBeMoved() const OVERRIDE { return true; }
5325
5326 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5327 return true;
5328 }
5329
5330 bool NeedsEnvironment() const OVERRIDE {
5331 // Instruction may throw a CheckCastError.
5332 return true;
5333 }
5334
5335 bool CanThrow() const OVERRIDE { return true; }
5336
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005337 bool MustDoNullCheck() const { return must_do_null_check_; }
5338 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005339 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005340
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005341 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005342
5343 DECLARE_INSTRUCTION(CheckCast);
5344
5345 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005346 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005347 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005348
5349 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005350};
5351
Calin Juravle27df7582015-04-17 19:12:31 +01005352class HMemoryBarrier : public HTemplateInstruction<0> {
5353 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005354 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005355 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005356 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005357 barrier_kind_(barrier_kind) {}
5358
5359 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5360
5361 DECLARE_INSTRUCTION(MemoryBarrier);
5362
5363 private:
5364 const MemBarrierKind barrier_kind_;
5365
5366 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5367};
5368
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005369class HMonitorOperation : public HTemplateInstruction<1> {
5370 public:
5371 enum OperationKind {
5372 kEnter,
5373 kExit,
5374 };
5375
5376 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005377 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005378 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5379 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005380 SetRawInputAt(0, object);
5381 }
5382
5383 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005384 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5385
5386 bool CanThrow() const OVERRIDE {
5387 // Verifier guarantees that monitor-exit cannot throw.
5388 // This is important because it allows the HGraphBuilder to remove
5389 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5390 return IsEnter();
5391 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005392
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005393
5394 bool IsEnter() const { return kind_ == kEnter; }
5395
5396 DECLARE_INSTRUCTION(MonitorOperation);
5397
Calin Juravle52c48962014-12-16 17:02:57 +00005398 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005399 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005400
5401 private:
5402 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5403};
5404
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005405/**
5406 * A HInstruction used as a marker for the replacement of new + <init>
5407 * of a String to a call to a StringFactory. Only baseline will see
5408 * the node at code generation, where it will be be treated as null.
5409 * When compiling non-baseline, `HFakeString` instructions are being removed
5410 * in the instruction simplifier.
5411 */
5412class HFakeString : public HTemplateInstruction<0> {
5413 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005414 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5415 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005416
5417 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5418
5419 DECLARE_INSTRUCTION(FakeString);
5420
5421 private:
5422 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5423};
5424
Vladimir Markof9f64412015-09-02 14:05:49 +01005425class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005426 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005427 MoveOperands(Location source,
5428 Location destination,
5429 Primitive::Type type,
5430 HInstruction* instruction)
5431 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005432
5433 Location GetSource() const { return source_; }
5434 Location GetDestination() const { return destination_; }
5435
5436 void SetSource(Location value) { source_ = value; }
5437 void SetDestination(Location value) { destination_ = value; }
5438
5439 // The parallel move resolver marks moves as "in-progress" by clearing the
5440 // destination (but not the source).
5441 Location MarkPending() {
5442 DCHECK(!IsPending());
5443 Location dest = destination_;
5444 destination_ = Location::NoLocation();
5445 return dest;
5446 }
5447
5448 void ClearPending(Location dest) {
5449 DCHECK(IsPending());
5450 destination_ = dest;
5451 }
5452
5453 bool IsPending() const {
5454 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5455 return destination_.IsInvalid() && !source_.IsInvalid();
5456 }
5457
5458 // True if this blocks a move from the given location.
5459 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005460 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005461 }
5462
5463 // A move is redundant if it's been eliminated, if its source and
5464 // destination are the same, or if its destination is unneeded.
5465 bool IsRedundant() const {
5466 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5467 }
5468
5469 // We clear both operands to indicate move that's been eliminated.
5470 void Eliminate() {
5471 source_ = destination_ = Location::NoLocation();
5472 }
5473
5474 bool IsEliminated() const {
5475 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5476 return source_.IsInvalid();
5477 }
5478
Alexey Frunze4dda3372015-06-01 18:31:49 -07005479 Primitive::Type GetType() const { return type_; }
5480
Nicolas Geoffray90218252015-04-15 11:56:51 +01005481 bool Is64BitMove() const {
5482 return Primitive::Is64BitType(type_);
5483 }
5484
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005485 HInstruction* GetInstruction() const { return instruction_; }
5486
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005487 private:
5488 Location source_;
5489 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005490 // The type this move is for.
5491 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005492 // The instruction this move is assocatied with. Null when this move is
5493 // for moving an input in the expected locations of user (including a phi user).
5494 // This is only used in debug mode, to ensure we do not connect interval siblings
5495 // in the same parallel move.
5496 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005497};
5498
5499static constexpr size_t kDefaultNumberOfMoves = 4;
5500
5501class HParallelMove : public HTemplateInstruction<0> {
5502 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005503 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005504 : HTemplateInstruction(SideEffects::None(), dex_pc),
5505 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5506 moves_.reserve(kDefaultNumberOfMoves);
5507 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005508
Nicolas Geoffray90218252015-04-15 11:56:51 +01005509 void AddMove(Location source,
5510 Location destination,
5511 Primitive::Type type,
5512 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005513 DCHECK(source.IsValid());
5514 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005515 if (kIsDebugBuild) {
5516 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005517 for (const MoveOperands& move : moves_) {
5518 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005519 // Special case the situation where the move is for the spill slot
5520 // of the instruction.
5521 if ((GetPrevious() == instruction)
5522 || ((GetPrevious() == nullptr)
5523 && instruction->IsPhi()
5524 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005525 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005526 << "Doing parallel moves for the same instruction.";
5527 } else {
5528 DCHECK(false) << "Doing parallel moves for the same instruction.";
5529 }
5530 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005531 }
5532 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005533 for (const MoveOperands& move : moves_) {
5534 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005535 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005536 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005537 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005538 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005539 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005540 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005541 }
5542
Vladimir Marko225b6462015-09-28 12:17:40 +01005543 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005544 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005545 }
5546
Vladimir Marko225b6462015-09-28 12:17:40 +01005547 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005548
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005549 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005550
5551 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005552 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005553
5554 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5555};
5556
Mark Mendell0616ae02015-04-17 12:49:27 -04005557} // namespace art
5558
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005559#ifdef ART_ENABLE_CODEGEN_arm64
5560#include "nodes_arm64.h"
5561#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005562#ifdef ART_ENABLE_CODEGEN_x86
5563#include "nodes_x86.h"
5564#endif
5565
5566namespace art {
5567
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005568class HGraphVisitor : public ValueObject {
5569 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005570 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5571 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005572
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005573 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005574 virtual void VisitBasicBlock(HBasicBlock* block);
5575
Roland Levillain633021e2014-10-01 14:12:25 +01005576 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005577 void VisitInsertionOrder();
5578
Roland Levillain633021e2014-10-01 14:12:25 +01005579 // Visit the graph following dominator tree reverse post-order.
5580 void VisitReversePostOrder();
5581
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005582 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005583
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005584 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005585#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005586 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5587
5588 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5589
5590#undef DECLARE_VISIT_INSTRUCTION
5591
5592 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005593 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005594
5595 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5596};
5597
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005598class HGraphDelegateVisitor : public HGraphVisitor {
5599 public:
5600 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5601 virtual ~HGraphDelegateVisitor() {}
5602
5603 // Visit functions that delegate to to super class.
5604#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005605 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005606
5607 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5608
5609#undef DECLARE_VISIT_INSTRUCTION
5610
5611 private:
5612 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5613};
5614
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005615class HInsertionOrderIterator : public ValueObject {
5616 public:
5617 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5618
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005619 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005620 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005621 void Advance() { ++index_; }
5622
5623 private:
5624 const HGraph& graph_;
5625 size_t index_;
5626
5627 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5628};
5629
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005630class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005631 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005632 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5633 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005634 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005635 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005636
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005637 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5638 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005639 void Advance() { ++index_; }
5640
5641 private:
5642 const HGraph& graph_;
5643 size_t index_;
5644
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005645 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005646};
5647
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005648class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005649 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005650 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005651 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005652 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005653 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005654 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005655
5656 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005657 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005658 void Advance() { --index_; }
5659
5660 private:
5661 const HGraph& graph_;
5662 size_t index_;
5663
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005664 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005665};
5666
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005667class HLinearPostOrderIterator : public ValueObject {
5668 public:
5669 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005670 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005671
5672 bool Done() const { return index_ == 0; }
5673
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005674 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005675
5676 void Advance() {
5677 --index_;
5678 DCHECK_GE(index_, 0U);
5679 }
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(HLinearPostOrderIterator);
5686};
5687
5688class HLinearOrderIterator : public ValueObject {
5689 public:
5690 explicit HLinearOrderIterator(const HGraph& graph)
5691 : order_(graph.GetLinearOrder()), index_(0) {}
5692
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005693 bool Done() const { return index_ == order_.size(); }
5694 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005695 void Advance() { ++index_; }
5696
5697 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005698 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005699 size_t index_;
5700
5701 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5702};
5703
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005704// Iterator over the blocks that art part of the loop. Includes blocks part
5705// of an inner loop. The order in which the blocks are iterated is on their
5706// block id.
5707class HBlocksInLoopIterator : public ValueObject {
5708 public:
5709 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5710 : blocks_in_loop_(info.GetBlocks()),
5711 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5712 index_(0) {
5713 if (!blocks_in_loop_.IsBitSet(index_)) {
5714 Advance();
5715 }
5716 }
5717
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005718 bool Done() const { return index_ == blocks_.size(); }
5719 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005720 void Advance() {
5721 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005722 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005723 if (blocks_in_loop_.IsBitSet(index_)) {
5724 break;
5725 }
5726 }
5727 }
5728
5729 private:
5730 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005731 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005732 size_t index_;
5733
5734 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5735};
5736
Mingyao Yang3584bce2015-05-19 16:01:59 -07005737// Iterator over the blocks that art part of the loop. Includes blocks part
5738// of an inner loop. The order in which the blocks are iterated is reverse
5739// post order.
5740class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5741 public:
5742 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5743 : blocks_in_loop_(info.GetBlocks()),
5744 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5745 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005746 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005747 Advance();
5748 }
5749 }
5750
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005751 bool Done() const { return index_ == blocks_.size(); }
5752 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005753 void Advance() {
5754 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005755 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5756 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005757 break;
5758 }
5759 }
5760 }
5761
5762 private:
5763 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005764 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005765 size_t index_;
5766
5767 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5768};
5769
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005770inline int64_t Int64FromConstant(HConstant* constant) {
5771 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5772 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5773 : constant->AsLongConstant()->GetValue();
5774}
5775
Vladimir Marko58155012015-08-19 12:49:41 +00005776inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5777 // For the purposes of the compiler, the dex files must actually be the same object
5778 // if we want to safely treat them as the same. This is especially important for JIT
5779 // as custom class loaders can open the same underlying file (or memory) multiple
5780 // times and provide different class resolution but no two class loaders should ever
5781 // use the same DexFile object - doing so is an unsupported hack that can lead to
5782 // all sorts of weird failures.
5783 return &lhs == &rhs;
5784}
5785
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005786} // namespace art
5787
5788#endif // ART_COMPILER_OPTIMIZING_NODES_H_