blob: 9d3c88c79ea99e3008143efc997f2f4625b58030 [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
Aart Bik73f1f3b2015-10-28 15:28:08 -0700567 // Returns true if instruction is not defined within this loop or any loop nested inside
568 // this loop. If must_dominate is set, only definitions that actually dominate the loop
569 // header can be invariant. Otherwise, any definition outside the loop, including
570 // definitions that appear after the loop, is invariant.
571 bool IsLoopInvariant(HInstruction* instruction, bool must_dominate) const;
572
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100573 const ArenaBitVector& GetBlocks() const { return blocks_; }
574
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000575 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000576 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000577
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000578 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100579 // Internal recursive implementation of `Populate`.
580 void PopulateRecursive(HBasicBlock* block);
581
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000582 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100583 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100584 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100585 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000586
587 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
588};
589
David Brazdilec16f792015-08-19 15:04:01 +0100590// Stores try/catch information for basic blocks.
591// Note that HGraph is constructed so that catch blocks cannot simultaneously
592// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100593class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100594 public:
595 // Try block information constructor.
596 explicit TryCatchInformation(const HTryBoundary& try_entry)
597 : try_entry_(&try_entry),
598 catch_dex_file_(nullptr),
599 catch_type_index_(DexFile::kDexNoIndex16) {
600 DCHECK(try_entry_ != nullptr);
601 }
602
603 // Catch block information constructor.
604 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
605 : try_entry_(nullptr),
606 catch_dex_file_(&dex_file),
607 catch_type_index_(catch_type_index) {}
608
609 bool IsTryBlock() const { return try_entry_ != nullptr; }
610
611 const HTryBoundary& GetTryEntry() const {
612 DCHECK(IsTryBlock());
613 return *try_entry_;
614 }
615
616 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
617
618 bool IsCatchAllTypeIndex() const {
619 DCHECK(IsCatchBlock());
620 return catch_type_index_ == DexFile::kDexNoIndex16;
621 }
622
623 uint16_t GetCatchTypeIndex() const {
624 DCHECK(IsCatchBlock());
625 return catch_type_index_;
626 }
627
628 const DexFile& GetCatchDexFile() const {
629 DCHECK(IsCatchBlock());
630 return *catch_dex_file_;
631 }
632
633 private:
634 // One of possibly several TryBoundary instructions entering the block's try.
635 // Only set for try blocks.
636 const HTryBoundary* try_entry_;
637
638 // Exception type information. Only set for catch blocks.
639 const DexFile* catch_dex_file_;
640 const uint16_t catch_type_index_;
641};
642
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100643static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100644static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100645
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000646// A block in a method. Contains the list of instructions represented
647// as a double linked list. Each block knows its predecessors and
648// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100649
Vladimir Markof9f64412015-09-02 14:05:49 +0100650class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000651 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600652 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000653 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000654 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
655 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000656 loop_information_(nullptr),
657 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000658 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100659 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100660 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100661 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000662 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000663 try_catch_information_(nullptr) {
664 predecessors_.reserve(kDefaultNumberOfPredecessors);
665 successors_.reserve(kDefaultNumberOfSuccessors);
666 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
667 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000668
Vladimir Marko60584552015-09-03 13:35:12 +0000669 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100670 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000671 }
672
Vladimir Marko60584552015-09-03 13:35:12 +0000673 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100674 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000675 }
676
David Brazdild26a4112015-11-10 11:07:31 +0000677 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
678 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
679
Vladimir Marko60584552015-09-03 13:35:12 +0000680 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
681 return ContainsElement(successors_, block, start_from);
682 }
683
684 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100685 return dominated_blocks_;
686 }
687
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100688 bool IsEntryBlock() const {
689 return graph_->GetEntryBlock() == this;
690 }
691
692 bool IsExitBlock() const {
693 return graph_->GetExitBlock() == this;
694 }
695
David Brazdil46e2a392015-03-16 17:31:52 +0000696 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000697 bool IsSingleTryBoundary() const;
698
699 // Returns true if this block emits nothing but a jump.
700 bool IsSingleJump() const {
701 HLoopInformation* loop_info = GetLoopInformation();
702 return (IsSingleGoto() || IsSingleTryBoundary())
703 // Back edges generate a suspend check.
704 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
705 }
David Brazdil46e2a392015-03-16 17:31:52 +0000706
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000707 void AddBackEdge(HBasicBlock* back_edge) {
708 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000709 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000710 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100711 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000712 loop_information_->AddBackEdge(back_edge);
713 }
714
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000715 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000716 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000717
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100718 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000719 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600720 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000721
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000722 HBasicBlock* GetDominator() const { return dominator_; }
723 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000724 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
725
726 void RemoveDominatedBlock(HBasicBlock* block) {
727 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100728 }
Vladimir Marko60584552015-09-03 13:35:12 +0000729
730 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
731 ReplaceElement(dominated_blocks_, existing, new_block);
732 }
733
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100734 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000735
736 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100737 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000738 }
739
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100740 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
741 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100742 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100743 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100744 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
745 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000746
747 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000748 successors_.push_back(block);
749 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000750 }
751
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100752 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
753 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100754 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000755 new_block->predecessors_.push_back(this);
756 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000757 }
758
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000759 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
760 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000761 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000762 new_block->successors_.push_back(this);
763 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000764 }
765
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100766 // Insert `this` between `predecessor` and `successor. This method
767 // preserves the indicies, and will update the first edge found between
768 // `predecessor` and `successor`.
769 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
770 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100771 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000772 successor->predecessors_[predecessor_index] = this;
773 predecessor->successors_[successor_index] = this;
774 successors_.push_back(successor);
775 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100776 }
777
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100778 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000779 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780 }
781
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000782 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000783 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000784 }
785
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000787 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100788 }
789
790 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000791 predecessors_.push_back(block);
792 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100793 }
794
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100795 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000796 DCHECK_EQ(predecessors_.size(), 2u);
797 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100798 }
799
David Brazdil769c9e52015-04-27 13:54:09 +0100800 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000801 DCHECK_EQ(successors_.size(), 2u);
802 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100803 }
804
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000806 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100807 }
808
David Brazdilfc6a86a2015-06-26 10:33:45 +0000809 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000810 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100811 }
812
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000814 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100815 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000816 }
817
818 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000819 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100820 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000821 }
822
823 // Returns whether the first occurrence of `predecessor` in the list of
824 // predecessors is at index `idx`.
825 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100826 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000827 return GetPredecessorIndexOf(predecessor) == idx;
828 }
829
David Brazdild7558da2015-09-22 13:04:14 +0100830 // Create a new block between this block and its predecessors. The new block
831 // is added to the graph, all predecessor edges are relinked to it and an edge
832 // is created to `this`. Returns the new empty block. Reverse post order or
833 // loop and try/catch information are not updated.
834 HBasicBlock* CreateImmediateDominator();
835
David Brazdilfc6a86a2015-06-26 10:33:45 +0000836 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100837 // created, latter block. Note that this method will add the block to the
838 // graph, create a Goto at the end of the former block and will create an edge
839 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100840 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000841 HBasicBlock* SplitBefore(HInstruction* cursor);
842
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000843 // Split the block into two blocks just after `cursor`. Returns the newly
844 // created block. Note that this method just updates raw block information,
845 // like predecessors, successors, dominators, and instruction list. It does not
846 // update the graph, reverse post order, loop information, nor make sure the
847 // blocks are consistent (for example ending with a control flow instruction).
848 HBasicBlock* SplitAfter(HInstruction* cursor);
849
David Brazdil9bc43612015-11-05 21:25:24 +0000850 // Split catch block into two blocks after the original move-exception bytecode
851 // instruction, or at the beginning if not present. Returns the newly created,
852 // latter block, or nullptr if such block could not be created (must be dead
853 // in that case). Note that this method just updates raw block information,
854 // like predecessors, successors, dominators, and instruction list. It does not
855 // update the graph, reverse post order, loop information, nor make sure the
856 // blocks are consistent (for example ending with a control flow instruction).
857 HBasicBlock* SplitCatchBlockAfterMoveException();
858
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000859 // Merge `other` at the end of `this`. Successors and dominated blocks of
860 // `other` are changed to be successors and dominated blocks of `this`. Note
861 // that this method does not update the graph, reverse post order, loop
862 // information, nor make sure the blocks are consistent (for example ending
863 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100864 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000865
866 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
867 // of `this` are moved to `other`.
868 // Note that this method does not update the graph, reverse post order, loop
869 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000870 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000871 void ReplaceWith(HBasicBlock* other);
872
David Brazdil2d7352b2015-04-20 14:52:42 +0100873 // Merge `other` at the end of `this`. This method updates loops, reverse post
874 // order, links to predecessors, successors, dominators and deletes the block
875 // from the graph. The two blocks must be successive, i.e. `this` the only
876 // predecessor of `other` and vice versa.
877 void MergeWith(HBasicBlock* other);
878
879 // Disconnects `this` from all its predecessors, successors and dominator,
880 // removes it from all loops it is included in and eventually from the graph.
881 // The block must not dominate any other block. Predecessors and successors
882 // are safely updated.
883 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000884
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000885 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100886 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100887 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100888 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100889 // Replace instruction `initial` with `replacement` within this block.
890 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
891 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100893 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000894 // RemoveInstruction and RemovePhi delete a given instruction from the respective
895 // instruction list. With 'ensure_safety' set to true, it verifies that the
896 // instruction is not in use and removes it from the use lists of its inputs.
897 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
898 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100899 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100900
901 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100902 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100903 }
904
Roland Levillain6b879dd2014-09-22 17:13:44 +0100905 bool IsLoopPreHeaderFirstPredecessor() const {
906 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100907 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100908 }
909
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100910 HLoopInformation* GetLoopInformation() const {
911 return loop_information_;
912 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000913
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000914 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100915 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000916 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100917 void SetInLoop(HLoopInformation* info) {
918 if (IsLoopHeader()) {
919 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100920 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100921 loop_information_ = info;
922 } else if (loop_information_->Contains(*info->GetHeader())) {
923 // Block is currently part of an outer loop. Make it part of this inner loop.
924 // Note that a non loop header having a loop information means this loop information
925 // has already been populated
926 loop_information_ = info;
927 } else {
928 // Block is part of an inner loop. Do not update the loop information.
929 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
930 // at this point, because this method is being called while populating `info`.
931 }
932 }
933
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000934 // Raw update of the loop information.
935 void SetLoopInformation(HLoopInformation* info) {
936 loop_information_ = info;
937 }
938
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100939 bool IsInLoop() const { return loop_information_ != nullptr; }
940
David Brazdilec16f792015-08-19 15:04:01 +0100941 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
942
943 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
944 try_catch_information_ = try_catch_information;
945 }
946
947 bool IsTryBlock() const {
948 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
949 }
950
951 bool IsCatchBlock() const {
952 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
953 }
David Brazdilffee3d32015-07-06 11:48:53 +0100954
955 // Returns the try entry that this block's successors should have. They will
956 // be in the same try, unless the block ends in a try boundary. In that case,
957 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100958 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100959
David Brazdild7558da2015-09-22 13:04:14 +0100960 bool HasThrowingInstructions() const;
961
David Brazdila4b8c212015-05-07 09:59:30 +0100962 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100963 bool Dominates(HBasicBlock* block) const;
964
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100965 size_t GetLifetimeStart() const { return lifetime_start_; }
966 size_t GetLifetimeEnd() const { return lifetime_end_; }
967
968 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
969 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
970
David Brazdil8d5b8b22015-03-24 10:51:52 +0000971 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000972 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100973 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000974 bool HasSinglePhi() const;
975
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000976 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000977 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000978 ArenaVector<HBasicBlock*> predecessors_;
979 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100980 HInstructionList instructions_;
981 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000982 HLoopInformation* loop_information_;
983 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000984 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100985 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100986 // The dex program counter of the first instruction of this block.
987 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100988 size_t lifetime_start_;
989 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100990 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100991
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000992 friend class HGraph;
993 friend class HInstruction;
994
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000995 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
996};
997
David Brazdilb2bd1c52015-03-25 11:17:37 +0000998// Iterates over the LoopInformation of all loops which contain 'block'
999// from the innermost to the outermost.
1000class HLoopInformationOutwardIterator : public ValueObject {
1001 public:
1002 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1003 : current_(block.GetLoopInformation()) {}
1004
1005 bool Done() const { return current_ == nullptr; }
1006
1007 void Advance() {
1008 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001009 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001010 }
1011
1012 HLoopInformation* Current() const {
1013 DCHECK(!Done());
1014 return current_;
1015 }
1016
1017 private:
1018 HLoopInformation* current_;
1019
1020 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1021};
1022
Alexandre Ramesef20f712015-06-09 10:29:30 +01001023#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001024 M(Above, Condition) \
1025 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001026 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001027 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001028 M(ArrayGet, Instruction) \
1029 M(ArrayLength, Instruction) \
1030 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001031 M(Below, Condition) \
1032 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001033 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001034 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001035 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001036 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001037 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001038 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001039 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001040 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001041 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001042 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001043 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001044 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001045 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001046 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001047 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001048 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001049 M(FloatConstant, Constant) \
1050 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001051 M(GreaterThan, Condition) \
1052 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001054 M(InstanceFieldGet, Instruction) \
1055 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001056 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001057 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001058 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001059 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001060 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001061 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001062 M(LessThan, Condition) \
1063 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001064 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001065 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001066 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001067 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001068 M(Local, Instruction) \
1069 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001070 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001071 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001072 M(Mul, BinaryOperation) \
1073 M(Neg, UnaryOperation) \
1074 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001075 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001076 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001077 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001078 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001079 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001080 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001081 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001082 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001083 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001084 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001085 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001086 M(Return, Instruction) \
1087 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001088 M(Shl, BinaryOperation) \
1089 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001090 M(StaticFieldGet, Instruction) \
1091 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001092 M(UnresolvedInstanceFieldGet, Instruction) \
1093 M(UnresolvedInstanceFieldSet, Instruction) \
1094 M(UnresolvedStaticFieldGet, Instruction) \
1095 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001096 M(StoreLocal, Instruction) \
1097 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001098 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001099 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001100 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001101 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001102 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001103 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001104 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001105
Vladimir Markob4536b72015-11-24 13:45:23 +00001106#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001107#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001108#else
1109#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1110 M(ArmDexCacheArraysBase, Instruction)
1111#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001112
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001113#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001114#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001115#else
1116#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001117 M(Arm64DataProcWithShifterOp, Instruction) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001118 M(Arm64IntermediateAddress, Instruction) \
1119 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001120#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001121
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001122#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1123
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001124#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1125
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001126#ifndef ART_ENABLE_CODEGEN_x86
1127#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1128#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001129#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1130 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001131 M(X86LoadFromConstantTable, Instruction) \
1132 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001133#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001134
1135#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1136
1137#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1138 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1139 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1140 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001141 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001142 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001143 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1144 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1145
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001146#define FOR_EACH_INSTRUCTION(M) \
1147 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1148 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001149 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001150 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001151 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001152
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001153#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001154FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1155#undef FORWARD_DECLARATION
1156
Roland Levillainccc07a92014-09-16 14:48:16 +01001157#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001158 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1159 const char* DebugName() const OVERRIDE { return #type; } \
1160 const H##type* As##type() const OVERRIDE { return this; } \
1161 H##type* As##type() OVERRIDE { return this; } \
1162 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001163 return other->Is##type(); \
1164 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001165 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001166
David Brazdiled596192015-01-23 10:39:45 +00001167template <typename T> class HUseList;
1168
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001169template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001170class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001171 public:
David Brazdiled596192015-01-23 10:39:45 +00001172 HUseListNode* GetPrevious() const { return prev_; }
1173 HUseListNode* GetNext() const { return next_; }
1174 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001175 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001176 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001177
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001178 private:
David Brazdiled596192015-01-23 10:39:45 +00001179 HUseListNode(T user, size_t index)
1180 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1181
1182 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001183 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001184 HUseListNode<T>* prev_;
1185 HUseListNode<T>* next_;
1186
1187 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001188
1189 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1190};
1191
David Brazdiled596192015-01-23 10:39:45 +00001192template <typename T>
1193class HUseList : public ValueObject {
1194 public:
1195 HUseList() : first_(nullptr) {}
1196
1197 void Clear() {
1198 first_ = nullptr;
1199 }
1200
1201 // Adds a new entry at the beginning of the use list and returns
1202 // the newly created node.
1203 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001204 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001205 if (IsEmpty()) {
1206 first_ = new_node;
1207 } else {
1208 first_->prev_ = new_node;
1209 new_node->next_ = first_;
1210 first_ = new_node;
1211 }
1212 return new_node;
1213 }
1214
1215 HUseListNode<T>* GetFirst() const {
1216 return first_;
1217 }
1218
1219 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001220 DCHECK(node != nullptr);
1221 DCHECK(Contains(node));
1222
David Brazdiled596192015-01-23 10:39:45 +00001223 if (node->prev_ != nullptr) {
1224 node->prev_->next_ = node->next_;
1225 }
1226 if (node->next_ != nullptr) {
1227 node->next_->prev_ = node->prev_;
1228 }
1229 if (node == first_) {
1230 first_ = node->next_;
1231 }
1232 }
1233
David Brazdil1abb4192015-02-17 18:33:36 +00001234 bool Contains(const HUseListNode<T>* node) const {
1235 if (node == nullptr) {
1236 return false;
1237 }
1238 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1239 if (current == node) {
1240 return true;
1241 }
1242 }
1243 return false;
1244 }
1245
David Brazdiled596192015-01-23 10:39:45 +00001246 bool IsEmpty() const {
1247 return first_ == nullptr;
1248 }
1249
1250 bool HasOnlyOneUse() const {
1251 return first_ != nullptr && first_->next_ == nullptr;
1252 }
1253
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001254 size_t SizeSlow() const {
1255 size_t count = 0;
1256 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1257 ++count;
1258 }
1259 return count;
1260 }
1261
David Brazdiled596192015-01-23 10:39:45 +00001262 private:
1263 HUseListNode<T>* first_;
1264};
1265
1266template<typename T>
1267class HUseIterator : public ValueObject {
1268 public:
1269 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1270
1271 bool Done() const { return current_ == nullptr; }
1272
1273 void Advance() {
1274 DCHECK(!Done());
1275 current_ = current_->GetNext();
1276 }
1277
1278 HUseListNode<T>* Current() const {
1279 DCHECK(!Done());
1280 return current_;
1281 }
1282
1283 private:
1284 HUseListNode<T>* current_;
1285
1286 friend class HValue;
1287};
1288
David Brazdil1abb4192015-02-17 18:33:36 +00001289// This class is used by HEnvironment and HInstruction classes to record the
1290// instructions they use and pointers to the corresponding HUseListNodes kept
1291// by the used instructions.
1292template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001293class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001294 public:
1295 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1296 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1297
1298 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1299 : instruction_(old_record.instruction_), use_node_(use_node) {
1300 DCHECK(instruction_ != nullptr);
1301 DCHECK(use_node_ != nullptr);
1302 DCHECK(old_record.use_node_ == nullptr);
1303 }
1304
1305 HInstruction* GetInstruction() const { return instruction_; }
1306 HUseListNode<T>* GetUseNode() const { return use_node_; }
1307
1308 private:
1309 // Instruction used by the user.
1310 HInstruction* instruction_;
1311
1312 // Corresponding entry in the use list kept by 'instruction_'.
1313 HUseListNode<T>* use_node_;
1314};
1315
Aart Bik854a02b2015-07-14 16:07:00 -07001316/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001317 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001318 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001319 * For write/read dependences on fields/arrays, the dependence analysis uses
1320 * type disambiguation (e.g. a float field write cannot modify the value of an
1321 * integer field read) and the access type (e.g. a reference array write cannot
1322 * modify the value of a reference field read [although it may modify the
1323 * reference fetch prior to reading the field, which is represented by its own
1324 * write/read dependence]). The analysis makes conservative points-to
1325 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1326 * the same, and any reference read depends on any reference read without
1327 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001328 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001329 * The internal representation uses 38-bit and is described in the table below.
1330 * The first line indicates the side effect, and for field/array accesses the
1331 * second line indicates the type of the access (in the order of the
1332 * Primitive::Type enum).
1333 * The two numbered lines below indicate the bit position in the bitfield (read
1334 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001335 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001336 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1337 * +-------------+---------+---------+--------------+---------+---------+
1338 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1339 * | 3 |333333322|222222221| 1 |111111110|000000000|
1340 * | 7 |654321098|765432109| 8 |765432109|876543210|
1341 *
1342 * Note that, to ease the implementation, 'changes' bits are least significant
1343 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001344 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001345class SideEffects : public ValueObject {
1346 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001347 SideEffects() : flags_(0) {}
1348
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001349 static SideEffects None() {
1350 return SideEffects(0);
1351 }
1352
1353 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001354 return SideEffects(kAllChangeBits | kAllDependOnBits);
1355 }
1356
1357 static SideEffects AllChanges() {
1358 return SideEffects(kAllChangeBits);
1359 }
1360
1361 static SideEffects AllDependencies() {
1362 return SideEffects(kAllDependOnBits);
1363 }
1364
1365 static SideEffects AllExceptGCDependency() {
1366 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1367 }
1368
1369 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001370 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001371 }
1372
Aart Bik34c3ba92015-07-20 14:08:59 -07001373 static SideEffects AllWrites() {
1374 return SideEffects(kAllWrites);
1375 }
1376
1377 static SideEffects AllReads() {
1378 return SideEffects(kAllReads);
1379 }
1380
1381 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1382 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001383 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001384 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001385 }
1386
Aart Bik854a02b2015-07-14 16:07:00 -07001387 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1388 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001389 }
1390
Aart Bik34c3ba92015-07-20 14:08:59 -07001391 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1392 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001393 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001394 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001395 }
1396
1397 static SideEffects ArrayReadOfType(Primitive::Type type) {
1398 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1399 }
1400
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001401 static SideEffects CanTriggerGC() {
1402 return SideEffects(1ULL << kCanTriggerGCBit);
1403 }
1404
1405 static SideEffects DependsOnGC() {
1406 return SideEffects(1ULL << kDependsOnGCBit);
1407 }
1408
Aart Bik854a02b2015-07-14 16:07:00 -07001409 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001410 SideEffects Union(SideEffects other) const {
1411 return SideEffects(flags_ | other.flags_);
1412 }
1413
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001414 SideEffects Exclusion(SideEffects other) const {
1415 return SideEffects(flags_ & ~other.flags_);
1416 }
1417
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001418 void Add(SideEffects other) {
1419 flags_ |= other.flags_;
1420 }
1421
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001422 bool Includes(SideEffects other) const {
1423 return (other.flags_ & flags_) == other.flags_;
1424 }
1425
1426 bool HasSideEffects() const {
1427 return (flags_ & kAllChangeBits);
1428 }
1429
1430 bool HasDependencies() const {
1431 return (flags_ & kAllDependOnBits);
1432 }
1433
1434 // Returns true if there are no side effects or dependencies.
1435 bool DoesNothing() const {
1436 return flags_ == 0;
1437 }
1438
Aart Bik854a02b2015-07-14 16:07:00 -07001439 // Returns true if something is written.
1440 bool DoesAnyWrite() const {
1441 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001442 }
1443
Aart Bik854a02b2015-07-14 16:07:00 -07001444 // Returns true if something is read.
1445 bool DoesAnyRead() const {
1446 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001447 }
1448
Aart Bik854a02b2015-07-14 16:07:00 -07001449 // Returns true if potentially everything is written and read
1450 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001451 bool DoesAllReadWrite() const {
1452 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1453 }
1454
Aart Bik854a02b2015-07-14 16:07:00 -07001455 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001456 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001457 }
1458
Roland Levillain0d5a2812015-11-13 10:07:31 +00001459 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001460 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001461 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1462 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001463 }
1464
1465 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001466 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001467 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001468 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001469 for (int s = kLastBit; s >= 0; s--) {
1470 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1471 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1472 // This is a bit for the GC side effect.
1473 if (current_bit_is_set) {
1474 flags += "GC";
1475 }
Aart Bik854a02b2015-07-14 16:07:00 -07001476 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001477 } else {
1478 // This is a bit for the array/field analysis.
1479 // The underscore character stands for the 'can trigger GC' bit.
1480 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1481 if (current_bit_is_set) {
1482 flags += kDebug[s];
1483 }
1484 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1485 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1486 flags += "|";
1487 }
1488 }
Aart Bik854a02b2015-07-14 16:07:00 -07001489 }
1490 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001491 }
1492
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001493 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001494
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001495 private:
1496 static constexpr int kFieldArrayAnalysisBits = 9;
1497
1498 static constexpr int kFieldWriteOffset = 0;
1499 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1500 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1501 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1502
1503 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1504
1505 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1506 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1507 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1508 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1509
1510 static constexpr int kLastBit = kDependsOnGCBit;
1511 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1512
1513 // Aliases.
1514
1515 static_assert(kChangeBits == kDependOnBits,
1516 "the 'change' bits should match the 'depend on' bits.");
1517
1518 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1519 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1520 static constexpr uint64_t kAllWrites =
1521 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1522 static constexpr uint64_t kAllReads =
1523 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001524
Aart Bik854a02b2015-07-14 16:07:00 -07001525 // Work around the fact that HIR aliases I/F and J/D.
1526 // TODO: remove this interceptor once HIR types are clean
1527 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1528 switch (type) {
1529 case Primitive::kPrimInt:
1530 case Primitive::kPrimFloat:
1531 return TypeFlag(Primitive::kPrimInt, offset) |
1532 TypeFlag(Primitive::kPrimFloat, offset);
1533 case Primitive::kPrimLong:
1534 case Primitive::kPrimDouble:
1535 return TypeFlag(Primitive::kPrimLong, offset) |
1536 TypeFlag(Primitive::kPrimDouble, offset);
1537 default:
1538 return TypeFlag(type, offset);
1539 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001540 }
1541
Aart Bik854a02b2015-07-14 16:07:00 -07001542 // Translates type to bit flag.
1543 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1544 CHECK_NE(type, Primitive::kPrimVoid);
1545 const uint64_t one = 1;
1546 const int shift = type; // 0-based consecutive enum
1547 DCHECK_LE(kFieldWriteOffset, shift);
1548 DCHECK_LT(shift, kArrayWriteOffset);
1549 return one << (type + offset);
1550 }
1551
1552 // Private constructor on direct flags value.
1553 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1554
1555 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001556};
1557
David Brazdiled596192015-01-23 10:39:45 +00001558// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001559class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001560 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001561 HEnvironment(ArenaAllocator* arena,
1562 size_t number_of_vregs,
1563 const DexFile& dex_file,
1564 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001565 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001566 InvokeType invoke_type,
1567 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001568 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1569 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001570 parent_(nullptr),
1571 dex_file_(dex_file),
1572 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001573 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001574 invoke_type_(invoke_type),
1575 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001576 }
1577
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001578 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001579 : HEnvironment(arena,
1580 to_copy.Size(),
1581 to_copy.GetDexFile(),
1582 to_copy.GetMethodIdx(),
1583 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001584 to_copy.GetInvokeType(),
1585 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001586
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001587 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001588 if (parent_ != nullptr) {
1589 parent_->SetAndCopyParentChain(allocator, parent);
1590 } else {
1591 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1592 parent_->CopyFrom(parent);
1593 if (parent->GetParent() != nullptr) {
1594 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1595 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001596 }
David Brazdiled596192015-01-23 10:39:45 +00001597 }
1598
Vladimir Marko71bf8092015-09-15 15:33:14 +01001599 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001600 void CopyFrom(HEnvironment* environment);
1601
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001602 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1603 // input to the loop phi instead. This is for inserting instructions that
1604 // require an environment (like HDeoptimization) in the loop pre-header.
1605 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001606
1607 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001608 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001609 }
1610
1611 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001612 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001613 }
1614
David Brazdil1abb4192015-02-17 18:33:36 +00001615 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001616
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001617 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001618
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001619 HEnvironment* GetParent() const { return parent_; }
1620
1621 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001622 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001623 }
1624
1625 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001626 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001627 }
1628
1629 uint32_t GetDexPc() const {
1630 return dex_pc_;
1631 }
1632
1633 uint32_t GetMethodIdx() const {
1634 return method_idx_;
1635 }
1636
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001637 InvokeType GetInvokeType() const {
1638 return invoke_type_;
1639 }
1640
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001641 const DexFile& GetDexFile() const {
1642 return dex_file_;
1643 }
1644
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001645 HInstruction* GetHolder() const {
1646 return holder_;
1647 }
1648
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001649
1650 bool IsFromInlinedInvoke() const {
1651 return GetParent() != nullptr;
1652 }
1653
David Brazdiled596192015-01-23 10:39:45 +00001654 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001655 // Record instructions' use entries of this environment for constant-time removal.
1656 // It should only be called by HInstruction when a new environment use is added.
1657 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1658 DCHECK(env_use->GetUser() == this);
1659 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001660 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001661 }
David Brazdiled596192015-01-23 10:39:45 +00001662
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001663 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1664 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001665 HEnvironment* parent_;
1666 const DexFile& dex_file_;
1667 const uint32_t method_idx_;
1668 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001669 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001670
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001671 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001672 HInstruction* const holder_;
1673
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001674 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001675
1676 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1677};
1678
Calin Juravleacf735c2015-02-12 15:25:22 +00001679class ReferenceTypeInfo : ValueObject {
1680 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001681 typedef Handle<mirror::Class> TypeHandle;
1682
Calin Juravle2e768302015-07-28 14:41:11 +00001683 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1684 // The constructor will check that the type_handle is valid.
1685 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001686 }
1687
Calin Juravle2e768302015-07-28 14:41:11 +00001688 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1689
1690 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1691 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001692 }
1693
Calin Juravle2e768302015-07-28 14:41:11 +00001694 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1695 return IsValidHandle(type_handle_);
1696 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001697
Calin Juravleacf735c2015-02-12 15:25:22 +00001698 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001699
1700 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1701 DCHECK(IsValid());
1702 return GetTypeHandle()->IsObjectClass();
1703 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001704
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001705 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001706 DCHECK(IsValid());
1707 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001708 }
1709
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001710 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1711 DCHECK(IsValid());
1712 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1713 }
1714
Mathieu Chartier90443472015-07-16 20:32:27 -07001715 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001716 DCHECK(IsValid());
1717 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001718 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001719
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001720 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001721 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001722 return GetTypeHandle()->IsArrayClass();
1723 }
1724
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001725 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1726 DCHECK(IsValid());
1727 return GetTypeHandle()->IsPrimitiveArray();
1728 }
1729
1730 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1731 DCHECK(IsValid());
1732 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1733 }
1734
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001735 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001736 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001737 if (!IsExact()) return false;
1738 if (!IsArrayClass()) return false;
1739 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1740 }
1741
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001742 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1743 DCHECK(IsValid());
1744 if (!IsExact()) return false;
1745 if (!IsArrayClass()) return false;
1746 if (!rti.IsArrayClass()) return false;
1747 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1748 rti.GetTypeHandle()->GetComponentType());
1749 }
1750
Calin Juravleacf735c2015-02-12 15:25:22 +00001751 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1752
Mathieu Chartier90443472015-07-16 20:32:27 -07001753 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001754 DCHECK(IsValid());
1755 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001756 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1757 }
1758
Calin Juravlecdfed3d2015-10-26 14:05:01 +00001759 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1760 DCHECK(IsValid());
1761 DCHECK(rti.IsValid());
1762 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
1763 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1764 }
1765
Calin Juravleacf735c2015-02-12 15:25:22 +00001766 // Returns true if the type information provide the same amount of details.
1767 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001768 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001769 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001770 if (!IsValid() && !rti.IsValid()) {
1771 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001772 return true;
1773 }
Calin Juravle2e768302015-07-28 14:41:11 +00001774 if (!IsValid() || !rti.IsValid()) {
1775 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001776 return false;
1777 }
Calin Juravle2e768302015-07-28 14:41:11 +00001778 return IsExact() == rti.IsExact()
1779 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001780 }
1781
1782 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001783 ReferenceTypeInfo();
1784 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001785
Calin Juravleacf735c2015-02-12 15:25:22 +00001786 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001787 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001788 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001789 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001790 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001791};
1792
1793std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1794
Vladimir Markof9f64412015-09-02 14:05:49 +01001795class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001796 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001797 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001798 : previous_(nullptr),
1799 next_(nullptr),
1800 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001801 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001802 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001803 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001804 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001805 locations_(nullptr),
1806 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001807 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001808 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001809 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001810
Dave Allison20dfc792014-06-16 20:44:29 -07001811 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001812
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001813#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001814 enum InstructionKind {
1815 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1816 };
1817#undef DECLARE_KIND
1818
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001819 HInstruction* GetNext() const { return next_; }
1820 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001821
Calin Juravle77520bc2015-01-12 18:45:46 +00001822 HInstruction* GetNextDisregardingMoves() const;
1823 HInstruction* GetPreviousDisregardingMoves() const;
1824
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001825 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001826 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001827 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001828 bool IsInBlock() const { return block_ != nullptr; }
1829 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001830 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001831
Roland Levillain6b879dd2014-09-22 17:13:44 +01001832 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001833 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001834
1835 virtual void Accept(HGraphVisitor* visitor) = 0;
1836 virtual const char* DebugName() const = 0;
1837
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001838 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001839 void SetRawInputAt(size_t index, HInstruction* input) {
1840 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1841 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001842
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001843 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001844
1845 uint32_t GetDexPc() const { return dex_pc_; }
1846
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001847 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001848
Roland Levillaine161a2a2014-10-03 12:45:18 +01001849 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001850 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001851
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001852 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001853 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001854
Calin Juravle10e244f2015-01-26 18:54:32 +00001855 // Does not apply for all instructions, but having this at top level greatly
1856 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001857 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001858 virtual bool CanBeNull() const {
1859 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1860 return true;
1861 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001862
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001863 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001864 return false;
1865 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001866
Calin Juravle2e768302015-07-28 14:41:11 +00001867 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001868
Calin Juravle61d544b2015-02-23 16:46:57 +00001869 ReferenceTypeInfo GetReferenceTypeInfo() const {
1870 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1871 return reference_type_info_;
1872 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001873
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001874 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001875 DCHECK(user != nullptr);
1876 HUseListNode<HInstruction*>* use =
1877 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1878 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001879 }
1880
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001881 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001882 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001883 HUseListNode<HEnvironment*>* env_use =
1884 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1885 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001886 }
1887
David Brazdil1abb4192015-02-17 18:33:36 +00001888 void RemoveAsUserOfInput(size_t input) {
1889 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1890 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1891 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001892
David Brazdil1abb4192015-02-17 18:33:36 +00001893 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1894 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001895
David Brazdiled596192015-01-23 10:39:45 +00001896 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1897 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001898 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001899 bool HasOnlyOneNonEnvironmentUse() const {
1900 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1901 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001902
Roland Levillain6c82d402014-10-13 16:10:27 +01001903 // Does this instruction strictly dominate `other_instruction`?
1904 // Returns false if this instruction and `other_instruction` are the same.
1905 // Aborts if this instruction and `other_instruction` are both phis.
1906 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001907
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001908 int GetId() const { return id_; }
1909 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001910
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001911 int GetSsaIndex() const { return ssa_index_; }
1912 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1913 bool HasSsaIndex() const { return ssa_index_ != -1; }
1914
1915 bool HasEnvironment() const { return environment_ != nullptr; }
1916 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001917 // Set the `environment_` field. Raw because this method does not
1918 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001919 void SetRawEnvironment(HEnvironment* environment) {
1920 DCHECK(environment_ == nullptr);
1921 DCHECK_EQ(environment->GetHolder(), this);
1922 environment_ = environment;
1923 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001924
1925 // Set the environment of this instruction, copying it from `environment`. While
1926 // copying, the uses lists are being updated.
1927 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001928 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001929 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001930 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001931 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001932 if (environment->GetParent() != nullptr) {
1933 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1934 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001935 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001936
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001937 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1938 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001939 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001940 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001941 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001942 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001943 if (environment->GetParent() != nullptr) {
1944 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1945 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001946 }
1947
Nicolas Geoffray39468442014-09-02 15:17:15 +01001948 // Returns the number of entries in the environment. Typically, that is the
1949 // number of dex registers in a method. It could be more in case of inlining.
1950 size_t EnvironmentSize() const;
1951
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001952 LocationSummary* GetLocations() const { return locations_; }
1953 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001954
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001955 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001956 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957
Alexandre Rames188d4312015-04-09 18:30:21 +01001958 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1959 // uses of this instruction by `other` are *not* updated.
1960 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1961 ReplaceWith(other);
1962 other->ReplaceInput(this, use_index);
1963 }
1964
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001965 // Move `this` instruction before `cursor`.
1966 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001967
Vladimir Markofb337ea2015-11-25 15:25:10 +00001968 // Move `this` before its first user and out of any loops. If there is no
1969 // out-of-loop user that dominates all other users, move the instruction
1970 // to the end of the out-of-loop common dominator of the user's blocks.
1971 //
1972 // This can be used only on non-throwing instructions with no side effects that
1973 // have at least one use but no environment uses.
1974 void MoveBeforeFirstUserAndOutOfLoops();
1975
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001976#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001977 bool Is##type() const { return (As##type() != nullptr); } \
1978 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001979 virtual H##type* As##type() { return nullptr; }
1980
1981 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1982#undef INSTRUCTION_TYPE_CHECK
1983
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001984 // Returns whether the instruction can be moved within the graph.
1985 virtual bool CanBeMoved() const { return false; }
1986
1987 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001988 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001989 return false;
1990 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001991
1992 // Returns whether any data encoded in the two instructions is equal.
1993 // This method does not look at the inputs. Both instructions must be
1994 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001995 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001996 return false;
1997 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001998
1999 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002000 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002001 // 2) Their inputs are identical.
2002 bool Equals(HInstruction* other) const;
2003
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002004 virtual InstructionKind GetKind() const = 0;
2005
2006 virtual size_t ComputeHashCode() const {
2007 size_t result = GetKind();
2008 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2009 result = (result * 31) + InputAt(i)->GetId();
2010 }
2011 return result;
2012 }
2013
2014 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002015 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002016
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002017 size_t GetLifetimePosition() const { return lifetime_position_; }
2018 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2019 LiveInterval* GetLiveInterval() const { return live_interval_; }
2020 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2021 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2022
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002023 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2024
2025 // Returns whether the code generation of the instruction will require to have access
2026 // to the current method. Such instructions are:
2027 // (1): Instructions that require an environment, as calling the runtime requires
2028 // to walk the stack and have the current method stored at a specific stack address.
2029 // (2): Object literals like classes and strings, that are loaded from the dex cache
2030 // fields of the current method.
2031 bool NeedsCurrentMethod() const {
2032 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2033 }
2034
Vladimir Markodc151b22015-10-15 18:02:30 +01002035 // Returns whether the code generation of the instruction will require to have access
2036 // to the dex cache of the current method's declaring class via the current method.
2037 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002038
Mark Mendellc4701932015-04-10 13:18:51 -04002039 // Does this instruction have any use in an environment before
2040 // control flow hits 'other'?
2041 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2042
2043 // Remove all references to environment uses of this instruction.
2044 // The caller must ensure that this is safe to do.
2045 void RemoveEnvironmentUsers();
2046
David Brazdil1abb4192015-02-17 18:33:36 +00002047 protected:
2048 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2049 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2050
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002051 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002052 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2053
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002054 HInstruction* previous_;
2055 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002056 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002057 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002058
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002059 // An instruction gets an id when it is added to the graph.
2060 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002061 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002062 int id_;
2063
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002064 // When doing liveness analysis, instructions that have uses get an SSA index.
2065 int ssa_index_;
2066
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002067 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002068 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002069
2070 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002071 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002072
Nicolas Geoffray39468442014-09-02 15:17:15 +01002073 // The environment associated with this instruction. Not null if the instruction
2074 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002075 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002076
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002077 // Set by the code generator.
2078 LocationSummary* locations_;
2079
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002080 // Set by the liveness analysis.
2081 LiveInterval* live_interval_;
2082
2083 // Set by the liveness analysis, this is the position in a linear
2084 // order of blocks where this instruction's live interval start.
2085 size_t lifetime_position_;
2086
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002087 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002088
Calin Juravleacf735c2015-02-12 15:25:22 +00002089 // TODO: for primitive types this should be marked as invalid.
2090 ReferenceTypeInfo reference_type_info_;
2091
David Brazdil1abb4192015-02-17 18:33:36 +00002092 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002093 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002094 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002095 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002096 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002097
2098 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2099};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002100std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002101
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002102class HInputIterator : public ValueObject {
2103 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002104 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002105
2106 bool Done() const { return index_ == instruction_->InputCount(); }
2107 HInstruction* Current() const { return instruction_->InputAt(index_); }
2108 void Advance() { index_++; }
2109
2110 private:
2111 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002112 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002113
2114 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2115};
2116
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002117class HInstructionIterator : public ValueObject {
2118 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002119 explicit HInstructionIterator(const HInstructionList& instructions)
2120 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002121 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002122 }
2123
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002124 bool Done() const { return instruction_ == nullptr; }
2125 HInstruction* Current() const { return instruction_; }
2126 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002128 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002129 }
2130
2131 private:
2132 HInstruction* instruction_;
2133 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002134
2135 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002136};
2137
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002138class HBackwardInstructionIterator : public ValueObject {
2139 public:
2140 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2141 : instruction_(instructions.last_instruction_) {
2142 next_ = Done() ? nullptr : instruction_->GetPrevious();
2143 }
2144
2145 bool Done() const { return instruction_ == nullptr; }
2146 HInstruction* Current() const { return instruction_; }
2147 void Advance() {
2148 instruction_ = next_;
2149 next_ = Done() ? nullptr : instruction_->GetPrevious();
2150 }
2151
2152 private:
2153 HInstruction* instruction_;
2154 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002155
2156 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002157};
2158
Vladimir Markof9f64412015-09-02 14:05:49 +01002159template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002160class HTemplateInstruction: public HInstruction {
2161 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002162 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002163 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002164 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002165
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002166 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002167
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002168 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002169 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2170 DCHECK_LT(i, N);
2171 return inputs_[i];
2172 }
David Brazdil1abb4192015-02-17 18:33:36 +00002173
2174 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002175 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002176 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002177 }
2178
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002179 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002180 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002181
2182 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002183};
2184
Vladimir Markof9f64412015-09-02 14:05:49 +01002185// HTemplateInstruction specialization for N=0.
2186template<>
2187class HTemplateInstruction<0>: public HInstruction {
2188 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002189 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002190 : HInstruction(side_effects, dex_pc) {}
2191
Vladimir Markof9f64412015-09-02 14:05:49 +01002192 virtual ~HTemplateInstruction() {}
2193
2194 size_t InputCount() const OVERRIDE { return 0; }
2195
2196 protected:
2197 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2198 LOG(FATAL) << "Unreachable";
2199 UNREACHABLE();
2200 }
2201
2202 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2203 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2204 LOG(FATAL) << "Unreachable";
2205 UNREACHABLE();
2206 }
2207
2208 private:
2209 friend class SsaBuilder;
2210};
2211
Dave Allison20dfc792014-06-16 20:44:29 -07002212template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002213class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002214 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002215 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002216 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002217 virtual ~HExpression() {}
2218
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002219 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002220
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002221 protected:
2222 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002223};
2224
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2226// instruction that branches to the exit block.
2227class HReturnVoid : public HTemplateInstruction<0> {
2228 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002229 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2230 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002231
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002232 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002233
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002234 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002235
2236 private:
2237 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2238};
2239
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002240// Represents dex's RETURN opcodes. A HReturn is a control flow
2241// instruction that branches to the exit block.
2242class HReturn : public HTemplateInstruction<1> {
2243 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002244 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2245 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002246 SetRawInputAt(0, value);
2247 }
2248
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002249 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002250
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002251 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002252
2253 private:
2254 DISALLOW_COPY_AND_ASSIGN(HReturn);
2255};
2256
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002258// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002259// exit block.
2260class HExit : public HTemplateInstruction<0> {
2261 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002262 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002263
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002264 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002265
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002266 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002267
2268 private:
2269 DISALLOW_COPY_AND_ASSIGN(HExit);
2270};
2271
2272// Jumps from one block to another.
2273class HGoto : public HTemplateInstruction<0> {
2274 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002275 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002276
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002277 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002278
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002279 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002280 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002281 }
2282
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002283 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002284
2285 private:
2286 DISALLOW_COPY_AND_ASSIGN(HGoto);
2287};
2288
Roland Levillain9867bc72015-08-05 10:21:34 +01002289class HConstant : public HExpression<0> {
2290 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002291 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2292 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002293
2294 bool CanBeMoved() const OVERRIDE { return true; }
2295
2296 virtual bool IsMinusOne() const { return false; }
2297 virtual bool IsZero() const { return false; }
2298 virtual bool IsOne() const { return false; }
2299
David Brazdil77a48ae2015-09-15 12:34:04 +00002300 virtual uint64_t GetValueAsUint64() const = 0;
2301
Roland Levillain9867bc72015-08-05 10:21:34 +01002302 DECLARE_INSTRUCTION(Constant);
2303
2304 private:
2305 DISALLOW_COPY_AND_ASSIGN(HConstant);
2306};
2307
2308class HNullConstant : public HConstant {
2309 public:
2310 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2311 return true;
2312 }
2313
David Brazdil77a48ae2015-09-15 12:34:04 +00002314 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2315
Roland Levillain9867bc72015-08-05 10:21:34 +01002316 size_t ComputeHashCode() const OVERRIDE { return 0; }
2317
2318 DECLARE_INSTRUCTION(NullConstant);
2319
2320 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002321 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002322
2323 friend class HGraph;
2324 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2325};
2326
2327// Constants of the type int. Those can be from Dex instructions, or
2328// synthesized (for example with the if-eqz instruction).
2329class HIntConstant : public HConstant {
2330 public:
2331 int32_t GetValue() const { return value_; }
2332
David Brazdil9f389d42015-10-01 14:32:56 +01002333 uint64_t GetValueAsUint64() const OVERRIDE {
2334 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2335 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002336
Roland Levillain9867bc72015-08-05 10:21:34 +01002337 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2338 DCHECK(other->IsIntConstant());
2339 return other->AsIntConstant()->value_ == value_;
2340 }
2341
2342 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2343
2344 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2345 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2346 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2347
2348 DECLARE_INSTRUCTION(IntConstant);
2349
2350 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002351 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2352 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2353 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2354 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002355
2356 const int32_t value_;
2357
2358 friend class HGraph;
2359 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2360 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2361 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2362};
2363
2364class HLongConstant : public HConstant {
2365 public:
2366 int64_t GetValue() const { return value_; }
2367
David Brazdil77a48ae2015-09-15 12:34:04 +00002368 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2369
Roland Levillain9867bc72015-08-05 10:21:34 +01002370 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2371 DCHECK(other->IsLongConstant());
2372 return other->AsLongConstant()->value_ == value_;
2373 }
2374
2375 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2376
2377 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2378 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2379 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2380
2381 DECLARE_INSTRUCTION(LongConstant);
2382
2383 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002384 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2385 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002386
2387 const int64_t value_;
2388
2389 friend class HGraph;
2390 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2391};
Dave Allison20dfc792014-06-16 20:44:29 -07002392
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002393// Conditional branch. A block ending with an HIf instruction must have
2394// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002395class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002396 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002397 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2398 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002399 SetRawInputAt(0, input);
2400 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002401
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002402 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002403
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002404 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002405 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002406 }
2407
2408 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002409 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002410 }
2411
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002412 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002413
2414 private:
2415 DISALLOW_COPY_AND_ASSIGN(HIf);
2416};
2417
David Brazdilfc6a86a2015-06-26 10:33:45 +00002418
2419// Abstract instruction which marks the beginning and/or end of a try block and
2420// links it to the respective exception handlers. Behaves the same as a Goto in
2421// non-exceptional control flow.
2422// Normal-flow successor is stored at index zero, exception handlers under
2423// higher indices in no particular order.
2424class HTryBoundary : public HTemplateInstruction<0> {
2425 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002426 enum BoundaryKind {
2427 kEntry,
2428 kExit,
2429 };
2430
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002431 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2432 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002433
2434 bool IsControlFlow() const OVERRIDE { return true; }
2435
2436 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002437 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002438
David Brazdild26a4112015-11-10 11:07:31 +00002439 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2440 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2441 }
2442
David Brazdilfc6a86a2015-06-26 10:33:45 +00002443 // Returns whether `handler` is among its exception handlers (non-zero index
2444 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002445 bool HasExceptionHandler(const HBasicBlock& handler) const {
2446 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002447 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002448 }
2449
2450 // If not present already, adds `handler` to its block's list of exception
2451 // handlers.
2452 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002453 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002454 GetBlock()->AddSuccessor(handler);
2455 }
2456 }
2457
David Brazdil56e1acc2015-06-30 15:41:36 +01002458 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002459
David Brazdilffee3d32015-07-06 11:48:53 +01002460 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2461
David Brazdilfc6a86a2015-06-26 10:33:45 +00002462 DECLARE_INSTRUCTION(TryBoundary);
2463
2464 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002465 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002466
2467 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2468};
2469
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002470// Deoptimize to interpreter, upon checking a condition.
2471class HDeoptimize : public HTemplateInstruction<1> {
2472 public:
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002473 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002474 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002475 SetRawInputAt(0, cond);
2476 }
2477
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002478 bool CanBeMoved() const OVERRIDE { return true; }
2479 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2480 return true;
2481 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002482 bool NeedsEnvironment() const OVERRIDE { return true; }
2483 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002484
2485 DECLARE_INSTRUCTION(Deoptimize);
2486
2487 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002488 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2489};
2490
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002491// Represents the ArtMethod that was passed as a first argument to
2492// the method. It is used by instructions that depend on it, like
2493// instructions that work with the dex cache.
2494class HCurrentMethod : public HExpression<0> {
2495 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002496 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2497 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002498
2499 DECLARE_INSTRUCTION(CurrentMethod);
2500
2501 private:
2502 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2503};
2504
Mark Mendellfe57faa2015-09-18 09:26:15 -04002505// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2506// have one successor for each entry in the switch table, and the final successor
2507// will be the block containing the next Dex opcode.
2508class HPackedSwitch : public HTemplateInstruction<1> {
2509 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002510 HPackedSwitch(int32_t start_value,
2511 uint32_t num_entries,
2512 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002513 uint32_t dex_pc = kNoDexPc)
2514 : HTemplateInstruction(SideEffects::None(), dex_pc),
2515 start_value_(start_value),
2516 num_entries_(num_entries) {
2517 SetRawInputAt(0, input);
2518 }
2519
2520 bool IsControlFlow() const OVERRIDE { return true; }
2521
2522 int32_t GetStartValue() const { return start_value_; }
2523
Vladimir Marko211c2112015-09-24 16:52:33 +01002524 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002525
2526 HBasicBlock* GetDefaultBlock() const {
2527 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002528 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002529 }
2530 DECLARE_INSTRUCTION(PackedSwitch);
2531
2532 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002533 const int32_t start_value_;
2534 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002535
2536 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2537};
2538
Roland Levillain88cb1752014-10-20 16:36:47 +01002539class HUnaryOperation : public HExpression<1> {
2540 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002541 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2542 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002543 SetRawInputAt(0, input);
2544 }
2545
2546 HInstruction* GetInput() const { return InputAt(0); }
2547 Primitive::Type GetResultType() const { return GetType(); }
2548
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002549 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002550 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002551 return true;
2552 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002553
Roland Levillain9240d6a2014-10-20 16:47:04 +01002554 // Try to statically evaluate `operation` and return a HConstant
2555 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002556 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002557 HConstant* TryStaticEvaluation() const;
2558
2559 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002560 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2561 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002562
Roland Levillain88cb1752014-10-20 16:36:47 +01002563 DECLARE_INSTRUCTION(UnaryOperation);
2564
2565 private:
2566 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2567};
2568
Dave Allison20dfc792014-06-16 20:44:29 -07002569class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002570 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002571 HBinaryOperation(Primitive::Type result_type,
2572 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002573 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002574 SideEffects side_effects = SideEffects::None(),
2575 uint32_t dex_pc = kNoDexPc)
2576 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002577 SetRawInputAt(0, left);
2578 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002579 }
2580
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002581 HInstruction* GetLeft() const { return InputAt(0); }
2582 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002583 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002584
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002585 virtual bool IsCommutative() const { return false; }
2586
2587 // Put constant on the right.
2588 // Returns whether order is changed.
2589 bool OrderInputsWithConstantOnTheRight() {
2590 HInstruction* left = InputAt(0);
2591 HInstruction* right = InputAt(1);
2592 if (left->IsConstant() && !right->IsConstant()) {
2593 ReplaceInput(right, 0);
2594 ReplaceInput(left, 1);
2595 return true;
2596 }
2597 return false;
2598 }
2599
2600 // Order inputs by instruction id, but favor constant on the right side.
2601 // This helps GVN for commutative ops.
2602 void OrderInputs() {
2603 DCHECK(IsCommutative());
2604 HInstruction* left = InputAt(0);
2605 HInstruction* right = InputAt(1);
2606 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2607 return;
2608 }
2609 if (OrderInputsWithConstantOnTheRight()) {
2610 return;
2611 }
2612 // Order according to instruction id.
2613 if (left->GetId() > right->GetId()) {
2614 ReplaceInput(right, 0);
2615 ReplaceInput(left, 1);
2616 }
2617 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002618
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002619 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002620 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002621 return true;
2622 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002623
Roland Levillain9240d6a2014-10-20 16:47:04 +01002624 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002625 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002626 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002627 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002628
2629 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002630 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2631 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2632 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2633 HLongConstant* y ATTRIBUTE_UNUSED) const {
2634 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2635 return nullptr;
2636 }
2637 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2638 HIntConstant* y ATTRIBUTE_UNUSED) const {
2639 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2640 return nullptr;
2641 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002642 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2643 HNullConstant* y ATTRIBUTE_UNUSED) const {
2644 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2645 return nullptr;
2646 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002647
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002648 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002649 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002650 HConstant* GetConstantRight() const;
2651
2652 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002653 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002654 HInstruction* GetLeastConstantLeft() const;
2655
Roland Levillainccc07a92014-09-16 14:48:16 +01002656 DECLARE_INSTRUCTION(BinaryOperation);
2657
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002658 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002659 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2660};
2661
Mark Mendellc4701932015-04-10 13:18:51 -04002662// The comparison bias applies for floating point operations and indicates how NaN
2663// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002664enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002665 kNoBias, // bias is not applicable (i.e. for long operation)
2666 kGtBias, // return 1 for NaN comparisons
2667 kLtBias, // return -1 for NaN comparisons
2668};
2669
Dave Allison20dfc792014-06-16 20:44:29 -07002670class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002671 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002672 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2673 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002674 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002675 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002676
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002677 bool NeedsMaterialization() const { return needs_materialization_; }
2678 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002679
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002680 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002681 // `instruction`, and disregard moves in between.
2682 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002683
Dave Allison20dfc792014-06-16 20:44:29 -07002684 DECLARE_INSTRUCTION(Condition);
2685
2686 virtual IfCondition GetCondition() const = 0;
2687
Mark Mendellc4701932015-04-10 13:18:51 -04002688 virtual IfCondition GetOppositeCondition() const = 0;
2689
Roland Levillain4fa13f62015-07-06 18:11:54 +01002690 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002691
2692 void SetBias(ComparisonBias bias) { bias_ = bias; }
2693
2694 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2695 return bias_ == other->AsCondition()->bias_;
2696 }
2697
Roland Levillain4fa13f62015-07-06 18:11:54 +01002698 bool IsFPConditionTrueIfNaN() const {
2699 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2700 IfCondition if_cond = GetCondition();
2701 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2702 }
2703
2704 bool IsFPConditionFalseIfNaN() const {
2705 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2706 IfCondition if_cond = GetCondition();
2707 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2708 }
2709
Dave Allison20dfc792014-06-16 20:44:29 -07002710 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002711 // For register allocation purposes, returns whether this instruction needs to be
2712 // materialized (that is, not just be in the processor flags).
2713 bool needs_materialization_;
2714
Mark Mendellc4701932015-04-10 13:18:51 -04002715 // Needed if we merge a HCompare into a HCondition.
2716 ComparisonBias bias_;
2717
Dave Allison20dfc792014-06-16 20:44:29 -07002718 DISALLOW_COPY_AND_ASSIGN(HCondition);
2719};
2720
2721// Instruction to check if two inputs are equal to each other.
2722class HEqual : public HCondition {
2723 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002724 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2725 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002726
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002727 bool IsCommutative() const OVERRIDE { return true; }
2728
Roland Levillain9867bc72015-08-05 10:21:34 +01002729 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002730 return GetBlock()->GetGraph()->GetIntConstant(
2731 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002732 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002733 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002734 return GetBlock()->GetGraph()->GetIntConstant(
2735 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002736 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002737 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2738 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002739 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002740 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002741
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002742 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002743
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002744 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002745 return kCondEQ;
2746 }
2747
Mark Mendellc4701932015-04-10 13:18:51 -04002748 IfCondition GetOppositeCondition() const OVERRIDE {
2749 return kCondNE;
2750 }
2751
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002752 private:
Aart Bike9f37602015-10-09 11:15:55 -07002753 template <typename T> bool Compute(T x, T y) const { return x == y; }
2754
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002755 DISALLOW_COPY_AND_ASSIGN(HEqual);
2756};
2757
Dave Allison20dfc792014-06-16 20:44:29 -07002758class HNotEqual : public HCondition {
2759 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002760 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2761 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002762
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002763 bool IsCommutative() const OVERRIDE { return true; }
2764
Roland Levillain9867bc72015-08-05 10:21:34 +01002765 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002766 return GetBlock()->GetGraph()->GetIntConstant(
2767 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002768 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002769 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002770 return GetBlock()->GetGraph()->GetIntConstant(
2771 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002772 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002773 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2774 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002775 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002776 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002777
Dave Allison20dfc792014-06-16 20:44:29 -07002778 DECLARE_INSTRUCTION(NotEqual);
2779
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002780 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002781 return kCondNE;
2782 }
2783
Mark Mendellc4701932015-04-10 13:18:51 -04002784 IfCondition GetOppositeCondition() const OVERRIDE {
2785 return kCondEQ;
2786 }
2787
Dave Allison20dfc792014-06-16 20:44:29 -07002788 private:
Aart Bike9f37602015-10-09 11:15:55 -07002789 template <typename T> bool Compute(T x, T y) const { return x != y; }
2790
Dave Allison20dfc792014-06-16 20:44:29 -07002791 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2792};
2793
2794class HLessThan : public HCondition {
2795 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002796 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2797 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002798
Roland Levillain9867bc72015-08-05 10:21:34 +01002799 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002800 return GetBlock()->GetGraph()->GetIntConstant(
2801 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002802 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002803 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002804 return GetBlock()->GetGraph()->GetIntConstant(
2805 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002806 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002807
Dave Allison20dfc792014-06-16 20:44:29 -07002808 DECLARE_INSTRUCTION(LessThan);
2809
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002810 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002811 return kCondLT;
2812 }
2813
Mark Mendellc4701932015-04-10 13:18:51 -04002814 IfCondition GetOppositeCondition() const OVERRIDE {
2815 return kCondGE;
2816 }
2817
Dave Allison20dfc792014-06-16 20:44:29 -07002818 private:
Aart Bike9f37602015-10-09 11:15:55 -07002819 template <typename T> bool Compute(T x, T y) const { return x < y; }
2820
Dave Allison20dfc792014-06-16 20:44:29 -07002821 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2822};
2823
2824class HLessThanOrEqual : public HCondition {
2825 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002826 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2827 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002828
Roland Levillain9867bc72015-08-05 10:21:34 +01002829 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002830 return GetBlock()->GetGraph()->GetIntConstant(
2831 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002832 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002833 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002834 return GetBlock()->GetGraph()->GetIntConstant(
2835 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002836 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002837
Dave Allison20dfc792014-06-16 20:44:29 -07002838 DECLARE_INSTRUCTION(LessThanOrEqual);
2839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002840 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002841 return kCondLE;
2842 }
2843
Mark Mendellc4701932015-04-10 13:18:51 -04002844 IfCondition GetOppositeCondition() const OVERRIDE {
2845 return kCondGT;
2846 }
2847
Dave Allison20dfc792014-06-16 20:44:29 -07002848 private:
Aart Bike9f37602015-10-09 11:15:55 -07002849 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2850
Dave Allison20dfc792014-06-16 20:44:29 -07002851 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2852};
2853
2854class HGreaterThan : public HCondition {
2855 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002856 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2857 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002858
Roland Levillain9867bc72015-08-05 10:21:34 +01002859 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002860 return GetBlock()->GetGraph()->GetIntConstant(
2861 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002862 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002863 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002864 return GetBlock()->GetGraph()->GetIntConstant(
2865 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002866 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002867
Dave Allison20dfc792014-06-16 20:44:29 -07002868 DECLARE_INSTRUCTION(GreaterThan);
2869
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002870 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002871 return kCondGT;
2872 }
2873
Mark Mendellc4701932015-04-10 13:18:51 -04002874 IfCondition GetOppositeCondition() const OVERRIDE {
2875 return kCondLE;
2876 }
2877
Dave Allison20dfc792014-06-16 20:44:29 -07002878 private:
Aart Bike9f37602015-10-09 11:15:55 -07002879 template <typename T> bool Compute(T x, T y) const { return x > y; }
2880
Dave Allison20dfc792014-06-16 20:44:29 -07002881 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2882};
2883
2884class HGreaterThanOrEqual : public HCondition {
2885 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002886 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2887 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002888
Roland Levillain9867bc72015-08-05 10:21:34 +01002889 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002890 return GetBlock()->GetGraph()->GetIntConstant(
2891 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002892 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002893 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002894 return GetBlock()->GetGraph()->GetIntConstant(
2895 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002896 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002897
Dave Allison20dfc792014-06-16 20:44:29 -07002898 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2899
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002900 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002901 return kCondGE;
2902 }
2903
Mark Mendellc4701932015-04-10 13:18:51 -04002904 IfCondition GetOppositeCondition() const OVERRIDE {
2905 return kCondLT;
2906 }
2907
Dave Allison20dfc792014-06-16 20:44:29 -07002908 private:
Aart Bike9f37602015-10-09 11:15:55 -07002909 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2910
Dave Allison20dfc792014-06-16 20:44:29 -07002911 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2912};
2913
Aart Bike9f37602015-10-09 11:15:55 -07002914class HBelow : public HCondition {
2915 public:
2916 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2917 : HCondition(first, second, dex_pc) {}
2918
2919 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2920 return GetBlock()->GetGraph()->GetIntConstant(
2921 Compute(static_cast<uint32_t>(x->GetValue()),
2922 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2923 }
2924 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2925 return GetBlock()->GetGraph()->GetIntConstant(
2926 Compute(static_cast<uint64_t>(x->GetValue()),
2927 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2928 }
2929
2930 DECLARE_INSTRUCTION(Below);
2931
2932 IfCondition GetCondition() const OVERRIDE {
2933 return kCondB;
2934 }
2935
2936 IfCondition GetOppositeCondition() const OVERRIDE {
2937 return kCondAE;
2938 }
2939
2940 private:
2941 template <typename T> bool Compute(T x, T y) const { return x < y; }
2942
2943 DISALLOW_COPY_AND_ASSIGN(HBelow);
2944};
2945
2946class HBelowOrEqual : public HCondition {
2947 public:
2948 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2949 : HCondition(first, second, dex_pc) {}
2950
2951 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2952 return GetBlock()->GetGraph()->GetIntConstant(
2953 Compute(static_cast<uint32_t>(x->GetValue()),
2954 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2955 }
2956 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2957 return GetBlock()->GetGraph()->GetIntConstant(
2958 Compute(static_cast<uint64_t>(x->GetValue()),
2959 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2960 }
2961
2962 DECLARE_INSTRUCTION(BelowOrEqual);
2963
2964 IfCondition GetCondition() const OVERRIDE {
2965 return kCondBE;
2966 }
2967
2968 IfCondition GetOppositeCondition() const OVERRIDE {
2969 return kCondA;
2970 }
2971
2972 private:
2973 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2974
2975 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2976};
2977
2978class HAbove : public HCondition {
2979 public:
2980 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2981 : HCondition(first, second, dex_pc) {}
2982
2983 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2984 return GetBlock()->GetGraph()->GetIntConstant(
2985 Compute(static_cast<uint32_t>(x->GetValue()),
2986 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2987 }
2988 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2989 return GetBlock()->GetGraph()->GetIntConstant(
2990 Compute(static_cast<uint64_t>(x->GetValue()),
2991 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2992 }
2993
2994 DECLARE_INSTRUCTION(Above);
2995
2996 IfCondition GetCondition() const OVERRIDE {
2997 return kCondA;
2998 }
2999
3000 IfCondition GetOppositeCondition() const OVERRIDE {
3001 return kCondBE;
3002 }
3003
3004 private:
3005 template <typename T> bool Compute(T x, T y) const { return x > y; }
3006
3007 DISALLOW_COPY_AND_ASSIGN(HAbove);
3008};
3009
3010class HAboveOrEqual : public HCondition {
3011 public:
3012 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3013 : HCondition(first, second, dex_pc) {}
3014
3015 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3016 return GetBlock()->GetGraph()->GetIntConstant(
3017 Compute(static_cast<uint32_t>(x->GetValue()),
3018 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3019 }
3020 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3021 return GetBlock()->GetGraph()->GetIntConstant(
3022 Compute(static_cast<uint64_t>(x->GetValue()),
3023 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3024 }
3025
3026 DECLARE_INSTRUCTION(AboveOrEqual);
3027
3028 IfCondition GetCondition() const OVERRIDE {
3029 return kCondAE;
3030 }
3031
3032 IfCondition GetOppositeCondition() const OVERRIDE {
3033 return kCondB;
3034 }
3035
3036 private:
3037 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3038
3039 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3040};
Dave Allison20dfc792014-06-16 20:44:29 -07003041
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003042// Instruction to check how two inputs compare to each other.
3043// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3044class HCompare : public HBinaryOperation {
3045 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003046 HCompare(Primitive::Type type,
3047 HInstruction* first,
3048 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003049 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003050 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003051 : HBinaryOperation(Primitive::kPrimInt,
3052 first,
3053 second,
3054 SideEffectsForArchRuntimeCalls(type),
3055 dex_pc),
3056 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003057 DCHECK_EQ(type, first->GetType());
3058 DCHECK_EQ(type, second->GetType());
3059 }
3060
Roland Levillain9867bc72015-08-05 10:21:34 +01003061 template <typename T>
3062 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003063
Roland Levillain9867bc72015-08-05 10:21:34 +01003064 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003065 return GetBlock()->GetGraph()->GetIntConstant(
3066 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003067 }
3068 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003069 return GetBlock()->GetGraph()->GetIntConstant(
3070 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003071 }
3072
Calin Juravleddb7df22014-11-25 20:56:51 +00003073 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3074 return bias_ == other->AsCompare()->bias_;
3075 }
3076
Mark Mendellc4701932015-04-10 13:18:51 -04003077 ComparisonBias GetBias() const { return bias_; }
3078
Roland Levillain4fa13f62015-07-06 18:11:54 +01003079 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003080
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003081
3082 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3083 // MIPS64 uses a runtime call for FP comparisons.
3084 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3085 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003086
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003087 DECLARE_INSTRUCTION(Compare);
3088
3089 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003090 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003091
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003092 DISALLOW_COPY_AND_ASSIGN(HCompare);
3093};
3094
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003095// A local in the graph. Corresponds to a Dex register.
3096class HLocal : public HTemplateInstruction<0> {
3097 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003098 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003099 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003100
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003101 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003102
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003103 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003104
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003105 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003106 // The Dex register number.
3107 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003108
3109 DISALLOW_COPY_AND_ASSIGN(HLocal);
3110};
3111
3112// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003113class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003114 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003115 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3116 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003117 SetRawInputAt(0, local);
3118 }
3119
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003120 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3121
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003122 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003123
3124 private:
3125 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3126};
3127
3128// Store a value in a given local. This instruction has two inputs: the value
3129// and the local.
3130class HStoreLocal : public HTemplateInstruction<2> {
3131 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003132 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3133 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003134 SetRawInputAt(0, local);
3135 SetRawInputAt(1, value);
3136 }
3137
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003138 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3139
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003140 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003141
3142 private:
3143 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3144};
3145
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003146class HFloatConstant : public HConstant {
3147 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003148 float GetValue() const { return value_; }
3149
David Brazdil77a48ae2015-09-15 12:34:04 +00003150 uint64_t GetValueAsUint64() const OVERRIDE {
3151 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3152 }
3153
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003154 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003155 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003156 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003157 }
3158
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003159 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003160
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003161 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003162 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003163 }
3164 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003165 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003166 }
3167 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003168 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3169 }
3170 bool IsNaN() const {
3171 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003172 }
3173
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003174 DECLARE_INSTRUCTION(FloatConstant);
3175
3176 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003177 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3178 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3179 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3180 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003181
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003182 const float value_;
3183
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003184 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003185 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003186 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003187 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3188};
3189
3190class HDoubleConstant : public HConstant {
3191 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003192 double GetValue() const { return value_; }
3193
David Brazdil77a48ae2015-09-15 12:34:04 +00003194 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3195
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003196 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003197 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003198 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003199 }
3200
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003201 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003202
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003203 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003204 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003205 }
3206 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003207 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003208 }
3209 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003210 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3211 }
3212 bool IsNaN() const {
3213 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003214 }
3215
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003216 DECLARE_INSTRUCTION(DoubleConstant);
3217
3218 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003219 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3220 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3221 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3222 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003223
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003224 const double value_;
3225
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003226 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003227 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003228 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003229 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3230};
3231
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003232enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003233#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003234#include "intrinsics_list.h"
3235 kNone,
3236 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3237#undef INTRINSICS_LIST
3238#undef OPTIMIZING_INTRINSICS
3239};
3240std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3241
Agi Csaki05f20562015-08-19 14:58:14 -07003242enum IntrinsicNeedsEnvironmentOrCache {
3243 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3244 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003245};
3246
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003247class HInvoke : public HInstruction {
3248 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003249 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003250
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003251 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003252
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003253 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003254 SetRawInputAt(index, argument);
3255 }
3256
Roland Levillain3e3d7332015-04-28 11:00:54 +01003257 // Return the number of arguments. This number can be lower than
3258 // the number of inputs returned by InputCount(), as some invoke
3259 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3260 // inputs at the end of their list of inputs.
3261 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3262
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003263 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003264
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003265
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003266 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003267 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003268
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003269 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3270
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003271 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003272 return intrinsic_;
3273 }
3274
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003275 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003276
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003277 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003278 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003279 }
3280
3281 bool CanThrow() const OVERRIDE { return true; }
3282
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003283 uint32_t* GetIntrinsicOptimizations() {
3284 return &intrinsic_optimizations_;
3285 }
3286
3287 const uint32_t* GetIntrinsicOptimizations() const {
3288 return &intrinsic_optimizations_;
3289 }
3290
3291 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3292
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003293 DECLARE_INSTRUCTION(Invoke);
3294
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003295 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003296 HInvoke(ArenaAllocator* arena,
3297 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003298 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003299 Primitive::Type return_type,
3300 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003301 uint32_t dex_method_index,
3302 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003303 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003304 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003305 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003306 inputs_(number_of_arguments + number_of_other_inputs,
3307 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003308 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003309 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003310 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003311 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003312 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003313 }
3314
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003315 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003316 return inputs_[index];
3317 }
3318
David Brazdil1abb4192015-02-17 18:33:36 +00003319 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003320 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003321 }
3322
Roland Levillain3e3d7332015-04-28 11:00:54 +01003323 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003324 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003325 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003326 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003327 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003328 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003329
3330 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3331 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003332
3333 private:
3334 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3335};
3336
Calin Juravle175dc732015-08-25 15:42:32 +01003337class HInvokeUnresolved : public HInvoke {
3338 public:
3339 HInvokeUnresolved(ArenaAllocator* arena,
3340 uint32_t number_of_arguments,
3341 Primitive::Type return_type,
3342 uint32_t dex_pc,
3343 uint32_t dex_method_index,
3344 InvokeType invoke_type)
3345 : HInvoke(arena,
3346 number_of_arguments,
3347 0u /* number_of_other_inputs */,
3348 return_type,
3349 dex_pc,
3350 dex_method_index,
3351 invoke_type) {
3352 }
3353
3354 DECLARE_INSTRUCTION(InvokeUnresolved);
3355
3356 private:
3357 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3358};
3359
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003360class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003361 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003362 // Requirements of this method call regarding the class
3363 // initialization (clinit) check of its declaring class.
3364 enum class ClinitCheckRequirement {
3365 kNone, // Class already initialized.
3366 kExplicit, // Static call having explicit clinit check as last input.
3367 kImplicit, // Static call implicitly requiring a clinit check.
3368 };
3369
Vladimir Marko58155012015-08-19 12:49:41 +00003370 // Determines how to load the target ArtMethod*.
3371 enum class MethodLoadKind {
3372 // Use a String init ArtMethod* loaded from Thread entrypoints.
3373 kStringInit,
3374
3375 // Use the method's own ArtMethod* loaded by the register allocator.
3376 kRecursive,
3377
3378 // Use ArtMethod* at a known address, embed the direct address in the code.
3379 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3380 kDirectAddress,
3381
3382 // Use ArtMethod* at an address that will be known at link time, embed the direct
3383 // address in the code. If the image is relocatable, emit .patch_oat entry.
3384 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3385 // the image relocatable or not.
3386 kDirectAddressWithFixup,
3387
3388 // Load from resoved methods array in the dex cache using a PC-relative load.
3389 // Used when we need to use the dex cache, for example for invoke-static that
3390 // may cause class initialization (the entry may point to a resolution method),
3391 // and we know that we can access the dex cache arrays using a PC-relative load.
3392 kDexCachePcRelative,
3393
3394 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3395 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3396 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3397 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3398 kDexCacheViaMethod,
3399 };
3400
3401 // Determines the location of the code pointer.
3402 enum class CodePtrLocation {
3403 // Recursive call, use local PC-relative call instruction.
3404 kCallSelf,
3405
3406 // Use PC-relative call instruction patched at link time.
3407 // Used for calls within an oat file, boot->boot or app->app.
3408 kCallPCRelative,
3409
3410 // Call to a known target address, embed the direct address in code.
3411 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3412 kCallDirect,
3413
3414 // Call to a target address that will be known at link time, embed the direct
3415 // address in code. If the image is relocatable, emit .patch_oat entry.
3416 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3417 // the image relocatable or not.
3418 kCallDirectWithFixup,
3419
3420 // Use code pointer from the ArtMethod*.
3421 // Used when we don't know the target code. This is also the last-resort-kind used when
3422 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3423 kCallArtMethod,
3424 };
3425
3426 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003427 MethodLoadKind method_load_kind;
3428 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003429 // The method load data holds
3430 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3431 // Note that there are multiple string init methods, each having its own offset.
3432 // - the method address for kDirectAddress
3433 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003434 uint64_t method_load_data;
3435 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003436 };
3437
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003438 HInvokeStaticOrDirect(ArenaAllocator* arena,
3439 uint32_t number_of_arguments,
3440 Primitive::Type return_type,
3441 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003442 uint32_t method_index,
3443 MethodReference target_method,
3444 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003445 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003446 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003447 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003448 : HInvoke(arena,
3449 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003450 // There is potentially one extra argument for the HCurrentMethod node, and
3451 // potentially one other if the clinit check is explicit, and potentially
3452 // one other if the method is a string factory.
3453 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3454 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3455 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003456 return_type,
3457 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003458 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003459 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003460 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003461 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003462 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003463 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003464
Vladimir Markodc151b22015-10-15 18:02:30 +01003465 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003466 bool had_current_method_input = HasCurrentMethodInput();
3467 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3468
3469 // Using the current method is the default and once we find a better
3470 // method load kind, we should not go back to using the current method.
3471 DCHECK(had_current_method_input || !needs_current_method_input);
3472
3473 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003474 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3475 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003476 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003477 dispatch_info_ = dispatch_info;
3478 }
3479
Vladimir Markoc53c0792015-11-19 15:48:33 +00003480 void AddSpecialInput(HInstruction* input) {
3481 // We allow only one special input.
3482 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3483 DCHECK(InputCount() == GetSpecialInputIndex() ||
3484 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3485 InsertInputAt(GetSpecialInputIndex(), input);
3486 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003487
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003488 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003489 // We access the method via the dex cache so we can't do an implicit null check.
3490 // TODO: for intrinsics we can generate implicit null checks.
3491 return false;
3492 }
3493
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003494 bool CanBeNull() const OVERRIDE {
3495 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3496 }
3497
Vladimir Markoc53c0792015-11-19 15:48:33 +00003498 // Get the index of the special input, if any.
3499 //
3500 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3501 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3502 // the "special input" is the current method pointer; otherwise there may be one
3503 // platform-specific special input, such as PC-relative addressing base.
3504 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3505
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003506 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3507 void SetOptimizedInvokeType(InvokeType invoke_type) {
3508 optimized_invoke_type_ = invoke_type;
3509 }
3510
Vladimir Marko58155012015-08-19 12:49:41 +00003511 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3512 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3513 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003514 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003515 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003516 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003517 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003518 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3519 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003520 bool HasCurrentMethodInput() const {
3521 // This function can be called only after the invoke has been fully initialized by the builder.
3522 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003523 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003524 return true;
3525 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003526 DCHECK(InputCount() == GetSpecialInputIndex() ||
3527 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003528 return false;
3529 }
3530 }
Vladimir Marko58155012015-08-19 12:49:41 +00003531 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3532 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003533 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003534
3535 int32_t GetStringInitOffset() const {
3536 DCHECK(IsStringInit());
3537 return dispatch_info_.method_load_data;
3538 }
3539
3540 uint64_t GetMethodAddress() const {
3541 DCHECK(HasMethodAddress());
3542 return dispatch_info_.method_load_data;
3543 }
3544
3545 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003546 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003547 return dispatch_info_.method_load_data;
3548 }
3549
3550 uint64_t GetDirectCodePtr() const {
3551 DCHECK(HasDirectCodePtr());
3552 return dispatch_info_.direct_code_ptr;
3553 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003554
Calin Juravle68ad6492015-08-18 17:08:12 +01003555 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3556
Roland Levillain4c0eb422015-04-24 16:43:49 +01003557 // Is this instruction a call to a static method?
3558 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003559 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003560 }
3561
Vladimir Markofbb184a2015-11-13 14:47:00 +00003562 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3563 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3564 // instruction; only relevant for static calls with explicit clinit check.
3565 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003566 DCHECK(IsStaticWithExplicitClinitCheck());
3567 size_t last_input_index = InputCount() - 1;
3568 HInstruction* last_input = InputAt(last_input_index);
3569 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003570 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003571 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003572 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003573 clinit_check_requirement_ = new_requirement;
3574 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003575 }
3576
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003577 bool IsStringFactoryFor(HFakeString* str) const {
3578 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003579 DCHECK(!HasCurrentMethodInput());
3580 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003581 return InputAt(InputCount() - 1)->AsFakeString() == str;
3582 }
3583
3584 void RemoveFakeStringArgumentAsLastInput() {
3585 DCHECK(IsStringInit());
3586 size_t last_input_index = InputCount() - 1;
3587 HInstruction* last_input = InputAt(last_input_index);
3588 DCHECK(last_input != nullptr);
3589 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3590 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003591 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003592 }
3593
Roland Levillain4c0eb422015-04-24 16:43:49 +01003594 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003595 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003596 bool IsStaticWithExplicitClinitCheck() const {
3597 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3598 }
3599
3600 // Is this a call to a static method whose declaring class has an
3601 // implicit intialization check requirement?
3602 bool IsStaticWithImplicitClinitCheck() const {
3603 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3604 }
3605
Vladimir Markob554b5a2015-11-06 12:57:55 +00003606 // Does this method load kind need the current method as an input?
3607 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3608 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3609 }
3610
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003611 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003612
Roland Levillain4c0eb422015-04-24 16:43:49 +01003613 protected:
3614 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3615 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3616 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3617 HInstruction* input = input_record.GetInstruction();
3618 // `input` is the last input of a static invoke marked as having
3619 // an explicit clinit check. It must either be:
3620 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3621 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3622 DCHECK(input != nullptr);
3623 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3624 }
3625 return input_record;
3626 }
3627
Vladimir Markoc53c0792015-11-19 15:48:33 +00003628 void InsertInputAt(size_t index, HInstruction* input);
3629 void RemoveInputAt(size_t index);
3630
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003631 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003632 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003633 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003634 // The target method may refer to different dex file or method index than the original
3635 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3636 // in derived class to increase visibility.
3637 MethodReference target_method_;
3638 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003639
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003640 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003641};
Vladimir Markof64242a2015-12-01 14:58:23 +00003642std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003643std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003644
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003645class HInvokeVirtual : public HInvoke {
3646 public:
3647 HInvokeVirtual(ArenaAllocator* arena,
3648 uint32_t number_of_arguments,
3649 Primitive::Type return_type,
3650 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003651 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003652 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003653 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003654 vtable_index_(vtable_index) {}
3655
Calin Juravle641547a2015-04-21 22:08:51 +01003656 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003657 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003658 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003659 }
3660
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003661 uint32_t GetVTableIndex() const { return vtable_index_; }
3662
3663 DECLARE_INSTRUCTION(InvokeVirtual);
3664
3665 private:
3666 const uint32_t vtable_index_;
3667
3668 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3669};
3670
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003671class HInvokeInterface : public HInvoke {
3672 public:
3673 HInvokeInterface(ArenaAllocator* arena,
3674 uint32_t number_of_arguments,
3675 Primitive::Type return_type,
3676 uint32_t dex_pc,
3677 uint32_t dex_method_index,
3678 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003679 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003680 imt_index_(imt_index) {}
3681
Calin Juravle641547a2015-04-21 22:08:51 +01003682 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003683 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003684 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003685 }
3686
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003687 uint32_t GetImtIndex() const { return imt_index_; }
3688 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3689
3690 DECLARE_INSTRUCTION(InvokeInterface);
3691
3692 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003693 const uint32_t imt_index_;
3694
3695 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3696};
3697
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003698class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003699 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003700 HNewInstance(HInstruction* cls,
3701 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003702 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003703 uint16_t type_index,
3704 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003705 bool can_throw,
3706 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003707 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003708 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003709 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003710 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003711 can_throw_(can_throw),
3712 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003713 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003714 SetRawInputAt(0, cls);
3715 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003716 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003717
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003718 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003719 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003720
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003721 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003722 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003723
3724 // It may throw when called on type that's not instantiable/accessible.
3725 // It can throw OOME.
3726 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3727 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3728
3729 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003730
Calin Juravle10e244f2015-01-26 18:54:32 +00003731 bool CanBeNull() const OVERRIDE { return false; }
3732
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003733 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3734
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003735 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3736 entrypoint_ = entrypoint;
3737 }
3738
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003739 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003740
3741 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003742 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003743 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003744 const bool can_throw_;
3745 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003746 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003747
3748 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3749};
3750
Roland Levillain88cb1752014-10-20 16:36:47 +01003751class HNeg : public HUnaryOperation {
3752 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003753 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3754 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003755
Roland Levillain9867bc72015-08-05 10:21:34 +01003756 template <typename T> T Compute(T x) const { return -x; }
3757
3758 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003759 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003760 }
3761 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003762 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003763 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003764
Roland Levillain88cb1752014-10-20 16:36:47 +01003765 DECLARE_INSTRUCTION(Neg);
3766
3767 private:
3768 DISALLOW_COPY_AND_ASSIGN(HNeg);
3769};
3770
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003771class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003772 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003773 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003774 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003775 uint32_t dex_pc,
3776 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003777 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003778 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003779 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003780 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003781 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003782 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003783 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003784 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003785 }
3786
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003787 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003788 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003789
3790 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003791 bool NeedsEnvironment() const OVERRIDE { return true; }
3792
Mingyao Yang0c365e62015-03-31 15:09:29 -07003793 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3794 bool CanThrow() const OVERRIDE { return true; }
3795
Calin Juravle10e244f2015-01-26 18:54:32 +00003796 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003797
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003798 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3799
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003800 DECLARE_INSTRUCTION(NewArray);
3801
3802 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003803 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003804 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003805 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003806
3807 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3808};
3809
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003810class HAdd : public HBinaryOperation {
3811 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003812 HAdd(Primitive::Type result_type,
3813 HInstruction* left,
3814 HInstruction* right,
3815 uint32_t dex_pc = kNoDexPc)
3816 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003817
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003818 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003819
Roland Levillain9867bc72015-08-05 10:21:34 +01003820 template <typename T> T Compute(T x, T y) const { return x + y; }
3821
3822 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003823 return GetBlock()->GetGraph()->GetIntConstant(
3824 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003825 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003826 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003827 return GetBlock()->GetGraph()->GetLongConstant(
3828 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003829 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003830
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003831 DECLARE_INSTRUCTION(Add);
3832
3833 private:
3834 DISALLOW_COPY_AND_ASSIGN(HAdd);
3835};
3836
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003837class HSub : public HBinaryOperation {
3838 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003839 HSub(Primitive::Type result_type,
3840 HInstruction* left,
3841 HInstruction* right,
3842 uint32_t dex_pc = kNoDexPc)
3843 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003844
Roland Levillain9867bc72015-08-05 10:21:34 +01003845 template <typename T> T Compute(T x, T y) const { return x - y; }
3846
3847 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003848 return GetBlock()->GetGraph()->GetIntConstant(
3849 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003850 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003851 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003852 return GetBlock()->GetGraph()->GetLongConstant(
3853 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003854 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003855
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003856 DECLARE_INSTRUCTION(Sub);
3857
3858 private:
3859 DISALLOW_COPY_AND_ASSIGN(HSub);
3860};
3861
Calin Juravle34bacdf2014-10-07 20:23:36 +01003862class HMul : public HBinaryOperation {
3863 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003864 HMul(Primitive::Type result_type,
3865 HInstruction* left,
3866 HInstruction* right,
3867 uint32_t dex_pc = kNoDexPc)
3868 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003869
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003870 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003871
Roland Levillain9867bc72015-08-05 10:21:34 +01003872 template <typename T> T Compute(T x, T y) const { return x * y; }
3873
3874 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003875 return GetBlock()->GetGraph()->GetIntConstant(
3876 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003877 }
3878 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003879 return GetBlock()->GetGraph()->GetLongConstant(
3880 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003881 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003882
3883 DECLARE_INSTRUCTION(Mul);
3884
3885 private:
3886 DISALLOW_COPY_AND_ASSIGN(HMul);
3887};
3888
Calin Juravle7c4954d2014-10-28 16:57:40 +00003889class HDiv : public HBinaryOperation {
3890 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003891 HDiv(Primitive::Type result_type,
3892 HInstruction* left,
3893 HInstruction* right,
3894 uint32_t dex_pc)
3895 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003896
Roland Levillain9867bc72015-08-05 10:21:34 +01003897 template <typename T>
3898 T Compute(T x, T y) const {
3899 // Our graph structure ensures we never have 0 for `y` during
3900 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003901 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003902 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003903 return (y == -1) ? -x : x / y;
3904 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003905
Roland Levillain9867bc72015-08-05 10:21:34 +01003906 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003907 return GetBlock()->GetGraph()->GetIntConstant(
3908 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003909 }
3910 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003911 return GetBlock()->GetGraph()->GetLongConstant(
3912 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003913 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003914
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003915 static SideEffects SideEffectsForArchRuntimeCalls() {
3916 // The generated code can use a runtime call.
3917 return SideEffects::CanTriggerGC();
3918 }
3919
Calin Juravle7c4954d2014-10-28 16:57:40 +00003920 DECLARE_INSTRUCTION(Div);
3921
3922 private:
3923 DISALLOW_COPY_AND_ASSIGN(HDiv);
3924};
3925
Calin Juravlebacfec32014-11-14 15:54:36 +00003926class HRem : public HBinaryOperation {
3927 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003928 HRem(Primitive::Type result_type,
3929 HInstruction* left,
3930 HInstruction* right,
3931 uint32_t dex_pc)
3932 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003933
Roland Levillain9867bc72015-08-05 10:21:34 +01003934 template <typename T>
3935 T Compute(T x, T y) const {
3936 // Our graph structure ensures we never have 0 for `y` during
3937 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003938 DCHECK_NE(y, 0);
3939 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3940 return (y == -1) ? 0 : x % y;
3941 }
3942
Roland Levillain9867bc72015-08-05 10:21:34 +01003943 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003944 return GetBlock()->GetGraph()->GetIntConstant(
3945 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003946 }
3947 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003948 return GetBlock()->GetGraph()->GetLongConstant(
3949 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003950 }
3951
Calin Juravlebacfec32014-11-14 15:54:36 +00003952
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003953 static SideEffects SideEffectsForArchRuntimeCalls() {
3954 return SideEffects::CanTriggerGC();
3955 }
3956
Calin Juravlebacfec32014-11-14 15:54:36 +00003957 DECLARE_INSTRUCTION(Rem);
3958
3959 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003960 DISALLOW_COPY_AND_ASSIGN(HRem);
3961};
3962
Calin Juravled0d48522014-11-04 16:40:20 +00003963class HDivZeroCheck : public HExpression<1> {
3964 public:
3965 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003966 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003967 SetRawInputAt(0, value);
3968 }
3969
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003970 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3971
Calin Juravled0d48522014-11-04 16:40:20 +00003972 bool CanBeMoved() const OVERRIDE { return true; }
3973
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003974 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003975 return true;
3976 }
3977
3978 bool NeedsEnvironment() const OVERRIDE { return true; }
3979 bool CanThrow() const OVERRIDE { return true; }
3980
Calin Juravled0d48522014-11-04 16:40:20 +00003981 DECLARE_INSTRUCTION(DivZeroCheck);
3982
3983 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003984 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3985};
3986
Calin Juravle9aec02f2014-11-18 23:06:35 +00003987class HShl : public HBinaryOperation {
3988 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003989 HShl(Primitive::Type result_type,
3990 HInstruction* left,
3991 HInstruction* right,
3992 uint32_t dex_pc = kNoDexPc)
3993 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003994
Roland Levillain9867bc72015-08-05 10:21:34 +01003995 template <typename T, typename U, typename V>
3996 T Compute(T x, U y, V max_shift_value) const {
3997 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3998 "V is not the unsigned integer type corresponding to T");
3999 return x << (y & max_shift_value);
4000 }
4001
4002 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4003 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004004 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004005 }
4006 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4007 // case is handled as `x << static_cast<int>(y)`.
4008 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4009 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004010 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004011 }
4012 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4013 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004014 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004015 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004016
4017 DECLARE_INSTRUCTION(Shl);
4018
4019 private:
4020 DISALLOW_COPY_AND_ASSIGN(HShl);
4021};
4022
4023class HShr : public HBinaryOperation {
4024 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004025 HShr(Primitive::Type result_type,
4026 HInstruction* left,
4027 HInstruction* right,
4028 uint32_t dex_pc = kNoDexPc)
4029 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004030
Roland Levillain9867bc72015-08-05 10:21:34 +01004031 template <typename T, typename U, typename V>
4032 T Compute(T x, U y, V max_shift_value) const {
4033 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4034 "V is not the unsigned integer type corresponding to T");
4035 return x >> (y & max_shift_value);
4036 }
4037
4038 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4039 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004040 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004041 }
4042 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4043 // case is handled as `x >> static_cast<int>(y)`.
4044 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4045 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004046 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004047 }
4048 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4049 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004050 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004051 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004052
4053 DECLARE_INSTRUCTION(Shr);
4054
4055 private:
4056 DISALLOW_COPY_AND_ASSIGN(HShr);
4057};
4058
4059class HUShr : public HBinaryOperation {
4060 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004061 HUShr(Primitive::Type result_type,
4062 HInstruction* left,
4063 HInstruction* right,
4064 uint32_t dex_pc = kNoDexPc)
4065 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004066
Roland Levillain9867bc72015-08-05 10:21:34 +01004067 template <typename T, typename U, typename V>
4068 T Compute(T x, U y, V max_shift_value) const {
4069 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4070 "V is not the unsigned integer type corresponding to T");
4071 V ux = static_cast<V>(x);
4072 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004073 }
4074
Roland Levillain9867bc72015-08-05 10:21:34 +01004075 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4076 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004077 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004078 }
4079 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4080 // case is handled as `x >>> static_cast<int>(y)`.
4081 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4082 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004083 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004084 }
4085 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4086 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004087 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004088 }
4089
4090 DECLARE_INSTRUCTION(UShr);
4091
4092 private:
4093 DISALLOW_COPY_AND_ASSIGN(HUShr);
4094};
4095
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004096class HAnd : public HBinaryOperation {
4097 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004098 HAnd(Primitive::Type result_type,
4099 HInstruction* left,
4100 HInstruction* right,
4101 uint32_t dex_pc = kNoDexPc)
4102 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004103
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004104 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004105
Roland Levillain9867bc72015-08-05 10:21:34 +01004106 template <typename T, typename U>
4107 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4108
4109 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004110 return GetBlock()->GetGraph()->GetIntConstant(
4111 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004112 }
4113 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004114 return GetBlock()->GetGraph()->GetLongConstant(
4115 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004116 }
4117 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004118 return GetBlock()->GetGraph()->GetLongConstant(
4119 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004120 }
4121 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004122 return GetBlock()->GetGraph()->GetLongConstant(
4123 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004124 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004125
4126 DECLARE_INSTRUCTION(And);
4127
4128 private:
4129 DISALLOW_COPY_AND_ASSIGN(HAnd);
4130};
4131
4132class HOr : public HBinaryOperation {
4133 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004134 HOr(Primitive::Type result_type,
4135 HInstruction* left,
4136 HInstruction* right,
4137 uint32_t dex_pc = kNoDexPc)
4138 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004139
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004140 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004141
Roland Levillain9867bc72015-08-05 10:21:34 +01004142 template <typename T, typename U>
4143 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4144
4145 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004146 return GetBlock()->GetGraph()->GetIntConstant(
4147 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004148 }
4149 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004150 return GetBlock()->GetGraph()->GetLongConstant(
4151 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004152 }
4153 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004154 return GetBlock()->GetGraph()->GetLongConstant(
4155 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004156 }
4157 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004158 return GetBlock()->GetGraph()->GetLongConstant(
4159 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004160 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004161
4162 DECLARE_INSTRUCTION(Or);
4163
4164 private:
4165 DISALLOW_COPY_AND_ASSIGN(HOr);
4166};
4167
4168class HXor : public HBinaryOperation {
4169 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004170 HXor(Primitive::Type result_type,
4171 HInstruction* left,
4172 HInstruction* right,
4173 uint32_t dex_pc = kNoDexPc)
4174 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004175
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004176 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004177
Roland Levillain9867bc72015-08-05 10:21:34 +01004178 template <typename T, typename U>
4179 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4180
4181 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004182 return GetBlock()->GetGraph()->GetIntConstant(
4183 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004184 }
4185 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004186 return GetBlock()->GetGraph()->GetLongConstant(
4187 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004188 }
4189 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004190 return GetBlock()->GetGraph()->GetLongConstant(
4191 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004192 }
4193 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004194 return GetBlock()->GetGraph()->GetLongConstant(
4195 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004196 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004197
4198 DECLARE_INSTRUCTION(Xor);
4199
4200 private:
4201 DISALLOW_COPY_AND_ASSIGN(HXor);
4202};
4203
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004204// The value of a parameter in this method. Its location depends on
4205// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004206class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004207 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004208 HParameterValue(const DexFile& dex_file,
4209 uint16_t type_index,
4210 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004211 Primitive::Type parameter_type,
4212 bool is_this = false)
4213 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004214 dex_file_(dex_file),
4215 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004216 index_(index),
4217 is_this_(is_this),
4218 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004219
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004220 const DexFile& GetDexFile() const { return dex_file_; }
4221 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004222 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004223 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004224
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004225 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4226 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004227
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004228 DECLARE_INSTRUCTION(ParameterValue);
4229
4230 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004231 const DexFile& dex_file_;
4232 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004233 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004234 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004235 const uint8_t index_;
4236
Calin Juravle10e244f2015-01-26 18:54:32 +00004237 // Whether or not the parameter value corresponds to 'this' argument.
4238 const bool is_this_;
4239
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004240 bool can_be_null_;
4241
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004242 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4243};
4244
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004245class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004246 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004247 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4248 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004249
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004250 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004251 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004252 return true;
4253 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004254
Roland Levillain9867bc72015-08-05 10:21:34 +01004255 template <typename T> T Compute(T x) const { return ~x; }
4256
4257 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004258 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004259 }
4260 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004261 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004262 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004263
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004264 DECLARE_INSTRUCTION(Not);
4265
4266 private:
4267 DISALLOW_COPY_AND_ASSIGN(HNot);
4268};
4269
David Brazdil66d126e2015-04-03 16:02:44 +01004270class HBooleanNot : public HUnaryOperation {
4271 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004272 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4273 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004274
4275 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004276 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004277 return true;
4278 }
4279
Roland Levillain9867bc72015-08-05 10:21:34 +01004280 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004281 DCHECK(IsUint<1>(x));
4282 return !x;
4283 }
4284
Roland Levillain9867bc72015-08-05 10:21:34 +01004285 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004286 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004287 }
4288 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4289 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004290 UNREACHABLE();
4291 }
4292
4293 DECLARE_INSTRUCTION(BooleanNot);
4294
4295 private:
4296 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4297};
4298
Roland Levillaindff1f282014-11-05 14:15:05 +00004299class HTypeConversion : public HExpression<1> {
4300 public:
4301 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004302 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004303 : HExpression(result_type,
4304 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4305 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004306 SetRawInputAt(0, input);
4307 DCHECK_NE(input->GetType(), result_type);
4308 }
4309
4310 HInstruction* GetInput() const { return InputAt(0); }
4311 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4312 Primitive::Type GetResultType() const { return GetType(); }
4313
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004314 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004315 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004316
Roland Levillaindff1f282014-11-05 14:15:05 +00004317 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004318 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004319
Mark Mendelle82549b2015-05-06 10:55:34 -04004320 // Try to statically evaluate the conversion and return a HConstant
4321 // containing the result. If the input cannot be converted, return nullptr.
4322 HConstant* TryStaticEvaluation() const;
4323
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004324 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4325 Primitive::Type result_type) {
4326 // Some architectures may not require the 'GC' side effects, but at this point
4327 // in the compilation process we do not know what architecture we will
4328 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004329 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4330 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004331 return SideEffects::CanTriggerGC();
4332 }
4333 return SideEffects::None();
4334 }
4335
Roland Levillaindff1f282014-11-05 14:15:05 +00004336 DECLARE_INSTRUCTION(TypeConversion);
4337
4338 private:
4339 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4340};
4341
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004342static constexpr uint32_t kNoRegNumber = -1;
4343
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004344class HPhi : public HInstruction {
4345 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004346 HPhi(ArenaAllocator* arena,
4347 uint32_t reg_number,
4348 size_t number_of_inputs,
4349 Primitive::Type type,
4350 uint32_t dex_pc = kNoDexPc)
4351 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004352 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004353 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004354 type_(ToPhiType(type)),
4355 // Phis are constructed live and marked dead if conflicting or unused.
4356 // Individual steps of SsaBuilder should assume that if a phi has been
4357 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4358 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004359 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004360 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004361 }
4362
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004363 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4364 static Primitive::Type ToPhiType(Primitive::Type type) {
4365 switch (type) {
4366 case Primitive::kPrimBoolean:
4367 case Primitive::kPrimByte:
4368 case Primitive::kPrimShort:
4369 case Primitive::kPrimChar:
4370 return Primitive::kPrimInt;
4371 default:
4372 return type;
4373 }
4374 }
4375
David Brazdilffee3d32015-07-06 11:48:53 +01004376 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4377
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004378 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004379
4380 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004381 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004382
Calin Juravle10e244f2015-01-26 18:54:32 +00004383 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004384 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004385
Calin Juravle10e244f2015-01-26 18:54:32 +00004386 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4387 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4388
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004389 uint32_t GetRegNumber() const { return reg_number_; }
4390
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004391 void SetDead() { is_live_ = false; }
4392 void SetLive() { is_live_ = true; }
4393 bool IsDead() const { return !is_live_; }
4394 bool IsLive() const { return is_live_; }
4395
David Brazdil77a48ae2015-09-15 12:34:04 +00004396 bool IsVRegEquivalentOf(HInstruction* other) const {
4397 return other != nullptr
4398 && other->IsPhi()
4399 && other->AsPhi()->GetBlock() == GetBlock()
4400 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4401 }
4402
Calin Juravlea4f88312015-04-16 12:57:19 +01004403 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4404 // An equivalent phi is a phi having the same dex register and type.
4405 // It assumes that phis with the same dex register are adjacent.
4406 HPhi* GetNextEquivalentPhiWithSameType() {
4407 HInstruction* next = GetNext();
4408 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4409 if (next->GetType() == GetType()) {
4410 return next->AsPhi();
4411 }
4412 next = next->GetNext();
4413 }
4414 return nullptr;
4415 }
4416
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004417 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004418
David Brazdil1abb4192015-02-17 18:33:36 +00004419 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004420 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004421 return inputs_[index];
4422 }
David Brazdil1abb4192015-02-17 18:33:36 +00004423
4424 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004425 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004426 }
4427
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004428 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004429 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004430 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004431 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004432 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004433 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004434
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004435 DISALLOW_COPY_AND_ASSIGN(HPhi);
4436};
4437
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004438class HNullCheck : public HExpression<1> {
4439 public:
4440 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004441 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004442 SetRawInputAt(0, value);
4443 }
4444
Calin Juravle10e244f2015-01-26 18:54:32 +00004445 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004446 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004447 return true;
4448 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004449
Calin Juravle10e244f2015-01-26 18:54:32 +00004450 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004451
Calin Juravle10e244f2015-01-26 18:54:32 +00004452 bool CanThrow() const OVERRIDE { return true; }
4453
4454 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004455
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004456
4457 DECLARE_INSTRUCTION(NullCheck);
4458
4459 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004460 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4461};
4462
4463class FieldInfo : public ValueObject {
4464 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004465 FieldInfo(MemberOffset field_offset,
4466 Primitive::Type field_type,
4467 bool is_volatile,
4468 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004469 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004470 const DexFile& dex_file,
4471 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004472 : field_offset_(field_offset),
4473 field_type_(field_type),
4474 is_volatile_(is_volatile),
4475 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004476 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004477 dex_file_(dex_file),
4478 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004479
4480 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004481 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004482 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004483 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004484 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004485 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004486 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004487
4488 private:
4489 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004490 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004491 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004492 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004493 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004494 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004495 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004496};
4497
4498class HInstanceFieldGet : public HExpression<1> {
4499 public:
4500 HInstanceFieldGet(HInstruction* value,
4501 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004502 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004503 bool is_volatile,
4504 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004505 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004506 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004507 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004508 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004509 : HExpression(field_type,
4510 SideEffects::FieldReadOfType(field_type, is_volatile),
4511 dex_pc),
4512 field_info_(field_offset,
4513 field_type,
4514 is_volatile,
4515 field_idx,
4516 declaring_class_def_index,
4517 dex_file,
4518 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004519 SetRawInputAt(0, value);
4520 }
4521
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004522 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004523
4524 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4525 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4526 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004527 }
4528
Calin Juravle641547a2015-04-21 22:08:51 +01004529 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4530 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004531 }
4532
4533 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004534 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4535 }
4536
Calin Juravle52c48962014-12-16 17:02:57 +00004537 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004538 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004539 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004540 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004541
4542 DECLARE_INSTRUCTION(InstanceFieldGet);
4543
4544 private:
4545 const FieldInfo field_info_;
4546
4547 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4548};
4549
4550class HInstanceFieldSet : public HTemplateInstruction<2> {
4551 public:
4552 HInstanceFieldSet(HInstruction* object,
4553 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004554 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004555 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004556 bool is_volatile,
4557 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004558 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004559 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004560 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004561 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004562 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4563 dex_pc),
4564 field_info_(field_offset,
4565 field_type,
4566 is_volatile,
4567 field_idx,
4568 declaring_class_def_index,
4569 dex_file,
4570 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004571 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004572 SetRawInputAt(0, object);
4573 SetRawInputAt(1, value);
4574 }
4575
Calin Juravle641547a2015-04-21 22:08:51 +01004576 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4577 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004578 }
4579
Calin Juravle52c48962014-12-16 17:02:57 +00004580 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004581 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004582 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004583 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004584 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004585 bool GetValueCanBeNull() const { return value_can_be_null_; }
4586 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004587
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004588 DECLARE_INSTRUCTION(InstanceFieldSet);
4589
4590 private:
4591 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004592 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004593
4594 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4595};
4596
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597class HArrayGet : public HExpression<2> {
4598 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004599 HArrayGet(HInstruction* array,
4600 HInstruction* index,
4601 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004602 uint32_t dex_pc,
4603 SideEffects additional_side_effects = SideEffects::None())
4604 : HExpression(type,
4605 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004606 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004607 SetRawInputAt(0, array);
4608 SetRawInputAt(1, index);
4609 }
4610
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004611 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004612 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004613 return true;
4614 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004615 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004616 // TODO: We can be smarter here.
4617 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4618 // which generates the implicit null check. There are cases when these can be removed
4619 // to produce better code. If we ever add optimizations to do so we should allow an
4620 // implicit check here (as long as the address falls in the first page).
4621 return false;
4622 }
4623
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004624 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004625
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004626 HInstruction* GetArray() const { return InputAt(0); }
4627 HInstruction* GetIndex() const { return InputAt(1); }
4628
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004629 DECLARE_INSTRUCTION(ArrayGet);
4630
4631 private:
4632 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4633};
4634
4635class HArraySet : public HTemplateInstruction<3> {
4636 public:
4637 HArraySet(HInstruction* array,
4638 HInstruction* index,
4639 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004640 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004641 uint32_t dex_pc,
4642 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004643 : HTemplateInstruction(
4644 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004645 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4646 additional_side_effects),
4647 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004648 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004649 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004650 value_can_be_null_(true),
4651 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004652 SetRawInputAt(0, array);
4653 SetRawInputAt(1, index);
4654 SetRawInputAt(2, value);
4655 }
4656
Calin Juravle77520bc2015-01-12 18:45:46 +00004657 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004658 // We currently always call a runtime method to catch array store
4659 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004660 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004661 }
4662
Mingyao Yang81014cb2015-06-02 03:16:27 -07004663 // Can throw ArrayStoreException.
4664 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4665
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004666 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004667 // TODO: Same as for ArrayGet.
4668 return false;
4669 }
4670
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004671 void ClearNeedsTypeCheck() {
4672 needs_type_check_ = false;
4673 }
4674
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004675 void ClearValueCanBeNull() {
4676 value_can_be_null_ = false;
4677 }
4678
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004679 void SetStaticTypeOfArrayIsObjectArray() {
4680 static_type_of_array_is_object_array_ = true;
4681 }
4682
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004683 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004684 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004685 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004686
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004687 HInstruction* GetArray() const { return InputAt(0); }
4688 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004689 HInstruction* GetValue() const { return InputAt(2); }
4690
4691 Primitive::Type GetComponentType() const {
4692 // The Dex format does not type floating point index operations. Since the
4693 // `expected_component_type_` is set during building and can therefore not
4694 // be correct, we also check what is the value type. If it is a floating
4695 // point type, we must use that type.
4696 Primitive::Type value_type = GetValue()->GetType();
4697 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4698 ? value_type
4699 : expected_component_type_;
4700 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004701
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004702 Primitive::Type GetRawExpectedComponentType() const {
4703 return expected_component_type_;
4704 }
4705
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004706 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4707 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4708 }
4709
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004710 DECLARE_INSTRUCTION(ArraySet);
4711
4712 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004713 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004714 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004715 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004716 // Cached information for the reference_type_info_ so that codegen
4717 // does not need to inspect the static type.
4718 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004719
4720 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4721};
4722
4723class HArrayLength : public HExpression<1> {
4724 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004725 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004726 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004727 // Note that arrays do not change length, so the instruction does not
4728 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004729 SetRawInputAt(0, array);
4730 }
4731
Calin Juravle77520bc2015-01-12 18:45:46 +00004732 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004733 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004734 return true;
4735 }
Calin Juravle641547a2015-04-21 22:08:51 +01004736 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4737 return obj == InputAt(0);
4738 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004739
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004740 DECLARE_INSTRUCTION(ArrayLength);
4741
4742 private:
4743 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4744};
4745
4746class HBoundsCheck : public HExpression<2> {
4747 public:
4748 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004749 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004750 DCHECK(index->GetType() == Primitive::kPrimInt);
4751 SetRawInputAt(0, index);
4752 SetRawInputAt(1, length);
4753 }
4754
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004755 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004756 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004757 return true;
4758 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004759
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004760 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004761
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004762 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004763
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004764 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004765
4766 DECLARE_INSTRUCTION(BoundsCheck);
4767
4768 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004769 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4770};
4771
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004772/**
4773 * Some DEX instructions are folded into multiple HInstructions that need
4774 * to stay live until the last HInstruction. This class
4775 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004776 * HInstruction stays live. `index` represents the stack location index of the
4777 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004778 */
4779class HTemporary : public HTemplateInstruction<0> {
4780 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004781 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4782 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004783
4784 size_t GetIndex() const { return index_; }
4785
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004786 Primitive::Type GetType() const OVERRIDE {
4787 // The previous instruction is the one that will be stored in the temporary location.
4788 DCHECK(GetPrevious() != nullptr);
4789 return GetPrevious()->GetType();
4790 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004791
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004792 DECLARE_INSTRUCTION(Temporary);
4793
4794 private:
4795 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004796 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4797};
4798
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004799class HSuspendCheck : public HTemplateInstruction<0> {
4800 public:
4801 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004803
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004804 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004805 return true;
4806 }
4807
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004808 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4809 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004810
4811 DECLARE_INSTRUCTION(SuspendCheck);
4812
4813 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004814 // Only used for code generation, in order to share the same slow path between back edges
4815 // of a same loop.
4816 SlowPathCode* slow_path_;
4817
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004818 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4819};
4820
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004821/**
4822 * Instruction to load a Class object.
4823 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004824class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004825 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004826 HLoadClass(HCurrentMethod* current_method,
4827 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004828 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004829 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004830 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004831 bool needs_access_check,
4832 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004833 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004834 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004835 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004836 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004837 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004838 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004839 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004840 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004841 // Referrers class should not need access check. We never inline unverified
4842 // methods so we can't possibly end up in this situation.
4843 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004844 SetRawInputAt(0, current_method);
4845 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004846
4847 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004848
4849 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004850 // Note that we don't need to test for generate_clinit_check_.
4851 // Whether or not we need to generate the clinit check is processed in
4852 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004853 return other->AsLoadClass()->type_index_ == type_index_ &&
4854 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004855 }
4856
4857 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4858
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004859 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004860 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004861 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004862
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004863 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004864 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004865 }
4866
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004867 bool MustGenerateClinitCheck() const {
4868 return generate_clinit_check_;
4869 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004870
Calin Juravle0ba218d2015-05-19 18:46:01 +01004871 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004872 // The entrypoint the code generator is going to call does not do
4873 // clinit of the class.
4874 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004875 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004876 }
4877
4878 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004879 return MustGenerateClinitCheck() ||
4880 (!is_referrers_class_ && !is_in_dex_cache_) ||
4881 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004882 }
4883
4884 bool NeedsAccessCheck() const {
4885 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004886 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004887
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004888 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004889 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004890 }
4891
Calin Juravleacf735c2015-02-12 15:25:22 +00004892 ReferenceTypeInfo GetLoadedClassRTI() {
4893 return loaded_class_rti_;
4894 }
4895
4896 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4897 // Make sure we only set exact types (the loaded class should never be merged).
4898 DCHECK(rti.IsExact());
4899 loaded_class_rti_ = rti;
4900 }
4901
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004902 const DexFile& GetDexFile() { return dex_file_; }
4903
Vladimir Markodc151b22015-10-15 18:02:30 +01004904 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004905
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004906 static SideEffects SideEffectsForArchRuntimeCalls() {
4907 return SideEffects::CanTriggerGC();
4908 }
4909
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004910 bool IsInDexCache() const { return is_in_dex_cache_; }
4911
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004912 DECLARE_INSTRUCTION(LoadClass);
4913
4914 private:
4915 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004916 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004917 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004918 // Whether this instruction must generate the initialization check.
4919 // Used for code generation.
4920 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004921 const bool needs_access_check_;
4922 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004923
Calin Juravleacf735c2015-02-12 15:25:22 +00004924 ReferenceTypeInfo loaded_class_rti_;
4925
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004926 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4927};
4928
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004929class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004930 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004931 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004932 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4933 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004934 SetRawInputAt(0, current_method);
4935 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004936
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004937 bool CanBeMoved() const OVERRIDE { return true; }
4938
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004939 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4940 return other->AsLoadString()->string_index_ == string_index_;
4941 }
4942
4943 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4944
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004945 uint32_t GetStringIndex() const { return string_index_; }
4946
4947 // TODO: Can we deopt or debug when we resolve a string?
4948 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004949 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004950 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004951
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004952 static SideEffects SideEffectsForArchRuntimeCalls() {
4953 return SideEffects::CanTriggerGC();
4954 }
4955
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004956 DECLARE_INSTRUCTION(LoadString);
4957
4958 private:
4959 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004960
4961 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4962};
4963
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004964/**
4965 * Performs an initialization check on its Class object input.
4966 */
4967class HClinitCheck : public HExpression<1> {
4968 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004969 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004970 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004971 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004972 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4973 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004974 SetRawInputAt(0, constant);
4975 }
4976
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004977 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004978 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004979 return true;
4980 }
4981
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004982 bool NeedsEnvironment() const OVERRIDE {
4983 // May call runtime to initialize the class.
4984 return true;
4985 }
4986
Nicolas Geoffray729645a2015-11-19 13:29:02 +00004987 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004988
4989 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4990
4991 DECLARE_INSTRUCTION(ClinitCheck);
4992
4993 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004994 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4995};
4996
4997class HStaticFieldGet : public HExpression<1> {
4998 public:
4999 HStaticFieldGet(HInstruction* cls,
5000 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005001 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005002 bool is_volatile,
5003 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005004 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005005 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005006 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005007 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005008 : HExpression(field_type,
5009 SideEffects::FieldReadOfType(field_type, is_volatile),
5010 dex_pc),
5011 field_info_(field_offset,
5012 field_type,
5013 is_volatile,
5014 field_idx,
5015 declaring_class_def_index,
5016 dex_file,
5017 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005018 SetRawInputAt(0, cls);
5019 }
5020
Calin Juravle52c48962014-12-16 17:02:57 +00005021
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005022 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005023
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005024 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005025 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5026 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005027 }
5028
5029 size_t ComputeHashCode() const OVERRIDE {
5030 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5031 }
5032
Calin Juravle52c48962014-12-16 17:02:57 +00005033 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005034 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5035 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005036 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005037
5038 DECLARE_INSTRUCTION(StaticFieldGet);
5039
5040 private:
5041 const FieldInfo field_info_;
5042
5043 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5044};
5045
5046class HStaticFieldSet : public HTemplateInstruction<2> {
5047 public:
5048 HStaticFieldSet(HInstruction* cls,
5049 HInstruction* value,
5050 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005051 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005052 bool is_volatile,
5053 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005054 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005055 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005056 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005057 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005058 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5059 dex_pc),
5060 field_info_(field_offset,
5061 field_type,
5062 is_volatile,
5063 field_idx,
5064 declaring_class_def_index,
5065 dex_file,
5066 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005067 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005068 SetRawInputAt(0, cls);
5069 SetRawInputAt(1, value);
5070 }
5071
Calin Juravle52c48962014-12-16 17:02:57 +00005072 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005073 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5074 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005075 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005076
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005077 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005078 bool GetValueCanBeNull() const { return value_can_be_null_; }
5079 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005080
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005081 DECLARE_INSTRUCTION(StaticFieldSet);
5082
5083 private:
5084 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005085 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005086
5087 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5088};
5089
Calin Juravlee460d1d2015-09-29 04:52:17 +01005090class HUnresolvedInstanceFieldGet : public HExpression<1> {
5091 public:
5092 HUnresolvedInstanceFieldGet(HInstruction* obj,
5093 Primitive::Type field_type,
5094 uint32_t field_index,
5095 uint32_t dex_pc)
5096 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5097 field_index_(field_index) {
5098 SetRawInputAt(0, obj);
5099 }
5100
5101 bool NeedsEnvironment() const OVERRIDE { return true; }
5102 bool CanThrow() const OVERRIDE { return true; }
5103
5104 Primitive::Type GetFieldType() const { return GetType(); }
5105 uint32_t GetFieldIndex() const { return field_index_; }
5106
5107 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5108
5109 private:
5110 const uint32_t field_index_;
5111
5112 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5113};
5114
5115class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5116 public:
5117 HUnresolvedInstanceFieldSet(HInstruction* obj,
5118 HInstruction* value,
5119 Primitive::Type field_type,
5120 uint32_t field_index,
5121 uint32_t dex_pc)
5122 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5123 field_type_(field_type),
5124 field_index_(field_index) {
5125 DCHECK_EQ(field_type, value->GetType());
5126 SetRawInputAt(0, obj);
5127 SetRawInputAt(1, value);
5128 }
5129
5130 bool NeedsEnvironment() const OVERRIDE { return true; }
5131 bool CanThrow() const OVERRIDE { return true; }
5132
5133 Primitive::Type GetFieldType() const { return field_type_; }
5134 uint32_t GetFieldIndex() const { return field_index_; }
5135
5136 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5137
5138 private:
5139 const Primitive::Type field_type_;
5140 const uint32_t field_index_;
5141
5142 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5143};
5144
5145class HUnresolvedStaticFieldGet : public HExpression<0> {
5146 public:
5147 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5148 uint32_t field_index,
5149 uint32_t dex_pc)
5150 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5151 field_index_(field_index) {
5152 }
5153
5154 bool NeedsEnvironment() const OVERRIDE { return true; }
5155 bool CanThrow() const OVERRIDE { return true; }
5156
5157 Primitive::Type GetFieldType() const { return GetType(); }
5158 uint32_t GetFieldIndex() const { return field_index_; }
5159
5160 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5161
5162 private:
5163 const uint32_t field_index_;
5164
5165 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5166};
5167
5168class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5169 public:
5170 HUnresolvedStaticFieldSet(HInstruction* value,
5171 Primitive::Type field_type,
5172 uint32_t field_index,
5173 uint32_t dex_pc)
5174 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5175 field_type_(field_type),
5176 field_index_(field_index) {
5177 DCHECK_EQ(field_type, value->GetType());
5178 SetRawInputAt(0, value);
5179 }
5180
5181 bool NeedsEnvironment() const OVERRIDE { return true; }
5182 bool CanThrow() const OVERRIDE { return true; }
5183
5184 Primitive::Type GetFieldType() const { return field_type_; }
5185 uint32_t GetFieldIndex() const { return field_index_; }
5186
5187 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5188
5189 private:
5190 const Primitive::Type field_type_;
5191 const uint32_t field_index_;
5192
5193 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5194};
5195
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005196// Implement the move-exception DEX instruction.
5197class HLoadException : public HExpression<0> {
5198 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005199 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5200 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005201
David Brazdilbbd733e2015-08-18 17:48:17 +01005202 bool CanBeNull() const OVERRIDE { return false; }
5203
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005204 DECLARE_INSTRUCTION(LoadException);
5205
5206 private:
5207 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5208};
5209
David Brazdilcb1c0552015-08-04 16:22:25 +01005210// Implicit part of move-exception which clears thread-local exception storage.
5211// Must not be removed because the runtime expects the TLS to get cleared.
5212class HClearException : public HTemplateInstruction<0> {
5213 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005214 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5215 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005216
5217 DECLARE_INSTRUCTION(ClearException);
5218
5219 private:
5220 DISALLOW_COPY_AND_ASSIGN(HClearException);
5221};
5222
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005223class HThrow : public HTemplateInstruction<1> {
5224 public:
5225 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005226 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005227 SetRawInputAt(0, exception);
5228 }
5229
5230 bool IsControlFlow() const OVERRIDE { return true; }
5231
5232 bool NeedsEnvironment() const OVERRIDE { return true; }
5233
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005234 bool CanThrow() const OVERRIDE { return true; }
5235
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005236
5237 DECLARE_INSTRUCTION(Throw);
5238
5239 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005240 DISALLOW_COPY_AND_ASSIGN(HThrow);
5241};
5242
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005243/**
5244 * Implementation strategies for the code generator of a HInstanceOf
5245 * or `HCheckCast`.
5246 */
5247enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005248 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005249 kExactCheck, // Can do a single class compare.
5250 kClassHierarchyCheck, // Can just walk the super class chain.
5251 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5252 kInterfaceCheck, // No optimization yet when checking against an interface.
5253 kArrayObjectCheck, // Can just check if the array is not primitive.
5254 kArrayCheck // No optimization yet when checking against a generic array.
5255};
5256
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005257class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005258 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005259 HInstanceOf(HInstruction* object,
5260 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005261 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005262 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005263 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005264 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005265 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005266 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005267 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005268 SetRawInputAt(0, object);
5269 SetRawInputAt(1, constant);
5270 }
5271
5272 bool CanBeMoved() const OVERRIDE { return true; }
5273
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005274 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005275 return true;
5276 }
5277
5278 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005279 return false;
5280 }
5281
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005282 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5283
5284 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005285
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005286 // Used only in code generation.
5287 bool MustDoNullCheck() const { return must_do_null_check_; }
5288 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5289
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005290 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5291 return (check_kind == TypeCheckKind::kExactCheck)
5292 ? SideEffects::None()
5293 // Mips currently does runtime calls for any other checks.
5294 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005295 }
5296
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005297 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005298
5299 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005300 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005301 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005302
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005303 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5304};
5305
Calin Juravleb1498f62015-02-16 13:13:29 +00005306class HBoundType : public HExpression<1> {
5307 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005308 // Constructs an HBoundType with the given upper_bound.
5309 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005310 HBoundType(HInstruction* input,
5311 ReferenceTypeInfo upper_bound,
5312 bool upper_can_be_null,
5313 uint32_t dex_pc = kNoDexPc)
5314 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005315 upper_bound_(upper_bound),
5316 upper_can_be_null_(upper_can_be_null),
5317 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005318 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005319 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005320 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005321 }
5322
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005323 // GetUpper* should only be used in reference type propagation.
5324 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5325 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005326
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005327 void SetCanBeNull(bool can_be_null) {
5328 DCHECK(upper_can_be_null_ || !can_be_null);
5329 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005330 }
5331
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005332 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5333
Calin Juravleb1498f62015-02-16 13:13:29 +00005334 DECLARE_INSTRUCTION(BoundType);
5335
5336 private:
5337 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005338 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5339 // It is used to bound the type in cases like:
5340 // if (x instanceof ClassX) {
5341 // // uper_bound_ will be ClassX
5342 // }
5343 const ReferenceTypeInfo upper_bound_;
5344 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5345 // is false then can_be_null_ cannot be true).
5346 const bool upper_can_be_null_;
5347 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005348
5349 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5350};
5351
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005352class HCheckCast : public HTemplateInstruction<2> {
5353 public:
5354 HCheckCast(HInstruction* object,
5355 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005356 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005357 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005358 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005359 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005360 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005361 SetRawInputAt(0, object);
5362 SetRawInputAt(1, constant);
5363 }
5364
5365 bool CanBeMoved() const OVERRIDE { return true; }
5366
5367 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5368 return true;
5369 }
5370
5371 bool NeedsEnvironment() const OVERRIDE {
5372 // Instruction may throw a CheckCastError.
5373 return true;
5374 }
5375
5376 bool CanThrow() const OVERRIDE { return true; }
5377
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005378 bool MustDoNullCheck() const { return must_do_null_check_; }
5379 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005380 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005381
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005382 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005383
5384 DECLARE_INSTRUCTION(CheckCast);
5385
5386 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005387 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005388 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005389
5390 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005391};
5392
Calin Juravle27df7582015-04-17 19:12:31 +01005393class HMemoryBarrier : public HTemplateInstruction<0> {
5394 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005395 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005396 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005397 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005398 barrier_kind_(barrier_kind) {}
5399
5400 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5401
5402 DECLARE_INSTRUCTION(MemoryBarrier);
5403
5404 private:
5405 const MemBarrierKind barrier_kind_;
5406
5407 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5408};
5409
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005410class HMonitorOperation : public HTemplateInstruction<1> {
5411 public:
5412 enum OperationKind {
5413 kEnter,
5414 kExit,
5415 };
5416
5417 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005418 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005419 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5420 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005421 SetRawInputAt(0, object);
5422 }
5423
5424 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005425 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5426
5427 bool CanThrow() const OVERRIDE {
5428 // Verifier guarantees that monitor-exit cannot throw.
5429 // This is important because it allows the HGraphBuilder to remove
5430 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5431 return IsEnter();
5432 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005433
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005434
5435 bool IsEnter() const { return kind_ == kEnter; }
5436
5437 DECLARE_INSTRUCTION(MonitorOperation);
5438
Calin Juravle52c48962014-12-16 17:02:57 +00005439 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005440 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005441
5442 private:
5443 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5444};
5445
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005446/**
5447 * A HInstruction used as a marker for the replacement of new + <init>
5448 * of a String to a call to a StringFactory. Only baseline will see
5449 * the node at code generation, where it will be be treated as null.
5450 * When compiling non-baseline, `HFakeString` instructions are being removed
5451 * in the instruction simplifier.
5452 */
5453class HFakeString : public HTemplateInstruction<0> {
5454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005455 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5456 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005457
5458 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5459
5460 DECLARE_INSTRUCTION(FakeString);
5461
5462 private:
5463 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5464};
5465
Vladimir Markof9f64412015-09-02 14:05:49 +01005466class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005467 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005468 MoveOperands(Location source,
5469 Location destination,
5470 Primitive::Type type,
5471 HInstruction* instruction)
5472 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005473
5474 Location GetSource() const { return source_; }
5475 Location GetDestination() const { return destination_; }
5476
5477 void SetSource(Location value) { source_ = value; }
5478 void SetDestination(Location value) { destination_ = value; }
5479
5480 // The parallel move resolver marks moves as "in-progress" by clearing the
5481 // destination (but not the source).
5482 Location MarkPending() {
5483 DCHECK(!IsPending());
5484 Location dest = destination_;
5485 destination_ = Location::NoLocation();
5486 return dest;
5487 }
5488
5489 void ClearPending(Location dest) {
5490 DCHECK(IsPending());
5491 destination_ = dest;
5492 }
5493
5494 bool IsPending() const {
5495 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5496 return destination_.IsInvalid() && !source_.IsInvalid();
5497 }
5498
5499 // True if this blocks a move from the given location.
5500 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005501 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005502 }
5503
5504 // A move is redundant if it's been eliminated, if its source and
5505 // destination are the same, or if its destination is unneeded.
5506 bool IsRedundant() const {
5507 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5508 }
5509
5510 // We clear both operands to indicate move that's been eliminated.
5511 void Eliminate() {
5512 source_ = destination_ = Location::NoLocation();
5513 }
5514
5515 bool IsEliminated() const {
5516 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5517 return source_.IsInvalid();
5518 }
5519
Alexey Frunze4dda3372015-06-01 18:31:49 -07005520 Primitive::Type GetType() const { return type_; }
5521
Nicolas Geoffray90218252015-04-15 11:56:51 +01005522 bool Is64BitMove() const {
5523 return Primitive::Is64BitType(type_);
5524 }
5525
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005526 HInstruction* GetInstruction() const { return instruction_; }
5527
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005528 private:
5529 Location source_;
5530 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005531 // The type this move is for.
5532 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005533 // The instruction this move is assocatied with. Null when this move is
5534 // for moving an input in the expected locations of user (including a phi user).
5535 // This is only used in debug mode, to ensure we do not connect interval siblings
5536 // in the same parallel move.
5537 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005538};
5539
5540static constexpr size_t kDefaultNumberOfMoves = 4;
5541
5542class HParallelMove : public HTemplateInstruction<0> {
5543 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005544 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005545 : HTemplateInstruction(SideEffects::None(), dex_pc),
5546 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5547 moves_.reserve(kDefaultNumberOfMoves);
5548 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005549
Nicolas Geoffray90218252015-04-15 11:56:51 +01005550 void AddMove(Location source,
5551 Location destination,
5552 Primitive::Type type,
5553 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005554 DCHECK(source.IsValid());
5555 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005556 if (kIsDebugBuild) {
5557 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005558 for (const MoveOperands& move : moves_) {
5559 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005560 // Special case the situation where the move is for the spill slot
5561 // of the instruction.
5562 if ((GetPrevious() == instruction)
5563 || ((GetPrevious() == nullptr)
5564 && instruction->IsPhi()
5565 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005566 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005567 << "Doing parallel moves for the same instruction.";
5568 } else {
5569 DCHECK(false) << "Doing parallel moves for the same instruction.";
5570 }
5571 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005572 }
5573 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005574 for (const MoveOperands& move : moves_) {
5575 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005576 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005577 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005578 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005579 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005580 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005581 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005582 }
5583
Vladimir Marko225b6462015-09-28 12:17:40 +01005584 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005585 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005586 }
5587
Vladimir Marko225b6462015-09-28 12:17:40 +01005588 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005589
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005590 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005591
5592 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005593 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005594
5595 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5596};
5597
Mark Mendell0616ae02015-04-17 12:49:27 -04005598} // namespace art
5599
Vladimir Markob4536b72015-11-24 13:45:23 +00005600#ifdef ART_ENABLE_CODEGEN_arm
5601#include "nodes_arm.h"
5602#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005603#ifdef ART_ENABLE_CODEGEN_arm64
5604#include "nodes_arm64.h"
5605#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005606#ifdef ART_ENABLE_CODEGEN_x86
5607#include "nodes_x86.h"
5608#endif
5609
5610namespace art {
5611
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005612class HGraphVisitor : public ValueObject {
5613 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005614 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5615 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005616
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005617 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005618 virtual void VisitBasicBlock(HBasicBlock* block);
5619
Roland Levillain633021e2014-10-01 14:12:25 +01005620 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005621 void VisitInsertionOrder();
5622
Roland Levillain633021e2014-10-01 14:12:25 +01005623 // Visit the graph following dominator tree reverse post-order.
5624 void VisitReversePostOrder();
5625
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005626 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005627
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005628 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005629#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005630 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5631
5632 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5633
5634#undef DECLARE_VISIT_INSTRUCTION
5635
5636 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005637 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005638
5639 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5640};
5641
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005642class HGraphDelegateVisitor : public HGraphVisitor {
5643 public:
5644 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5645 virtual ~HGraphDelegateVisitor() {}
5646
5647 // Visit functions that delegate to to super class.
5648#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005649 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005650
5651 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5652
5653#undef DECLARE_VISIT_INSTRUCTION
5654
5655 private:
5656 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5657};
5658
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005659class HInsertionOrderIterator : public ValueObject {
5660 public:
5661 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5662
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005663 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005664 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005665 void Advance() { ++index_; }
5666
5667 private:
5668 const HGraph& graph_;
5669 size_t index_;
5670
5671 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5672};
5673
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005674class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005675 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005676 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5677 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005678 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005679 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005680
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005681 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5682 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005683 void Advance() { ++index_; }
5684
5685 private:
5686 const HGraph& graph_;
5687 size_t index_;
5688
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005689 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005690};
5691
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005692class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005693 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005694 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005695 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005696 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005697 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005698 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005699
5700 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005701 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005702 void Advance() { --index_; }
5703
5704 private:
5705 const HGraph& graph_;
5706 size_t index_;
5707
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005708 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005709};
5710
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005711class HLinearPostOrderIterator : public ValueObject {
5712 public:
5713 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005714 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005715
5716 bool Done() const { return index_ == 0; }
5717
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005718 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005719
5720 void Advance() {
5721 --index_;
5722 DCHECK_GE(index_, 0U);
5723 }
5724
5725 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005726 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005727 size_t index_;
5728
5729 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5730};
5731
5732class HLinearOrderIterator : public ValueObject {
5733 public:
5734 explicit HLinearOrderIterator(const HGraph& graph)
5735 : order_(graph.GetLinearOrder()), index_(0) {}
5736
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005737 bool Done() const { return index_ == order_.size(); }
5738 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005739 void Advance() { ++index_; }
5740
5741 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005742 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005743 size_t index_;
5744
5745 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5746};
5747
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005748// Iterator over the blocks that art part of the loop. Includes blocks part
5749// of an inner loop. The order in which the blocks are iterated is on their
5750// block id.
5751class HBlocksInLoopIterator : public ValueObject {
5752 public:
5753 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5754 : blocks_in_loop_(info.GetBlocks()),
5755 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5756 index_(0) {
5757 if (!blocks_in_loop_.IsBitSet(index_)) {
5758 Advance();
5759 }
5760 }
5761
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005762 bool Done() const { return index_ == blocks_.size(); }
5763 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005764 void Advance() {
5765 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005766 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005767 if (blocks_in_loop_.IsBitSet(index_)) {
5768 break;
5769 }
5770 }
5771 }
5772
5773 private:
5774 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005775 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005776 size_t index_;
5777
5778 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5779};
5780
Mingyao Yang3584bce2015-05-19 16:01:59 -07005781// Iterator over the blocks that art part of the loop. Includes blocks part
5782// of an inner loop. The order in which the blocks are iterated is reverse
5783// post order.
5784class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5785 public:
5786 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5787 : blocks_in_loop_(info.GetBlocks()),
5788 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5789 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005790 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005791 Advance();
5792 }
5793 }
5794
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005795 bool Done() const { return index_ == blocks_.size(); }
5796 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005797 void Advance() {
5798 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005799 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5800 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005801 break;
5802 }
5803 }
5804 }
5805
5806 private:
5807 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005808 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005809 size_t index_;
5810
5811 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5812};
5813
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005814inline int64_t Int64FromConstant(HConstant* constant) {
5815 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5816 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5817 : constant->AsLongConstant()->GetValue();
5818}
5819
Vladimir Marko58155012015-08-19 12:49:41 +00005820inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5821 // For the purposes of the compiler, the dex files must actually be the same object
5822 // if we want to safely treat them as the same. This is especially important for JIT
5823 // as custom class loaders can open the same underlying file (or memory) multiple
5824 // times and provide different class resolution but no two class loaders should ever
5825 // use the same DexFile object - doing so is an unsupported hack that can lead to
5826 // all sorts of weird failures.
5827 return &lhs == &rhs;
5828}
5829
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005830} // namespace art
5831
5832#endif // ART_COMPILER_OPTIMIZING_NODES_H_