blob: 3e38e9f2a4db78de7b24fe05fb92f39edeeabd5b [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
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100374 ArtMethod* GetArtMethod() const { return art_method_; }
375 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
376
Mark Mendellf6529172015-11-17 11:16:56 -0500377 // Returns an instruction with the opposite boolean value from 'cond'.
378 // The instruction has been inserted into the graph, either as a constant, or
379 // before cursor.
380 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
381
David Brazdil2d7352b2015-04-20 14:52:42 +0100382 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100383 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000384 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100385 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000386
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000387 template <class InstructionType, typename ValueType>
388 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600389 ArenaSafeMap<ValueType, InstructionType*>* cache,
390 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000391 // Try to find an existing constant of the given value.
392 InstructionType* constant = nullptr;
393 auto cached_constant = cache->find(value);
394 if (cached_constant != cache->end()) {
395 constant = cached_constant->second;
396 }
397
398 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100399 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000400 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600401 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000402 cache->Overwrite(value, constant);
403 InsertConstant(constant);
404 }
405 return constant;
406 }
407
David Brazdil8d5b8b22015-03-24 10:51:52 +0000408 void InsertConstant(HConstant* instruction);
409
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000410 // Cache a float constant into the graph. This method should only be
411 // called by the SsaBuilder when creating "equivalent" instructions.
412 void CacheFloatConstant(HFloatConstant* constant);
413
414 // See CacheFloatConstant comment.
415 void CacheDoubleConstant(HDoubleConstant* constant);
416
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000417 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000418
419 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100420 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000421
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100423 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000424
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100425 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100426 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100427
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000428 HBasicBlock* entry_block_;
429 HBasicBlock* exit_block_;
430
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100431 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100432 uint16_t maximum_number_of_out_vregs_;
433
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100434 // The number of virtual registers in this method. Contains the parameters.
435 uint16_t number_of_vregs_;
436
437 // The number of virtual registers used by parameters of this method.
438 uint16_t number_of_in_vregs_;
439
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000440 // Number of vreg size slots that the temporaries use (used in baseline compiler).
441 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100442
Mark Mendell1152c922015-04-24 17:06:35 -0400443 // Has bounds checks. We can totally skip BCE if it's false.
444 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800445
David Brazdil77a48ae2015-09-15 12:34:04 +0000446 // Flag whether there are any try/catch blocks in the graph. We will skip
447 // try/catch-related passes if false.
448 bool has_try_catch_;
449
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000450 // Indicates whether the graph should be compiled in a way that
451 // ensures full debuggability. If false, we can apply more
452 // aggressive optimizations that may limit the level of debugging.
453 const bool debuggable_;
454
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000455 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000456 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000457
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100458 // The dex file from which the method is from.
459 const DexFile& dex_file_;
460
461 // The method index in the dex file.
462 const uint32_t method_idx_;
463
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100464 // If inlined, this encodes how the callee is being invoked.
465 const InvokeType invoke_type_;
466
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100467 // Whether the graph has been transformed to SSA form. Only used
468 // in debug mode to ensure we are not using properties only valid
469 // for non-SSA form (like the number of temporaries).
470 bool in_ssa_form_;
471
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100472 const bool should_generate_constructor_barrier_;
473
Mathieu Chartiere401d142015-04-22 13:56:20 -0700474 const InstructionSet instruction_set_;
475
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000476 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000477 HNullConstant* cached_null_constant_;
478 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000479 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000480 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000481 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000482
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100483 HCurrentMethod* cached_current_method_;
484
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100485 // The ArtMethod this graph is for. Note that for AOT, it may be null,
486 // for example for methods whose declaring class could not be resolved
487 // (such as when the superclass could not be found).
488 ArtMethod* art_method_;
489
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000490 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100491 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000492 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000493 DISALLOW_COPY_AND_ASSIGN(HGraph);
494};
495
Vladimir Markof9f64412015-09-02 14:05:49 +0100496class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000497 public:
498 HLoopInformation(HBasicBlock* header, HGraph* graph)
499 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100500 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100501 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100502 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100503 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
504 back_edges_.reserve(kDefaultNumberOfBackEdges);
505 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100506
507 HBasicBlock* GetHeader() const {
508 return header_;
509 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000510
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000511 void SetHeader(HBasicBlock* block) {
512 header_ = block;
513 }
514
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100515 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
516 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
517 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
518
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000519 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100520 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000521 }
522
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100523 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100524 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100525 }
526
David Brazdil46e2a392015-03-16 17:31:52 +0000527 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100528 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100529 }
530
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000531 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100532 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000533 }
534
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100535 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100536
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100537 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100538 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100539 }
540
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100541 // Returns the lifetime position of the back edge that has the
542 // greatest lifetime position.
543 size_t GetLifetimeEnd() const;
544
545 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100546 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100547 }
548
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100549 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100550 // that is the header dominates the back edge.
551 bool Populate();
552
David Brazdila4b8c212015-05-07 09:59:30 +0100553 // Reanalyzes the loop by removing loop info from its blocks and re-running
554 // Populate(). If there are no back edges left, the loop info is completely
555 // removed as well as its SuspendCheck instruction. It must be run on nested
556 // inner loops first.
557 void Update();
558
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 // Returns whether this loop information contains `block`.
560 // Note that this loop information *must* be populated before entering this function.
561 bool Contains(const HBasicBlock& block) const;
562
563 // Returns whether this loop information is an inner loop of `other`.
564 // Note that `other` *must* be populated before entering this function.
565 bool IsIn(const HLoopInformation& other) const;
566
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800567 // Returns true if instruction is not defined within this loop.
568 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700569
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100570 const ArenaBitVector& GetBlocks() const { return blocks_; }
571
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000572 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000573 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000574
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000575 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100576 // Internal recursive implementation of `Populate`.
577 void PopulateRecursive(HBasicBlock* block);
578
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000579 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100580 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100581 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100582 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000583
584 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
585};
586
David Brazdilec16f792015-08-19 15:04:01 +0100587// Stores try/catch information for basic blocks.
588// Note that HGraph is constructed so that catch blocks cannot simultaneously
589// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100590class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100591 public:
592 // Try block information constructor.
593 explicit TryCatchInformation(const HTryBoundary& try_entry)
594 : try_entry_(&try_entry),
595 catch_dex_file_(nullptr),
596 catch_type_index_(DexFile::kDexNoIndex16) {
597 DCHECK(try_entry_ != nullptr);
598 }
599
600 // Catch block information constructor.
601 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
602 : try_entry_(nullptr),
603 catch_dex_file_(&dex_file),
604 catch_type_index_(catch_type_index) {}
605
606 bool IsTryBlock() const { return try_entry_ != nullptr; }
607
608 const HTryBoundary& GetTryEntry() const {
609 DCHECK(IsTryBlock());
610 return *try_entry_;
611 }
612
613 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
614
615 bool IsCatchAllTypeIndex() const {
616 DCHECK(IsCatchBlock());
617 return catch_type_index_ == DexFile::kDexNoIndex16;
618 }
619
620 uint16_t GetCatchTypeIndex() const {
621 DCHECK(IsCatchBlock());
622 return catch_type_index_;
623 }
624
625 const DexFile& GetCatchDexFile() const {
626 DCHECK(IsCatchBlock());
627 return *catch_dex_file_;
628 }
629
630 private:
631 // One of possibly several TryBoundary instructions entering the block's try.
632 // Only set for try blocks.
633 const HTryBoundary* try_entry_;
634
635 // Exception type information. Only set for catch blocks.
636 const DexFile* catch_dex_file_;
637 const uint16_t catch_type_index_;
638};
639
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100640static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100641static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100642
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643// A block in a method. Contains the list of instructions represented
644// as a double linked list. Each block knows its predecessors and
645// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100646
Vladimir Markof9f64412015-09-02 14:05:49 +0100647class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000648 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600649 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000650 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000651 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
652 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000653 loop_information_(nullptr),
654 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000655 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100657 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100658 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000659 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000660 try_catch_information_(nullptr) {
661 predecessors_.reserve(kDefaultNumberOfPredecessors);
662 successors_.reserve(kDefaultNumberOfSuccessors);
663 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
664 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000665
Vladimir Marko60584552015-09-03 13:35:12 +0000666 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100667 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000668 }
669
Vladimir Marko60584552015-09-03 13:35:12 +0000670 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100671 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000672 }
673
David Brazdild26a4112015-11-10 11:07:31 +0000674 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
675 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
676
Vladimir Marko60584552015-09-03 13:35:12 +0000677 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
678 return ContainsElement(successors_, block, start_from);
679 }
680
681 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100682 return dominated_blocks_;
683 }
684
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100685 bool IsEntryBlock() const {
686 return graph_->GetEntryBlock() == this;
687 }
688
689 bool IsExitBlock() const {
690 return graph_->GetExitBlock() == this;
691 }
692
David Brazdil46e2a392015-03-16 17:31:52 +0000693 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000694 bool IsSingleTryBoundary() const;
695
696 // Returns true if this block emits nothing but a jump.
697 bool IsSingleJump() const {
698 HLoopInformation* loop_info = GetLoopInformation();
699 return (IsSingleGoto() || IsSingleTryBoundary())
700 // Back edges generate a suspend check.
701 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
702 }
David Brazdil46e2a392015-03-16 17:31:52 +0000703
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000704 void AddBackEdge(HBasicBlock* back_edge) {
705 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000706 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000707 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100708 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000709 loop_information_->AddBackEdge(back_edge);
710 }
711
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000712 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000713 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000714
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100715 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000716 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600717 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000718
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000719 HBasicBlock* GetDominator() const { return dominator_; }
720 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000721 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
722
723 void RemoveDominatedBlock(HBasicBlock* block) {
724 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100725 }
Vladimir Marko60584552015-09-03 13:35:12 +0000726
727 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
728 ReplaceElement(dominated_blocks_, existing, new_block);
729 }
730
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100731 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000732
733 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100734 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000735 }
736
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100737 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
738 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100739 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100740 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100741 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
742 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000743
744 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000745 successors_.push_back(block);
746 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000747 }
748
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100749 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
750 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100751 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000752 new_block->predecessors_.push_back(this);
753 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000754 }
755
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
757 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000758 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000759 new_block->successors_.push_back(this);
760 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000761 }
762
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100763 // Insert `this` between `predecessor` and `successor. This method
764 // preserves the indicies, and will update the first edge found between
765 // `predecessor` and `successor`.
766 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
767 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100768 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000769 successor->predecessors_[predecessor_index] = this;
770 predecessor->successors_[successor_index] = this;
771 successors_.push_back(successor);
772 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100773 }
774
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100775 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000776 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100777 }
778
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000779 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000780 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000781 }
782
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100783 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000784 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 }
786
787 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 predecessors_.push_back(block);
789 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100790 }
791
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100792 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000793 DCHECK_EQ(predecessors_.size(), 2u);
794 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100795 }
796
David Brazdil769c9e52015-04-27 13:54:09 +0100797 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000798 DCHECK_EQ(successors_.size(), 2u);
799 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100800 }
801
David Brazdilfc6a86a2015-06-26 10:33:45 +0000802 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000803 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100804 }
805
David Brazdilfc6a86a2015-06-26 10:33:45 +0000806 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000807 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100808 }
809
David Brazdilfc6a86a2015-06-26 10:33:45 +0000810 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000811 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100812 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 }
814
815 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000816 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100817 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000818 }
819
820 // Returns whether the first occurrence of `predecessor` in the list of
821 // predecessors is at index `idx`.
822 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100823 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000824 return GetPredecessorIndexOf(predecessor) == idx;
825 }
826
David Brazdild7558da2015-09-22 13:04:14 +0100827 // Create a new block between this block and its predecessors. The new block
828 // is added to the graph, all predecessor edges are relinked to it and an edge
829 // is created to `this`. Returns the new empty block. Reverse post order or
830 // loop and try/catch information are not updated.
831 HBasicBlock* CreateImmediateDominator();
832
David Brazdilfc6a86a2015-06-26 10:33:45 +0000833 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100834 // created, latter block. Note that this method will add the block to the
835 // graph, create a Goto at the end of the former block and will create an edge
836 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100837 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000838 HBasicBlock* SplitBefore(HInstruction* cursor);
839
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000840 // Split the block into two blocks just after `cursor`. Returns the newly
841 // created block. Note that this method just updates raw block information,
842 // like predecessors, successors, dominators, and instruction list. It does not
843 // update the graph, reverse post order, loop information, nor make sure the
844 // blocks are consistent (for example ending with a control flow instruction).
845 HBasicBlock* SplitAfter(HInstruction* cursor);
846
David Brazdil9bc43612015-11-05 21:25:24 +0000847 // Split catch block into two blocks after the original move-exception bytecode
848 // instruction, or at the beginning if not present. Returns the newly created,
849 // latter block, or nullptr if such block could not be created (must be dead
850 // in that case). Note that this method just updates raw block information,
851 // like predecessors, successors, dominators, and instruction list. It does not
852 // update the graph, reverse post order, loop information, nor make sure the
853 // blocks are consistent (for example ending with a control flow instruction).
854 HBasicBlock* SplitCatchBlockAfterMoveException();
855
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000856 // Merge `other` at the end of `this`. Successors and dominated blocks of
857 // `other` are changed to be successors and dominated blocks of `this`. Note
858 // that this method does not update the graph, reverse post order, loop
859 // information, nor make sure the blocks are consistent (for example ending
860 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100861 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000862
863 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
864 // of `this` are moved to `other`.
865 // Note that this method does not update the graph, reverse post order, loop
866 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000867 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000868 void ReplaceWith(HBasicBlock* other);
869
David Brazdil2d7352b2015-04-20 14:52:42 +0100870 // Merge `other` at the end of `this`. This method updates loops, reverse post
871 // order, links to predecessors, successors, dominators and deletes the block
872 // from the graph. The two blocks must be successive, i.e. `this` the only
873 // predecessor of `other` and vice versa.
874 void MergeWith(HBasicBlock* other);
875
876 // Disconnects `this` from all its predecessors, successors and dominator,
877 // removes it from all loops it is included in and eventually from the graph.
878 // The block must not dominate any other block. Predecessors and successors
879 // are safely updated.
880 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000881
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100883 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100884 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100885 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100886 // Replace instruction `initial` with `replacement` within this block.
887 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
888 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100889 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100890 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000891 // RemoveInstruction and RemovePhi delete a given instruction from the respective
892 // instruction list. With 'ensure_safety' set to true, it verifies that the
893 // instruction is not in use and removes it from the use lists of its inputs.
894 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
895 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100896 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100897
898 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100899 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100900 }
901
Roland Levillain6b879dd2014-09-22 17:13:44 +0100902 bool IsLoopPreHeaderFirstPredecessor() const {
903 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100904 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100905 }
906
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100907 HLoopInformation* GetLoopInformation() const {
908 return loop_information_;
909 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000911 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100912 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000913 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100914 void SetInLoop(HLoopInformation* info) {
915 if (IsLoopHeader()) {
916 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100917 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100918 loop_information_ = info;
919 } else if (loop_information_->Contains(*info->GetHeader())) {
920 // Block is currently part of an outer loop. Make it part of this inner loop.
921 // Note that a non loop header having a loop information means this loop information
922 // has already been populated
923 loop_information_ = info;
924 } else {
925 // Block is part of an inner loop. Do not update the loop information.
926 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
927 // at this point, because this method is being called while populating `info`.
928 }
929 }
930
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000931 // Raw update of the loop information.
932 void SetLoopInformation(HLoopInformation* info) {
933 loop_information_ = info;
934 }
935
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100936 bool IsInLoop() const { return loop_information_ != nullptr; }
937
David Brazdilec16f792015-08-19 15:04:01 +0100938 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
939
940 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
941 try_catch_information_ = try_catch_information;
942 }
943
944 bool IsTryBlock() const {
945 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
946 }
947
948 bool IsCatchBlock() const {
949 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
950 }
David Brazdilffee3d32015-07-06 11:48:53 +0100951
952 // Returns the try entry that this block's successors should have. They will
953 // be in the same try, unless the block ends in a try boundary. In that case,
954 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100955 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100956
David Brazdild7558da2015-09-22 13:04:14 +0100957 bool HasThrowingInstructions() const;
958
David Brazdila4b8c212015-05-07 09:59:30 +0100959 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100960 bool Dominates(HBasicBlock* block) const;
961
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100962 size_t GetLifetimeStart() const { return lifetime_start_; }
963 size_t GetLifetimeEnd() const { return lifetime_end_; }
964
965 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
966 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
967
David Brazdil8d5b8b22015-03-24 10:51:52 +0000968 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000969 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100970 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000971 bool HasSinglePhi() const;
972
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000973 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000974 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000975 ArenaVector<HBasicBlock*> predecessors_;
976 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100977 HInstructionList instructions_;
978 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000979 HLoopInformation* loop_information_;
980 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000981 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100982 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100983 // The dex program counter of the first instruction of this block.
984 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100985 size_t lifetime_start_;
986 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100987 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100988
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000989 friend class HGraph;
990 friend class HInstruction;
991
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
993};
994
David Brazdilb2bd1c52015-03-25 11:17:37 +0000995// Iterates over the LoopInformation of all loops which contain 'block'
996// from the innermost to the outermost.
997class HLoopInformationOutwardIterator : public ValueObject {
998 public:
999 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1000 : current_(block.GetLoopInformation()) {}
1001
1002 bool Done() const { return current_ == nullptr; }
1003
1004 void Advance() {
1005 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001006 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001007 }
1008
1009 HLoopInformation* Current() const {
1010 DCHECK(!Done());
1011 return current_;
1012 }
1013
1014 private:
1015 HLoopInformation* current_;
1016
1017 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1018};
1019
Alexandre Ramesef20f712015-06-09 10:29:30 +01001020#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001021 M(Above, Condition) \
1022 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001023 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001024 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001025 M(ArrayGet, Instruction) \
1026 M(ArrayLength, Instruction) \
1027 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001028 M(Below, Condition) \
1029 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001030 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001031 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001032 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001033 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001034 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001035 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001036 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001037 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001038 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001039 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001040 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001041 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001042 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001043 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001044 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001045 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001046 M(FloatConstant, Constant) \
1047 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001048 M(GreaterThan, Condition) \
1049 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001050 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001051 M(InstanceFieldGet, Instruction) \
1052 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001053 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001054 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001055 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001056 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001057 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001058 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001059 M(LessThan, Condition) \
1060 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001061 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001062 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001063 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001064 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001065 M(Local, Instruction) \
1066 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001067 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001068 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001069 M(Mul, BinaryOperation) \
1070 M(Neg, UnaryOperation) \
1071 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001072 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001073 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001074 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001075 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001076 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001077 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001078 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001079 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001080 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001081 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001082 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001083 M(Return, Instruction) \
1084 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001085 M(Shl, BinaryOperation) \
1086 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001087 M(StaticFieldGet, Instruction) \
1088 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001089 M(UnresolvedInstanceFieldGet, Instruction) \
1090 M(UnresolvedInstanceFieldSet, Instruction) \
1091 M(UnresolvedStaticFieldGet, Instruction) \
1092 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001093 M(StoreLocal, Instruction) \
1094 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001095 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001096 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001097 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001098 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001099 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001100 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001101 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001102
Vladimir Markob4536b72015-11-24 13:45:23 +00001103#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001104#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001105#else
1106#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1107 M(ArmDexCacheArraysBase, Instruction)
1108#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001109
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001110#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001111#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001112#else
1113#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001114 M(Arm64DataProcWithShifterOp, Instruction) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001115 M(Arm64IntermediateAddress, Instruction) \
1116 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001117#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001118
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001119#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1120
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001121#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1122
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001123#ifndef ART_ENABLE_CODEGEN_x86
1124#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1125#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001126#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1127 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001128 M(X86LoadFromConstantTable, Instruction) \
1129 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001130#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001131
1132#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1133
1134#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1135 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1136 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1137 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001138 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001139 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001140 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1141 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1142
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001143#define FOR_EACH_INSTRUCTION(M) \
1144 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1145 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001146 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001147 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001148 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001149
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001150#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001151FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1152#undef FORWARD_DECLARATION
1153
Roland Levillainccc07a92014-09-16 14:48:16 +01001154#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001155 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1156 const char* DebugName() const OVERRIDE { return #type; } \
1157 const H##type* As##type() const OVERRIDE { return this; } \
1158 H##type* As##type() OVERRIDE { return this; } \
1159 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001160 return other->Is##type(); \
1161 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001162 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001163
David Brazdiled596192015-01-23 10:39:45 +00001164template <typename T> class HUseList;
1165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001166template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001167class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001168 public:
David Brazdiled596192015-01-23 10:39:45 +00001169 HUseListNode* GetPrevious() const { return prev_; }
1170 HUseListNode* GetNext() const { return next_; }
1171 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001172 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001173 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001174
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001175 private:
David Brazdiled596192015-01-23 10:39:45 +00001176 HUseListNode(T user, size_t index)
1177 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1178
1179 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001180 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001181 HUseListNode<T>* prev_;
1182 HUseListNode<T>* next_;
1183
1184 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001185
1186 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1187};
1188
David Brazdiled596192015-01-23 10:39:45 +00001189template <typename T>
1190class HUseList : public ValueObject {
1191 public:
1192 HUseList() : first_(nullptr) {}
1193
1194 void Clear() {
1195 first_ = nullptr;
1196 }
1197
1198 // Adds a new entry at the beginning of the use list and returns
1199 // the newly created node.
1200 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001201 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001202 if (IsEmpty()) {
1203 first_ = new_node;
1204 } else {
1205 first_->prev_ = new_node;
1206 new_node->next_ = first_;
1207 first_ = new_node;
1208 }
1209 return new_node;
1210 }
1211
1212 HUseListNode<T>* GetFirst() const {
1213 return first_;
1214 }
1215
1216 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001217 DCHECK(node != nullptr);
1218 DCHECK(Contains(node));
1219
David Brazdiled596192015-01-23 10:39:45 +00001220 if (node->prev_ != nullptr) {
1221 node->prev_->next_ = node->next_;
1222 }
1223 if (node->next_ != nullptr) {
1224 node->next_->prev_ = node->prev_;
1225 }
1226 if (node == first_) {
1227 first_ = node->next_;
1228 }
1229 }
1230
David Brazdil1abb4192015-02-17 18:33:36 +00001231 bool Contains(const HUseListNode<T>* node) const {
1232 if (node == nullptr) {
1233 return false;
1234 }
1235 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1236 if (current == node) {
1237 return true;
1238 }
1239 }
1240 return false;
1241 }
1242
David Brazdiled596192015-01-23 10:39:45 +00001243 bool IsEmpty() const {
1244 return first_ == nullptr;
1245 }
1246
1247 bool HasOnlyOneUse() const {
1248 return first_ != nullptr && first_->next_ == nullptr;
1249 }
1250
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001251 size_t SizeSlow() const {
1252 size_t count = 0;
1253 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1254 ++count;
1255 }
1256 return count;
1257 }
1258
David Brazdiled596192015-01-23 10:39:45 +00001259 private:
1260 HUseListNode<T>* first_;
1261};
1262
1263template<typename T>
1264class HUseIterator : public ValueObject {
1265 public:
1266 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1267
1268 bool Done() const { return current_ == nullptr; }
1269
1270 void Advance() {
1271 DCHECK(!Done());
1272 current_ = current_->GetNext();
1273 }
1274
1275 HUseListNode<T>* Current() const {
1276 DCHECK(!Done());
1277 return current_;
1278 }
1279
1280 private:
1281 HUseListNode<T>* current_;
1282
1283 friend class HValue;
1284};
1285
David Brazdil1abb4192015-02-17 18:33:36 +00001286// This class is used by HEnvironment and HInstruction classes to record the
1287// instructions they use and pointers to the corresponding HUseListNodes kept
1288// by the used instructions.
1289template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001290class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001291 public:
1292 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1293 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1294
1295 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1296 : instruction_(old_record.instruction_), use_node_(use_node) {
1297 DCHECK(instruction_ != nullptr);
1298 DCHECK(use_node_ != nullptr);
1299 DCHECK(old_record.use_node_ == nullptr);
1300 }
1301
1302 HInstruction* GetInstruction() const { return instruction_; }
1303 HUseListNode<T>* GetUseNode() const { return use_node_; }
1304
1305 private:
1306 // Instruction used by the user.
1307 HInstruction* instruction_;
1308
1309 // Corresponding entry in the use list kept by 'instruction_'.
1310 HUseListNode<T>* use_node_;
1311};
1312
Aart Bik854a02b2015-07-14 16:07:00 -07001313/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001314 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001315 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001316 * For write/read dependences on fields/arrays, the dependence analysis uses
1317 * type disambiguation (e.g. a float field write cannot modify the value of an
1318 * integer field read) and the access type (e.g. a reference array write cannot
1319 * modify the value of a reference field read [although it may modify the
1320 * reference fetch prior to reading the field, which is represented by its own
1321 * write/read dependence]). The analysis makes conservative points-to
1322 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1323 * the same, and any reference read depends on any reference read without
1324 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001325 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001326 * The internal representation uses 38-bit and is described in the table below.
1327 * The first line indicates the side effect, and for field/array accesses the
1328 * second line indicates the type of the access (in the order of the
1329 * Primitive::Type enum).
1330 * The two numbered lines below indicate the bit position in the bitfield (read
1331 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001332 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001333 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1334 * +-------------+---------+---------+--------------+---------+---------+
1335 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1336 * | 3 |333333322|222222221| 1 |111111110|000000000|
1337 * | 7 |654321098|765432109| 8 |765432109|876543210|
1338 *
1339 * Note that, to ease the implementation, 'changes' bits are least significant
1340 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001341 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001342class SideEffects : public ValueObject {
1343 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001344 SideEffects() : flags_(0) {}
1345
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001346 static SideEffects None() {
1347 return SideEffects(0);
1348 }
1349
1350 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001351 return SideEffects(kAllChangeBits | kAllDependOnBits);
1352 }
1353
1354 static SideEffects AllChanges() {
1355 return SideEffects(kAllChangeBits);
1356 }
1357
1358 static SideEffects AllDependencies() {
1359 return SideEffects(kAllDependOnBits);
1360 }
1361
1362 static SideEffects AllExceptGCDependency() {
1363 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1364 }
1365
1366 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001367 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001368 }
1369
Aart Bik34c3ba92015-07-20 14:08:59 -07001370 static SideEffects AllWrites() {
1371 return SideEffects(kAllWrites);
1372 }
1373
1374 static SideEffects AllReads() {
1375 return SideEffects(kAllReads);
1376 }
1377
1378 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1379 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001380 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001381 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001382 }
1383
Aart Bik854a02b2015-07-14 16:07:00 -07001384 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1385 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001386 }
1387
Aart Bik34c3ba92015-07-20 14:08:59 -07001388 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1389 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001390 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001391 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001392 }
1393
1394 static SideEffects ArrayReadOfType(Primitive::Type type) {
1395 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1396 }
1397
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001398 static SideEffects CanTriggerGC() {
1399 return SideEffects(1ULL << kCanTriggerGCBit);
1400 }
1401
1402 static SideEffects DependsOnGC() {
1403 return SideEffects(1ULL << kDependsOnGCBit);
1404 }
1405
Aart Bik854a02b2015-07-14 16:07:00 -07001406 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001407 SideEffects Union(SideEffects other) const {
1408 return SideEffects(flags_ | other.flags_);
1409 }
1410
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001411 SideEffects Exclusion(SideEffects other) const {
1412 return SideEffects(flags_ & ~other.flags_);
1413 }
1414
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001415 void Add(SideEffects other) {
1416 flags_ |= other.flags_;
1417 }
1418
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001419 bool Includes(SideEffects other) const {
1420 return (other.flags_ & flags_) == other.flags_;
1421 }
1422
1423 bool HasSideEffects() const {
1424 return (flags_ & kAllChangeBits);
1425 }
1426
1427 bool HasDependencies() const {
1428 return (flags_ & kAllDependOnBits);
1429 }
1430
1431 // Returns true if there are no side effects or dependencies.
1432 bool DoesNothing() const {
1433 return flags_ == 0;
1434 }
1435
Aart Bik854a02b2015-07-14 16:07:00 -07001436 // Returns true if something is written.
1437 bool DoesAnyWrite() const {
1438 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001439 }
1440
Aart Bik854a02b2015-07-14 16:07:00 -07001441 // Returns true if something is read.
1442 bool DoesAnyRead() const {
1443 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001444 }
1445
Aart Bik854a02b2015-07-14 16:07:00 -07001446 // Returns true if potentially everything is written and read
1447 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001448 bool DoesAllReadWrite() const {
1449 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1450 }
1451
Aart Bik854a02b2015-07-14 16:07:00 -07001452 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001453 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001454 }
1455
Roland Levillain0d5a2812015-11-13 10:07:31 +00001456 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001457 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001458 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1459 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001460 }
1461
1462 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001463 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001464 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001465 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001466 for (int s = kLastBit; s >= 0; s--) {
1467 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1468 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1469 // This is a bit for the GC side effect.
1470 if (current_bit_is_set) {
1471 flags += "GC";
1472 }
Aart Bik854a02b2015-07-14 16:07:00 -07001473 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001474 } else {
1475 // This is a bit for the array/field analysis.
1476 // The underscore character stands for the 'can trigger GC' bit.
1477 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1478 if (current_bit_is_set) {
1479 flags += kDebug[s];
1480 }
1481 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1482 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1483 flags += "|";
1484 }
1485 }
Aart Bik854a02b2015-07-14 16:07:00 -07001486 }
1487 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001488 }
1489
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001490 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001491
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001492 private:
1493 static constexpr int kFieldArrayAnalysisBits = 9;
1494
1495 static constexpr int kFieldWriteOffset = 0;
1496 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1497 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1498 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1499
1500 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1501
1502 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1503 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1504 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1505 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1506
1507 static constexpr int kLastBit = kDependsOnGCBit;
1508 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1509
1510 // Aliases.
1511
1512 static_assert(kChangeBits == kDependOnBits,
1513 "the 'change' bits should match the 'depend on' bits.");
1514
1515 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1516 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1517 static constexpr uint64_t kAllWrites =
1518 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1519 static constexpr uint64_t kAllReads =
1520 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001521
Aart Bik854a02b2015-07-14 16:07:00 -07001522 // Work around the fact that HIR aliases I/F and J/D.
1523 // TODO: remove this interceptor once HIR types are clean
1524 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1525 switch (type) {
1526 case Primitive::kPrimInt:
1527 case Primitive::kPrimFloat:
1528 return TypeFlag(Primitive::kPrimInt, offset) |
1529 TypeFlag(Primitive::kPrimFloat, offset);
1530 case Primitive::kPrimLong:
1531 case Primitive::kPrimDouble:
1532 return TypeFlag(Primitive::kPrimLong, offset) |
1533 TypeFlag(Primitive::kPrimDouble, offset);
1534 default:
1535 return TypeFlag(type, offset);
1536 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001537 }
1538
Aart Bik854a02b2015-07-14 16:07:00 -07001539 // Translates type to bit flag.
1540 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1541 CHECK_NE(type, Primitive::kPrimVoid);
1542 const uint64_t one = 1;
1543 const int shift = type; // 0-based consecutive enum
1544 DCHECK_LE(kFieldWriteOffset, shift);
1545 DCHECK_LT(shift, kArrayWriteOffset);
1546 return one << (type + offset);
1547 }
1548
1549 // Private constructor on direct flags value.
1550 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1551
1552 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001553};
1554
David Brazdiled596192015-01-23 10:39:45 +00001555// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001556class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001557 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001558 HEnvironment(ArenaAllocator* arena,
1559 size_t number_of_vregs,
1560 const DexFile& dex_file,
1561 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001562 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001563 InvokeType invoke_type,
1564 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001565 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1566 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001567 parent_(nullptr),
1568 dex_file_(dex_file),
1569 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001570 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001571 invoke_type_(invoke_type),
1572 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001573 }
1574
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001575 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001576 : HEnvironment(arena,
1577 to_copy.Size(),
1578 to_copy.GetDexFile(),
1579 to_copy.GetMethodIdx(),
1580 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001581 to_copy.GetInvokeType(),
1582 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001583
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001584 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001585 if (parent_ != nullptr) {
1586 parent_->SetAndCopyParentChain(allocator, parent);
1587 } else {
1588 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1589 parent_->CopyFrom(parent);
1590 if (parent->GetParent() != nullptr) {
1591 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1592 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001593 }
David Brazdiled596192015-01-23 10:39:45 +00001594 }
1595
Vladimir Marko71bf8092015-09-15 15:33:14 +01001596 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001597 void CopyFrom(HEnvironment* environment);
1598
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001599 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1600 // input to the loop phi instead. This is for inserting instructions that
1601 // require an environment (like HDeoptimization) in the loop pre-header.
1602 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001603
1604 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001605 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001606 }
1607
1608 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001609 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001610 }
1611
David Brazdil1abb4192015-02-17 18:33:36 +00001612 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001613
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001614 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001615
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001616 HEnvironment* GetParent() const { return parent_; }
1617
1618 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001619 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001620 }
1621
1622 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001623 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001624 }
1625
1626 uint32_t GetDexPc() const {
1627 return dex_pc_;
1628 }
1629
1630 uint32_t GetMethodIdx() const {
1631 return method_idx_;
1632 }
1633
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001634 InvokeType GetInvokeType() const {
1635 return invoke_type_;
1636 }
1637
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001638 const DexFile& GetDexFile() const {
1639 return dex_file_;
1640 }
1641
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001642 HInstruction* GetHolder() const {
1643 return holder_;
1644 }
1645
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001646
1647 bool IsFromInlinedInvoke() const {
1648 return GetParent() != nullptr;
1649 }
1650
David Brazdiled596192015-01-23 10:39:45 +00001651 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001652 // Record instructions' use entries of this environment for constant-time removal.
1653 // It should only be called by HInstruction when a new environment use is added.
1654 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1655 DCHECK(env_use->GetUser() == this);
1656 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001657 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001658 }
David Brazdiled596192015-01-23 10:39:45 +00001659
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001660 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1661 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001662 HEnvironment* parent_;
1663 const DexFile& dex_file_;
1664 const uint32_t method_idx_;
1665 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001666 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001667
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001668 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001669 HInstruction* const holder_;
1670
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001671 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001672
1673 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1674};
1675
Calin Juravleacf735c2015-02-12 15:25:22 +00001676class ReferenceTypeInfo : ValueObject {
1677 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001678 typedef Handle<mirror::Class> TypeHandle;
1679
Calin Juravle2e768302015-07-28 14:41:11 +00001680 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1681 // The constructor will check that the type_handle is valid.
1682 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001683 }
1684
Calin Juravle2e768302015-07-28 14:41:11 +00001685 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1686
1687 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1688 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001689 }
1690
Calin Juravle2e768302015-07-28 14:41:11 +00001691 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1692 return IsValidHandle(type_handle_);
1693 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001694
Calin Juravleacf735c2015-02-12 15:25:22 +00001695 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001696
1697 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1698 DCHECK(IsValid());
1699 return GetTypeHandle()->IsObjectClass();
1700 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001701
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001702 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001703 DCHECK(IsValid());
1704 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001705 }
1706
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001707 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1708 DCHECK(IsValid());
1709 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1710 }
1711
Mathieu Chartier90443472015-07-16 20:32:27 -07001712 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001713 DCHECK(IsValid());
1714 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001715 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001716
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001717 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001718 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001719 return GetTypeHandle()->IsArrayClass();
1720 }
1721
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001722 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1723 DCHECK(IsValid());
1724 return GetTypeHandle()->IsPrimitiveArray();
1725 }
1726
1727 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1728 DCHECK(IsValid());
1729 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1730 }
1731
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001732 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001733 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001734 if (!IsExact()) return false;
1735 if (!IsArrayClass()) return false;
1736 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1737 }
1738
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001739 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1740 DCHECK(IsValid());
1741 if (!IsExact()) return false;
1742 if (!IsArrayClass()) return false;
1743 if (!rti.IsArrayClass()) return false;
1744 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1745 rti.GetTypeHandle()->GetComponentType());
1746 }
1747
Calin Juravleacf735c2015-02-12 15:25:22 +00001748 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1749
Mathieu Chartier90443472015-07-16 20:32:27 -07001750 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001751 DCHECK(IsValid());
1752 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001753 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1754 }
1755
Calin Juravlecdfed3d2015-10-26 14:05:01 +00001756 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1757 DCHECK(IsValid());
1758 DCHECK(rti.IsValid());
1759 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
1760 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1761 }
1762
Calin Juravleacf735c2015-02-12 15:25:22 +00001763 // Returns true if the type information provide the same amount of details.
1764 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001765 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001766 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001767 if (!IsValid() && !rti.IsValid()) {
1768 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001769 return true;
1770 }
Calin Juravle2e768302015-07-28 14:41:11 +00001771 if (!IsValid() || !rti.IsValid()) {
1772 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001773 return false;
1774 }
Calin Juravle2e768302015-07-28 14:41:11 +00001775 return IsExact() == rti.IsExact()
1776 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001777 }
1778
1779 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001780 ReferenceTypeInfo();
1781 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001782
Calin Juravleacf735c2015-02-12 15:25:22 +00001783 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001784 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001785 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001786 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001787 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001788};
1789
1790std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1791
Vladimir Markof9f64412015-09-02 14:05:49 +01001792class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001793 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001794 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795 : previous_(nullptr),
1796 next_(nullptr),
1797 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001798 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001799 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001800 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001801 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001802 locations_(nullptr),
1803 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001804 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001805 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001806 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001807
Dave Allison20dfc792014-06-16 20:44:29 -07001808 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001809
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001810#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001811 enum InstructionKind {
1812 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1813 };
1814#undef DECLARE_KIND
1815
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001816 HInstruction* GetNext() const { return next_; }
1817 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001818
Calin Juravle77520bc2015-01-12 18:45:46 +00001819 HInstruction* GetNextDisregardingMoves() const;
1820 HInstruction* GetPreviousDisregardingMoves() const;
1821
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001822 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001823 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001824 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001825 bool IsInBlock() const { return block_ != nullptr; }
1826 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001827 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001828
Roland Levillain6b879dd2014-09-22 17:13:44 +01001829 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001830 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001831
1832 virtual void Accept(HGraphVisitor* visitor) = 0;
1833 virtual const char* DebugName() const = 0;
1834
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001835 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001836 void SetRawInputAt(size_t index, HInstruction* input) {
1837 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1838 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001839
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001840 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001841
1842 uint32_t GetDexPc() const { return dex_pc_; }
1843
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001844 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001845
Roland Levillaine161a2a2014-10-03 12:45:18 +01001846 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001847 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001848
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001849 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001850 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001851
Calin Juravle10e244f2015-01-26 18:54:32 +00001852 // Does not apply for all instructions, but having this at top level greatly
1853 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001854 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001855 virtual bool CanBeNull() const {
1856 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1857 return true;
1858 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001859
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001860 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001861 return false;
1862 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001863
Calin Juravle2e768302015-07-28 14:41:11 +00001864 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001865
Calin Juravle61d544b2015-02-23 16:46:57 +00001866 ReferenceTypeInfo GetReferenceTypeInfo() const {
1867 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1868 return reference_type_info_;
1869 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001870
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001871 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001872 DCHECK(user != nullptr);
1873 HUseListNode<HInstruction*>* use =
1874 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1875 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001876 }
1877
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001879 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001880 HUseListNode<HEnvironment*>* env_use =
1881 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1882 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001883 }
1884
David Brazdil1abb4192015-02-17 18:33:36 +00001885 void RemoveAsUserOfInput(size_t input) {
1886 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1887 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1888 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889
David Brazdil1abb4192015-02-17 18:33:36 +00001890 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1891 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001892
David Brazdiled596192015-01-23 10:39:45 +00001893 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1894 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001895 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001896 bool HasOnlyOneNonEnvironmentUse() const {
1897 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1898 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001899
Roland Levillain6c82d402014-10-13 16:10:27 +01001900 // Does this instruction strictly dominate `other_instruction`?
1901 // Returns false if this instruction and `other_instruction` are the same.
1902 // Aborts if this instruction and `other_instruction` are both phis.
1903 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001904
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001905 int GetId() const { return id_; }
1906 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001907
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001908 int GetSsaIndex() const { return ssa_index_; }
1909 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1910 bool HasSsaIndex() const { return ssa_index_ != -1; }
1911
1912 bool HasEnvironment() const { return environment_ != nullptr; }
1913 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001914 // Set the `environment_` field. Raw because this method does not
1915 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001916 void SetRawEnvironment(HEnvironment* environment) {
1917 DCHECK(environment_ == nullptr);
1918 DCHECK_EQ(environment->GetHolder(), this);
1919 environment_ = environment;
1920 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001921
1922 // Set the environment of this instruction, copying it from `environment`. While
1923 // copying, the uses lists are being updated.
1924 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001925 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001926 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001927 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001928 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001929 if (environment->GetParent() != nullptr) {
1930 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1931 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001932 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001933
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001934 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1935 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001936 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001937 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001938 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001939 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001940 if (environment->GetParent() != nullptr) {
1941 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1942 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001943 }
1944
Nicolas Geoffray39468442014-09-02 15:17:15 +01001945 // Returns the number of entries in the environment. Typically, that is the
1946 // number of dex registers in a method. It could be more in case of inlining.
1947 size_t EnvironmentSize() const;
1948
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001949 LocationSummary* GetLocations() const { return locations_; }
1950 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001951
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001952 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001953 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001954
Alexandre Rames188d4312015-04-09 18:30:21 +01001955 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1956 // uses of this instruction by `other` are *not* updated.
1957 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1958 ReplaceWith(other);
1959 other->ReplaceInput(this, use_index);
1960 }
1961
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001962 // Move `this` instruction before `cursor`.
1963 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001964
Vladimir Markofb337ea2015-11-25 15:25:10 +00001965 // Move `this` before its first user and out of any loops. If there is no
1966 // out-of-loop user that dominates all other users, move the instruction
1967 // to the end of the out-of-loop common dominator of the user's blocks.
1968 //
1969 // This can be used only on non-throwing instructions with no side effects that
1970 // have at least one use but no environment uses.
1971 void MoveBeforeFirstUserAndOutOfLoops();
1972
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001973#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001974 bool Is##type() const { return (As##type() != nullptr); } \
1975 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001976 virtual H##type* As##type() { return nullptr; }
1977
1978 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1979#undef INSTRUCTION_TYPE_CHECK
1980
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001981 // Returns whether the instruction can be moved within the graph.
1982 virtual bool CanBeMoved() const { return false; }
1983
1984 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001985 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001986 return false;
1987 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001988
1989 // Returns whether any data encoded in the two instructions is equal.
1990 // This method does not look at the inputs. Both instructions must be
1991 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001992 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001993 return false;
1994 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001995
1996 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001997 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001998 // 2) Their inputs are identical.
1999 bool Equals(HInstruction* other) const;
2000
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002001 virtual InstructionKind GetKind() const = 0;
2002
2003 virtual size_t ComputeHashCode() const {
2004 size_t result = GetKind();
2005 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2006 result = (result * 31) + InputAt(i)->GetId();
2007 }
2008 return result;
2009 }
2010
2011 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002012 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002013
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002014 size_t GetLifetimePosition() const { return lifetime_position_; }
2015 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2016 LiveInterval* GetLiveInterval() const { return live_interval_; }
2017 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2018 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2019
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002020 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2021
2022 // Returns whether the code generation of the instruction will require to have access
2023 // to the current method. Such instructions are:
2024 // (1): Instructions that require an environment, as calling the runtime requires
2025 // to walk the stack and have the current method stored at a specific stack address.
2026 // (2): Object literals like classes and strings, that are loaded from the dex cache
2027 // fields of the current method.
2028 bool NeedsCurrentMethod() const {
2029 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2030 }
2031
Vladimir Markodc151b22015-10-15 18:02:30 +01002032 // Returns whether the code generation of the instruction will require to have access
2033 // to the dex cache of the current method's declaring class via the current method.
2034 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002035
Mark Mendellc4701932015-04-10 13:18:51 -04002036 // Does this instruction have any use in an environment before
2037 // control flow hits 'other'?
2038 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2039
2040 // Remove all references to environment uses of this instruction.
2041 // The caller must ensure that this is safe to do.
2042 void RemoveEnvironmentUsers();
2043
David Brazdil1abb4192015-02-17 18:33:36 +00002044 protected:
2045 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2046 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2047
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002048 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002049 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2050
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002051 HInstruction* previous_;
2052 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002053 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002054 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002056 // An instruction gets an id when it is added to the graph.
2057 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002058 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002059 int id_;
2060
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002061 // When doing liveness analysis, instructions that have uses get an SSA index.
2062 int ssa_index_;
2063
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002064 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002065 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002066
2067 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002068 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002069
Nicolas Geoffray39468442014-09-02 15:17:15 +01002070 // The environment associated with this instruction. Not null if the instruction
2071 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002072 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002073
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002074 // Set by the code generator.
2075 LocationSummary* locations_;
2076
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002077 // Set by the liveness analysis.
2078 LiveInterval* live_interval_;
2079
2080 // Set by the liveness analysis, this is the position in a linear
2081 // order of blocks where this instruction's live interval start.
2082 size_t lifetime_position_;
2083
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002084 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002085
Calin Juravleacf735c2015-02-12 15:25:22 +00002086 // TODO: for primitive types this should be marked as invalid.
2087 ReferenceTypeInfo reference_type_info_;
2088
David Brazdil1abb4192015-02-17 18:33:36 +00002089 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002090 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002091 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002092 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002093 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002094
2095 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2096};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002097std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002098
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002099class HInputIterator : public ValueObject {
2100 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002101 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002102
2103 bool Done() const { return index_ == instruction_->InputCount(); }
2104 HInstruction* Current() const { return instruction_->InputAt(index_); }
2105 void Advance() { index_++; }
2106
2107 private:
2108 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002109 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002110
2111 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2112};
2113
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114class HInstructionIterator : public ValueObject {
2115 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002116 explicit HInstructionIterator(const HInstructionList& instructions)
2117 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002118 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119 }
2120
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002121 bool Done() const { return instruction_ == nullptr; }
2122 HInstruction* Current() const { return instruction_; }
2123 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002124 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002125 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002126 }
2127
2128 private:
2129 HInstruction* instruction_;
2130 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002131
2132 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002133};
2134
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002135class HBackwardInstructionIterator : public ValueObject {
2136 public:
2137 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2138 : instruction_(instructions.last_instruction_) {
2139 next_ = Done() ? nullptr : instruction_->GetPrevious();
2140 }
2141
2142 bool Done() const { return instruction_ == nullptr; }
2143 HInstruction* Current() const { return instruction_; }
2144 void Advance() {
2145 instruction_ = next_;
2146 next_ = Done() ? nullptr : instruction_->GetPrevious();
2147 }
2148
2149 private:
2150 HInstruction* instruction_;
2151 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002152
2153 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002154};
2155
Vladimir Markof9f64412015-09-02 14:05:49 +01002156template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157class HTemplateInstruction: public HInstruction {
2158 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002159 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002160 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002161 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002162
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002163 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002164
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002165 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002166 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2167 DCHECK_LT(i, N);
2168 return inputs_[i];
2169 }
David Brazdil1abb4192015-02-17 18:33:36 +00002170
2171 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002172 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002173 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002174 }
2175
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002177 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002178
2179 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002180};
2181
Vladimir Markof9f64412015-09-02 14:05:49 +01002182// HTemplateInstruction specialization for N=0.
2183template<>
2184class HTemplateInstruction<0>: public HInstruction {
2185 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002186 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002187 : HInstruction(side_effects, dex_pc) {}
2188
Vladimir Markof9f64412015-09-02 14:05:49 +01002189 virtual ~HTemplateInstruction() {}
2190
2191 size_t InputCount() const OVERRIDE { return 0; }
2192
2193 protected:
2194 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2195 LOG(FATAL) << "Unreachable";
2196 UNREACHABLE();
2197 }
2198
2199 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2200 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2201 LOG(FATAL) << "Unreachable";
2202 UNREACHABLE();
2203 }
2204
2205 private:
2206 friend class SsaBuilder;
2207};
2208
Dave Allison20dfc792014-06-16 20:44:29 -07002209template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002210class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002211 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002212 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002213 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002214 virtual ~HExpression() {}
2215
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002216 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002217
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002218 protected:
2219 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002220};
2221
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002222// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2223// instruction that branches to the exit block.
2224class HReturnVoid : public HTemplateInstruction<0> {
2225 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002226 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2227 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002228
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002229 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002230
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002231 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232
2233 private:
2234 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2235};
2236
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002237// Represents dex's RETURN opcodes. A HReturn is a control flow
2238// instruction that branches to the exit block.
2239class HReturn : public HTemplateInstruction<1> {
2240 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002241 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2242 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002243 SetRawInputAt(0, value);
2244 }
2245
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002246 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002247
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002248 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002249
2250 private:
2251 DISALLOW_COPY_AND_ASSIGN(HReturn);
2252};
2253
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002254// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002255// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002256// exit block.
2257class HExit : public HTemplateInstruction<0> {
2258 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002259 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002260
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002261 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002262
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002263 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002264
2265 private:
2266 DISALLOW_COPY_AND_ASSIGN(HExit);
2267};
2268
2269// Jumps from one block to another.
2270class HGoto : public HTemplateInstruction<0> {
2271 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002272 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002273
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002274 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002275
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002276 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002277 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002278 }
2279
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002280 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002281
2282 private:
2283 DISALLOW_COPY_AND_ASSIGN(HGoto);
2284};
2285
Roland Levillain9867bc72015-08-05 10:21:34 +01002286class HConstant : public HExpression<0> {
2287 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002288 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2289 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002290
2291 bool CanBeMoved() const OVERRIDE { return true; }
2292
2293 virtual bool IsMinusOne() const { return false; }
2294 virtual bool IsZero() const { return false; }
2295 virtual bool IsOne() const { return false; }
2296
David Brazdil77a48ae2015-09-15 12:34:04 +00002297 virtual uint64_t GetValueAsUint64() const = 0;
2298
Roland Levillain9867bc72015-08-05 10:21:34 +01002299 DECLARE_INSTRUCTION(Constant);
2300
2301 private:
2302 DISALLOW_COPY_AND_ASSIGN(HConstant);
2303};
2304
2305class HNullConstant : public HConstant {
2306 public:
2307 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2308 return true;
2309 }
2310
David Brazdil77a48ae2015-09-15 12:34:04 +00002311 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2312
Roland Levillain9867bc72015-08-05 10:21:34 +01002313 size_t ComputeHashCode() const OVERRIDE { return 0; }
2314
2315 DECLARE_INSTRUCTION(NullConstant);
2316
2317 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002318 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002319
2320 friend class HGraph;
2321 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2322};
2323
2324// Constants of the type int. Those can be from Dex instructions, or
2325// synthesized (for example with the if-eqz instruction).
2326class HIntConstant : public HConstant {
2327 public:
2328 int32_t GetValue() const { return value_; }
2329
David Brazdil9f389d42015-10-01 14:32:56 +01002330 uint64_t GetValueAsUint64() const OVERRIDE {
2331 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2332 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002333
Roland Levillain9867bc72015-08-05 10:21:34 +01002334 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2335 DCHECK(other->IsIntConstant());
2336 return other->AsIntConstant()->value_ == value_;
2337 }
2338
2339 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2340
2341 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2342 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2343 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2344
2345 DECLARE_INSTRUCTION(IntConstant);
2346
2347 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002348 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2349 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2350 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2351 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002352
2353 const int32_t value_;
2354
2355 friend class HGraph;
2356 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2357 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2358 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2359};
2360
2361class HLongConstant : public HConstant {
2362 public:
2363 int64_t GetValue() const { return value_; }
2364
David Brazdil77a48ae2015-09-15 12:34:04 +00002365 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2366
Roland Levillain9867bc72015-08-05 10:21:34 +01002367 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2368 DCHECK(other->IsLongConstant());
2369 return other->AsLongConstant()->value_ == value_;
2370 }
2371
2372 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2373
2374 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2375 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2376 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2377
2378 DECLARE_INSTRUCTION(LongConstant);
2379
2380 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002381 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2382 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002383
2384 const int64_t value_;
2385
2386 friend class HGraph;
2387 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2388};
Dave Allison20dfc792014-06-16 20:44:29 -07002389
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002390// Conditional branch. A block ending with an HIf instruction must have
2391// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002392class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002393 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002394 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2395 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002396 SetRawInputAt(0, input);
2397 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002398
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002399 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002400
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002401 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002402 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002403 }
2404
2405 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002406 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002407 }
2408
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002409 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002410
2411 private:
2412 DISALLOW_COPY_AND_ASSIGN(HIf);
2413};
2414
David Brazdilfc6a86a2015-06-26 10:33:45 +00002415
2416// Abstract instruction which marks the beginning and/or end of a try block and
2417// links it to the respective exception handlers. Behaves the same as a Goto in
2418// non-exceptional control flow.
2419// Normal-flow successor is stored at index zero, exception handlers under
2420// higher indices in no particular order.
2421class HTryBoundary : public HTemplateInstruction<0> {
2422 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002423 enum BoundaryKind {
2424 kEntry,
2425 kExit,
2426 };
2427
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002428 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2429 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002430
2431 bool IsControlFlow() const OVERRIDE { return true; }
2432
2433 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002434 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002435
David Brazdild26a4112015-11-10 11:07:31 +00002436 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2437 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2438 }
2439
David Brazdilfc6a86a2015-06-26 10:33:45 +00002440 // Returns whether `handler` is among its exception handlers (non-zero index
2441 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002442 bool HasExceptionHandler(const HBasicBlock& handler) const {
2443 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002444 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002445 }
2446
2447 // If not present already, adds `handler` to its block's list of exception
2448 // handlers.
2449 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002450 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002451 GetBlock()->AddSuccessor(handler);
2452 }
2453 }
2454
David Brazdil56e1acc2015-06-30 15:41:36 +01002455 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002456
David Brazdilffee3d32015-07-06 11:48:53 +01002457 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2458
David Brazdilfc6a86a2015-06-26 10:33:45 +00002459 DECLARE_INSTRUCTION(TryBoundary);
2460
2461 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002462 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002463
2464 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2465};
2466
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002467// Deoptimize to interpreter, upon checking a condition.
2468class HDeoptimize : public HTemplateInstruction<1> {
2469 public:
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002470 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002471 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002472 SetRawInputAt(0, cond);
2473 }
2474
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002475 bool CanBeMoved() const OVERRIDE { return true; }
2476 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2477 return true;
2478 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002479 bool NeedsEnvironment() const OVERRIDE { return true; }
2480 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002481
2482 DECLARE_INSTRUCTION(Deoptimize);
2483
2484 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002485 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2486};
2487
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002488// Represents the ArtMethod that was passed as a first argument to
2489// the method. It is used by instructions that depend on it, like
2490// instructions that work with the dex cache.
2491class HCurrentMethod : public HExpression<0> {
2492 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002493 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2494 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002495
2496 DECLARE_INSTRUCTION(CurrentMethod);
2497
2498 private:
2499 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2500};
2501
Mark Mendellfe57faa2015-09-18 09:26:15 -04002502// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2503// have one successor for each entry in the switch table, and the final successor
2504// will be the block containing the next Dex opcode.
2505class HPackedSwitch : public HTemplateInstruction<1> {
2506 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002507 HPackedSwitch(int32_t start_value,
2508 uint32_t num_entries,
2509 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002510 uint32_t dex_pc = kNoDexPc)
2511 : HTemplateInstruction(SideEffects::None(), dex_pc),
2512 start_value_(start_value),
2513 num_entries_(num_entries) {
2514 SetRawInputAt(0, input);
2515 }
2516
2517 bool IsControlFlow() const OVERRIDE { return true; }
2518
2519 int32_t GetStartValue() const { return start_value_; }
2520
Vladimir Marko211c2112015-09-24 16:52:33 +01002521 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002522
2523 HBasicBlock* GetDefaultBlock() const {
2524 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002525 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002526 }
2527 DECLARE_INSTRUCTION(PackedSwitch);
2528
2529 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002530 const int32_t start_value_;
2531 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002532
2533 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2534};
2535
Roland Levillain88cb1752014-10-20 16:36:47 +01002536class HUnaryOperation : public HExpression<1> {
2537 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002538 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2539 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002540 SetRawInputAt(0, input);
2541 }
2542
2543 HInstruction* GetInput() const { return InputAt(0); }
2544 Primitive::Type GetResultType() const { return GetType(); }
2545
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002546 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002547 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002548 return true;
2549 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002550
Roland Levillain9240d6a2014-10-20 16:47:04 +01002551 // Try to statically evaluate `operation` and return a HConstant
2552 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002553 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002554 HConstant* TryStaticEvaluation() const;
2555
2556 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002557 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2558 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002559
Roland Levillain88cb1752014-10-20 16:36:47 +01002560 DECLARE_INSTRUCTION(UnaryOperation);
2561
2562 private:
2563 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2564};
2565
Dave Allison20dfc792014-06-16 20:44:29 -07002566class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002567 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002568 HBinaryOperation(Primitive::Type result_type,
2569 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002570 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002571 SideEffects side_effects = SideEffects::None(),
2572 uint32_t dex_pc = kNoDexPc)
2573 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002574 SetRawInputAt(0, left);
2575 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002576 }
2577
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002578 HInstruction* GetLeft() const { return InputAt(0); }
2579 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002580 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002581
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002582 virtual bool IsCommutative() const { return false; }
2583
2584 // Put constant on the right.
2585 // Returns whether order is changed.
2586 bool OrderInputsWithConstantOnTheRight() {
2587 HInstruction* left = InputAt(0);
2588 HInstruction* right = InputAt(1);
2589 if (left->IsConstant() && !right->IsConstant()) {
2590 ReplaceInput(right, 0);
2591 ReplaceInput(left, 1);
2592 return true;
2593 }
2594 return false;
2595 }
2596
2597 // Order inputs by instruction id, but favor constant on the right side.
2598 // This helps GVN for commutative ops.
2599 void OrderInputs() {
2600 DCHECK(IsCommutative());
2601 HInstruction* left = InputAt(0);
2602 HInstruction* right = InputAt(1);
2603 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2604 return;
2605 }
2606 if (OrderInputsWithConstantOnTheRight()) {
2607 return;
2608 }
2609 // Order according to instruction id.
2610 if (left->GetId() > right->GetId()) {
2611 ReplaceInput(right, 0);
2612 ReplaceInput(left, 1);
2613 }
2614 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002615
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002616 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002617 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002618 return true;
2619 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002620
Roland Levillain9240d6a2014-10-20 16:47:04 +01002621 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002622 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002623 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002624 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002625
2626 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002627 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2628 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2629 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2630 HLongConstant* y ATTRIBUTE_UNUSED) const {
2631 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2632 return nullptr;
2633 }
2634 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2635 HIntConstant* y ATTRIBUTE_UNUSED) const {
2636 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2637 return nullptr;
2638 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002639 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2640 HNullConstant* y ATTRIBUTE_UNUSED) const {
2641 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2642 return nullptr;
2643 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002644
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002645 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002646 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002647 HConstant* GetConstantRight() const;
2648
2649 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002650 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002651 HInstruction* GetLeastConstantLeft() const;
2652
Roland Levillainccc07a92014-09-16 14:48:16 +01002653 DECLARE_INSTRUCTION(BinaryOperation);
2654
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002655 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002656 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2657};
2658
Mark Mendellc4701932015-04-10 13:18:51 -04002659// The comparison bias applies for floating point operations and indicates how NaN
2660// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002661enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002662 kNoBias, // bias is not applicable (i.e. for long operation)
2663 kGtBias, // return 1 for NaN comparisons
2664 kLtBias, // return -1 for NaN comparisons
2665};
2666
Dave Allison20dfc792014-06-16 20:44:29 -07002667class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002668 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002669 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2670 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002671 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002672 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002673
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002674 bool NeedsMaterialization() const { return needs_materialization_; }
2675 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002676
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002677 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002678 // `instruction`, and disregard moves in between.
2679 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002680
Dave Allison20dfc792014-06-16 20:44:29 -07002681 DECLARE_INSTRUCTION(Condition);
2682
2683 virtual IfCondition GetCondition() const = 0;
2684
Mark Mendellc4701932015-04-10 13:18:51 -04002685 virtual IfCondition GetOppositeCondition() const = 0;
2686
Roland Levillain4fa13f62015-07-06 18:11:54 +01002687 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002688
2689 void SetBias(ComparisonBias bias) { bias_ = bias; }
2690
2691 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2692 return bias_ == other->AsCondition()->bias_;
2693 }
2694
Roland Levillain4fa13f62015-07-06 18:11:54 +01002695 bool IsFPConditionTrueIfNaN() const {
2696 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2697 IfCondition if_cond = GetCondition();
2698 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2699 }
2700
2701 bool IsFPConditionFalseIfNaN() const {
2702 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2703 IfCondition if_cond = GetCondition();
2704 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2705 }
2706
Dave Allison20dfc792014-06-16 20:44:29 -07002707 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002708 // For register allocation purposes, returns whether this instruction needs to be
2709 // materialized (that is, not just be in the processor flags).
2710 bool needs_materialization_;
2711
Mark Mendellc4701932015-04-10 13:18:51 -04002712 // Needed if we merge a HCompare into a HCondition.
2713 ComparisonBias bias_;
2714
Dave Allison20dfc792014-06-16 20:44:29 -07002715 DISALLOW_COPY_AND_ASSIGN(HCondition);
2716};
2717
2718// Instruction to check if two inputs are equal to each other.
2719class HEqual : public HCondition {
2720 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2722 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002723
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002724 bool IsCommutative() const OVERRIDE { return true; }
2725
Roland Levillain9867bc72015-08-05 10:21:34 +01002726 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002727 return GetBlock()->GetGraph()->GetIntConstant(
2728 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002729 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002730 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002731 return GetBlock()->GetGraph()->GetIntConstant(
2732 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002733 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002734 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2735 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002736 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002737 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002738
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002739 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002740
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002741 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002742 return kCondEQ;
2743 }
2744
Mark Mendellc4701932015-04-10 13:18:51 -04002745 IfCondition GetOppositeCondition() const OVERRIDE {
2746 return kCondNE;
2747 }
2748
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002749 private:
Aart Bike9f37602015-10-09 11:15:55 -07002750 template <typename T> bool Compute(T x, T y) const { return x == y; }
2751
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002752 DISALLOW_COPY_AND_ASSIGN(HEqual);
2753};
2754
Dave Allison20dfc792014-06-16 20:44:29 -07002755class HNotEqual : public HCondition {
2756 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002757 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2758 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002759
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002760 bool IsCommutative() const OVERRIDE { return true; }
2761
Roland Levillain9867bc72015-08-05 10:21:34 +01002762 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002763 return GetBlock()->GetGraph()->GetIntConstant(
2764 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002765 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002766 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002767 return GetBlock()->GetGraph()->GetIntConstant(
2768 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002769 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002770 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2771 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002772 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002773 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002774
Dave Allison20dfc792014-06-16 20:44:29 -07002775 DECLARE_INSTRUCTION(NotEqual);
2776
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002777 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002778 return kCondNE;
2779 }
2780
Mark Mendellc4701932015-04-10 13:18:51 -04002781 IfCondition GetOppositeCondition() const OVERRIDE {
2782 return kCondEQ;
2783 }
2784
Dave Allison20dfc792014-06-16 20:44:29 -07002785 private:
Aart Bike9f37602015-10-09 11:15:55 -07002786 template <typename T> bool Compute(T x, T y) const { return x != y; }
2787
Dave Allison20dfc792014-06-16 20:44:29 -07002788 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2789};
2790
2791class HLessThan : public HCondition {
2792 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002793 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2794 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002795
Roland Levillain9867bc72015-08-05 10:21:34 +01002796 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002797 return GetBlock()->GetGraph()->GetIntConstant(
2798 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002799 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002800 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002801 return GetBlock()->GetGraph()->GetIntConstant(
2802 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002803 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002804
Dave Allison20dfc792014-06-16 20:44:29 -07002805 DECLARE_INSTRUCTION(LessThan);
2806
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002807 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002808 return kCondLT;
2809 }
2810
Mark Mendellc4701932015-04-10 13:18:51 -04002811 IfCondition GetOppositeCondition() const OVERRIDE {
2812 return kCondGE;
2813 }
2814
Dave Allison20dfc792014-06-16 20:44:29 -07002815 private:
Aart Bike9f37602015-10-09 11:15:55 -07002816 template <typename T> bool Compute(T x, T y) const { return x < y; }
2817
Dave Allison20dfc792014-06-16 20:44:29 -07002818 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2819};
2820
2821class HLessThanOrEqual : public HCondition {
2822 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002823 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2824 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002825
Roland Levillain9867bc72015-08-05 10:21:34 +01002826 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002827 return GetBlock()->GetGraph()->GetIntConstant(
2828 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002829 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002830 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002831 return GetBlock()->GetGraph()->GetIntConstant(
2832 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002833 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002834
Dave Allison20dfc792014-06-16 20:44:29 -07002835 DECLARE_INSTRUCTION(LessThanOrEqual);
2836
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002837 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002838 return kCondLE;
2839 }
2840
Mark Mendellc4701932015-04-10 13:18:51 -04002841 IfCondition GetOppositeCondition() const OVERRIDE {
2842 return kCondGT;
2843 }
2844
Dave Allison20dfc792014-06-16 20:44:29 -07002845 private:
Aart Bike9f37602015-10-09 11:15:55 -07002846 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2847
Dave Allison20dfc792014-06-16 20:44:29 -07002848 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2849};
2850
2851class HGreaterThan : public HCondition {
2852 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002853 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2854 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002855
Roland Levillain9867bc72015-08-05 10:21:34 +01002856 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002857 return GetBlock()->GetGraph()->GetIntConstant(
2858 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002859 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002860 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002861 return GetBlock()->GetGraph()->GetIntConstant(
2862 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002863 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002864
Dave Allison20dfc792014-06-16 20:44:29 -07002865 DECLARE_INSTRUCTION(GreaterThan);
2866
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002867 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002868 return kCondGT;
2869 }
2870
Mark Mendellc4701932015-04-10 13:18:51 -04002871 IfCondition GetOppositeCondition() const OVERRIDE {
2872 return kCondLE;
2873 }
2874
Dave Allison20dfc792014-06-16 20:44:29 -07002875 private:
Aart Bike9f37602015-10-09 11:15:55 -07002876 template <typename T> bool Compute(T x, T y) const { return x > y; }
2877
Dave Allison20dfc792014-06-16 20:44:29 -07002878 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2879};
2880
2881class HGreaterThanOrEqual : public HCondition {
2882 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002883 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2884 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002885
Roland Levillain9867bc72015-08-05 10:21:34 +01002886 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002887 return GetBlock()->GetGraph()->GetIntConstant(
2888 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002889 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002890 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002891 return GetBlock()->GetGraph()->GetIntConstant(
2892 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002893 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002894
Dave Allison20dfc792014-06-16 20:44:29 -07002895 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2896
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002897 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002898 return kCondGE;
2899 }
2900
Mark Mendellc4701932015-04-10 13:18:51 -04002901 IfCondition GetOppositeCondition() const OVERRIDE {
2902 return kCondLT;
2903 }
2904
Dave Allison20dfc792014-06-16 20:44:29 -07002905 private:
Aart Bike9f37602015-10-09 11:15:55 -07002906 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2907
Dave Allison20dfc792014-06-16 20:44:29 -07002908 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2909};
2910
Aart Bike9f37602015-10-09 11:15:55 -07002911class HBelow : public HCondition {
2912 public:
2913 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2914 : HCondition(first, second, dex_pc) {}
2915
2916 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2917 return GetBlock()->GetGraph()->GetIntConstant(
2918 Compute(static_cast<uint32_t>(x->GetValue()),
2919 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2920 }
2921 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2922 return GetBlock()->GetGraph()->GetIntConstant(
2923 Compute(static_cast<uint64_t>(x->GetValue()),
2924 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2925 }
2926
2927 DECLARE_INSTRUCTION(Below);
2928
2929 IfCondition GetCondition() const OVERRIDE {
2930 return kCondB;
2931 }
2932
2933 IfCondition GetOppositeCondition() const OVERRIDE {
2934 return kCondAE;
2935 }
2936
2937 private:
2938 template <typename T> bool Compute(T x, T y) const { return x < y; }
2939
2940 DISALLOW_COPY_AND_ASSIGN(HBelow);
2941};
2942
2943class HBelowOrEqual : public HCondition {
2944 public:
2945 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2946 : HCondition(first, second, dex_pc) {}
2947
2948 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2949 return GetBlock()->GetGraph()->GetIntConstant(
2950 Compute(static_cast<uint32_t>(x->GetValue()),
2951 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2952 }
2953 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2954 return GetBlock()->GetGraph()->GetIntConstant(
2955 Compute(static_cast<uint64_t>(x->GetValue()),
2956 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2957 }
2958
2959 DECLARE_INSTRUCTION(BelowOrEqual);
2960
2961 IfCondition GetCondition() const OVERRIDE {
2962 return kCondBE;
2963 }
2964
2965 IfCondition GetOppositeCondition() const OVERRIDE {
2966 return kCondA;
2967 }
2968
2969 private:
2970 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2971
2972 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2973};
2974
2975class HAbove : public HCondition {
2976 public:
2977 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2978 : HCondition(first, second, dex_pc) {}
2979
2980 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2981 return GetBlock()->GetGraph()->GetIntConstant(
2982 Compute(static_cast<uint32_t>(x->GetValue()),
2983 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2984 }
2985 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2986 return GetBlock()->GetGraph()->GetIntConstant(
2987 Compute(static_cast<uint64_t>(x->GetValue()),
2988 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2989 }
2990
2991 DECLARE_INSTRUCTION(Above);
2992
2993 IfCondition GetCondition() const OVERRIDE {
2994 return kCondA;
2995 }
2996
2997 IfCondition GetOppositeCondition() const OVERRIDE {
2998 return kCondBE;
2999 }
3000
3001 private:
3002 template <typename T> bool Compute(T x, T y) const { return x > y; }
3003
3004 DISALLOW_COPY_AND_ASSIGN(HAbove);
3005};
3006
3007class HAboveOrEqual : public HCondition {
3008 public:
3009 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3010 : HCondition(first, second, dex_pc) {}
3011
3012 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3013 return GetBlock()->GetGraph()->GetIntConstant(
3014 Compute(static_cast<uint32_t>(x->GetValue()),
3015 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3016 }
3017 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3018 return GetBlock()->GetGraph()->GetIntConstant(
3019 Compute(static_cast<uint64_t>(x->GetValue()),
3020 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3021 }
3022
3023 DECLARE_INSTRUCTION(AboveOrEqual);
3024
3025 IfCondition GetCondition() const OVERRIDE {
3026 return kCondAE;
3027 }
3028
3029 IfCondition GetOppositeCondition() const OVERRIDE {
3030 return kCondB;
3031 }
3032
3033 private:
3034 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3035
3036 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3037};
Dave Allison20dfc792014-06-16 20:44:29 -07003038
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003039// Instruction to check how two inputs compare to each other.
3040// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3041class HCompare : public HBinaryOperation {
3042 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003043 HCompare(Primitive::Type type,
3044 HInstruction* first,
3045 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003046 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003047 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003048 : HBinaryOperation(Primitive::kPrimInt,
3049 first,
3050 second,
3051 SideEffectsForArchRuntimeCalls(type),
3052 dex_pc),
3053 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003054 DCHECK_EQ(type, first->GetType());
3055 DCHECK_EQ(type, second->GetType());
3056 }
3057
Roland Levillain9867bc72015-08-05 10:21:34 +01003058 template <typename T>
3059 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003060
Roland Levillain9867bc72015-08-05 10:21:34 +01003061 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003062 return GetBlock()->GetGraph()->GetIntConstant(
3063 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003064 }
3065 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003066 return GetBlock()->GetGraph()->GetIntConstant(
3067 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003068 }
3069
Calin Juravleddb7df22014-11-25 20:56:51 +00003070 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3071 return bias_ == other->AsCompare()->bias_;
3072 }
3073
Mark Mendellc4701932015-04-10 13:18:51 -04003074 ComparisonBias GetBias() const { return bias_; }
3075
Roland Levillain4fa13f62015-07-06 18:11:54 +01003076 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003077
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003078
3079 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3080 // MIPS64 uses a runtime call for FP comparisons.
3081 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3082 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003083
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003084 DECLARE_INSTRUCTION(Compare);
3085
3086 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003087 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003088
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003089 DISALLOW_COPY_AND_ASSIGN(HCompare);
3090};
3091
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003092// A local in the graph. Corresponds to a Dex register.
3093class HLocal : public HTemplateInstruction<0> {
3094 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003095 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003096 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003097
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003098 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003099
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003100 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003101
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003102 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003103 // The Dex register number.
3104 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003105
3106 DISALLOW_COPY_AND_ASSIGN(HLocal);
3107};
3108
3109// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003110class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003111 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003112 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3113 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003114 SetRawInputAt(0, local);
3115 }
3116
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003117 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3118
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003119 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003120
3121 private:
3122 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3123};
3124
3125// Store a value in a given local. This instruction has two inputs: the value
3126// and the local.
3127class HStoreLocal : public HTemplateInstruction<2> {
3128 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003129 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3130 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003131 SetRawInputAt(0, local);
3132 SetRawInputAt(1, value);
3133 }
3134
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003135 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3136
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003137 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003138
3139 private:
3140 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3141};
3142
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003143class HFloatConstant : public HConstant {
3144 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003145 float GetValue() const { return value_; }
3146
David Brazdil77a48ae2015-09-15 12:34:04 +00003147 uint64_t GetValueAsUint64() const OVERRIDE {
3148 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3149 }
3150
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003151 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003152 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003153 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003154 }
3155
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003156 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003157
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003158 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003159 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003160 }
3161 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003162 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003163 }
3164 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003165 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3166 }
3167 bool IsNaN() const {
3168 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003169 }
3170
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003171 DECLARE_INSTRUCTION(FloatConstant);
3172
3173 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003174 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3175 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3176 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3177 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003178
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003179 const float value_;
3180
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003181 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003182 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003183 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003184 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3185};
3186
3187class HDoubleConstant : public HConstant {
3188 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003189 double GetValue() const { return value_; }
3190
David Brazdil77a48ae2015-09-15 12:34:04 +00003191 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3192
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003193 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003194 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003195 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003196 }
3197
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003198 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003199
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003200 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003201 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003202 }
3203 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003204 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003205 }
3206 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003207 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3208 }
3209 bool IsNaN() const {
3210 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003211 }
3212
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003213 DECLARE_INSTRUCTION(DoubleConstant);
3214
3215 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003216 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3217 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3218 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3219 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003220
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003221 const double value_;
3222
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003223 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003224 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003225 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003226 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3227};
3228
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003229enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003230#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003231#include "intrinsics_list.h"
3232 kNone,
3233 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3234#undef INTRINSICS_LIST
3235#undef OPTIMIZING_INTRINSICS
3236};
3237std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3238
Agi Csaki05f20562015-08-19 14:58:14 -07003239enum IntrinsicNeedsEnvironmentOrCache {
3240 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3241 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003242};
3243
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003244class HInvoke : public HInstruction {
3245 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003246 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003247
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003248 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003249
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003250 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003251 SetRawInputAt(index, argument);
3252 }
3253
Roland Levillain3e3d7332015-04-28 11:00:54 +01003254 // Return the number of arguments. This number can be lower than
3255 // the number of inputs returned by InputCount(), as some invoke
3256 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3257 // inputs at the end of their list of inputs.
3258 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3259
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003260 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003261
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003262
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003263 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003264 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003265
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003266 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3267
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003268 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003269 return intrinsic_;
3270 }
3271
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003272 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003273
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003274 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003275 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003276 }
3277
3278 bool CanThrow() const OVERRIDE { return true; }
3279
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003280 uint32_t* GetIntrinsicOptimizations() {
3281 return &intrinsic_optimizations_;
3282 }
3283
3284 const uint32_t* GetIntrinsicOptimizations() const {
3285 return &intrinsic_optimizations_;
3286 }
3287
3288 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3289
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003290 DECLARE_INSTRUCTION(Invoke);
3291
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003292 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003293 HInvoke(ArenaAllocator* arena,
3294 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003295 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003296 Primitive::Type return_type,
3297 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003298 uint32_t dex_method_index,
3299 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003300 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003301 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003302 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003303 inputs_(number_of_arguments + number_of_other_inputs,
3304 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003305 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003306 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003307 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003308 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003309 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003310 }
3311
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003312 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003313 return inputs_[index];
3314 }
3315
David Brazdil1abb4192015-02-17 18:33:36 +00003316 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003317 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003318 }
3319
Roland Levillain3e3d7332015-04-28 11:00:54 +01003320 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003321 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003322 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003323 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003324 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003325 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003326
3327 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3328 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003329
3330 private:
3331 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3332};
3333
Calin Juravle175dc732015-08-25 15:42:32 +01003334class HInvokeUnresolved : public HInvoke {
3335 public:
3336 HInvokeUnresolved(ArenaAllocator* arena,
3337 uint32_t number_of_arguments,
3338 Primitive::Type return_type,
3339 uint32_t dex_pc,
3340 uint32_t dex_method_index,
3341 InvokeType invoke_type)
3342 : HInvoke(arena,
3343 number_of_arguments,
3344 0u /* number_of_other_inputs */,
3345 return_type,
3346 dex_pc,
3347 dex_method_index,
3348 invoke_type) {
3349 }
3350
3351 DECLARE_INSTRUCTION(InvokeUnresolved);
3352
3353 private:
3354 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3355};
3356
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003357class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003358 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003359 // Requirements of this method call regarding the class
3360 // initialization (clinit) check of its declaring class.
3361 enum class ClinitCheckRequirement {
3362 kNone, // Class already initialized.
3363 kExplicit, // Static call having explicit clinit check as last input.
3364 kImplicit, // Static call implicitly requiring a clinit check.
3365 };
3366
Vladimir Marko58155012015-08-19 12:49:41 +00003367 // Determines how to load the target ArtMethod*.
3368 enum class MethodLoadKind {
3369 // Use a String init ArtMethod* loaded from Thread entrypoints.
3370 kStringInit,
3371
3372 // Use the method's own ArtMethod* loaded by the register allocator.
3373 kRecursive,
3374
3375 // Use ArtMethod* at a known address, embed the direct address in the code.
3376 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3377 kDirectAddress,
3378
3379 // Use ArtMethod* at an address that will be known at link time, embed the direct
3380 // address in the code. If the image is relocatable, emit .patch_oat entry.
3381 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3382 // the image relocatable or not.
3383 kDirectAddressWithFixup,
3384
3385 // Load from resoved methods array in the dex cache using a PC-relative load.
3386 // Used when we need to use the dex cache, for example for invoke-static that
3387 // may cause class initialization (the entry may point to a resolution method),
3388 // and we know that we can access the dex cache arrays using a PC-relative load.
3389 kDexCachePcRelative,
3390
3391 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3392 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3393 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3394 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3395 kDexCacheViaMethod,
3396 };
3397
3398 // Determines the location of the code pointer.
3399 enum class CodePtrLocation {
3400 // Recursive call, use local PC-relative call instruction.
3401 kCallSelf,
3402
3403 // Use PC-relative call instruction patched at link time.
3404 // Used for calls within an oat file, boot->boot or app->app.
3405 kCallPCRelative,
3406
3407 // Call to a known target address, embed the direct address in code.
3408 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3409 kCallDirect,
3410
3411 // Call to a target address that will be known at link time, embed the direct
3412 // address in code. If the image is relocatable, emit .patch_oat entry.
3413 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3414 // the image relocatable or not.
3415 kCallDirectWithFixup,
3416
3417 // Use code pointer from the ArtMethod*.
3418 // Used when we don't know the target code. This is also the last-resort-kind used when
3419 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3420 kCallArtMethod,
3421 };
3422
3423 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003424 MethodLoadKind method_load_kind;
3425 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003426 // The method load data holds
3427 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3428 // Note that there are multiple string init methods, each having its own offset.
3429 // - the method address for kDirectAddress
3430 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003431 uint64_t method_load_data;
3432 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003433 };
3434
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003435 HInvokeStaticOrDirect(ArenaAllocator* arena,
3436 uint32_t number_of_arguments,
3437 Primitive::Type return_type,
3438 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003439 uint32_t method_index,
3440 MethodReference target_method,
3441 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003442 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003443 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003444 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003445 : HInvoke(arena,
3446 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003447 // There is potentially one extra argument for the HCurrentMethod node, and
3448 // potentially one other if the clinit check is explicit, and potentially
3449 // one other if the method is a string factory.
3450 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3451 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3452 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003453 return_type,
3454 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003455 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003456 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003457 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003458 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003459 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003460 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003461
Vladimir Markodc151b22015-10-15 18:02:30 +01003462 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003463 bool had_current_method_input = HasCurrentMethodInput();
3464 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3465
3466 // Using the current method is the default and once we find a better
3467 // method load kind, we should not go back to using the current method.
3468 DCHECK(had_current_method_input || !needs_current_method_input);
3469
3470 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003471 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3472 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003473 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003474 dispatch_info_ = dispatch_info;
3475 }
3476
Vladimir Markoc53c0792015-11-19 15:48:33 +00003477 void AddSpecialInput(HInstruction* input) {
3478 // We allow only one special input.
3479 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3480 DCHECK(InputCount() == GetSpecialInputIndex() ||
3481 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3482 InsertInputAt(GetSpecialInputIndex(), input);
3483 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003484
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003485 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003486 // We access the method via the dex cache so we can't do an implicit null check.
3487 // TODO: for intrinsics we can generate implicit null checks.
3488 return false;
3489 }
3490
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003491 bool CanBeNull() const OVERRIDE {
3492 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3493 }
3494
Vladimir Markoc53c0792015-11-19 15:48:33 +00003495 // Get the index of the special input, if any.
3496 //
3497 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3498 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3499 // the "special input" is the current method pointer; otherwise there may be one
3500 // platform-specific special input, such as PC-relative addressing base.
3501 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3502
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003503 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3504 void SetOptimizedInvokeType(InvokeType invoke_type) {
3505 optimized_invoke_type_ = invoke_type;
3506 }
3507
Vladimir Marko58155012015-08-19 12:49:41 +00003508 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3509 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3510 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003511 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003512 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003513 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003514 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003515 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3516 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003517 bool HasCurrentMethodInput() const {
3518 // This function can be called only after the invoke has been fully initialized by the builder.
3519 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003520 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003521 return true;
3522 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003523 DCHECK(InputCount() == GetSpecialInputIndex() ||
3524 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003525 return false;
3526 }
3527 }
Vladimir Marko58155012015-08-19 12:49:41 +00003528 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3529 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003530 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003531
3532 int32_t GetStringInitOffset() const {
3533 DCHECK(IsStringInit());
3534 return dispatch_info_.method_load_data;
3535 }
3536
3537 uint64_t GetMethodAddress() const {
3538 DCHECK(HasMethodAddress());
3539 return dispatch_info_.method_load_data;
3540 }
3541
3542 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003543 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003544 return dispatch_info_.method_load_data;
3545 }
3546
3547 uint64_t GetDirectCodePtr() const {
3548 DCHECK(HasDirectCodePtr());
3549 return dispatch_info_.direct_code_ptr;
3550 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003551
Calin Juravle68ad6492015-08-18 17:08:12 +01003552 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3553
Roland Levillain4c0eb422015-04-24 16:43:49 +01003554 // Is this instruction a call to a static method?
3555 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003556 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003557 }
3558
Vladimir Markofbb184a2015-11-13 14:47:00 +00003559 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3560 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3561 // instruction; only relevant for static calls with explicit clinit check.
3562 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003563 DCHECK(IsStaticWithExplicitClinitCheck());
3564 size_t last_input_index = InputCount() - 1;
3565 HInstruction* last_input = InputAt(last_input_index);
3566 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003567 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003568 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003569 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003570 clinit_check_requirement_ = new_requirement;
3571 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003572 }
3573
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003574 bool IsStringFactoryFor(HFakeString* str) const {
3575 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003576 DCHECK(!HasCurrentMethodInput());
3577 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003578 return InputAt(InputCount() - 1)->AsFakeString() == str;
3579 }
3580
3581 void RemoveFakeStringArgumentAsLastInput() {
3582 DCHECK(IsStringInit());
3583 size_t last_input_index = InputCount() - 1;
3584 HInstruction* last_input = InputAt(last_input_index);
3585 DCHECK(last_input != nullptr);
3586 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3587 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003588 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003589 }
3590
Roland Levillain4c0eb422015-04-24 16:43:49 +01003591 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003592 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003593 bool IsStaticWithExplicitClinitCheck() const {
3594 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3595 }
3596
3597 // Is this a call to a static method whose declaring class has an
3598 // implicit intialization check requirement?
3599 bool IsStaticWithImplicitClinitCheck() const {
3600 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3601 }
3602
Vladimir Markob554b5a2015-11-06 12:57:55 +00003603 // Does this method load kind need the current method as an input?
3604 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3605 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3606 }
3607
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003608 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003609
Roland Levillain4c0eb422015-04-24 16:43:49 +01003610 protected:
3611 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3612 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3613 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3614 HInstruction* input = input_record.GetInstruction();
3615 // `input` is the last input of a static invoke marked as having
3616 // an explicit clinit check. It must either be:
3617 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3618 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3619 DCHECK(input != nullptr);
3620 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3621 }
3622 return input_record;
3623 }
3624
Vladimir Markoc53c0792015-11-19 15:48:33 +00003625 void InsertInputAt(size_t index, HInstruction* input);
3626 void RemoveInputAt(size_t index);
3627
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003628 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003629 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003630 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003631 // The target method may refer to different dex file or method index than the original
3632 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3633 // in derived class to increase visibility.
3634 MethodReference target_method_;
3635 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003636
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003637 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003638};
Vladimir Markof64242a2015-12-01 14:58:23 +00003639std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003640std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003641
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003642class HInvokeVirtual : public HInvoke {
3643 public:
3644 HInvokeVirtual(ArenaAllocator* arena,
3645 uint32_t number_of_arguments,
3646 Primitive::Type return_type,
3647 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003648 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003649 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003650 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003651 vtable_index_(vtable_index) {}
3652
Calin Juravle641547a2015-04-21 22:08:51 +01003653 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003654 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003655 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003656 }
3657
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003658 uint32_t GetVTableIndex() const { return vtable_index_; }
3659
3660 DECLARE_INSTRUCTION(InvokeVirtual);
3661
3662 private:
3663 const uint32_t vtable_index_;
3664
3665 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3666};
3667
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003668class HInvokeInterface : public HInvoke {
3669 public:
3670 HInvokeInterface(ArenaAllocator* arena,
3671 uint32_t number_of_arguments,
3672 Primitive::Type return_type,
3673 uint32_t dex_pc,
3674 uint32_t dex_method_index,
3675 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003676 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003677 imt_index_(imt_index) {}
3678
Calin Juravle641547a2015-04-21 22:08:51 +01003679 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003680 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003681 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003682 }
3683
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003684 uint32_t GetImtIndex() const { return imt_index_; }
3685 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3686
3687 DECLARE_INSTRUCTION(InvokeInterface);
3688
3689 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003690 const uint32_t imt_index_;
3691
3692 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3693};
3694
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003695class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003696 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003697 HNewInstance(HInstruction* cls,
3698 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003699 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003700 uint16_t type_index,
3701 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003702 bool can_throw,
3703 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003704 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003705 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003706 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003707 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003708 can_throw_(can_throw),
3709 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003710 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003711 SetRawInputAt(0, cls);
3712 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003713 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003714
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003715 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003716 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003717
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003718 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003719 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003720
3721 // It may throw when called on type that's not instantiable/accessible.
3722 // It can throw OOME.
3723 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3724 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3725
3726 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003727
Calin Juravle10e244f2015-01-26 18:54:32 +00003728 bool CanBeNull() const OVERRIDE { return false; }
3729
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003730 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3731
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003732 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3733 entrypoint_ = entrypoint;
3734 }
3735
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003736 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003737
3738 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003739 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003740 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003741 const bool can_throw_;
3742 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003743 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003744
3745 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3746};
3747
Roland Levillain88cb1752014-10-20 16:36:47 +01003748class HNeg : public HUnaryOperation {
3749 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003750 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3751 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003752
Roland Levillain9867bc72015-08-05 10:21:34 +01003753 template <typename T> T Compute(T x) const { return -x; }
3754
3755 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003756 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003757 }
3758 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003759 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003760 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003761
Roland Levillain88cb1752014-10-20 16:36:47 +01003762 DECLARE_INSTRUCTION(Neg);
3763
3764 private:
3765 DISALLOW_COPY_AND_ASSIGN(HNeg);
3766};
3767
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003768class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003769 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003770 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003771 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003772 uint32_t dex_pc,
3773 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003774 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003775 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003776 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003777 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003778 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003779 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003780 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003781 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003782 }
3783
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003784 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003785 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003786
3787 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003788 bool NeedsEnvironment() const OVERRIDE { return true; }
3789
Mingyao Yang0c365e62015-03-31 15:09:29 -07003790 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3791 bool CanThrow() const OVERRIDE { return true; }
3792
Calin Juravle10e244f2015-01-26 18:54:32 +00003793 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003794
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003795 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3796
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003797 DECLARE_INSTRUCTION(NewArray);
3798
3799 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003800 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003801 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003802 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003803
3804 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3805};
3806
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003807class HAdd : public HBinaryOperation {
3808 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003809 HAdd(Primitive::Type result_type,
3810 HInstruction* left,
3811 HInstruction* right,
3812 uint32_t dex_pc = kNoDexPc)
3813 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003815 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003816
Roland Levillain9867bc72015-08-05 10:21:34 +01003817 template <typename T> T Compute(T x, T y) const { return x + y; }
3818
3819 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003820 return GetBlock()->GetGraph()->GetIntConstant(
3821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003822 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003823 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003824 return GetBlock()->GetGraph()->GetLongConstant(
3825 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003826 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003827
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003828 DECLARE_INSTRUCTION(Add);
3829
3830 private:
3831 DISALLOW_COPY_AND_ASSIGN(HAdd);
3832};
3833
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003834class HSub : public HBinaryOperation {
3835 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003836 HSub(Primitive::Type result_type,
3837 HInstruction* left,
3838 HInstruction* right,
3839 uint32_t dex_pc = kNoDexPc)
3840 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003841
Roland Levillain9867bc72015-08-05 10:21:34 +01003842 template <typename T> T Compute(T x, T y) const { return x - y; }
3843
3844 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003845 return GetBlock()->GetGraph()->GetIntConstant(
3846 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003847 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003848 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003849 return GetBlock()->GetGraph()->GetLongConstant(
3850 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003851 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003852
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003853 DECLARE_INSTRUCTION(Sub);
3854
3855 private:
3856 DISALLOW_COPY_AND_ASSIGN(HSub);
3857};
3858
Calin Juravle34bacdf2014-10-07 20:23:36 +01003859class HMul : public HBinaryOperation {
3860 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003861 HMul(Primitive::Type result_type,
3862 HInstruction* left,
3863 HInstruction* right,
3864 uint32_t dex_pc = kNoDexPc)
3865 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003866
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003867 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003868
Roland Levillain9867bc72015-08-05 10:21:34 +01003869 template <typename T> T Compute(T x, T y) const { return x * y; }
3870
3871 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003872 return GetBlock()->GetGraph()->GetIntConstant(
3873 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003874 }
3875 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003876 return GetBlock()->GetGraph()->GetLongConstant(
3877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003878 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003879
3880 DECLARE_INSTRUCTION(Mul);
3881
3882 private:
3883 DISALLOW_COPY_AND_ASSIGN(HMul);
3884};
3885
Calin Juravle7c4954d2014-10-28 16:57:40 +00003886class HDiv : public HBinaryOperation {
3887 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003888 HDiv(Primitive::Type result_type,
3889 HInstruction* left,
3890 HInstruction* right,
3891 uint32_t dex_pc)
3892 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003893
Roland Levillain9867bc72015-08-05 10:21:34 +01003894 template <typename T>
3895 T Compute(T x, T y) const {
3896 // Our graph structure ensures we never have 0 for `y` during
3897 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003898 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003899 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003900 return (y == -1) ? -x : x / y;
3901 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003902
Roland Levillain9867bc72015-08-05 10:21:34 +01003903 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003904 return GetBlock()->GetGraph()->GetIntConstant(
3905 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003906 }
3907 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003908 return GetBlock()->GetGraph()->GetLongConstant(
3909 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003910 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003911
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003912 static SideEffects SideEffectsForArchRuntimeCalls() {
3913 // The generated code can use a runtime call.
3914 return SideEffects::CanTriggerGC();
3915 }
3916
Calin Juravle7c4954d2014-10-28 16:57:40 +00003917 DECLARE_INSTRUCTION(Div);
3918
3919 private:
3920 DISALLOW_COPY_AND_ASSIGN(HDiv);
3921};
3922
Calin Juravlebacfec32014-11-14 15:54:36 +00003923class HRem : public HBinaryOperation {
3924 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003925 HRem(Primitive::Type result_type,
3926 HInstruction* left,
3927 HInstruction* right,
3928 uint32_t dex_pc)
3929 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003930
Roland Levillain9867bc72015-08-05 10:21:34 +01003931 template <typename T>
3932 T Compute(T x, T y) const {
3933 // Our graph structure ensures we never have 0 for `y` during
3934 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003935 DCHECK_NE(y, 0);
3936 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3937 return (y == -1) ? 0 : x % y;
3938 }
3939
Roland Levillain9867bc72015-08-05 10:21:34 +01003940 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003941 return GetBlock()->GetGraph()->GetIntConstant(
3942 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003943 }
3944 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003945 return GetBlock()->GetGraph()->GetLongConstant(
3946 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003947 }
3948
Calin Juravlebacfec32014-11-14 15:54:36 +00003949
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003950 static SideEffects SideEffectsForArchRuntimeCalls() {
3951 return SideEffects::CanTriggerGC();
3952 }
3953
Calin Juravlebacfec32014-11-14 15:54:36 +00003954 DECLARE_INSTRUCTION(Rem);
3955
3956 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003957 DISALLOW_COPY_AND_ASSIGN(HRem);
3958};
3959
Calin Juravled0d48522014-11-04 16:40:20 +00003960class HDivZeroCheck : public HExpression<1> {
3961 public:
3962 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003963 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003964 SetRawInputAt(0, value);
3965 }
3966
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003967 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3968
Calin Juravled0d48522014-11-04 16:40:20 +00003969 bool CanBeMoved() const OVERRIDE { return true; }
3970
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003971 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003972 return true;
3973 }
3974
3975 bool NeedsEnvironment() const OVERRIDE { return true; }
3976 bool CanThrow() const OVERRIDE { return true; }
3977
Calin Juravled0d48522014-11-04 16:40:20 +00003978 DECLARE_INSTRUCTION(DivZeroCheck);
3979
3980 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003981 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3982};
3983
Calin Juravle9aec02f2014-11-18 23:06:35 +00003984class HShl : public HBinaryOperation {
3985 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003986 HShl(Primitive::Type result_type,
3987 HInstruction* left,
3988 HInstruction* right,
3989 uint32_t dex_pc = kNoDexPc)
3990 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003991
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 template <typename T, typename U, typename V>
3993 T Compute(T x, U y, V max_shift_value) const {
3994 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3995 "V is not the unsigned integer type corresponding to T");
3996 return x << (y & max_shift_value);
3997 }
3998
3999 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4000 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004001 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004002 }
4003 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4004 // case is handled as `x << static_cast<int>(y)`.
4005 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4006 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004007 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004008 }
4009 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4010 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004011 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004012 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004013
4014 DECLARE_INSTRUCTION(Shl);
4015
4016 private:
4017 DISALLOW_COPY_AND_ASSIGN(HShl);
4018};
4019
4020class HShr : public HBinaryOperation {
4021 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004022 HShr(Primitive::Type result_type,
4023 HInstruction* left,
4024 HInstruction* right,
4025 uint32_t dex_pc = kNoDexPc)
4026 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004027
Roland Levillain9867bc72015-08-05 10:21:34 +01004028 template <typename T, typename U, typename V>
4029 T Compute(T x, U y, V max_shift_value) const {
4030 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4031 "V is not the unsigned integer type corresponding to T");
4032 return x >> (y & max_shift_value);
4033 }
4034
4035 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4036 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004037 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004038 }
4039 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4040 // case is handled as `x >> static_cast<int>(y)`.
4041 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4042 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004043 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004044 }
4045 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4046 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004047 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004048 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004049
4050 DECLARE_INSTRUCTION(Shr);
4051
4052 private:
4053 DISALLOW_COPY_AND_ASSIGN(HShr);
4054};
4055
4056class HUShr : public HBinaryOperation {
4057 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004058 HUShr(Primitive::Type result_type,
4059 HInstruction* left,
4060 HInstruction* right,
4061 uint32_t dex_pc = kNoDexPc)
4062 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004063
Roland Levillain9867bc72015-08-05 10:21:34 +01004064 template <typename T, typename U, typename V>
4065 T Compute(T x, U y, V max_shift_value) const {
4066 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4067 "V is not the unsigned integer type corresponding to T");
4068 V ux = static_cast<V>(x);
4069 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004070 }
4071
Roland Levillain9867bc72015-08-05 10:21:34 +01004072 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4073 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004074 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004075 }
4076 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4077 // case is handled as `x >>> static_cast<int>(y)`.
4078 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4079 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004080 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004081 }
4082 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4083 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004084 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004085 }
4086
4087 DECLARE_INSTRUCTION(UShr);
4088
4089 private:
4090 DISALLOW_COPY_AND_ASSIGN(HUShr);
4091};
4092
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004093class HAnd : public HBinaryOperation {
4094 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004095 HAnd(Primitive::Type result_type,
4096 HInstruction* left,
4097 HInstruction* right,
4098 uint32_t dex_pc = kNoDexPc)
4099 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004100
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004101 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004102
Roland Levillain9867bc72015-08-05 10:21:34 +01004103 template <typename T, typename U>
4104 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4105
4106 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004107 return GetBlock()->GetGraph()->GetIntConstant(
4108 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004109 }
4110 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004111 return GetBlock()->GetGraph()->GetLongConstant(
4112 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004113 }
4114 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004115 return GetBlock()->GetGraph()->GetLongConstant(
4116 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004117 }
4118 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004119 return GetBlock()->GetGraph()->GetLongConstant(
4120 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004121 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004122
4123 DECLARE_INSTRUCTION(And);
4124
4125 private:
4126 DISALLOW_COPY_AND_ASSIGN(HAnd);
4127};
4128
4129class HOr : public HBinaryOperation {
4130 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004131 HOr(Primitive::Type result_type,
4132 HInstruction* left,
4133 HInstruction* right,
4134 uint32_t dex_pc = kNoDexPc)
4135 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004136
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004137 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004138
Roland Levillain9867bc72015-08-05 10:21:34 +01004139 template <typename T, typename U>
4140 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4141
4142 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004143 return GetBlock()->GetGraph()->GetIntConstant(
4144 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004145 }
4146 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004147 return GetBlock()->GetGraph()->GetLongConstant(
4148 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004149 }
4150 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004151 return GetBlock()->GetGraph()->GetLongConstant(
4152 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004153 }
4154 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004155 return GetBlock()->GetGraph()->GetLongConstant(
4156 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004157 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004158
4159 DECLARE_INSTRUCTION(Or);
4160
4161 private:
4162 DISALLOW_COPY_AND_ASSIGN(HOr);
4163};
4164
4165class HXor : public HBinaryOperation {
4166 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004167 HXor(Primitive::Type result_type,
4168 HInstruction* left,
4169 HInstruction* right,
4170 uint32_t dex_pc = kNoDexPc)
4171 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004172
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004173 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004174
Roland Levillain9867bc72015-08-05 10:21:34 +01004175 template <typename T, typename U>
4176 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4177
4178 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004179 return GetBlock()->GetGraph()->GetIntConstant(
4180 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004181 }
4182 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004183 return GetBlock()->GetGraph()->GetLongConstant(
4184 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004185 }
4186 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004187 return GetBlock()->GetGraph()->GetLongConstant(
4188 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004189 }
4190 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004191 return GetBlock()->GetGraph()->GetLongConstant(
4192 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004193 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004194
4195 DECLARE_INSTRUCTION(Xor);
4196
4197 private:
4198 DISALLOW_COPY_AND_ASSIGN(HXor);
4199};
4200
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004201// The value of a parameter in this method. Its location depends on
4202// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004203class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004204 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004205 HParameterValue(const DexFile& dex_file,
4206 uint16_t type_index,
4207 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004208 Primitive::Type parameter_type,
4209 bool is_this = false)
4210 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004211 dex_file_(dex_file),
4212 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004213 index_(index),
4214 is_this_(is_this),
4215 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004216
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004217 const DexFile& GetDexFile() const { return dex_file_; }
4218 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004219 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004220 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004221
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004222 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4223 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004224
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004225 DECLARE_INSTRUCTION(ParameterValue);
4226
4227 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004228 const DexFile& dex_file_;
4229 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004230 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004231 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004232 const uint8_t index_;
4233
Calin Juravle10e244f2015-01-26 18:54:32 +00004234 // Whether or not the parameter value corresponds to 'this' argument.
4235 const bool is_this_;
4236
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004237 bool can_be_null_;
4238
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004239 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4240};
4241
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004242class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004243 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004244 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4245 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004246
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004247 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004248 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004249 return true;
4250 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004251
Roland Levillain9867bc72015-08-05 10:21:34 +01004252 template <typename T> T Compute(T x) const { return ~x; }
4253
4254 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004255 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004256 }
4257 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004258 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004259 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004260
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004261 DECLARE_INSTRUCTION(Not);
4262
4263 private:
4264 DISALLOW_COPY_AND_ASSIGN(HNot);
4265};
4266
David Brazdil66d126e2015-04-03 16:02:44 +01004267class HBooleanNot : public HUnaryOperation {
4268 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004269 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4270 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004271
4272 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004273 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004274 return true;
4275 }
4276
Roland Levillain9867bc72015-08-05 10:21:34 +01004277 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004278 DCHECK(IsUint<1>(x));
4279 return !x;
4280 }
4281
Roland Levillain9867bc72015-08-05 10:21:34 +01004282 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004283 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004284 }
4285 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4286 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004287 UNREACHABLE();
4288 }
4289
4290 DECLARE_INSTRUCTION(BooleanNot);
4291
4292 private:
4293 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4294};
4295
Roland Levillaindff1f282014-11-05 14:15:05 +00004296class HTypeConversion : public HExpression<1> {
4297 public:
4298 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004299 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004300 : HExpression(result_type,
4301 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4302 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004303 SetRawInputAt(0, input);
4304 DCHECK_NE(input->GetType(), result_type);
4305 }
4306
4307 HInstruction* GetInput() const { return InputAt(0); }
4308 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4309 Primitive::Type GetResultType() const { return GetType(); }
4310
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004311 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004312 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004313
Roland Levillaindff1f282014-11-05 14:15:05 +00004314 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004315 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004316
Mark Mendelle82549b2015-05-06 10:55:34 -04004317 // Try to statically evaluate the conversion and return a HConstant
4318 // containing the result. If the input cannot be converted, return nullptr.
4319 HConstant* TryStaticEvaluation() const;
4320
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004321 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4322 Primitive::Type result_type) {
4323 // Some architectures may not require the 'GC' side effects, but at this point
4324 // in the compilation process we do not know what architecture we will
4325 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004326 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4327 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004328 return SideEffects::CanTriggerGC();
4329 }
4330 return SideEffects::None();
4331 }
4332
Roland Levillaindff1f282014-11-05 14:15:05 +00004333 DECLARE_INSTRUCTION(TypeConversion);
4334
4335 private:
4336 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4337};
4338
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004339static constexpr uint32_t kNoRegNumber = -1;
4340
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004341class HPhi : public HInstruction {
4342 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004343 HPhi(ArenaAllocator* arena,
4344 uint32_t reg_number,
4345 size_t number_of_inputs,
4346 Primitive::Type type,
4347 uint32_t dex_pc = kNoDexPc)
4348 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004349 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004350 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004351 type_(ToPhiType(type)),
4352 // Phis are constructed live and marked dead if conflicting or unused.
4353 // Individual steps of SsaBuilder should assume that if a phi has been
4354 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4355 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004356 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004357 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004358 }
4359
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004360 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4361 static Primitive::Type ToPhiType(Primitive::Type type) {
4362 switch (type) {
4363 case Primitive::kPrimBoolean:
4364 case Primitive::kPrimByte:
4365 case Primitive::kPrimShort:
4366 case Primitive::kPrimChar:
4367 return Primitive::kPrimInt;
4368 default:
4369 return type;
4370 }
4371 }
4372
David Brazdilffee3d32015-07-06 11:48:53 +01004373 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4374
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004375 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004376
4377 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004378 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004379
Calin Juravle10e244f2015-01-26 18:54:32 +00004380 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004381 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004382
Calin Juravle10e244f2015-01-26 18:54:32 +00004383 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4384 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4385
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004386 uint32_t GetRegNumber() const { return reg_number_; }
4387
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004388 void SetDead() { is_live_ = false; }
4389 void SetLive() { is_live_ = true; }
4390 bool IsDead() const { return !is_live_; }
4391 bool IsLive() const { return is_live_; }
4392
David Brazdil77a48ae2015-09-15 12:34:04 +00004393 bool IsVRegEquivalentOf(HInstruction* other) const {
4394 return other != nullptr
4395 && other->IsPhi()
4396 && other->AsPhi()->GetBlock() == GetBlock()
4397 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4398 }
4399
Calin Juravlea4f88312015-04-16 12:57:19 +01004400 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4401 // An equivalent phi is a phi having the same dex register and type.
4402 // It assumes that phis with the same dex register are adjacent.
4403 HPhi* GetNextEquivalentPhiWithSameType() {
4404 HInstruction* next = GetNext();
4405 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4406 if (next->GetType() == GetType()) {
4407 return next->AsPhi();
4408 }
4409 next = next->GetNext();
4410 }
4411 return nullptr;
4412 }
4413
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004414 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004415
David Brazdil1abb4192015-02-17 18:33:36 +00004416 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004417 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004418 return inputs_[index];
4419 }
David Brazdil1abb4192015-02-17 18:33:36 +00004420
4421 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004422 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004423 }
4424
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004425 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004426 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004427 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004428 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004429 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004430 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004431
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004432 DISALLOW_COPY_AND_ASSIGN(HPhi);
4433};
4434
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004435class HNullCheck : public HExpression<1> {
4436 public:
4437 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004438 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004439 SetRawInputAt(0, value);
4440 }
4441
Calin Juravle10e244f2015-01-26 18:54:32 +00004442 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004443 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004444 return true;
4445 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004446
Calin Juravle10e244f2015-01-26 18:54:32 +00004447 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004448
Calin Juravle10e244f2015-01-26 18:54:32 +00004449 bool CanThrow() const OVERRIDE { return true; }
4450
4451 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004452
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004453
4454 DECLARE_INSTRUCTION(NullCheck);
4455
4456 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004457 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4458};
4459
4460class FieldInfo : public ValueObject {
4461 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004462 FieldInfo(MemberOffset field_offset,
4463 Primitive::Type field_type,
4464 bool is_volatile,
4465 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004466 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004467 const DexFile& dex_file,
4468 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004469 : field_offset_(field_offset),
4470 field_type_(field_type),
4471 is_volatile_(is_volatile),
4472 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004473 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004474 dex_file_(dex_file),
4475 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476
4477 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004478 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004479 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004480 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004481 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004482 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004483 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484
4485 private:
4486 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004487 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004488 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004489 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004490 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004491 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004492 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004493};
4494
4495class HInstanceFieldGet : public HExpression<1> {
4496 public:
4497 HInstanceFieldGet(HInstruction* value,
4498 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004499 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004500 bool is_volatile,
4501 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004502 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004503 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004504 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004505 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004506 : HExpression(field_type,
4507 SideEffects::FieldReadOfType(field_type, is_volatile),
4508 dex_pc),
4509 field_info_(field_offset,
4510 field_type,
4511 is_volatile,
4512 field_idx,
4513 declaring_class_def_index,
4514 dex_file,
4515 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004516 SetRawInputAt(0, value);
4517 }
4518
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004519 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004520
4521 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4522 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4523 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004524 }
4525
Calin Juravle641547a2015-04-21 22:08:51 +01004526 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4527 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004528 }
4529
4530 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004531 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4532 }
4533
Calin Juravle52c48962014-12-16 17:02:57 +00004534 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004535 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004536 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004537 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004538
4539 DECLARE_INSTRUCTION(InstanceFieldGet);
4540
4541 private:
4542 const FieldInfo field_info_;
4543
4544 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4545};
4546
4547class HInstanceFieldSet : public HTemplateInstruction<2> {
4548 public:
4549 HInstanceFieldSet(HInstruction* object,
4550 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004551 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004552 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004553 bool is_volatile,
4554 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004555 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004556 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004557 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004558 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004559 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4560 dex_pc),
4561 field_info_(field_offset,
4562 field_type,
4563 is_volatile,
4564 field_idx,
4565 declaring_class_def_index,
4566 dex_file,
4567 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004568 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004569 SetRawInputAt(0, object);
4570 SetRawInputAt(1, value);
4571 }
4572
Calin Juravle641547a2015-04-21 22:08:51 +01004573 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4574 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004575 }
4576
Calin Juravle52c48962014-12-16 17:02:57 +00004577 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004578 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004579 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004580 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004581 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004582 bool GetValueCanBeNull() const { return value_can_be_null_; }
4583 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004584
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004585 DECLARE_INSTRUCTION(InstanceFieldSet);
4586
4587 private:
4588 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004589 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004590
4591 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4592};
4593
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594class HArrayGet : public HExpression<2> {
4595 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004596 HArrayGet(HInstruction* array,
4597 HInstruction* index,
4598 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004599 uint32_t dex_pc,
4600 SideEffects additional_side_effects = SideEffects::None())
4601 : HExpression(type,
4602 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004603 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004604 SetRawInputAt(0, array);
4605 SetRawInputAt(1, index);
4606 }
4607
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004608 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004609 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004610 return true;
4611 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004612 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004613 // TODO: We can be smarter here.
4614 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4615 // which generates the implicit null check. There are cases when these can be removed
4616 // to produce better code. If we ever add optimizations to do so we should allow an
4617 // implicit check here (as long as the address falls in the first page).
4618 return false;
4619 }
4620
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004621 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004622
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004623 HInstruction* GetArray() const { return InputAt(0); }
4624 HInstruction* GetIndex() const { return InputAt(1); }
4625
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004626 DECLARE_INSTRUCTION(ArrayGet);
4627
4628 private:
4629 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4630};
4631
4632class HArraySet : public HTemplateInstruction<3> {
4633 public:
4634 HArraySet(HInstruction* array,
4635 HInstruction* index,
4636 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004637 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004638 uint32_t dex_pc,
4639 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004640 : HTemplateInstruction(
4641 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004642 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4643 additional_side_effects),
4644 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004645 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004646 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004647 value_can_be_null_(true),
4648 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004649 SetRawInputAt(0, array);
4650 SetRawInputAt(1, index);
4651 SetRawInputAt(2, value);
4652 }
4653
Calin Juravle77520bc2015-01-12 18:45:46 +00004654 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004655 // We currently always call a runtime method to catch array store
4656 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004657 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004658 }
4659
Mingyao Yang81014cb2015-06-02 03:16:27 -07004660 // Can throw ArrayStoreException.
4661 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4662
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004663 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004664 // TODO: Same as for ArrayGet.
4665 return false;
4666 }
4667
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004668 void ClearNeedsTypeCheck() {
4669 needs_type_check_ = false;
4670 }
4671
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004672 void ClearValueCanBeNull() {
4673 value_can_be_null_ = false;
4674 }
4675
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004676 void SetStaticTypeOfArrayIsObjectArray() {
4677 static_type_of_array_is_object_array_ = true;
4678 }
4679
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004680 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004681 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004682 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004683
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004684 HInstruction* GetArray() const { return InputAt(0); }
4685 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004686 HInstruction* GetValue() const { return InputAt(2); }
4687
4688 Primitive::Type GetComponentType() const {
4689 // The Dex format does not type floating point index operations. Since the
4690 // `expected_component_type_` is set during building and can therefore not
4691 // be correct, we also check what is the value type. If it is a floating
4692 // point type, we must use that type.
4693 Primitive::Type value_type = GetValue()->GetType();
4694 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4695 ? value_type
4696 : expected_component_type_;
4697 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004698
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004699 Primitive::Type GetRawExpectedComponentType() const {
4700 return expected_component_type_;
4701 }
4702
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004703 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4704 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4705 }
4706
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004707 DECLARE_INSTRUCTION(ArraySet);
4708
4709 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004710 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004711 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004712 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004713 // Cached information for the reference_type_info_ so that codegen
4714 // does not need to inspect the static type.
4715 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004716
4717 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4718};
4719
4720class HArrayLength : public HExpression<1> {
4721 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004722 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004723 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004724 // Note that arrays do not change length, so the instruction does not
4725 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004726 SetRawInputAt(0, array);
4727 }
4728
Calin Juravle77520bc2015-01-12 18:45:46 +00004729 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004730 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004731 return true;
4732 }
Calin Juravle641547a2015-04-21 22:08:51 +01004733 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4734 return obj == InputAt(0);
4735 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004736
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737 DECLARE_INSTRUCTION(ArrayLength);
4738
4739 private:
4740 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4741};
4742
4743class HBoundsCheck : public HExpression<2> {
4744 public:
4745 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004746 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004747 DCHECK(index->GetType() == Primitive::kPrimInt);
4748 SetRawInputAt(0, index);
4749 SetRawInputAt(1, length);
4750 }
4751
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004752 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004753 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004754 return true;
4755 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004756
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004757 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004758
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004759 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004760
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004761 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004762
4763 DECLARE_INSTRUCTION(BoundsCheck);
4764
4765 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004766 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4767};
4768
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004769/**
4770 * Some DEX instructions are folded into multiple HInstructions that need
4771 * to stay live until the last HInstruction. This class
4772 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004773 * HInstruction stays live. `index` represents the stack location index of the
4774 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004775 */
4776class HTemporary : public HTemplateInstruction<0> {
4777 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004778 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4779 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004780
4781 size_t GetIndex() const { return index_; }
4782
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004783 Primitive::Type GetType() const OVERRIDE {
4784 // The previous instruction is the one that will be stored in the temporary location.
4785 DCHECK(GetPrevious() != nullptr);
4786 return GetPrevious()->GetType();
4787 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004788
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004789 DECLARE_INSTRUCTION(Temporary);
4790
4791 private:
4792 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004793 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4794};
4795
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004796class HSuspendCheck : public HTemplateInstruction<0> {
4797 public:
4798 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004799 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004800
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004801 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004802 return true;
4803 }
4804
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004805 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4806 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004807
4808 DECLARE_INSTRUCTION(SuspendCheck);
4809
4810 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004811 // Only used for code generation, in order to share the same slow path between back edges
4812 // of a same loop.
4813 SlowPathCode* slow_path_;
4814
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004815 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4816};
4817
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004818/**
4819 * Instruction to load a Class object.
4820 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004821class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004822 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004823 HLoadClass(HCurrentMethod* current_method,
4824 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004825 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004826 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004827 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004828 bool needs_access_check,
4829 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004830 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004831 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004832 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004833 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004834 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004835 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004836 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004837 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004838 // Referrers class should not need access check. We never inline unverified
4839 // methods so we can't possibly end up in this situation.
4840 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004841 SetRawInputAt(0, current_method);
4842 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004843
4844 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004845
4846 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004847 // Note that we don't need to test for generate_clinit_check_.
4848 // Whether or not we need to generate the clinit check is processed in
4849 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004850 return other->AsLoadClass()->type_index_ == type_index_ &&
4851 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004852 }
4853
4854 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4855
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004856 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004857 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004858 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004859
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004860 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004861 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004862 }
4863
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004864 bool MustGenerateClinitCheck() const {
4865 return generate_clinit_check_;
4866 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004867
Calin Juravle0ba218d2015-05-19 18:46:01 +01004868 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004869 // The entrypoint the code generator is going to call does not do
4870 // clinit of the class.
4871 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004872 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004873 }
4874
4875 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004876 return MustGenerateClinitCheck() ||
4877 (!is_referrers_class_ && !is_in_dex_cache_) ||
4878 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004879 }
4880
4881 bool NeedsAccessCheck() const {
4882 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004883 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004884
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004885 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004886 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004887 }
4888
Calin Juravleacf735c2015-02-12 15:25:22 +00004889 ReferenceTypeInfo GetLoadedClassRTI() {
4890 return loaded_class_rti_;
4891 }
4892
4893 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4894 // Make sure we only set exact types (the loaded class should never be merged).
4895 DCHECK(rti.IsExact());
4896 loaded_class_rti_ = rti;
4897 }
4898
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004899 const DexFile& GetDexFile() { return dex_file_; }
4900
Vladimir Markodc151b22015-10-15 18:02:30 +01004901 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004902
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004903 static SideEffects SideEffectsForArchRuntimeCalls() {
4904 return SideEffects::CanTriggerGC();
4905 }
4906
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004907 bool IsInDexCache() const { return is_in_dex_cache_; }
4908
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004909 DECLARE_INSTRUCTION(LoadClass);
4910
4911 private:
4912 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004913 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004914 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004915 // Whether this instruction must generate the initialization check.
4916 // Used for code generation.
4917 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004918 const bool needs_access_check_;
4919 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004920
Calin Juravleacf735c2015-02-12 15:25:22 +00004921 ReferenceTypeInfo loaded_class_rti_;
4922
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004923 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4924};
4925
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004926class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004927 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004928 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004929 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4930 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004931 SetRawInputAt(0, current_method);
4932 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004933
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004934 bool CanBeMoved() const OVERRIDE { return true; }
4935
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004936 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4937 return other->AsLoadString()->string_index_ == string_index_;
4938 }
4939
4940 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4941
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004942 uint32_t GetStringIndex() const { return string_index_; }
4943
4944 // TODO: Can we deopt or debug when we resolve a string?
4945 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004946 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004947 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004948
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004949 static SideEffects SideEffectsForArchRuntimeCalls() {
4950 return SideEffects::CanTriggerGC();
4951 }
4952
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004953 DECLARE_INSTRUCTION(LoadString);
4954
4955 private:
4956 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004957
4958 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4959};
4960
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004961/**
4962 * Performs an initialization check on its Class object input.
4963 */
4964class HClinitCheck : public HExpression<1> {
4965 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004966 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004967 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004968 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004969 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4970 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004971 SetRawInputAt(0, constant);
4972 }
4973
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004974 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004975 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004976 return true;
4977 }
4978
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004979 bool NeedsEnvironment() const OVERRIDE {
4980 // May call runtime to initialize the class.
4981 return true;
4982 }
4983
Nicolas Geoffray729645a2015-11-19 13:29:02 +00004984 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004985
4986 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4987
4988 DECLARE_INSTRUCTION(ClinitCheck);
4989
4990 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004991 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4992};
4993
4994class HStaticFieldGet : public HExpression<1> {
4995 public:
4996 HStaticFieldGet(HInstruction* cls,
4997 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004998 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004999 bool is_volatile,
5000 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005001 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005002 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005003 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005004 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005005 : HExpression(field_type,
5006 SideEffects::FieldReadOfType(field_type, is_volatile),
5007 dex_pc),
5008 field_info_(field_offset,
5009 field_type,
5010 is_volatile,
5011 field_idx,
5012 declaring_class_def_index,
5013 dex_file,
5014 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005015 SetRawInputAt(0, cls);
5016 }
5017
Calin Juravle52c48962014-12-16 17:02:57 +00005018
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005019 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005020
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005021 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005022 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5023 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005024 }
5025
5026 size_t ComputeHashCode() const OVERRIDE {
5027 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5028 }
5029
Calin Juravle52c48962014-12-16 17:02:57 +00005030 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005031 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5032 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005033 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005034
5035 DECLARE_INSTRUCTION(StaticFieldGet);
5036
5037 private:
5038 const FieldInfo field_info_;
5039
5040 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5041};
5042
5043class HStaticFieldSet : public HTemplateInstruction<2> {
5044 public:
5045 HStaticFieldSet(HInstruction* cls,
5046 HInstruction* value,
5047 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005048 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005049 bool is_volatile,
5050 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005051 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005052 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005053 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005054 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005055 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5056 dex_pc),
5057 field_info_(field_offset,
5058 field_type,
5059 is_volatile,
5060 field_idx,
5061 declaring_class_def_index,
5062 dex_file,
5063 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005064 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005065 SetRawInputAt(0, cls);
5066 SetRawInputAt(1, value);
5067 }
5068
Calin Juravle52c48962014-12-16 17:02:57 +00005069 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005070 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5071 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005072 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005073
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005074 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005075 bool GetValueCanBeNull() const { return value_can_be_null_; }
5076 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005077
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005078 DECLARE_INSTRUCTION(StaticFieldSet);
5079
5080 private:
5081 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005082 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005083
5084 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5085};
5086
Calin Juravlee460d1d2015-09-29 04:52:17 +01005087class HUnresolvedInstanceFieldGet : public HExpression<1> {
5088 public:
5089 HUnresolvedInstanceFieldGet(HInstruction* obj,
5090 Primitive::Type field_type,
5091 uint32_t field_index,
5092 uint32_t dex_pc)
5093 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5094 field_index_(field_index) {
5095 SetRawInputAt(0, obj);
5096 }
5097
5098 bool NeedsEnvironment() const OVERRIDE { return true; }
5099 bool CanThrow() const OVERRIDE { return true; }
5100
5101 Primitive::Type GetFieldType() const { return GetType(); }
5102 uint32_t GetFieldIndex() const { return field_index_; }
5103
5104 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5105
5106 private:
5107 const uint32_t field_index_;
5108
5109 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5110};
5111
5112class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5113 public:
5114 HUnresolvedInstanceFieldSet(HInstruction* obj,
5115 HInstruction* value,
5116 Primitive::Type field_type,
5117 uint32_t field_index,
5118 uint32_t dex_pc)
5119 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5120 field_type_(field_type),
5121 field_index_(field_index) {
5122 DCHECK_EQ(field_type, value->GetType());
5123 SetRawInputAt(0, obj);
5124 SetRawInputAt(1, value);
5125 }
5126
5127 bool NeedsEnvironment() const OVERRIDE { return true; }
5128 bool CanThrow() const OVERRIDE { return true; }
5129
5130 Primitive::Type GetFieldType() const { return field_type_; }
5131 uint32_t GetFieldIndex() const { return field_index_; }
5132
5133 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5134
5135 private:
5136 const Primitive::Type field_type_;
5137 const uint32_t field_index_;
5138
5139 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5140};
5141
5142class HUnresolvedStaticFieldGet : public HExpression<0> {
5143 public:
5144 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5145 uint32_t field_index,
5146 uint32_t dex_pc)
5147 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5148 field_index_(field_index) {
5149 }
5150
5151 bool NeedsEnvironment() const OVERRIDE { return true; }
5152 bool CanThrow() const OVERRIDE { return true; }
5153
5154 Primitive::Type GetFieldType() const { return GetType(); }
5155 uint32_t GetFieldIndex() const { return field_index_; }
5156
5157 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5158
5159 private:
5160 const uint32_t field_index_;
5161
5162 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5163};
5164
5165class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5166 public:
5167 HUnresolvedStaticFieldSet(HInstruction* value,
5168 Primitive::Type field_type,
5169 uint32_t field_index,
5170 uint32_t dex_pc)
5171 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5172 field_type_(field_type),
5173 field_index_(field_index) {
5174 DCHECK_EQ(field_type, value->GetType());
5175 SetRawInputAt(0, value);
5176 }
5177
5178 bool NeedsEnvironment() const OVERRIDE { return true; }
5179 bool CanThrow() const OVERRIDE { return true; }
5180
5181 Primitive::Type GetFieldType() const { return field_type_; }
5182 uint32_t GetFieldIndex() const { return field_index_; }
5183
5184 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5185
5186 private:
5187 const Primitive::Type field_type_;
5188 const uint32_t field_index_;
5189
5190 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5191};
5192
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005193// Implement the move-exception DEX instruction.
5194class HLoadException : public HExpression<0> {
5195 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005196 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5197 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005198
David Brazdilbbd733e2015-08-18 17:48:17 +01005199 bool CanBeNull() const OVERRIDE { return false; }
5200
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005201 DECLARE_INSTRUCTION(LoadException);
5202
5203 private:
5204 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5205};
5206
David Brazdilcb1c0552015-08-04 16:22:25 +01005207// Implicit part of move-exception which clears thread-local exception storage.
5208// Must not be removed because the runtime expects the TLS to get cleared.
5209class HClearException : public HTemplateInstruction<0> {
5210 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005211 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5212 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005213
5214 DECLARE_INSTRUCTION(ClearException);
5215
5216 private:
5217 DISALLOW_COPY_AND_ASSIGN(HClearException);
5218};
5219
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005220class HThrow : public HTemplateInstruction<1> {
5221 public:
5222 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005223 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005224 SetRawInputAt(0, exception);
5225 }
5226
5227 bool IsControlFlow() const OVERRIDE { return true; }
5228
5229 bool NeedsEnvironment() const OVERRIDE { return true; }
5230
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005231 bool CanThrow() const OVERRIDE { return true; }
5232
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005233
5234 DECLARE_INSTRUCTION(Throw);
5235
5236 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005237 DISALLOW_COPY_AND_ASSIGN(HThrow);
5238};
5239
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005240/**
5241 * Implementation strategies for the code generator of a HInstanceOf
5242 * or `HCheckCast`.
5243 */
5244enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005245 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005246 kExactCheck, // Can do a single class compare.
5247 kClassHierarchyCheck, // Can just walk the super class chain.
5248 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5249 kInterfaceCheck, // No optimization yet when checking against an interface.
5250 kArrayObjectCheck, // Can just check if the array is not primitive.
5251 kArrayCheck // No optimization yet when checking against a generic array.
5252};
5253
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005254class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005255 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005256 HInstanceOf(HInstruction* object,
5257 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005258 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005259 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005260 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005261 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005262 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005263 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005264 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005265 SetRawInputAt(0, object);
5266 SetRawInputAt(1, constant);
5267 }
5268
5269 bool CanBeMoved() const OVERRIDE { return true; }
5270
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005271 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005272 return true;
5273 }
5274
5275 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005276 return false;
5277 }
5278
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005279 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5280
5281 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005282
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005283 // Used only in code generation.
5284 bool MustDoNullCheck() const { return must_do_null_check_; }
5285 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5286
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005287 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5288 return (check_kind == TypeCheckKind::kExactCheck)
5289 ? SideEffects::None()
5290 // Mips currently does runtime calls for any other checks.
5291 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005292 }
5293
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005294 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005295
5296 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005297 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005298 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005299
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005300 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5301};
5302
Calin Juravleb1498f62015-02-16 13:13:29 +00005303class HBoundType : public HExpression<1> {
5304 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005305 // Constructs an HBoundType with the given upper_bound.
5306 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005307 HBoundType(HInstruction* input,
5308 ReferenceTypeInfo upper_bound,
5309 bool upper_can_be_null,
5310 uint32_t dex_pc = kNoDexPc)
5311 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005312 upper_bound_(upper_bound),
5313 upper_can_be_null_(upper_can_be_null),
5314 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005315 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005316 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005317 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005318 }
5319
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005320 // GetUpper* should only be used in reference type propagation.
5321 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5322 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005323
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005324 void SetCanBeNull(bool can_be_null) {
5325 DCHECK(upper_can_be_null_ || !can_be_null);
5326 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005327 }
5328
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005329 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5330
Calin Juravleb1498f62015-02-16 13:13:29 +00005331 DECLARE_INSTRUCTION(BoundType);
5332
5333 private:
5334 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005335 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5336 // It is used to bound the type in cases like:
5337 // if (x instanceof ClassX) {
5338 // // uper_bound_ will be ClassX
5339 // }
5340 const ReferenceTypeInfo upper_bound_;
5341 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5342 // is false then can_be_null_ cannot be true).
5343 const bool upper_can_be_null_;
5344 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005345
5346 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5347};
5348
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005349class HCheckCast : public HTemplateInstruction<2> {
5350 public:
5351 HCheckCast(HInstruction* object,
5352 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005353 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005354 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005355 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005356 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005357 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005358 SetRawInputAt(0, object);
5359 SetRawInputAt(1, constant);
5360 }
5361
5362 bool CanBeMoved() const OVERRIDE { return true; }
5363
5364 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5365 return true;
5366 }
5367
5368 bool NeedsEnvironment() const OVERRIDE {
5369 // Instruction may throw a CheckCastError.
5370 return true;
5371 }
5372
5373 bool CanThrow() const OVERRIDE { return true; }
5374
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005375 bool MustDoNullCheck() const { return must_do_null_check_; }
5376 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005377 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005378
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005379 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005380
5381 DECLARE_INSTRUCTION(CheckCast);
5382
5383 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005384 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005385 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005386
5387 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005388};
5389
Calin Juravle27df7582015-04-17 19:12:31 +01005390class HMemoryBarrier : public HTemplateInstruction<0> {
5391 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005392 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005393 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005394 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005395 barrier_kind_(barrier_kind) {}
5396
5397 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5398
5399 DECLARE_INSTRUCTION(MemoryBarrier);
5400
5401 private:
5402 const MemBarrierKind barrier_kind_;
5403
5404 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5405};
5406
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005407class HMonitorOperation : public HTemplateInstruction<1> {
5408 public:
5409 enum OperationKind {
5410 kEnter,
5411 kExit,
5412 };
5413
5414 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005415 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005416 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5417 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005418 SetRawInputAt(0, object);
5419 }
5420
5421 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005422 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5423
5424 bool CanThrow() const OVERRIDE {
5425 // Verifier guarantees that monitor-exit cannot throw.
5426 // This is important because it allows the HGraphBuilder to remove
5427 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5428 return IsEnter();
5429 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005430
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005431
5432 bool IsEnter() const { return kind_ == kEnter; }
5433
5434 DECLARE_INSTRUCTION(MonitorOperation);
5435
Calin Juravle52c48962014-12-16 17:02:57 +00005436 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005437 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005438
5439 private:
5440 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5441};
5442
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005443/**
5444 * A HInstruction used as a marker for the replacement of new + <init>
5445 * of a String to a call to a StringFactory. Only baseline will see
5446 * the node at code generation, where it will be be treated as null.
5447 * When compiling non-baseline, `HFakeString` instructions are being removed
5448 * in the instruction simplifier.
5449 */
5450class HFakeString : public HTemplateInstruction<0> {
5451 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005452 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5453 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005454
5455 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5456
5457 DECLARE_INSTRUCTION(FakeString);
5458
5459 private:
5460 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5461};
5462
Vladimir Markof9f64412015-09-02 14:05:49 +01005463class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005464 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005465 MoveOperands(Location source,
5466 Location destination,
5467 Primitive::Type type,
5468 HInstruction* instruction)
5469 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005470
5471 Location GetSource() const { return source_; }
5472 Location GetDestination() const { return destination_; }
5473
5474 void SetSource(Location value) { source_ = value; }
5475 void SetDestination(Location value) { destination_ = value; }
5476
5477 // The parallel move resolver marks moves as "in-progress" by clearing the
5478 // destination (but not the source).
5479 Location MarkPending() {
5480 DCHECK(!IsPending());
5481 Location dest = destination_;
5482 destination_ = Location::NoLocation();
5483 return dest;
5484 }
5485
5486 void ClearPending(Location dest) {
5487 DCHECK(IsPending());
5488 destination_ = dest;
5489 }
5490
5491 bool IsPending() const {
5492 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5493 return destination_.IsInvalid() && !source_.IsInvalid();
5494 }
5495
5496 // True if this blocks a move from the given location.
5497 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005498 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005499 }
5500
5501 // A move is redundant if it's been eliminated, if its source and
5502 // destination are the same, or if its destination is unneeded.
5503 bool IsRedundant() const {
5504 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5505 }
5506
5507 // We clear both operands to indicate move that's been eliminated.
5508 void Eliminate() {
5509 source_ = destination_ = Location::NoLocation();
5510 }
5511
5512 bool IsEliminated() const {
5513 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5514 return source_.IsInvalid();
5515 }
5516
Alexey Frunze4dda3372015-06-01 18:31:49 -07005517 Primitive::Type GetType() const { return type_; }
5518
Nicolas Geoffray90218252015-04-15 11:56:51 +01005519 bool Is64BitMove() const {
5520 return Primitive::Is64BitType(type_);
5521 }
5522
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005523 HInstruction* GetInstruction() const { return instruction_; }
5524
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005525 private:
5526 Location source_;
5527 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005528 // The type this move is for.
5529 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005530 // The instruction this move is assocatied with. Null when this move is
5531 // for moving an input in the expected locations of user (including a phi user).
5532 // This is only used in debug mode, to ensure we do not connect interval siblings
5533 // in the same parallel move.
5534 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005535};
5536
5537static constexpr size_t kDefaultNumberOfMoves = 4;
5538
5539class HParallelMove : public HTemplateInstruction<0> {
5540 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005541 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005542 : HTemplateInstruction(SideEffects::None(), dex_pc),
5543 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5544 moves_.reserve(kDefaultNumberOfMoves);
5545 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005546
Nicolas Geoffray90218252015-04-15 11:56:51 +01005547 void AddMove(Location source,
5548 Location destination,
5549 Primitive::Type type,
5550 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005551 DCHECK(source.IsValid());
5552 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005553 if (kIsDebugBuild) {
5554 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005555 for (const MoveOperands& move : moves_) {
5556 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005557 // Special case the situation where the move is for the spill slot
5558 // of the instruction.
5559 if ((GetPrevious() == instruction)
5560 || ((GetPrevious() == nullptr)
5561 && instruction->IsPhi()
5562 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005563 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005564 << "Doing parallel moves for the same instruction.";
5565 } else {
5566 DCHECK(false) << "Doing parallel moves for the same instruction.";
5567 }
5568 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005569 }
5570 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005571 for (const MoveOperands& move : moves_) {
5572 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005573 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005574 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005575 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005576 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005577 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005578 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005579 }
5580
Vladimir Marko225b6462015-09-28 12:17:40 +01005581 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005582 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005583 }
5584
Vladimir Marko225b6462015-09-28 12:17:40 +01005585 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005586
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005587 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005588
5589 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005590 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005591
5592 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5593};
5594
Mark Mendell0616ae02015-04-17 12:49:27 -04005595} // namespace art
5596
Vladimir Markob4536b72015-11-24 13:45:23 +00005597#ifdef ART_ENABLE_CODEGEN_arm
5598#include "nodes_arm.h"
5599#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005600#ifdef ART_ENABLE_CODEGEN_arm64
5601#include "nodes_arm64.h"
5602#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005603#ifdef ART_ENABLE_CODEGEN_x86
5604#include "nodes_x86.h"
5605#endif
5606
5607namespace art {
5608
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005609class HGraphVisitor : public ValueObject {
5610 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005611 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5612 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005613
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005614 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005615 virtual void VisitBasicBlock(HBasicBlock* block);
5616
Roland Levillain633021e2014-10-01 14:12:25 +01005617 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005618 void VisitInsertionOrder();
5619
Roland Levillain633021e2014-10-01 14:12:25 +01005620 // Visit the graph following dominator tree reverse post-order.
5621 void VisitReversePostOrder();
5622
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005623 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005624
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005625 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005626#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005627 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5628
5629 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5630
5631#undef DECLARE_VISIT_INSTRUCTION
5632
5633 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005634 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005635
5636 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5637};
5638
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005639class HGraphDelegateVisitor : public HGraphVisitor {
5640 public:
5641 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5642 virtual ~HGraphDelegateVisitor() {}
5643
5644 // Visit functions that delegate to to super class.
5645#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005646 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005647
5648 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5649
5650#undef DECLARE_VISIT_INSTRUCTION
5651
5652 private:
5653 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5654};
5655
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005656class HInsertionOrderIterator : public ValueObject {
5657 public:
5658 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5659
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005660 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005661 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005662 void Advance() { ++index_; }
5663
5664 private:
5665 const HGraph& graph_;
5666 size_t index_;
5667
5668 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5669};
5670
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005671class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005672 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005673 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5674 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005675 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005676 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005677
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005678 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5679 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005680 void Advance() { ++index_; }
5681
5682 private:
5683 const HGraph& graph_;
5684 size_t index_;
5685
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005686 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005687};
5688
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005689class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005690 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005691 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005692 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005693 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005694 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005695 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005696
5697 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005698 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005699 void Advance() { --index_; }
5700
5701 private:
5702 const HGraph& graph_;
5703 size_t index_;
5704
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005705 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005706};
5707
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005708class HLinearPostOrderIterator : public ValueObject {
5709 public:
5710 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005711 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005712
5713 bool Done() const { return index_ == 0; }
5714
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005715 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005716
5717 void Advance() {
5718 --index_;
5719 DCHECK_GE(index_, 0U);
5720 }
5721
5722 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005723 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005724 size_t index_;
5725
5726 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5727};
5728
5729class HLinearOrderIterator : public ValueObject {
5730 public:
5731 explicit HLinearOrderIterator(const HGraph& graph)
5732 : order_(graph.GetLinearOrder()), index_(0) {}
5733
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005734 bool Done() const { return index_ == order_.size(); }
5735 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005736 void Advance() { ++index_; }
5737
5738 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005739 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005740 size_t index_;
5741
5742 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5743};
5744
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005745// Iterator over the blocks that art part of the loop. Includes blocks part
5746// of an inner loop. The order in which the blocks are iterated is on their
5747// block id.
5748class HBlocksInLoopIterator : public ValueObject {
5749 public:
5750 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5751 : blocks_in_loop_(info.GetBlocks()),
5752 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5753 index_(0) {
5754 if (!blocks_in_loop_.IsBitSet(index_)) {
5755 Advance();
5756 }
5757 }
5758
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005759 bool Done() const { return index_ == blocks_.size(); }
5760 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005761 void Advance() {
5762 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005763 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005764 if (blocks_in_loop_.IsBitSet(index_)) {
5765 break;
5766 }
5767 }
5768 }
5769
5770 private:
5771 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005772 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005773 size_t index_;
5774
5775 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5776};
5777
Mingyao Yang3584bce2015-05-19 16:01:59 -07005778// Iterator over the blocks that art part of the loop. Includes blocks part
5779// of an inner loop. The order in which the blocks are iterated is reverse
5780// post order.
5781class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5782 public:
5783 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5784 : blocks_in_loop_(info.GetBlocks()),
5785 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5786 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005787 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005788 Advance();
5789 }
5790 }
5791
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005792 bool Done() const { return index_ == blocks_.size(); }
5793 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005794 void Advance() {
5795 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005796 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5797 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005798 break;
5799 }
5800 }
5801 }
5802
5803 private:
5804 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005805 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005806 size_t index_;
5807
5808 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5809};
5810
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005811inline int64_t Int64FromConstant(HConstant* constant) {
5812 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5813 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5814 : constant->AsLongConstant()->GetValue();
5815}
5816
Vladimir Marko58155012015-08-19 12:49:41 +00005817inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5818 // For the purposes of the compiler, the dex files must actually be the same object
5819 // if we want to safely treat them as the same. This is especially important for JIT
5820 // as custom class loaders can open the same underlying file (or memory) multiple
5821 // times and provide different class resolution but no two class loaders should ever
5822 // use the same DexFile object - doing so is an unsupported hack that can lead to
5823 // all sorts of weird failures.
5824 return &lhs == &rhs;
5825}
5826
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005827} // namespace art
5828
5829#endif // ART_COMPILER_OPTIMIZING_NODES_H_