blob: c126b59bccda727e2be103bf66240059358a5744 [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
David Brazdil8d5b8b22015-03-24 10:51:52 +000024#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080025#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000026#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010027#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000028#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000029#include "handle.h"
30#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000031#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010032#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000033#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010035#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070036#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000037#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39namespace art {
40
David Brazdil1abb4192015-02-17 18:33:36 +000041class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010045class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010046class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
78
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010079static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
80
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060081static constexpr uint32_t kNoDexPc = -1;
82
Dave Allison20dfc792014-06-16 20:44:29 -070083enum IfCondition {
84 kCondEQ,
85 kCondNE,
86 kCondLT,
87 kCondLE,
88 kCondGT,
89 kCondGE,
90};
91
Vladimir Markof9f64412015-09-02 14:05:49 +010092class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010093 public:
94 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
95
96 void AddInstruction(HInstruction* instruction);
97 void RemoveInstruction(HInstruction* instruction);
98
David Brazdilc3d743f2015-04-22 13:40:50 +010099 // Insert `instruction` before/after an existing instruction `cursor`.
100 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
101 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
102
Roland Levillain6b469232014-09-25 10:10:38 +0100103 // Return true if this list contains `instruction`.
104 bool Contains(HInstruction* instruction) const;
105
Roland Levillainccc07a92014-09-16 14:48:16 +0100106 // Return true if `instruction1` is found before `instruction2` in
107 // this instruction list and false otherwise. Abort if none
108 // of these instructions is found.
109 bool FoundBefore(const HInstruction* instruction1,
110 const HInstruction* instruction2) const;
111
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000112 bool IsEmpty() const { return first_instruction_ == nullptr; }
113 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
114
115 // Update the block of all instructions to be `block`.
116 void SetBlockOfInstructions(HBasicBlock* block) const;
117
118 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
119 void Add(const HInstructionList& instruction_list);
120
David Brazdil2d7352b2015-04-20 14:52:42 +0100121 // Return the number of instructions in the list. This is an expensive operation.
122 size_t CountSize() const;
123
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100124 private:
125 HInstruction* first_instruction_;
126 HInstruction* last_instruction_;
127
128 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000129 friend class HGraph;
130 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100131 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100132 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100133
134 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
135};
136
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000137// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100138class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000139 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100140 HGraph(ArenaAllocator* arena,
141 const DexFile& dex_file,
142 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100143 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100145 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100146 bool debuggable = false,
147 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000148 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100149 blocks_(arena->Adapter(kArenaAllocBlockList)),
150 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
151 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700152 entry_block_(nullptr),
153 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100154 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100155 number_of_vregs_(0),
156 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000157 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400158 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000159 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000160 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000161 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100162 dex_file_(dex_file),
163 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100164 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100165 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100166 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000168 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100169 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
170 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
171 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
172 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
173 cached_current_method_(nullptr) {
174 blocks_.reserve(kDefaultNumberOfBlocks);
175 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000177 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100178 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
179
David Brazdil69ba7b72015-06-23 18:27:30 +0100180 bool IsInSsaForm() const { return in_ssa_form_; }
181
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000182 HBasicBlock* GetEntryBlock() const { return entry_block_; }
183 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100184 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000185
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000186 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
187 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000188
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000189 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100190
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000191 // Try building the SSA form of this graph, with dominance computation and loop
192 // recognition. Returns whether it was successful in doing all these steps.
193 bool TryBuildingSsa() {
194 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000195 // The SSA builder requires loops to all be natural. Specifically, the dead phi
196 // elimination phase checks the consistency of the graph when doing a post-order
197 // visit for eliminating dead phis: a dead phi can only have loop header phi
198 // users remaining when being visited.
199 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100200 // Precompute per-block try membership before entering the SSA builder,
201 // which needs the information to build catch block phis from values of
202 // locals at throwing instructions inside try blocks.
203 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000204 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100205 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000206 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000207 }
208
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100209 void ComputeDominanceInformation();
210 void ClearDominanceInformation();
211
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000212 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100214 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100215 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000216
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000217 // Analyze all natural loops in this graph. Returns false if one
218 // loop is not natural, that is the header does not dominate the
219 // back edge.
220 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100221
David Brazdilffee3d32015-07-06 11:48:53 +0100222 // Iterate over blocks to compute try block membership. Needs reverse post
223 // order and loop information.
224 void ComputeTryBlockInformation();
225
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000227 // Returns the instruction used to replace the invoke expression or null if the
228 // invoke is for a void method.
229 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000230
Mingyao Yang3584bce2015-05-19 16:01:59 -0700231 // Need to add a couple of blocks to test if the loop body is entered and
232 // put deoptimization instructions, etc.
233 void TransformLoopHeaderForBCE(HBasicBlock* header);
234
David Brazdil2d7352b2015-04-20 14:52:42 +0100235 // Removes `block` from the graph.
236 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000237
David Brazdilfc6a86a2015-06-26 10:33:45 +0000238 // Splits the edge between `block` and `successor` while preserving the
239 // indices in the predecessor/successor lists. If there are multiple edges
240 // between the blocks, the lowest indices are used.
241 // Returns the new block which is empty and has the same dex pc as `successor`.
242 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
243
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100244 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
245 void SimplifyLoop(HBasicBlock* header);
246
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000247 int32_t GetNextInstructionId() {
248 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000249 return current_instruction_id_++;
250 }
251
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000252 int32_t GetCurrentInstructionId() const {
253 return current_instruction_id_;
254 }
255
256 void SetCurrentInstructionId(int32_t id) {
257 current_instruction_id_ = id;
258 }
259
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100260 uint16_t GetMaximumNumberOfOutVRegs() const {
261 return maximum_number_of_out_vregs_;
262 }
263
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000264 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
265 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100266 }
267
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100268 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
269 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
270 }
271
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000272 void UpdateTemporariesVRegSlots(size_t slots) {
273 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100274 }
275
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000276 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100277 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000278 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100279 }
280
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100281 void SetNumberOfVRegs(uint16_t number_of_vregs) {
282 number_of_vregs_ = number_of_vregs;
283 }
284
285 uint16_t GetNumberOfVRegs() const {
286 return number_of_vregs_;
287 }
288
289 void SetNumberOfInVRegs(uint16_t value) {
290 number_of_in_vregs_ = value;
291 }
292
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100293 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100294 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100295 return number_of_vregs_ - number_of_in_vregs_;
296 }
297
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100298 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100300 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100301
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100302 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100303 return linear_order_;
304 }
305
Mark Mendell1152c922015-04-24 17:06:35 -0400306 bool HasBoundsChecks() const {
307 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800308 }
309
Mark Mendell1152c922015-04-24 17:06:35 -0400310 void SetHasBoundsChecks(bool value) {
311 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800312 }
313
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100314 bool ShouldGenerateConstructorBarrier() const {
315 return should_generate_constructor_barrier_;
316 }
317
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000318 bool IsDebuggable() const { return debuggable_; }
319
David Brazdil8d5b8b22015-03-24 10:51:52 +0000320 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000321 // already, it is created and inserted into the graph. This method is only for
322 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600323 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000324
325 // TODO: This is problematic for the consistency of reference type propagation
326 // because it can be created anytime after the pass and thus it will be left
327 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600328 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000329
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600330 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
331 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000332 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600333 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
334 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000335 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600336 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
337 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000338 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600339 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
340 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000341 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000342
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100343 HCurrentMethod* GetCurrentMethod();
344
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000345 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100346
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100347 const DexFile& GetDexFile() const {
348 return dex_file_;
349 }
350
351 uint32_t GetMethodIdx() const {
352 return method_idx_;
353 }
354
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100355 InvokeType GetInvokeType() const {
356 return invoke_type_;
357 }
358
Mark Mendellc4701932015-04-10 13:18:51 -0400359 InstructionSet GetInstructionSet() const {
360 return instruction_set_;
361 }
362
David Brazdil77a48ae2015-09-15 12:34:04 +0000363 bool HasTryCatch() const { return has_try_catch_; }
364 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100365
David Brazdil2d7352b2015-04-20 14:52:42 +0100366 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100367 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000368 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100369 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000370
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000371 template <class InstructionType, typename ValueType>
372 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600373 ArenaSafeMap<ValueType, InstructionType*>* cache,
374 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000375 // Try to find an existing constant of the given value.
376 InstructionType* constant = nullptr;
377 auto cached_constant = cache->find(value);
378 if (cached_constant != cache->end()) {
379 constant = cached_constant->second;
380 }
381
382 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100383 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000384 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600385 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000386 cache->Overwrite(value, constant);
387 InsertConstant(constant);
388 }
389 return constant;
390 }
391
David Brazdil8d5b8b22015-03-24 10:51:52 +0000392 void InsertConstant(HConstant* instruction);
393
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000394 // Cache a float constant into the graph. This method should only be
395 // called by the SsaBuilder when creating "equivalent" instructions.
396 void CacheFloatConstant(HFloatConstant* constant);
397
398 // See CacheFloatConstant comment.
399 void CacheDoubleConstant(HDoubleConstant* constant);
400
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000401 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000402
403 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100404 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000405
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100406 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100407 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000408
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100409 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100410 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100411
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000412 HBasicBlock* entry_block_;
413 HBasicBlock* exit_block_;
414
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100415 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100416 uint16_t maximum_number_of_out_vregs_;
417
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100418 // The number of virtual registers in this method. Contains the parameters.
419 uint16_t number_of_vregs_;
420
421 // The number of virtual registers used by parameters of this method.
422 uint16_t number_of_in_vregs_;
423
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000424 // Number of vreg size slots that the temporaries use (used in baseline compiler).
425 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100426
Mark Mendell1152c922015-04-24 17:06:35 -0400427 // Has bounds checks. We can totally skip BCE if it's false.
428 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800429
David Brazdil77a48ae2015-09-15 12:34:04 +0000430 // Flag whether there are any try/catch blocks in the graph. We will skip
431 // try/catch-related passes if false.
432 bool has_try_catch_;
433
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000434 // Indicates whether the graph should be compiled in a way that
435 // ensures full debuggability. If false, we can apply more
436 // aggressive optimizations that may limit the level of debugging.
437 const bool debuggable_;
438
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000439 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000440 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000441
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100442 // The dex file from which the method is from.
443 const DexFile& dex_file_;
444
445 // The method index in the dex file.
446 const uint32_t method_idx_;
447
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100448 // If inlined, this encodes how the callee is being invoked.
449 const InvokeType invoke_type_;
450
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100451 // Whether the graph has been transformed to SSA form. Only used
452 // in debug mode to ensure we are not using properties only valid
453 // for non-SSA form (like the number of temporaries).
454 bool in_ssa_form_;
455
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100456 const bool should_generate_constructor_barrier_;
457
Mathieu Chartiere401d142015-04-22 13:56:20 -0700458 const InstructionSet instruction_set_;
459
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000460 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000461 HNullConstant* cached_null_constant_;
462 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000463 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000464 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000465 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000466
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100467 HCurrentMethod* cached_current_method_;
468
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000469 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100470 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000471 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472 DISALLOW_COPY_AND_ASSIGN(HGraph);
473};
474
Vladimir Markof9f64412015-09-02 14:05:49 +0100475class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000476 public:
477 HLoopInformation(HBasicBlock* header, HGraph* graph)
478 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100479 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100480 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100481 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100482 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
483 back_edges_.reserve(kDefaultNumberOfBackEdges);
484 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485
486 HBasicBlock* GetHeader() const {
487 return header_;
488 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000490 void SetHeader(HBasicBlock* block) {
491 header_ = block;
492 }
493
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100494 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
495 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
496 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
497
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000498 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100499 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000500 }
501
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100502 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100503 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100504 }
505
David Brazdil46e2a392015-03-16 17:31:52 +0000506 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100507 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100508 }
509
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000510 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100511 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000512 }
513
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100514 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100515
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100516 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100517 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100518 }
519
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100520 // Returns the lifetime position of the back edge that has the
521 // greatest lifetime position.
522 size_t GetLifetimeEnd() const;
523
524 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100525 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100526 }
527
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100528 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100529 // that is the header dominates the back edge.
530 bool Populate();
531
David Brazdila4b8c212015-05-07 09:59:30 +0100532 // Reanalyzes the loop by removing loop info from its blocks and re-running
533 // Populate(). If there are no back edges left, the loop info is completely
534 // removed as well as its SuspendCheck instruction. It must be run on nested
535 // inner loops first.
536 void Update();
537
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100538 // Returns whether this loop information contains `block`.
539 // Note that this loop information *must* be populated before entering this function.
540 bool Contains(const HBasicBlock& block) const;
541
542 // Returns whether this loop information is an inner loop of `other`.
543 // Note that `other` *must* be populated before entering this function.
544 bool IsIn(const HLoopInformation& other) const;
545
546 const ArenaBitVector& GetBlocks() const { return blocks_; }
547
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000548 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000549 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000550
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100552 // Internal recursive implementation of `Populate`.
553 void PopulateRecursive(HBasicBlock* block);
554
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000555 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100556 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100557 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100558 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000559
560 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
561};
562
David Brazdilec16f792015-08-19 15:04:01 +0100563// Stores try/catch information for basic blocks.
564// Note that HGraph is constructed so that catch blocks cannot simultaneously
565// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100566class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100567 public:
568 // Try block information constructor.
569 explicit TryCatchInformation(const HTryBoundary& try_entry)
570 : try_entry_(&try_entry),
571 catch_dex_file_(nullptr),
572 catch_type_index_(DexFile::kDexNoIndex16) {
573 DCHECK(try_entry_ != nullptr);
574 }
575
576 // Catch block information constructor.
577 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
578 : try_entry_(nullptr),
579 catch_dex_file_(&dex_file),
580 catch_type_index_(catch_type_index) {}
581
582 bool IsTryBlock() const { return try_entry_ != nullptr; }
583
584 const HTryBoundary& GetTryEntry() const {
585 DCHECK(IsTryBlock());
586 return *try_entry_;
587 }
588
589 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
590
591 bool IsCatchAllTypeIndex() const {
592 DCHECK(IsCatchBlock());
593 return catch_type_index_ == DexFile::kDexNoIndex16;
594 }
595
596 uint16_t GetCatchTypeIndex() const {
597 DCHECK(IsCatchBlock());
598 return catch_type_index_;
599 }
600
601 const DexFile& GetCatchDexFile() const {
602 DCHECK(IsCatchBlock());
603 return *catch_dex_file_;
604 }
605
606 private:
607 // One of possibly several TryBoundary instructions entering the block's try.
608 // Only set for try blocks.
609 const HTryBoundary* try_entry_;
610
611 // Exception type information. Only set for catch blocks.
612 const DexFile* catch_dex_file_;
613 const uint16_t catch_type_index_;
614};
615
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100616static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100617static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100618
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000619// A block in a method. Contains the list of instructions represented
620// as a double linked list. Each block knows its predecessors and
621// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100622
Vladimir Markof9f64412015-09-02 14:05:49 +0100623class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000624 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600625 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000627 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
628 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000629 loop_information_(nullptr),
630 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000631 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100632 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100633 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100634 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000635 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000636 try_catch_information_(nullptr) {
637 predecessors_.reserve(kDefaultNumberOfPredecessors);
638 successors_.reserve(kDefaultNumberOfSuccessors);
639 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
640 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641
Vladimir Marko60584552015-09-03 13:35:12 +0000642 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100643 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000644 }
645
Vladimir Marko60584552015-09-03 13:35:12 +0000646 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100647 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000648 }
649
Vladimir Marko60584552015-09-03 13:35:12 +0000650 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
651 return ContainsElement(successors_, block, start_from);
652 }
653
654 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100655 return dominated_blocks_;
656 }
657
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100658 bool IsEntryBlock() const {
659 return graph_->GetEntryBlock() == this;
660 }
661
662 bool IsExitBlock() const {
663 return graph_->GetExitBlock() == this;
664 }
665
David Brazdil46e2a392015-03-16 17:31:52 +0000666 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000667 bool IsSingleTryBoundary() const;
668
669 // Returns true if this block emits nothing but a jump.
670 bool IsSingleJump() const {
671 HLoopInformation* loop_info = GetLoopInformation();
672 return (IsSingleGoto() || IsSingleTryBoundary())
673 // Back edges generate a suspend check.
674 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
675 }
David Brazdil46e2a392015-03-16 17:31:52 +0000676
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000677 void AddBackEdge(HBasicBlock* back_edge) {
678 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000679 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000680 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000682 loop_information_->AddBackEdge(back_edge);
683 }
684
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000685 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000686 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000687
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100688 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000689 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600690 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000691
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000692 HBasicBlock* GetDominator() const { return dominator_; }
693 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000694 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
695
696 void RemoveDominatedBlock(HBasicBlock* block) {
697 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100698 }
Vladimir Marko60584552015-09-03 13:35:12 +0000699
700 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
701 ReplaceElement(dominated_blocks_, existing, new_block);
702 }
703
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100704 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000705
706 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100707 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000708 }
709
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100710 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
711 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100712 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100713 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100714 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
715 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000716
717 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000718 successors_.push_back(block);
719 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000720 }
721
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100722 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
723 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100724 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000725 new_block->predecessors_.push_back(this);
726 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000727 }
728
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000729 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
730 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000731 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000732 new_block->successors_.push_back(this);
733 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000734 }
735
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100736 // Insert `this` between `predecessor` and `successor. This method
737 // preserves the indicies, and will update the first edge found between
738 // `predecessor` and `successor`.
739 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
740 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100741 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000742 successor->predecessors_[predecessor_index] = this;
743 predecessor->successors_[successor_index] = this;
744 successors_.push_back(successor);
745 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100746 }
747
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100748 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000749 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100750 }
751
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000752 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000753 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000754 }
755
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100756 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000757 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100758 }
759
760 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000761 predecessors_.push_back(block);
762 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 }
764
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100765 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000766 DCHECK_EQ(predecessors_.size(), 2u);
767 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100768 }
769
David Brazdil769c9e52015-04-27 13:54:09 +0100770 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000771 DCHECK_EQ(successors_.size(), 2u);
772 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100773 }
774
David Brazdilfc6a86a2015-06-26 10:33:45 +0000775 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000776 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100777 }
778
David Brazdilfc6a86a2015-06-26 10:33:45 +0000779 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000780 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100781 }
782
David Brazdilfc6a86a2015-06-26 10:33:45 +0000783 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000784 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100785 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000786 }
787
788 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000789 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100790 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000791 }
792
793 // Returns whether the first occurrence of `predecessor` in the list of
794 // predecessors is at index `idx`.
795 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100796 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000797 return GetPredecessorIndexOf(predecessor) == idx;
798 }
799
David Brazdilffee3d32015-07-06 11:48:53 +0100800 // Returns the number of non-exceptional successors. SsaChecker ensures that
801 // these are stored at the beginning of the successor list.
802 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000803 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100804 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805
David Brazdild7558da2015-09-22 13:04:14 +0100806 // Create a new block between this block and its predecessors. The new block
807 // is added to the graph, all predecessor edges are relinked to it and an edge
808 // is created to `this`. Returns the new empty block. Reverse post order or
809 // loop and try/catch information are not updated.
810 HBasicBlock* CreateImmediateDominator();
811
David Brazdilfc6a86a2015-06-26 10:33:45 +0000812 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100813 // created, latter block. Note that this method will add the block to the
814 // graph, create a Goto at the end of the former block and will create an edge
815 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100816 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000817 HBasicBlock* SplitBefore(HInstruction* cursor);
818
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000819 // Split the block into two blocks just after `cursor`. Returns the newly
820 // created block. Note that this method just updates raw block information,
821 // like predecessors, successors, dominators, and instruction list. It does not
822 // update the graph, reverse post order, loop information, nor make sure the
823 // blocks are consistent (for example ending with a control flow instruction).
824 HBasicBlock* SplitAfter(HInstruction* cursor);
825
826 // Merge `other` at the end of `this`. Successors and dominated blocks of
827 // `other` are changed to be successors and dominated blocks of `this`. Note
828 // that this method does not update the graph, reverse post order, loop
829 // information, nor make sure the blocks are consistent (for example ending
830 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100831 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000832
833 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
834 // of `this` are moved to `other`.
835 // Note that this method does not update the graph, reverse post order, loop
836 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000837 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000838 void ReplaceWith(HBasicBlock* other);
839
David Brazdil2d7352b2015-04-20 14:52:42 +0100840 // Merge `other` at the end of `this`. This method updates loops, reverse post
841 // order, links to predecessors, successors, dominators and deletes the block
842 // from the graph. The two blocks must be successive, i.e. `this` the only
843 // predecessor of `other` and vice versa.
844 void MergeWith(HBasicBlock* other);
845
846 // Disconnects `this` from all its predecessors, successors and dominator,
847 // removes it from all loops it is included in and eventually from the graph.
848 // The block must not dominate any other block. Predecessors and successors
849 // are safely updated.
850 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000851
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000852 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100853 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100854 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100855 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100856 // Replace instruction `initial` with `replacement` within this block.
857 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
858 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100859 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100860 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000861 // RemoveInstruction and RemovePhi delete a given instruction from the respective
862 // instruction list. With 'ensure_safety' set to true, it verifies that the
863 // instruction is not in use and removes it from the use lists of its inputs.
864 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
865 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100866 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100867
868 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100869 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100870 }
871
Roland Levillain6b879dd2014-09-22 17:13:44 +0100872 bool IsLoopPreHeaderFirstPredecessor() const {
873 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100874 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100875 }
876
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100877 HLoopInformation* GetLoopInformation() const {
878 return loop_information_;
879 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000880
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000881 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100882 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000883 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100884 void SetInLoop(HLoopInformation* info) {
885 if (IsLoopHeader()) {
886 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100887 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100888 loop_information_ = info;
889 } else if (loop_information_->Contains(*info->GetHeader())) {
890 // Block is currently part of an outer loop. Make it part of this inner loop.
891 // Note that a non loop header having a loop information means this loop information
892 // has already been populated
893 loop_information_ = info;
894 } else {
895 // Block is part of an inner loop. Do not update the loop information.
896 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
897 // at this point, because this method is being called while populating `info`.
898 }
899 }
900
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000901 // Raw update of the loop information.
902 void SetLoopInformation(HLoopInformation* info) {
903 loop_information_ = info;
904 }
905
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100906 bool IsInLoop() const { return loop_information_ != nullptr; }
907
David Brazdilec16f792015-08-19 15:04:01 +0100908 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
909
910 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
911 try_catch_information_ = try_catch_information;
912 }
913
914 bool IsTryBlock() const {
915 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
916 }
917
918 bool IsCatchBlock() const {
919 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
920 }
David Brazdilffee3d32015-07-06 11:48:53 +0100921
922 // Returns the try entry that this block's successors should have. They will
923 // be in the same try, unless the block ends in a try boundary. In that case,
924 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100925 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100926
David Brazdild7558da2015-09-22 13:04:14 +0100927 bool HasThrowingInstructions() const;
928
David Brazdila4b8c212015-05-07 09:59:30 +0100929 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100930 bool Dominates(HBasicBlock* block) const;
931
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100932 size_t GetLifetimeStart() const { return lifetime_start_; }
933 size_t GetLifetimeEnd() const { return lifetime_end_; }
934
935 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
936 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
937
David Brazdil8d5b8b22015-03-24 10:51:52 +0000938 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000939 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100940 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000941 bool HasSinglePhi() const;
942
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000943 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000944 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000945 ArenaVector<HBasicBlock*> predecessors_;
946 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100947 HInstructionList instructions_;
948 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000949 HLoopInformation* loop_information_;
950 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000951 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100952 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100953 // The dex program counter of the first instruction of this block.
954 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100955 size_t lifetime_start_;
956 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100957 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100958
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000959 friend class HGraph;
960 friend class HInstruction;
961
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
963};
964
David Brazdilb2bd1c52015-03-25 11:17:37 +0000965// Iterates over the LoopInformation of all loops which contain 'block'
966// from the innermost to the outermost.
967class HLoopInformationOutwardIterator : public ValueObject {
968 public:
969 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
970 : current_(block.GetLoopInformation()) {}
971
972 bool Done() const { return current_ == nullptr; }
973
974 void Advance() {
975 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100976 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000977 }
978
979 HLoopInformation* Current() const {
980 DCHECK(!Done());
981 return current_;
982 }
983
984 private:
985 HLoopInformation* current_;
986
987 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
988};
989
Alexandre Ramesef20f712015-06-09 10:29:30 +0100990#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100991 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000992 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000993 M(ArrayGet, Instruction) \
994 M(ArrayLength, Instruction) \
995 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100996 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000997 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000998 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000999 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001000 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001001 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001002 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001003 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001004 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001005 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001006 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001007 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001008 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001009 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001010 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001011 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001012 M(FloatConstant, Constant) \
1013 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001014 M(GreaterThan, Condition) \
1015 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001016 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001017 M(InstanceFieldGet, Instruction) \
1018 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001019 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001020 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001021 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001022 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001023 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001024 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001025 M(LessThan, Condition) \
1026 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001027 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001028 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001029 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001030 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001031 M(Local, Instruction) \
1032 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001033 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001034 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001035 M(Mul, BinaryOperation) \
1036 M(Neg, UnaryOperation) \
1037 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001039 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001040 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001041 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001042 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001043 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001044 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001045 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001046 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001047 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001048 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(Return, Instruction) \
1050 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001051 M(Shl, BinaryOperation) \
1052 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001053 M(StaticFieldGet, Instruction) \
1054 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001055 M(UnresolvedInstanceFieldGet, Instruction) \
1056 M(UnresolvedInstanceFieldSet, Instruction) \
1057 M(UnresolvedStaticFieldGet, Instruction) \
1058 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001059 M(StoreLocal, Instruction) \
1060 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001061 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001062 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001063 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001064 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001065 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001066 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001067 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001068
Alexandre Ramesef20f712015-06-09 10:29:30 +01001069#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1070
1071#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1072
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001073#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1074
Mark Mendell0616ae02015-04-17 12:49:27 -04001075#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1076 M(X86ComputeBaseMethodAddress, Instruction) \
1077 M(X86LoadFromConstantTable, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001078
1079#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1080
1081#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1082 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1083 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1084 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001085 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001086 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1087 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1088
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001089#define FOR_EACH_INSTRUCTION(M) \
1090 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1091 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001092 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001093 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001094 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001095
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001096#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001097FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1098#undef FORWARD_DECLARATION
1099
Roland Levillainccc07a92014-09-16 14:48:16 +01001100#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001101 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1102 const char* DebugName() const OVERRIDE { return #type; } \
1103 const H##type* As##type() const OVERRIDE { return this; } \
1104 H##type* As##type() OVERRIDE { return this; } \
1105 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001106 return other->Is##type(); \
1107 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001108 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001109
David Brazdiled596192015-01-23 10:39:45 +00001110template <typename T> class HUseList;
1111
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001112template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001113class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001114 public:
David Brazdiled596192015-01-23 10:39:45 +00001115 HUseListNode* GetPrevious() const { return prev_; }
1116 HUseListNode* GetNext() const { return next_; }
1117 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001118 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001119 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001120
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001121 private:
David Brazdiled596192015-01-23 10:39:45 +00001122 HUseListNode(T user, size_t index)
1123 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1124
1125 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001126 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001127 HUseListNode<T>* prev_;
1128 HUseListNode<T>* next_;
1129
1130 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001131
1132 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1133};
1134
David Brazdiled596192015-01-23 10:39:45 +00001135template <typename T>
1136class HUseList : public ValueObject {
1137 public:
1138 HUseList() : first_(nullptr) {}
1139
1140 void Clear() {
1141 first_ = nullptr;
1142 }
1143
1144 // Adds a new entry at the beginning of the use list and returns
1145 // the newly created node.
1146 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001147 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001148 if (IsEmpty()) {
1149 first_ = new_node;
1150 } else {
1151 first_->prev_ = new_node;
1152 new_node->next_ = first_;
1153 first_ = new_node;
1154 }
1155 return new_node;
1156 }
1157
1158 HUseListNode<T>* GetFirst() const {
1159 return first_;
1160 }
1161
1162 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001163 DCHECK(node != nullptr);
1164 DCHECK(Contains(node));
1165
David Brazdiled596192015-01-23 10:39:45 +00001166 if (node->prev_ != nullptr) {
1167 node->prev_->next_ = node->next_;
1168 }
1169 if (node->next_ != nullptr) {
1170 node->next_->prev_ = node->prev_;
1171 }
1172 if (node == first_) {
1173 first_ = node->next_;
1174 }
1175 }
1176
David Brazdil1abb4192015-02-17 18:33:36 +00001177 bool Contains(const HUseListNode<T>* node) const {
1178 if (node == nullptr) {
1179 return false;
1180 }
1181 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1182 if (current == node) {
1183 return true;
1184 }
1185 }
1186 return false;
1187 }
1188
David Brazdiled596192015-01-23 10:39:45 +00001189 bool IsEmpty() const {
1190 return first_ == nullptr;
1191 }
1192
1193 bool HasOnlyOneUse() const {
1194 return first_ != nullptr && first_->next_ == nullptr;
1195 }
1196
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001197 size_t SizeSlow() const {
1198 size_t count = 0;
1199 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1200 ++count;
1201 }
1202 return count;
1203 }
1204
David Brazdiled596192015-01-23 10:39:45 +00001205 private:
1206 HUseListNode<T>* first_;
1207};
1208
1209template<typename T>
1210class HUseIterator : public ValueObject {
1211 public:
1212 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1213
1214 bool Done() const { return current_ == nullptr; }
1215
1216 void Advance() {
1217 DCHECK(!Done());
1218 current_ = current_->GetNext();
1219 }
1220
1221 HUseListNode<T>* Current() const {
1222 DCHECK(!Done());
1223 return current_;
1224 }
1225
1226 private:
1227 HUseListNode<T>* current_;
1228
1229 friend class HValue;
1230};
1231
David Brazdil1abb4192015-02-17 18:33:36 +00001232// This class is used by HEnvironment and HInstruction classes to record the
1233// instructions they use and pointers to the corresponding HUseListNodes kept
1234// by the used instructions.
1235template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001236class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001237 public:
1238 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1239 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1240
1241 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1242 : instruction_(old_record.instruction_), use_node_(use_node) {
1243 DCHECK(instruction_ != nullptr);
1244 DCHECK(use_node_ != nullptr);
1245 DCHECK(old_record.use_node_ == nullptr);
1246 }
1247
1248 HInstruction* GetInstruction() const { return instruction_; }
1249 HUseListNode<T>* GetUseNode() const { return use_node_; }
1250
1251 private:
1252 // Instruction used by the user.
1253 HInstruction* instruction_;
1254
1255 // Corresponding entry in the use list kept by 'instruction_'.
1256 HUseListNode<T>* use_node_;
1257};
1258
Aart Bik854a02b2015-07-14 16:07:00 -07001259/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001260 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001261 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001262 * For write/read dependences on fields/arrays, the dependence analysis uses
1263 * type disambiguation (e.g. a float field write cannot modify the value of an
1264 * integer field read) and the access type (e.g. a reference array write cannot
1265 * modify the value of a reference field read [although it may modify the
1266 * reference fetch prior to reading the field, which is represented by its own
1267 * write/read dependence]). The analysis makes conservative points-to
1268 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1269 * the same, and any reference read depends on any reference read without
1270 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001271 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001272 * The internal representation uses 38-bit and is described in the table below.
1273 * The first line indicates the side effect, and for field/array accesses the
1274 * second line indicates the type of the access (in the order of the
1275 * Primitive::Type enum).
1276 * The two numbered lines below indicate the bit position in the bitfield (read
1277 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001278 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001279 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1280 * +-------------+---------+---------+--------------+---------+---------+
1281 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1282 * | 3 |333333322|222222221| 1 |111111110|000000000|
1283 * | 7 |654321098|765432109| 8 |765432109|876543210|
1284 *
1285 * Note that, to ease the implementation, 'changes' bits are least significant
1286 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001287 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001288class SideEffects : public ValueObject {
1289 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001290 SideEffects() : flags_(0) {}
1291
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001292 static SideEffects None() {
1293 return SideEffects(0);
1294 }
1295
1296 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001297 return SideEffects(kAllChangeBits | kAllDependOnBits);
1298 }
1299
1300 static SideEffects AllChanges() {
1301 return SideEffects(kAllChangeBits);
1302 }
1303
1304 static SideEffects AllDependencies() {
1305 return SideEffects(kAllDependOnBits);
1306 }
1307
1308 static SideEffects AllExceptGCDependency() {
1309 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1310 }
1311
1312 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001313 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001314 }
1315
Aart Bik34c3ba92015-07-20 14:08:59 -07001316 static SideEffects AllWrites() {
1317 return SideEffects(kAllWrites);
1318 }
1319
1320 static SideEffects AllReads() {
1321 return SideEffects(kAllReads);
1322 }
1323
1324 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1325 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001326 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001327 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001328 }
1329
Aart Bik854a02b2015-07-14 16:07:00 -07001330 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1331 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001332 }
1333
Aart Bik34c3ba92015-07-20 14:08:59 -07001334 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1335 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001336 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001337 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001338 }
1339
1340 static SideEffects ArrayReadOfType(Primitive::Type type) {
1341 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1342 }
1343
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001344 static SideEffects CanTriggerGC() {
1345 return SideEffects(1ULL << kCanTriggerGCBit);
1346 }
1347
1348 static SideEffects DependsOnGC() {
1349 return SideEffects(1ULL << kDependsOnGCBit);
1350 }
1351
Aart Bik854a02b2015-07-14 16:07:00 -07001352 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001353 SideEffects Union(SideEffects other) const {
1354 return SideEffects(flags_ | other.flags_);
1355 }
1356
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001357 SideEffects Exclusion(SideEffects other) const {
1358 return SideEffects(flags_ & ~other.flags_);
1359 }
1360
1361 bool Includes(SideEffects other) const {
1362 return (other.flags_ & flags_) == other.flags_;
1363 }
1364
1365 bool HasSideEffects() const {
1366 return (flags_ & kAllChangeBits);
1367 }
1368
1369 bool HasDependencies() const {
1370 return (flags_ & kAllDependOnBits);
1371 }
1372
1373 // Returns true if there are no side effects or dependencies.
1374 bool DoesNothing() const {
1375 return flags_ == 0;
1376 }
1377
Aart Bik854a02b2015-07-14 16:07:00 -07001378 // Returns true if something is written.
1379 bool DoesAnyWrite() const {
1380 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001381 }
1382
Aart Bik854a02b2015-07-14 16:07:00 -07001383 // Returns true if something is read.
1384 bool DoesAnyRead() const {
1385 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001386 }
1387
Aart Bik854a02b2015-07-14 16:07:00 -07001388 // Returns true if potentially everything is written and read
1389 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001390 bool DoesAllReadWrite() const {
1391 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1392 }
1393
Aart Bik854a02b2015-07-14 16:07:00 -07001394 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001395 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001396 }
1397
1398 // Returns true if this may read something written by other.
1399 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001400 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1401 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001402 }
1403
1404 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001406 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001407 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001408 for (int s = kLastBit; s >= 0; s--) {
1409 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1410 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1411 // This is a bit for the GC side effect.
1412 if (current_bit_is_set) {
1413 flags += "GC";
1414 }
Aart Bik854a02b2015-07-14 16:07:00 -07001415 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001416 } else {
1417 // This is a bit for the array/field analysis.
1418 // The underscore character stands for the 'can trigger GC' bit.
1419 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1420 if (current_bit_is_set) {
1421 flags += kDebug[s];
1422 }
1423 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1424 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1425 flags += "|";
1426 }
1427 }
Aart Bik854a02b2015-07-14 16:07:00 -07001428 }
1429 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001430 }
1431
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001432 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001433
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001434 private:
1435 static constexpr int kFieldArrayAnalysisBits = 9;
1436
1437 static constexpr int kFieldWriteOffset = 0;
1438 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1439 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1440 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1441
1442 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1443
1444 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1445 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1446 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1447 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1448
1449 static constexpr int kLastBit = kDependsOnGCBit;
1450 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1451
1452 // Aliases.
1453
1454 static_assert(kChangeBits == kDependOnBits,
1455 "the 'change' bits should match the 'depend on' bits.");
1456
1457 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1458 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1459 static constexpr uint64_t kAllWrites =
1460 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1461 static constexpr uint64_t kAllReads =
1462 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001463
Aart Bik854a02b2015-07-14 16:07:00 -07001464 // Work around the fact that HIR aliases I/F and J/D.
1465 // TODO: remove this interceptor once HIR types are clean
1466 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1467 switch (type) {
1468 case Primitive::kPrimInt:
1469 case Primitive::kPrimFloat:
1470 return TypeFlag(Primitive::kPrimInt, offset) |
1471 TypeFlag(Primitive::kPrimFloat, offset);
1472 case Primitive::kPrimLong:
1473 case Primitive::kPrimDouble:
1474 return TypeFlag(Primitive::kPrimLong, offset) |
1475 TypeFlag(Primitive::kPrimDouble, offset);
1476 default:
1477 return TypeFlag(type, offset);
1478 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001479 }
1480
Aart Bik854a02b2015-07-14 16:07:00 -07001481 // Translates type to bit flag.
1482 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1483 CHECK_NE(type, Primitive::kPrimVoid);
1484 const uint64_t one = 1;
1485 const int shift = type; // 0-based consecutive enum
1486 DCHECK_LE(kFieldWriteOffset, shift);
1487 DCHECK_LT(shift, kArrayWriteOffset);
1488 return one << (type + offset);
1489 }
1490
1491 // Private constructor on direct flags value.
1492 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1493
1494 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001495};
1496
David Brazdiled596192015-01-23 10:39:45 +00001497// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001498class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001499 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001500 HEnvironment(ArenaAllocator* arena,
1501 size_t number_of_vregs,
1502 const DexFile& dex_file,
1503 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001504 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001505 InvokeType invoke_type,
1506 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001507 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1508 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001509 parent_(nullptr),
1510 dex_file_(dex_file),
1511 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001512 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001513 invoke_type_(invoke_type),
1514 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001515 }
1516
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001517 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001518 : HEnvironment(arena,
1519 to_copy.Size(),
1520 to_copy.GetDexFile(),
1521 to_copy.GetMethodIdx(),
1522 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001523 to_copy.GetInvokeType(),
1524 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001525
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001526 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001527 if (parent_ != nullptr) {
1528 parent_->SetAndCopyParentChain(allocator, parent);
1529 } else {
1530 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1531 parent_->CopyFrom(parent);
1532 if (parent->GetParent() != nullptr) {
1533 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1534 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001535 }
David Brazdiled596192015-01-23 10:39:45 +00001536 }
1537
Vladimir Marko71bf8092015-09-15 15:33:14 +01001538 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001539 void CopyFrom(HEnvironment* environment);
1540
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001541 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1542 // input to the loop phi instead. This is for inserting instructions that
1543 // require an environment (like HDeoptimization) in the loop pre-header.
1544 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001545
1546 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001547 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001548 }
1549
1550 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001551 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001552 }
1553
David Brazdil1abb4192015-02-17 18:33:36 +00001554 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001555
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001556 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001557
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001558 HEnvironment* GetParent() const { return parent_; }
1559
1560 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001561 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001562 }
1563
1564 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001565 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001566 }
1567
1568 uint32_t GetDexPc() const {
1569 return dex_pc_;
1570 }
1571
1572 uint32_t GetMethodIdx() const {
1573 return method_idx_;
1574 }
1575
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001576 InvokeType GetInvokeType() const {
1577 return invoke_type_;
1578 }
1579
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001580 const DexFile& GetDexFile() const {
1581 return dex_file_;
1582 }
1583
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001584 HInstruction* GetHolder() const {
1585 return holder_;
1586 }
1587
David Brazdiled596192015-01-23 10:39:45 +00001588 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001589 // Record instructions' use entries of this environment for constant-time removal.
1590 // It should only be called by HInstruction when a new environment use is added.
1591 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1592 DCHECK(env_use->GetUser() == this);
1593 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001594 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001595 }
David Brazdiled596192015-01-23 10:39:45 +00001596
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001597 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1598 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001599 HEnvironment* parent_;
1600 const DexFile& dex_file_;
1601 const uint32_t method_idx_;
1602 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001603 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001604
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001605 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001606 HInstruction* const holder_;
1607
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001608 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001609
1610 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1611};
1612
Calin Juravleacf735c2015-02-12 15:25:22 +00001613class ReferenceTypeInfo : ValueObject {
1614 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001615 typedef Handle<mirror::Class> TypeHandle;
1616
Calin Juravle2e768302015-07-28 14:41:11 +00001617 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1618 // The constructor will check that the type_handle is valid.
1619 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001620 }
1621
Calin Juravle2e768302015-07-28 14:41:11 +00001622 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1623
1624 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1625 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001626 }
1627
Calin Juravle2e768302015-07-28 14:41:11 +00001628 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1629 return IsValidHandle(type_handle_);
1630 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001631
Calin Juravleacf735c2015-02-12 15:25:22 +00001632 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001633
1634 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1635 DCHECK(IsValid());
1636 return GetTypeHandle()->IsObjectClass();
1637 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001638
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001639 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1640 return IsValid() && GetTypeHandle()->IsStringClass();
1641 }
1642
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001643 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1644 DCHECK(IsValid());
1645 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1646 }
1647
Mathieu Chartier90443472015-07-16 20:32:27 -07001648 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001649 DCHECK(IsValid());
1650 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001651 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001652
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001653 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1654 return GetTypeHandle()->IsArrayClass();
1655 }
1656
1657 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1658 if (!IsExact()) return false;
1659 if (!IsArrayClass()) return false;
1660 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1661 }
1662
Calin Juravleacf735c2015-02-12 15:25:22 +00001663 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1664
Mathieu Chartier90443472015-07-16 20:32:27 -07001665 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001666 DCHECK(IsValid());
1667 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001668 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1669 }
1670
1671 // Returns true if the type information provide the same amount of details.
1672 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001673 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001674 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001675 if (!IsValid() && !rti.IsValid()) {
1676 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001677 return true;
1678 }
Calin Juravle2e768302015-07-28 14:41:11 +00001679 if (!IsValid() || !rti.IsValid()) {
1680 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001681 return false;
1682 }
Calin Juravle2e768302015-07-28 14:41:11 +00001683 return IsExact() == rti.IsExact()
1684 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001685 }
1686
1687 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001688 ReferenceTypeInfo();
1689 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001690
Calin Juravleacf735c2015-02-12 15:25:22 +00001691 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001692 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001693 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001694 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001695 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001696};
1697
1698std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1699
Vladimir Markof9f64412015-09-02 14:05:49 +01001700class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001701 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001702 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001703 : previous_(nullptr),
1704 next_(nullptr),
1705 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001706 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001707 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001708 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001709 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001710 locations_(nullptr),
1711 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001712 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001713 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001714 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001715
Dave Allison20dfc792014-06-16 20:44:29 -07001716 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001717
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001718#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001719 enum InstructionKind {
1720 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1721 };
1722#undef DECLARE_KIND
1723
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001724 HInstruction* GetNext() const { return next_; }
1725 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001726
Calin Juravle77520bc2015-01-12 18:45:46 +00001727 HInstruction* GetNextDisregardingMoves() const;
1728 HInstruction* GetPreviousDisregardingMoves() const;
1729
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001730 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001731 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001732 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001733 bool IsInBlock() const { return block_ != nullptr; }
1734 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001735 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001736
Roland Levillain6b879dd2014-09-22 17:13:44 +01001737 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001738 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001739
1740 virtual void Accept(HGraphVisitor* visitor) = 0;
1741 virtual const char* DebugName() const = 0;
1742
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001743 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001744 void SetRawInputAt(size_t index, HInstruction* input) {
1745 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1746 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001747
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001748 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001749
1750 uint32_t GetDexPc() const { return dex_pc_; }
1751
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001752 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001753
Roland Levillaine161a2a2014-10-03 12:45:18 +01001754 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001755 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001756
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001757 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001758 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001759
Calin Juravle10e244f2015-01-26 18:54:32 +00001760 // Does not apply for all instructions, but having this at top level greatly
1761 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001762 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001763 virtual bool CanBeNull() const {
1764 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1765 return true;
1766 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001767
Calin Juravle641547a2015-04-21 22:08:51 +01001768 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1769 UNUSED(obj);
1770 return false;
1771 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001772
Calin Juravle2e768302015-07-28 14:41:11 +00001773 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001774
Calin Juravle61d544b2015-02-23 16:46:57 +00001775 ReferenceTypeInfo GetReferenceTypeInfo() const {
1776 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1777 return reference_type_info_;
1778 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001779
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001780 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001781 DCHECK(user != nullptr);
1782 HUseListNode<HInstruction*>* use =
1783 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1784 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001785 }
1786
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001787 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001788 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001789 HUseListNode<HEnvironment*>* env_use =
1790 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1791 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001792 }
1793
David Brazdil1abb4192015-02-17 18:33:36 +00001794 void RemoveAsUserOfInput(size_t input) {
1795 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1796 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1797 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001798
David Brazdil1abb4192015-02-17 18:33:36 +00001799 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1800 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001801
David Brazdiled596192015-01-23 10:39:45 +00001802 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1803 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001804 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001805 bool HasOnlyOneNonEnvironmentUse() const {
1806 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1807 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001808
Roland Levillain6c82d402014-10-13 16:10:27 +01001809 // Does this instruction strictly dominate `other_instruction`?
1810 // Returns false if this instruction and `other_instruction` are the same.
1811 // Aborts if this instruction and `other_instruction` are both phis.
1812 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001813
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001814 int GetId() const { return id_; }
1815 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001816
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001817 int GetSsaIndex() const { return ssa_index_; }
1818 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1819 bool HasSsaIndex() const { return ssa_index_ != -1; }
1820
1821 bool HasEnvironment() const { return environment_ != nullptr; }
1822 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001823 // Set the `environment_` field. Raw because this method does not
1824 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001825 void SetRawEnvironment(HEnvironment* environment) {
1826 DCHECK(environment_ == nullptr);
1827 DCHECK_EQ(environment->GetHolder(), this);
1828 environment_ = environment;
1829 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001830
1831 // Set the environment of this instruction, copying it from `environment`. While
1832 // copying, the uses lists are being updated.
1833 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001834 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001835 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001836 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001837 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001838 if (environment->GetParent() != nullptr) {
1839 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1840 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001841 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001842
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001843 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1844 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001845 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001846 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001847 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001848 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001849 if (environment->GetParent() != nullptr) {
1850 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1851 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001852 }
1853
Nicolas Geoffray39468442014-09-02 15:17:15 +01001854 // Returns the number of entries in the environment. Typically, that is the
1855 // number of dex registers in a method. It could be more in case of inlining.
1856 size_t EnvironmentSize() const;
1857
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001858 LocationSummary* GetLocations() const { return locations_; }
1859 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001860
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001861 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001862 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001863
Alexandre Rames188d4312015-04-09 18:30:21 +01001864 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1865 // uses of this instruction by `other` are *not* updated.
1866 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1867 ReplaceWith(other);
1868 other->ReplaceInput(this, use_index);
1869 }
1870
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001871 // Move `this` instruction before `cursor`.
1872 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001873
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001874#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001875 bool Is##type() const { return (As##type() != nullptr); } \
1876 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001877 virtual H##type* As##type() { return nullptr; }
1878
1879 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1880#undef INSTRUCTION_TYPE_CHECK
1881
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001882 // Returns whether the instruction can be moved within the graph.
1883 virtual bool CanBeMoved() const { return false; }
1884
1885 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001886 virtual bool InstructionTypeEquals(HInstruction* other) const {
1887 UNUSED(other);
1888 return false;
1889 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001890
1891 // Returns whether any data encoded in the two instructions is equal.
1892 // This method does not look at the inputs. Both instructions must be
1893 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001894 virtual bool InstructionDataEquals(HInstruction* other) const {
1895 UNUSED(other);
1896 return false;
1897 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001898
1899 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001900 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001901 // 2) Their inputs are identical.
1902 bool Equals(HInstruction* other) const;
1903
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001904 virtual InstructionKind GetKind() const = 0;
1905
1906 virtual size_t ComputeHashCode() const {
1907 size_t result = GetKind();
1908 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1909 result = (result * 31) + InputAt(i)->GetId();
1910 }
1911 return result;
1912 }
1913
1914 SideEffects GetSideEffects() const { return side_effects_; }
1915
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001916 size_t GetLifetimePosition() const { return lifetime_position_; }
1917 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1918 LiveInterval* GetLiveInterval() const { return live_interval_; }
1919 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1920 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1921
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001922 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1923
1924 // Returns whether the code generation of the instruction will require to have access
1925 // to the current method. Such instructions are:
1926 // (1): Instructions that require an environment, as calling the runtime requires
1927 // to walk the stack and have the current method stored at a specific stack address.
1928 // (2): Object literals like classes and strings, that are loaded from the dex cache
1929 // fields of the current method.
1930 bool NeedsCurrentMethod() const {
1931 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1932 }
1933
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001934 virtual bool NeedsDexCache() const { return false; }
1935
Mark Mendellc4701932015-04-10 13:18:51 -04001936 // Does this instruction have any use in an environment before
1937 // control flow hits 'other'?
1938 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1939
1940 // Remove all references to environment uses of this instruction.
1941 // The caller must ensure that this is safe to do.
1942 void RemoveEnvironmentUsers();
1943
David Brazdil1abb4192015-02-17 18:33:36 +00001944 protected:
1945 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1946 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1947
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001948 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001949 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1950
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001951 HInstruction* previous_;
1952 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001953 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001954 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001955
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001956 // An instruction gets an id when it is added to the graph.
1957 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001958 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001959 int id_;
1960
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001961 // When doing liveness analysis, instructions that have uses get an SSA index.
1962 int ssa_index_;
1963
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001964 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001965 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001966
1967 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001968 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001969
Nicolas Geoffray39468442014-09-02 15:17:15 +01001970 // The environment associated with this instruction. Not null if the instruction
1971 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001972 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001973
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001974 // Set by the code generator.
1975 LocationSummary* locations_;
1976
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001977 // Set by the liveness analysis.
1978 LiveInterval* live_interval_;
1979
1980 // Set by the liveness analysis, this is the position in a linear
1981 // order of blocks where this instruction's live interval start.
1982 size_t lifetime_position_;
1983
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001984 const SideEffects side_effects_;
1985
Calin Juravleacf735c2015-02-12 15:25:22 +00001986 // TODO: for primitive types this should be marked as invalid.
1987 ReferenceTypeInfo reference_type_info_;
1988
David Brazdil1abb4192015-02-17 18:33:36 +00001989 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001990 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001991 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001992 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001993 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001994
1995 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1996};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001997std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001998
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001999class HInputIterator : public ValueObject {
2000 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002001 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002002
2003 bool Done() const { return index_ == instruction_->InputCount(); }
2004 HInstruction* Current() const { return instruction_->InputAt(index_); }
2005 void Advance() { index_++; }
2006
2007 private:
2008 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002009 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002010
2011 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2012};
2013
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002014class HInstructionIterator : public ValueObject {
2015 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002016 explicit HInstructionIterator(const HInstructionList& instructions)
2017 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002018 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002019 }
2020
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002021 bool Done() const { return instruction_ == nullptr; }
2022 HInstruction* Current() const { return instruction_; }
2023 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002024 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002025 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002026 }
2027
2028 private:
2029 HInstruction* instruction_;
2030 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002031
2032 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002033};
2034
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002035class HBackwardInstructionIterator : public ValueObject {
2036 public:
2037 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2038 : instruction_(instructions.last_instruction_) {
2039 next_ = Done() ? nullptr : instruction_->GetPrevious();
2040 }
2041
2042 bool Done() const { return instruction_ == nullptr; }
2043 HInstruction* Current() const { return instruction_; }
2044 void Advance() {
2045 instruction_ = next_;
2046 next_ = Done() ? nullptr : instruction_->GetPrevious();
2047 }
2048
2049 private:
2050 HInstruction* instruction_;
2051 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002052
2053 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002054};
2055
Vladimir Markof9f64412015-09-02 14:05:49 +01002056template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002057class HTemplateInstruction: public HInstruction {
2058 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002059 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002060 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002061 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002062
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002063 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002064
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002065 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002066 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2067 DCHECK_LT(i, N);
2068 return inputs_[i];
2069 }
David Brazdil1abb4192015-02-17 18:33:36 +00002070
2071 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002072 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002073 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002074 }
2075
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002076 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002077 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002078
2079 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002080};
2081
Vladimir Markof9f64412015-09-02 14:05:49 +01002082// HTemplateInstruction specialization for N=0.
2083template<>
2084class HTemplateInstruction<0>: public HInstruction {
2085 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002086 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002087 : HInstruction(side_effects, dex_pc) {}
2088
Vladimir Markof9f64412015-09-02 14:05:49 +01002089 virtual ~HTemplateInstruction() {}
2090
2091 size_t InputCount() const OVERRIDE { return 0; }
2092
2093 protected:
2094 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2095 LOG(FATAL) << "Unreachable";
2096 UNREACHABLE();
2097 }
2098
2099 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2100 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2101 LOG(FATAL) << "Unreachable";
2102 UNREACHABLE();
2103 }
2104
2105 private:
2106 friend class SsaBuilder;
2107};
2108
Dave Allison20dfc792014-06-16 20:44:29 -07002109template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002110class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002111 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002112 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002113 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002114 virtual ~HExpression() {}
2115
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002116 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002117
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002118 protected:
2119 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002120};
2121
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002122// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2123// instruction that branches to the exit block.
2124class HReturnVoid : public HTemplateInstruction<0> {
2125 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002126 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2127 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002128
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002129 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002130
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002131 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002132
2133 private:
2134 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2135};
2136
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002137// Represents dex's RETURN opcodes. A HReturn is a control flow
2138// instruction that branches to the exit block.
2139class HReturn : public HTemplateInstruction<1> {
2140 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002141 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2142 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002143 SetRawInputAt(0, value);
2144 }
2145
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002146 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002147
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002148 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002149
2150 private:
2151 DISALLOW_COPY_AND_ASSIGN(HReturn);
2152};
2153
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002154// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002155// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156// exit block.
2157class HExit : public HTemplateInstruction<0> {
2158 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002159 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002160
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002161 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002162
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002163 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002164
2165 private:
2166 DISALLOW_COPY_AND_ASSIGN(HExit);
2167};
2168
2169// Jumps from one block to another.
2170class HGoto : public HTemplateInstruction<0> {
2171 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002172 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002173
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002174 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002176 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002177 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002178 }
2179
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002180 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181
2182 private:
2183 DISALLOW_COPY_AND_ASSIGN(HGoto);
2184};
2185
Roland Levillain9867bc72015-08-05 10:21:34 +01002186class HConstant : public HExpression<0> {
2187 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002188 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2189 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002190
2191 bool CanBeMoved() const OVERRIDE { return true; }
2192
2193 virtual bool IsMinusOne() const { return false; }
2194 virtual bool IsZero() const { return false; }
2195 virtual bool IsOne() const { return false; }
2196
David Brazdil77a48ae2015-09-15 12:34:04 +00002197 virtual uint64_t GetValueAsUint64() const = 0;
2198
Roland Levillain9867bc72015-08-05 10:21:34 +01002199 DECLARE_INSTRUCTION(Constant);
2200
2201 private:
2202 DISALLOW_COPY_AND_ASSIGN(HConstant);
2203};
2204
2205class HNullConstant : public HConstant {
2206 public:
2207 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2208 return true;
2209 }
2210
David Brazdil77a48ae2015-09-15 12:34:04 +00002211 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2212
Roland Levillain9867bc72015-08-05 10:21:34 +01002213 size_t ComputeHashCode() const OVERRIDE { return 0; }
2214
2215 DECLARE_INSTRUCTION(NullConstant);
2216
2217 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002218 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002219
2220 friend class HGraph;
2221 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2222};
2223
2224// Constants of the type int. Those can be from Dex instructions, or
2225// synthesized (for example with the if-eqz instruction).
2226class HIntConstant : public HConstant {
2227 public:
2228 int32_t GetValue() const { return value_; }
2229
David Brazdil9f389d42015-10-01 14:32:56 +01002230 uint64_t GetValueAsUint64() const OVERRIDE {
2231 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2232 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002233
Roland Levillain9867bc72015-08-05 10:21:34 +01002234 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2235 DCHECK(other->IsIntConstant());
2236 return other->AsIntConstant()->value_ == value_;
2237 }
2238
2239 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2240
2241 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2242 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2243 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2244
2245 DECLARE_INSTRUCTION(IntConstant);
2246
2247 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002248 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2249 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2250 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2251 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002252
2253 const int32_t value_;
2254
2255 friend class HGraph;
2256 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2257 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2258 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2259};
2260
2261class HLongConstant : public HConstant {
2262 public:
2263 int64_t GetValue() const { return value_; }
2264
David Brazdil77a48ae2015-09-15 12:34:04 +00002265 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2266
Roland Levillain9867bc72015-08-05 10:21:34 +01002267 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2268 DCHECK(other->IsLongConstant());
2269 return other->AsLongConstant()->value_ == value_;
2270 }
2271
2272 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2273
2274 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2275 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2276 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2277
2278 DECLARE_INSTRUCTION(LongConstant);
2279
2280 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002281 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2282 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002283
2284 const int64_t value_;
2285
2286 friend class HGraph;
2287 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2288};
Dave Allison20dfc792014-06-16 20:44:29 -07002289
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002290// Conditional branch. A block ending with an HIf instruction must have
2291// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002292class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002293 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002294 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2295 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002296 SetRawInputAt(0, input);
2297 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002298
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002299 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002300
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002301 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002302 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002303 }
2304
2305 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002306 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002307 }
2308
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002309 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002310
2311 private:
2312 DISALLOW_COPY_AND_ASSIGN(HIf);
2313};
2314
David Brazdilfc6a86a2015-06-26 10:33:45 +00002315
2316// Abstract instruction which marks the beginning and/or end of a try block and
2317// links it to the respective exception handlers. Behaves the same as a Goto in
2318// non-exceptional control flow.
2319// Normal-flow successor is stored at index zero, exception handlers under
2320// higher indices in no particular order.
2321class HTryBoundary : public HTemplateInstruction<0> {
2322 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002323 enum BoundaryKind {
2324 kEntry,
2325 kExit,
2326 };
2327
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002328 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2329 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002330
2331 bool IsControlFlow() const OVERRIDE { return true; }
2332
2333 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002334 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002335
2336 // Returns whether `handler` is among its exception handlers (non-zero index
2337 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002338 bool HasExceptionHandler(const HBasicBlock& handler) const {
2339 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002340 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002341 }
2342
2343 // If not present already, adds `handler` to its block's list of exception
2344 // handlers.
2345 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002346 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002347 GetBlock()->AddSuccessor(handler);
2348 }
2349 }
2350
David Brazdil56e1acc2015-06-30 15:41:36 +01002351 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002352
David Brazdilffee3d32015-07-06 11:48:53 +01002353 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2354
David Brazdilfc6a86a2015-06-26 10:33:45 +00002355 DECLARE_INSTRUCTION(TryBoundary);
2356
2357 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002358 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002359
2360 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2361};
2362
David Brazdilffee3d32015-07-06 11:48:53 +01002363// Iterator over exception handlers of a given HTryBoundary, i.e. over
2364// exceptional successors of its basic block.
2365class HExceptionHandlerIterator : public ValueObject {
2366 public:
2367 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2368 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2369
Vladimir Marko60584552015-09-03 13:35:12 +00002370 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002371 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002372 size_t CurrentSuccessorIndex() const { return index_; }
2373 void Advance() { ++index_; }
2374
2375 private:
2376 const HBasicBlock& block_;
2377 size_t index_;
2378
2379 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2380};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002381
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002382// Deoptimize to interpreter, upon checking a condition.
2383class HDeoptimize : public HTemplateInstruction<1> {
2384 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002385 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2386 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002387 SetRawInputAt(0, cond);
2388 }
2389
2390 bool NeedsEnvironment() const OVERRIDE { return true; }
2391 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002392
2393 DECLARE_INSTRUCTION(Deoptimize);
2394
2395 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002396 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2397};
2398
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002399// Represents the ArtMethod that was passed as a first argument to
2400// the method. It is used by instructions that depend on it, like
2401// instructions that work with the dex cache.
2402class HCurrentMethod : public HExpression<0> {
2403 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002404 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2405 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002406
2407 DECLARE_INSTRUCTION(CurrentMethod);
2408
2409 private:
2410 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2411};
2412
Mark Mendellfe57faa2015-09-18 09:26:15 -04002413// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2414// have one successor for each entry in the switch table, and the final successor
2415// will be the block containing the next Dex opcode.
2416class HPackedSwitch : public HTemplateInstruction<1> {
2417 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002418 HPackedSwitch(int32_t start_value,
2419 uint32_t num_entries,
2420 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002421 uint32_t dex_pc = kNoDexPc)
2422 : HTemplateInstruction(SideEffects::None(), dex_pc),
2423 start_value_(start_value),
2424 num_entries_(num_entries) {
2425 SetRawInputAt(0, input);
2426 }
2427
2428 bool IsControlFlow() const OVERRIDE { return true; }
2429
2430 int32_t GetStartValue() const { return start_value_; }
2431
Vladimir Marko211c2112015-09-24 16:52:33 +01002432 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002433
2434 HBasicBlock* GetDefaultBlock() const {
2435 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002436 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002437 }
2438 DECLARE_INSTRUCTION(PackedSwitch);
2439
2440 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002441 const int32_t start_value_;
2442 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002443
2444 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2445};
2446
Roland Levillain88cb1752014-10-20 16:36:47 +01002447class HUnaryOperation : public HExpression<1> {
2448 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002449 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2450 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002451 SetRawInputAt(0, input);
2452 }
2453
2454 HInstruction* GetInput() const { return InputAt(0); }
2455 Primitive::Type GetResultType() const { return GetType(); }
2456
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002457 bool CanBeMoved() const OVERRIDE { return true; }
2458 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002459 UNUSED(other);
2460 return true;
2461 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002462
Roland Levillain9240d6a2014-10-20 16:47:04 +01002463 // Try to statically evaluate `operation` and return a HConstant
2464 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002465 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002466 HConstant* TryStaticEvaluation() const;
2467
2468 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002469 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2470 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002471
Roland Levillain88cb1752014-10-20 16:36:47 +01002472 DECLARE_INSTRUCTION(UnaryOperation);
2473
2474 private:
2475 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2476};
2477
Dave Allison20dfc792014-06-16 20:44:29 -07002478class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002479 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002480 HBinaryOperation(Primitive::Type result_type,
2481 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002482 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002483 SideEffects side_effects = SideEffects::None(),
2484 uint32_t dex_pc = kNoDexPc)
2485 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002486 SetRawInputAt(0, left);
2487 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002488 }
2489
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002490 HInstruction* GetLeft() const { return InputAt(0); }
2491 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002492 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002493
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002494 virtual bool IsCommutative() const { return false; }
2495
2496 // Put constant on the right.
2497 // Returns whether order is changed.
2498 bool OrderInputsWithConstantOnTheRight() {
2499 HInstruction* left = InputAt(0);
2500 HInstruction* right = InputAt(1);
2501 if (left->IsConstant() && !right->IsConstant()) {
2502 ReplaceInput(right, 0);
2503 ReplaceInput(left, 1);
2504 return true;
2505 }
2506 return false;
2507 }
2508
2509 // Order inputs by instruction id, but favor constant on the right side.
2510 // This helps GVN for commutative ops.
2511 void OrderInputs() {
2512 DCHECK(IsCommutative());
2513 HInstruction* left = InputAt(0);
2514 HInstruction* right = InputAt(1);
2515 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2516 return;
2517 }
2518 if (OrderInputsWithConstantOnTheRight()) {
2519 return;
2520 }
2521 // Order according to instruction id.
2522 if (left->GetId() > right->GetId()) {
2523 ReplaceInput(right, 0);
2524 ReplaceInput(left, 1);
2525 }
2526 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002527
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002528 bool CanBeMoved() const OVERRIDE { return true; }
2529 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002530 UNUSED(other);
2531 return true;
2532 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002533
Roland Levillain9240d6a2014-10-20 16:47:04 +01002534 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002535 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002536 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002537 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002538
2539 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002540 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2541 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2542 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2543 HLongConstant* y ATTRIBUTE_UNUSED) const {
2544 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2545 return nullptr;
2546 }
2547 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2548 HIntConstant* y ATTRIBUTE_UNUSED) const {
2549 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2550 return nullptr;
2551 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002552
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002553 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002554 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002555 HConstant* GetConstantRight() const;
2556
2557 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002558 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002559 HInstruction* GetLeastConstantLeft() const;
2560
Roland Levillainccc07a92014-09-16 14:48:16 +01002561 DECLARE_INSTRUCTION(BinaryOperation);
2562
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002563 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002564 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2565};
2566
Mark Mendellc4701932015-04-10 13:18:51 -04002567// The comparison bias applies for floating point operations and indicates how NaN
2568// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002569enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002570 kNoBias, // bias is not applicable (i.e. for long operation)
2571 kGtBias, // return 1 for NaN comparisons
2572 kLtBias, // return -1 for NaN comparisons
2573};
2574
Dave Allison20dfc792014-06-16 20:44:29 -07002575class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002576 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002577 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2578 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002579 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002580 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002581
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002582 bool NeedsMaterialization() const { return needs_materialization_; }
2583 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002584
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002585 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002586 // `instruction`, and disregard moves in between.
2587 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002588
Dave Allison20dfc792014-06-16 20:44:29 -07002589 DECLARE_INSTRUCTION(Condition);
2590
2591 virtual IfCondition GetCondition() const = 0;
2592
Mark Mendellc4701932015-04-10 13:18:51 -04002593 virtual IfCondition GetOppositeCondition() const = 0;
2594
Roland Levillain4fa13f62015-07-06 18:11:54 +01002595 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002596
2597 void SetBias(ComparisonBias bias) { bias_ = bias; }
2598
2599 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2600 return bias_ == other->AsCondition()->bias_;
2601 }
2602
Roland Levillain4fa13f62015-07-06 18:11:54 +01002603 bool IsFPConditionTrueIfNaN() const {
2604 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2605 IfCondition if_cond = GetCondition();
2606 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2607 }
2608
2609 bool IsFPConditionFalseIfNaN() const {
2610 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2611 IfCondition if_cond = GetCondition();
2612 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2613 }
2614
Dave Allison20dfc792014-06-16 20:44:29 -07002615 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002616 // For register allocation purposes, returns whether this instruction needs to be
2617 // materialized (that is, not just be in the processor flags).
2618 bool needs_materialization_;
2619
Mark Mendellc4701932015-04-10 13:18:51 -04002620 // Needed if we merge a HCompare into a HCondition.
2621 ComparisonBias bias_;
2622
Dave Allison20dfc792014-06-16 20:44:29 -07002623 DISALLOW_COPY_AND_ASSIGN(HCondition);
2624};
2625
2626// Instruction to check if two inputs are equal to each other.
2627class HEqual : public HCondition {
2628 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002629 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2630 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002631
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002632 bool IsCommutative() const OVERRIDE { return true; }
2633
Roland Levillain9867bc72015-08-05 10:21:34 +01002634 template <typename T> bool Compute(T x, T y) const { return x == y; }
2635
2636 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002637 return GetBlock()->GetGraph()->GetIntConstant(
2638 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002639 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002640 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002641 return GetBlock()->GetGraph()->GetIntConstant(
2642 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002643 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002644
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002645 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002646
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002647 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002648 return kCondEQ;
2649 }
2650
Mark Mendellc4701932015-04-10 13:18:51 -04002651 IfCondition GetOppositeCondition() const OVERRIDE {
2652 return kCondNE;
2653 }
2654
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002655 private:
2656 DISALLOW_COPY_AND_ASSIGN(HEqual);
2657};
2658
Dave Allison20dfc792014-06-16 20:44:29 -07002659class HNotEqual : public HCondition {
2660 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002661 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2662 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002663
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002664 bool IsCommutative() const OVERRIDE { return true; }
2665
Roland Levillain9867bc72015-08-05 10:21:34 +01002666 template <typename T> bool Compute(T x, T y) const { return x != y; }
2667
2668 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002669 return GetBlock()->GetGraph()->GetIntConstant(
2670 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002671 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002672 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002673 return GetBlock()->GetGraph()->GetIntConstant(
2674 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002675 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002676
Dave Allison20dfc792014-06-16 20:44:29 -07002677 DECLARE_INSTRUCTION(NotEqual);
2678
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002679 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002680 return kCondNE;
2681 }
2682
Mark Mendellc4701932015-04-10 13:18:51 -04002683 IfCondition GetOppositeCondition() const OVERRIDE {
2684 return kCondEQ;
2685 }
2686
Dave Allison20dfc792014-06-16 20:44:29 -07002687 private:
2688 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2689};
2690
2691class HLessThan : public HCondition {
2692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002693 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2694 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002695
Roland Levillain9867bc72015-08-05 10:21:34 +01002696 template <typename T> bool Compute(T x, T y) const { return x < y; }
2697
2698 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002699 return GetBlock()->GetGraph()->GetIntConstant(
2700 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002701 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002702 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002703 return GetBlock()->GetGraph()->GetIntConstant(
2704 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002705 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002706
Dave Allison20dfc792014-06-16 20:44:29 -07002707 DECLARE_INSTRUCTION(LessThan);
2708
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002709 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002710 return kCondLT;
2711 }
2712
Mark Mendellc4701932015-04-10 13:18:51 -04002713 IfCondition GetOppositeCondition() const OVERRIDE {
2714 return kCondGE;
2715 }
2716
Dave Allison20dfc792014-06-16 20:44:29 -07002717 private:
2718 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2719};
2720
2721class HLessThanOrEqual : public HCondition {
2722 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002723 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2724 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002725
Roland Levillain9867bc72015-08-05 10:21:34 +01002726 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2727
2728 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002729 return GetBlock()->GetGraph()->GetIntConstant(
2730 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002731 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002732 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002733 return GetBlock()->GetGraph()->GetIntConstant(
2734 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002735 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002736
Dave Allison20dfc792014-06-16 20:44:29 -07002737 DECLARE_INSTRUCTION(LessThanOrEqual);
2738
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002739 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002740 return kCondLE;
2741 }
2742
Mark Mendellc4701932015-04-10 13:18:51 -04002743 IfCondition GetOppositeCondition() const OVERRIDE {
2744 return kCondGT;
2745 }
2746
Dave Allison20dfc792014-06-16 20:44:29 -07002747 private:
2748 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2749};
2750
2751class HGreaterThan : public HCondition {
2752 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002753 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2754 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002755
Roland Levillain9867bc72015-08-05 10:21:34 +01002756 template <typename T> bool Compute(T x, T y) const { return x > y; }
2757
2758 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002759 return GetBlock()->GetGraph()->GetIntConstant(
2760 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002761 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002762 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002763 return GetBlock()->GetGraph()->GetIntConstant(
2764 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002765 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002766
Dave Allison20dfc792014-06-16 20:44:29 -07002767 DECLARE_INSTRUCTION(GreaterThan);
2768
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002769 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002770 return kCondGT;
2771 }
2772
Mark Mendellc4701932015-04-10 13:18:51 -04002773 IfCondition GetOppositeCondition() const OVERRIDE {
2774 return kCondLE;
2775 }
2776
Dave Allison20dfc792014-06-16 20:44:29 -07002777 private:
2778 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2779};
2780
2781class HGreaterThanOrEqual : public HCondition {
2782 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002783 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2784 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002785
Roland Levillain9867bc72015-08-05 10:21:34 +01002786 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2787
2788 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002789 return GetBlock()->GetGraph()->GetIntConstant(
2790 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002791 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002792 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002793 return GetBlock()->GetGraph()->GetIntConstant(
2794 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002795 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002796
Dave Allison20dfc792014-06-16 20:44:29 -07002797 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2798
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002799 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002800 return kCondGE;
2801 }
2802
Mark Mendellc4701932015-04-10 13:18:51 -04002803 IfCondition GetOppositeCondition() const OVERRIDE {
2804 return kCondLT;
2805 }
2806
Dave Allison20dfc792014-06-16 20:44:29 -07002807 private:
2808 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2809};
2810
2811
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002812// Instruction to check how two inputs compare to each other.
2813// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2814class HCompare : public HBinaryOperation {
2815 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002816 HCompare(Primitive::Type type,
2817 HInstruction* first,
2818 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002819 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002820 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002821 : HBinaryOperation(Primitive::kPrimInt,
2822 first,
2823 second,
2824 SideEffectsForArchRuntimeCalls(type),
2825 dex_pc),
2826 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002827 DCHECK_EQ(type, first->GetType());
2828 DCHECK_EQ(type, second->GetType());
2829 }
2830
Roland Levillain9867bc72015-08-05 10:21:34 +01002831 template <typename T>
2832 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002833
Roland Levillain9867bc72015-08-05 10:21:34 +01002834 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002835 return GetBlock()->GetGraph()->GetIntConstant(
2836 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002837 }
2838 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002839 return GetBlock()->GetGraph()->GetIntConstant(
2840 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002841 }
2842
Calin Juravleddb7df22014-11-25 20:56:51 +00002843 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2844 return bias_ == other->AsCompare()->bias_;
2845 }
2846
Mark Mendellc4701932015-04-10 13:18:51 -04002847 ComparisonBias GetBias() const { return bias_; }
2848
Roland Levillain4fa13f62015-07-06 18:11:54 +01002849 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002850
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002851
2852 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2853 // MIPS64 uses a runtime call for FP comparisons.
2854 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2855 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002856
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002857 DECLARE_INSTRUCTION(Compare);
2858
2859 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002860 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002861
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002862 DISALLOW_COPY_AND_ASSIGN(HCompare);
2863};
2864
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002865// A local in the graph. Corresponds to a Dex register.
2866class HLocal : public HTemplateInstruction<0> {
2867 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002868 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002869 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002870
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002871 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002872
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002873 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002874
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002875 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002876 // The Dex register number.
2877 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002878
2879 DISALLOW_COPY_AND_ASSIGN(HLocal);
2880};
2881
2882// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002883class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002884 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002885 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2886 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002887 SetRawInputAt(0, local);
2888 }
2889
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002890 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2891
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002892 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002893
2894 private:
2895 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2896};
2897
2898// Store a value in a given local. This instruction has two inputs: the value
2899// and the local.
2900class HStoreLocal : public HTemplateInstruction<2> {
2901 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002902 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2903 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002904 SetRawInputAt(0, local);
2905 SetRawInputAt(1, value);
2906 }
2907
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002908 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2909
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002910 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002911
2912 private:
2913 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2914};
2915
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002916class HFloatConstant : public HConstant {
2917 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002918 float GetValue() const { return value_; }
2919
David Brazdil77a48ae2015-09-15 12:34:04 +00002920 uint64_t GetValueAsUint64() const OVERRIDE {
2921 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2922 }
2923
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002924 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002925 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002926 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002927 }
2928
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002929 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002930
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002931 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002932 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002933 }
2934 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002935 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002936 }
2937 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002938 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2939 }
2940 bool IsNaN() const {
2941 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002942 }
2943
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002944 DECLARE_INSTRUCTION(FloatConstant);
2945
2946 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002947 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2948 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2949 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2950 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002951
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002952 const float value_;
2953
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002954 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002955 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002956 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002957 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2958};
2959
2960class HDoubleConstant : public HConstant {
2961 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002962 double GetValue() const { return value_; }
2963
David Brazdil77a48ae2015-09-15 12:34:04 +00002964 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2965
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002966 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002967 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002968 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002969 }
2970
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002971 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002972
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002973 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002974 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002975 }
2976 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002977 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002978 }
2979 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002980 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2981 }
2982 bool IsNaN() const {
2983 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002984 }
2985
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002986 DECLARE_INSTRUCTION(DoubleConstant);
2987
2988 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002989 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2990 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2991 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2992 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002993
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002994 const double value_;
2995
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002996 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002997 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002998 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002999 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3000};
3001
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003002enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003003#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003004#include "intrinsics_list.h"
3005 kNone,
3006 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3007#undef INTRINSICS_LIST
3008#undef OPTIMIZING_INTRINSICS
3009};
3010std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3011
Agi Csaki05f20562015-08-19 14:58:14 -07003012enum IntrinsicNeedsEnvironmentOrCache {
3013 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3014 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003015};
3016
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003017class HInvoke : public HInstruction {
3018 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003019 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003020
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003021 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003022
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003023 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003024 SetRawInputAt(index, argument);
3025 }
3026
Roland Levillain3e3d7332015-04-28 11:00:54 +01003027 // Return the number of arguments. This number can be lower than
3028 // the number of inputs returned by InputCount(), as some invoke
3029 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3030 // inputs at the end of their list of inputs.
3031 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3032
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003033 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003034
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003035
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003036 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003037 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003038
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003039 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3040
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003041 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003042 return intrinsic_;
3043 }
3044
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003045 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003046
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003047 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003048 return GetEnvironment()->GetParent() != nullptr;
3049 }
3050
3051 bool CanThrow() const OVERRIDE { return true; }
3052
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003053 uint32_t* GetIntrinsicOptimizations() {
3054 return &intrinsic_optimizations_;
3055 }
3056
3057 const uint32_t* GetIntrinsicOptimizations() const {
3058 return &intrinsic_optimizations_;
3059 }
3060
3061 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3062
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003063 DECLARE_INSTRUCTION(Invoke);
3064
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003065 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003066 HInvoke(ArenaAllocator* arena,
3067 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003068 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003069 Primitive::Type return_type,
3070 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003071 uint32_t dex_method_index,
3072 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003073 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003074 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003075 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003076 inputs_(number_of_arguments + number_of_other_inputs,
3077 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003078 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003079 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003080 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003081 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003082 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003083 }
3084
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003085 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003086 return inputs_[index];
3087 }
3088
David Brazdil1abb4192015-02-17 18:33:36 +00003089 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003090 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003091 }
3092
Roland Levillain3e3d7332015-04-28 11:00:54 +01003093 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003094 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003095 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003096 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003097 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003098 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003099
3100 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3101 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003102
3103 private:
3104 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3105};
3106
Calin Juravle175dc732015-08-25 15:42:32 +01003107class HInvokeUnresolved : public HInvoke {
3108 public:
3109 HInvokeUnresolved(ArenaAllocator* arena,
3110 uint32_t number_of_arguments,
3111 Primitive::Type return_type,
3112 uint32_t dex_pc,
3113 uint32_t dex_method_index,
3114 InvokeType invoke_type)
3115 : HInvoke(arena,
3116 number_of_arguments,
3117 0u /* number_of_other_inputs */,
3118 return_type,
3119 dex_pc,
3120 dex_method_index,
3121 invoke_type) {
3122 }
3123
3124 DECLARE_INSTRUCTION(InvokeUnresolved);
3125
3126 private:
3127 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3128};
3129
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003130class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003131 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003132 // Requirements of this method call regarding the class
3133 // initialization (clinit) check of its declaring class.
3134 enum class ClinitCheckRequirement {
3135 kNone, // Class already initialized.
3136 kExplicit, // Static call having explicit clinit check as last input.
3137 kImplicit, // Static call implicitly requiring a clinit check.
3138 };
3139
Vladimir Marko58155012015-08-19 12:49:41 +00003140 // Determines how to load the target ArtMethod*.
3141 enum class MethodLoadKind {
3142 // Use a String init ArtMethod* loaded from Thread entrypoints.
3143 kStringInit,
3144
3145 // Use the method's own ArtMethod* loaded by the register allocator.
3146 kRecursive,
3147
3148 // Use ArtMethod* at a known address, embed the direct address in the code.
3149 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3150 kDirectAddress,
3151
3152 // Use ArtMethod* at an address that will be known at link time, embed the direct
3153 // address in the code. If the image is relocatable, emit .patch_oat entry.
3154 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3155 // the image relocatable or not.
3156 kDirectAddressWithFixup,
3157
3158 // Load from resoved methods array in the dex cache using a PC-relative load.
3159 // Used when we need to use the dex cache, for example for invoke-static that
3160 // may cause class initialization (the entry may point to a resolution method),
3161 // and we know that we can access the dex cache arrays using a PC-relative load.
3162 kDexCachePcRelative,
3163
3164 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3165 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3166 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3167 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3168 kDexCacheViaMethod,
3169 };
3170
3171 // Determines the location of the code pointer.
3172 enum class CodePtrLocation {
3173 // Recursive call, use local PC-relative call instruction.
3174 kCallSelf,
3175
3176 // Use PC-relative call instruction patched at link time.
3177 // Used for calls within an oat file, boot->boot or app->app.
3178 kCallPCRelative,
3179
3180 // Call to a known target address, embed the direct address in code.
3181 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3182 kCallDirect,
3183
3184 // Call to a target address that will be known at link time, embed the direct
3185 // address in code. If the image is relocatable, emit .patch_oat entry.
3186 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3187 // the image relocatable or not.
3188 kCallDirectWithFixup,
3189
3190 // Use code pointer from the ArtMethod*.
3191 // Used when we don't know the target code. This is also the last-resort-kind used when
3192 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3193 kCallArtMethod,
3194 };
3195
3196 struct DispatchInfo {
3197 const MethodLoadKind method_load_kind;
3198 const CodePtrLocation code_ptr_location;
3199 // The method load data holds
3200 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3201 // Note that there are multiple string init methods, each having its own offset.
3202 // - the method address for kDirectAddress
3203 // - the dex cache arrays offset for kDexCachePcRel.
3204 const uint64_t method_load_data;
3205 const uint64_t direct_code_ptr;
3206 };
3207
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003208 HInvokeStaticOrDirect(ArenaAllocator* arena,
3209 uint32_t number_of_arguments,
3210 Primitive::Type return_type,
3211 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003212 uint32_t method_index,
3213 MethodReference target_method,
3214 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003215 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003216 InvokeType invoke_type,
3217 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003218 : HInvoke(arena,
3219 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003220 // There is one extra argument for the HCurrentMethod node, and
3221 // potentially one other if the clinit check is explicit, and one other
3222 // if the method is a string factory.
3223 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003224 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003225 return_type,
3226 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003227 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003228 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003229 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003230 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003231 target_method_(target_method),
3232 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003233
Calin Juravle641547a2015-04-21 22:08:51 +01003234 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3235 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003236 // We access the method via the dex cache so we can't do an implicit null check.
3237 // TODO: for intrinsics we can generate implicit null checks.
3238 return false;
3239 }
3240
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003241 bool CanBeNull() const OVERRIDE {
3242 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3243 }
3244
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003245 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003246 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3247 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3248 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003249 bool NeedsDexCache() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003250 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003251 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003252 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3253 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3254 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3255 MethodReference GetTargetMethod() const { return target_method_; }
3256
3257 int32_t GetStringInitOffset() const {
3258 DCHECK(IsStringInit());
3259 return dispatch_info_.method_load_data;
3260 }
3261
3262 uint64_t GetMethodAddress() const {
3263 DCHECK(HasMethodAddress());
3264 return dispatch_info_.method_load_data;
3265 }
3266
3267 uint32_t GetDexCacheArrayOffset() const {
3268 DCHECK(HasPcRelDexCache());
3269 return dispatch_info_.method_load_data;
3270 }
3271
3272 uint64_t GetDirectCodePtr() const {
3273 DCHECK(HasDirectCodePtr());
3274 return dispatch_info_.direct_code_ptr;
3275 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003276
Calin Juravle68ad6492015-08-18 17:08:12 +01003277 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3278
Roland Levillain4c0eb422015-04-24 16:43:49 +01003279 // Is this instruction a call to a static method?
3280 bool IsStatic() const {
3281 return GetInvokeType() == kStatic;
3282 }
3283
Roland Levillain3e3d7332015-04-28 11:00:54 +01003284 // Remove the art::HLoadClass instruction set as last input by
3285 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3286 // the initial art::HClinitCheck instruction (only relevant for
3287 // static calls with explicit clinit check).
3288 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003289 DCHECK(IsStaticWithExplicitClinitCheck());
3290 size_t last_input_index = InputCount() - 1;
3291 HInstruction* last_input = InputAt(last_input_index);
3292 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003293 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003294 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003295 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003296 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3297 DCHECK(IsStaticWithImplicitClinitCheck());
3298 }
3299
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003300 bool IsStringFactoryFor(HFakeString* str) const {
3301 if (!IsStringInit()) return false;
3302 // +1 for the current method.
3303 if (InputCount() == (number_of_arguments_ + 1)) return false;
3304 return InputAt(InputCount() - 1)->AsFakeString() == str;
3305 }
3306
3307 void RemoveFakeStringArgumentAsLastInput() {
3308 DCHECK(IsStringInit());
3309 size_t last_input_index = InputCount() - 1;
3310 HInstruction* last_input = InputAt(last_input_index);
3311 DCHECK(last_input != nullptr);
3312 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3313 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003314 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003315 }
3316
Roland Levillain4c0eb422015-04-24 16:43:49 +01003317 // Is this a call to a static method whose declaring class has an
3318 // explicit intialization check in the graph?
3319 bool IsStaticWithExplicitClinitCheck() const {
3320 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3321 }
3322
3323 // Is this a call to a static method whose declaring class has an
3324 // implicit intialization check requirement?
3325 bool IsStaticWithImplicitClinitCheck() const {
3326 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3327 }
3328
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003329 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003330
Roland Levillain4c0eb422015-04-24 16:43:49 +01003331 protected:
3332 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3333 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3334 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3335 HInstruction* input = input_record.GetInstruction();
3336 // `input` is the last input of a static invoke marked as having
3337 // an explicit clinit check. It must either be:
3338 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3339 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3340 DCHECK(input != nullptr);
3341 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3342 }
3343 return input_record;
3344 }
3345
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003346 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003347 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003348 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003349 // The target method may refer to different dex file or method index than the original
3350 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3351 // in derived class to increase visibility.
3352 MethodReference target_method_;
3353 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003354
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003355 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003356};
3357
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003358class HInvokeVirtual : public HInvoke {
3359 public:
3360 HInvokeVirtual(ArenaAllocator* arena,
3361 uint32_t number_of_arguments,
3362 Primitive::Type return_type,
3363 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003364 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003365 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003366 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003367 vtable_index_(vtable_index) {}
3368
Calin Juravle641547a2015-04-21 22:08:51 +01003369 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003370 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003371 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003372 }
3373
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003374 uint32_t GetVTableIndex() const { return vtable_index_; }
3375
3376 DECLARE_INSTRUCTION(InvokeVirtual);
3377
3378 private:
3379 const uint32_t vtable_index_;
3380
3381 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3382};
3383
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003384class HInvokeInterface : public HInvoke {
3385 public:
3386 HInvokeInterface(ArenaAllocator* arena,
3387 uint32_t number_of_arguments,
3388 Primitive::Type return_type,
3389 uint32_t dex_pc,
3390 uint32_t dex_method_index,
3391 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003392 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003393 imt_index_(imt_index) {}
3394
Calin Juravle641547a2015-04-21 22:08:51 +01003395 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003396 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003397 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003398 }
3399
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003400 uint32_t GetImtIndex() const { return imt_index_; }
3401 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3402
3403 DECLARE_INSTRUCTION(InvokeInterface);
3404
3405 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003406 const uint32_t imt_index_;
3407
3408 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3409};
3410
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003411class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003412 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003413 HNewInstance(HCurrentMethod* current_method,
3414 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003415 uint16_t type_index,
3416 const DexFile& dex_file,
3417 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003418 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003419 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003420 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003421 entrypoint_(entrypoint) {
3422 SetRawInputAt(0, current_method);
3423 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003424
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003425 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003426 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003427
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003428 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003429 bool NeedsEnvironment() const OVERRIDE { return true; }
3430 // It may throw when called on:
3431 // - interfaces
3432 // - abstract/innaccessible/unknown classes
3433 // TODO: optimize when possible.
3434 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003435
Calin Juravle10e244f2015-01-26 18:54:32 +00003436 bool CanBeNull() const OVERRIDE { return false; }
3437
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003438 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3439
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003440 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003441
3442 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003443 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003444 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003445 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003446
3447 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3448};
3449
Roland Levillain88cb1752014-10-20 16:36:47 +01003450class HNeg : public HUnaryOperation {
3451 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003452 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3453 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003454
Roland Levillain9867bc72015-08-05 10:21:34 +01003455 template <typename T> T Compute(T x) const { return -x; }
3456
3457 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003458 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003459 }
3460 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003461 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003462 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003463
Roland Levillain88cb1752014-10-20 16:36:47 +01003464 DECLARE_INSTRUCTION(Neg);
3465
3466 private:
3467 DISALLOW_COPY_AND_ASSIGN(HNeg);
3468};
3469
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003470class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003471 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003472 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003473 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003474 uint32_t dex_pc,
3475 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003476 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003477 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003478 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003479 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003480 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003481 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003482 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003483 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003484 }
3485
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003486 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003487 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003488
3489 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003490 bool NeedsEnvironment() const OVERRIDE { return true; }
3491
Mingyao Yang0c365e62015-03-31 15:09:29 -07003492 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3493 bool CanThrow() const OVERRIDE { return true; }
3494
Calin Juravle10e244f2015-01-26 18:54:32 +00003495 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003496
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003497 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3498
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003499 DECLARE_INSTRUCTION(NewArray);
3500
3501 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003502 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003503 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003504 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003505
3506 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3507};
3508
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003509class HAdd : public HBinaryOperation {
3510 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003511 HAdd(Primitive::Type result_type,
3512 HInstruction* left,
3513 HInstruction* right,
3514 uint32_t dex_pc = kNoDexPc)
3515 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003516
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003517 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003518
Roland Levillain9867bc72015-08-05 10:21:34 +01003519 template <typename T> T Compute(T x, T y) const { return x + y; }
3520
3521 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003522 return GetBlock()->GetGraph()->GetIntConstant(
3523 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003524 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003525 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003526 return GetBlock()->GetGraph()->GetLongConstant(
3527 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003528 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003529
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003530 DECLARE_INSTRUCTION(Add);
3531
3532 private:
3533 DISALLOW_COPY_AND_ASSIGN(HAdd);
3534};
3535
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003536class HSub : public HBinaryOperation {
3537 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003538 HSub(Primitive::Type result_type,
3539 HInstruction* left,
3540 HInstruction* right,
3541 uint32_t dex_pc = kNoDexPc)
3542 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003543
Roland Levillain9867bc72015-08-05 10:21:34 +01003544 template <typename T> T Compute(T x, T y) const { return x - y; }
3545
3546 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003547 return GetBlock()->GetGraph()->GetIntConstant(
3548 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003549 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003550 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003551 return GetBlock()->GetGraph()->GetLongConstant(
3552 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003553 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003554
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003555 DECLARE_INSTRUCTION(Sub);
3556
3557 private:
3558 DISALLOW_COPY_AND_ASSIGN(HSub);
3559};
3560
Calin Juravle34bacdf2014-10-07 20:23:36 +01003561class HMul : public HBinaryOperation {
3562 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003563 HMul(Primitive::Type result_type,
3564 HInstruction* left,
3565 HInstruction* right,
3566 uint32_t dex_pc = kNoDexPc)
3567 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003568
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003569 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003570
Roland Levillain9867bc72015-08-05 10:21:34 +01003571 template <typename T> T Compute(T x, T y) const { return x * y; }
3572
3573 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003574 return GetBlock()->GetGraph()->GetIntConstant(
3575 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003576 }
3577 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003578 return GetBlock()->GetGraph()->GetLongConstant(
3579 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003580 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003581
3582 DECLARE_INSTRUCTION(Mul);
3583
3584 private:
3585 DISALLOW_COPY_AND_ASSIGN(HMul);
3586};
3587
Calin Juravle7c4954d2014-10-28 16:57:40 +00003588class HDiv : public HBinaryOperation {
3589 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003590 HDiv(Primitive::Type result_type,
3591 HInstruction* left,
3592 HInstruction* right,
3593 uint32_t dex_pc)
3594 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003595
Roland Levillain9867bc72015-08-05 10:21:34 +01003596 template <typename T>
3597 T Compute(T x, T y) const {
3598 // Our graph structure ensures we never have 0 for `y` during
3599 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003600 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003601 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003602 return (y == -1) ? -x : x / y;
3603 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003604
Roland Levillain9867bc72015-08-05 10:21:34 +01003605 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003606 return GetBlock()->GetGraph()->GetIntConstant(
3607 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003608 }
3609 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003610 return GetBlock()->GetGraph()->GetLongConstant(
3611 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003612 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003613
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003614 static SideEffects SideEffectsForArchRuntimeCalls() {
3615 // The generated code can use a runtime call.
3616 return SideEffects::CanTriggerGC();
3617 }
3618
Calin Juravle7c4954d2014-10-28 16:57:40 +00003619 DECLARE_INSTRUCTION(Div);
3620
3621 private:
3622 DISALLOW_COPY_AND_ASSIGN(HDiv);
3623};
3624
Calin Juravlebacfec32014-11-14 15:54:36 +00003625class HRem : public HBinaryOperation {
3626 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003627 HRem(Primitive::Type result_type,
3628 HInstruction* left,
3629 HInstruction* right,
3630 uint32_t dex_pc)
3631 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003632
Roland Levillain9867bc72015-08-05 10:21:34 +01003633 template <typename T>
3634 T Compute(T x, T y) const {
3635 // Our graph structure ensures we never have 0 for `y` during
3636 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003637 DCHECK_NE(y, 0);
3638 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3639 return (y == -1) ? 0 : x % y;
3640 }
3641
Roland Levillain9867bc72015-08-05 10:21:34 +01003642 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003643 return GetBlock()->GetGraph()->GetIntConstant(
3644 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003645 }
3646 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003647 return GetBlock()->GetGraph()->GetLongConstant(
3648 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003649 }
3650
Calin Juravlebacfec32014-11-14 15:54:36 +00003651
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003652 static SideEffects SideEffectsForArchRuntimeCalls() {
3653 return SideEffects::CanTriggerGC();
3654 }
3655
Calin Juravlebacfec32014-11-14 15:54:36 +00003656 DECLARE_INSTRUCTION(Rem);
3657
3658 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003659 DISALLOW_COPY_AND_ASSIGN(HRem);
3660};
3661
Calin Juravled0d48522014-11-04 16:40:20 +00003662class HDivZeroCheck : public HExpression<1> {
3663 public:
3664 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003665 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003666 SetRawInputAt(0, value);
3667 }
3668
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003669 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3670
Calin Juravled0d48522014-11-04 16:40:20 +00003671 bool CanBeMoved() const OVERRIDE { return true; }
3672
3673 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3674 UNUSED(other);
3675 return true;
3676 }
3677
3678 bool NeedsEnvironment() const OVERRIDE { return true; }
3679 bool CanThrow() const OVERRIDE { return true; }
3680
Calin Juravled0d48522014-11-04 16:40:20 +00003681 DECLARE_INSTRUCTION(DivZeroCheck);
3682
3683 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003684 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3685};
3686
Calin Juravle9aec02f2014-11-18 23:06:35 +00003687class HShl : public HBinaryOperation {
3688 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003689 HShl(Primitive::Type result_type,
3690 HInstruction* left,
3691 HInstruction* right,
3692 uint32_t dex_pc = kNoDexPc)
3693 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003694
Roland Levillain9867bc72015-08-05 10:21:34 +01003695 template <typename T, typename U, typename V>
3696 T Compute(T x, U y, V max_shift_value) const {
3697 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3698 "V is not the unsigned integer type corresponding to T");
3699 return x << (y & max_shift_value);
3700 }
3701
3702 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3703 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003704 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003705 }
3706 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3707 // case is handled as `x << static_cast<int>(y)`.
3708 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3709 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003710 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003711 }
3712 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3713 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003714 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003715 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003716
3717 DECLARE_INSTRUCTION(Shl);
3718
3719 private:
3720 DISALLOW_COPY_AND_ASSIGN(HShl);
3721};
3722
3723class HShr : public HBinaryOperation {
3724 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003725 HShr(Primitive::Type result_type,
3726 HInstruction* left,
3727 HInstruction* right,
3728 uint32_t dex_pc = kNoDexPc)
3729 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003730
Roland Levillain9867bc72015-08-05 10:21:34 +01003731 template <typename T, typename U, typename V>
3732 T Compute(T x, U y, V max_shift_value) const {
3733 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3734 "V is not the unsigned integer type corresponding to T");
3735 return x >> (y & max_shift_value);
3736 }
3737
3738 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3739 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003740 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003741 }
3742 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3743 // case is handled as `x >> static_cast<int>(y)`.
3744 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3745 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003746 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003747 }
3748 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3749 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003750 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003751 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003752
3753 DECLARE_INSTRUCTION(Shr);
3754
3755 private:
3756 DISALLOW_COPY_AND_ASSIGN(HShr);
3757};
3758
3759class HUShr : public HBinaryOperation {
3760 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003761 HUShr(Primitive::Type result_type,
3762 HInstruction* left,
3763 HInstruction* right,
3764 uint32_t dex_pc = kNoDexPc)
3765 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003766
Roland Levillain9867bc72015-08-05 10:21:34 +01003767 template <typename T, typename U, typename V>
3768 T Compute(T x, U y, V max_shift_value) const {
3769 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3770 "V is not the unsigned integer type corresponding to T");
3771 V ux = static_cast<V>(x);
3772 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003773 }
3774
Roland Levillain9867bc72015-08-05 10:21:34 +01003775 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3776 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003777 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003778 }
3779 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3780 // case is handled as `x >>> static_cast<int>(y)`.
3781 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3782 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003783 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003784 }
3785 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3786 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003787 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003788 }
3789
3790 DECLARE_INSTRUCTION(UShr);
3791
3792 private:
3793 DISALLOW_COPY_AND_ASSIGN(HUShr);
3794};
3795
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003796class HAnd : public HBinaryOperation {
3797 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003798 HAnd(Primitive::Type result_type,
3799 HInstruction* left,
3800 HInstruction* right,
3801 uint32_t dex_pc = kNoDexPc)
3802 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003803
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003804 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003805
Roland Levillain9867bc72015-08-05 10:21:34 +01003806 template <typename T, typename U>
3807 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3808
3809 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003810 return GetBlock()->GetGraph()->GetIntConstant(
3811 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003812 }
3813 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003814 return GetBlock()->GetGraph()->GetLongConstant(
3815 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003816 }
3817 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003818 return GetBlock()->GetGraph()->GetLongConstant(
3819 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003820 }
3821 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003822 return GetBlock()->GetGraph()->GetLongConstant(
3823 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003824 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003825
3826 DECLARE_INSTRUCTION(And);
3827
3828 private:
3829 DISALLOW_COPY_AND_ASSIGN(HAnd);
3830};
3831
3832class HOr : public HBinaryOperation {
3833 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003834 HOr(Primitive::Type result_type,
3835 HInstruction* left,
3836 HInstruction* right,
3837 uint32_t dex_pc = kNoDexPc)
3838 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003839
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003840 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003841
Roland Levillain9867bc72015-08-05 10:21:34 +01003842 template <typename T, typename U>
3843 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3844
3845 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003846 return GetBlock()->GetGraph()->GetIntConstant(
3847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003848 }
3849 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003850 return GetBlock()->GetGraph()->GetLongConstant(
3851 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003852 }
3853 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003854 return GetBlock()->GetGraph()->GetLongConstant(
3855 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003856 }
3857 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003858 return GetBlock()->GetGraph()->GetLongConstant(
3859 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003860 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003861
3862 DECLARE_INSTRUCTION(Or);
3863
3864 private:
3865 DISALLOW_COPY_AND_ASSIGN(HOr);
3866};
3867
3868class HXor : public HBinaryOperation {
3869 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003870 HXor(Primitive::Type result_type,
3871 HInstruction* left,
3872 HInstruction* right,
3873 uint32_t dex_pc = kNoDexPc)
3874 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003875
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003876 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003877
Roland Levillain9867bc72015-08-05 10:21:34 +01003878 template <typename T, typename U>
3879 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3880
3881 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003882 return GetBlock()->GetGraph()->GetIntConstant(
3883 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003884 }
3885 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003886 return GetBlock()->GetGraph()->GetLongConstant(
3887 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003888 }
3889 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003890 return GetBlock()->GetGraph()->GetLongConstant(
3891 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003892 }
3893 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003894 return GetBlock()->GetGraph()->GetLongConstant(
3895 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003896 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003897
3898 DECLARE_INSTRUCTION(Xor);
3899
3900 private:
3901 DISALLOW_COPY_AND_ASSIGN(HXor);
3902};
3903
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003904// The value of a parameter in this method. Its location depends on
3905// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003906class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003907 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003908 HParameterValue(uint8_t index,
3909 Primitive::Type parameter_type,
3910 bool is_this = false)
3911 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003912 index_(index),
3913 is_this_(is_this),
3914 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003915
3916 uint8_t GetIndex() const { return index_; }
3917
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003918 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3919 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003920
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003921 bool IsThis() const { return is_this_; }
3922
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003923 DECLARE_INSTRUCTION(ParameterValue);
3924
3925 private:
3926 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003927 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003928 const uint8_t index_;
3929
Calin Juravle10e244f2015-01-26 18:54:32 +00003930 // Whether or not the parameter value corresponds to 'this' argument.
3931 const bool is_this_;
3932
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003933 bool can_be_null_;
3934
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003935 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3936};
3937
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003938class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003939 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003940 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3941 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003942
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003943 bool CanBeMoved() const OVERRIDE { return true; }
3944 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003945 UNUSED(other);
3946 return true;
3947 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003948
Roland Levillain9867bc72015-08-05 10:21:34 +01003949 template <typename T> T Compute(T x) const { return ~x; }
3950
3951 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003952 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003953 }
3954 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003955 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003956 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003957
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003958 DECLARE_INSTRUCTION(Not);
3959
3960 private:
3961 DISALLOW_COPY_AND_ASSIGN(HNot);
3962};
3963
David Brazdil66d126e2015-04-03 16:02:44 +01003964class HBooleanNot : public HUnaryOperation {
3965 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003966 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3967 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003968
3969 bool CanBeMoved() const OVERRIDE { return true; }
3970 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3971 UNUSED(other);
3972 return true;
3973 }
3974
Roland Levillain9867bc72015-08-05 10:21:34 +01003975 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003976 DCHECK(IsUint<1>(x));
3977 return !x;
3978 }
3979
Roland Levillain9867bc72015-08-05 10:21:34 +01003980 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003981 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003982 }
3983 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3984 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003985 UNREACHABLE();
3986 }
3987
3988 DECLARE_INSTRUCTION(BooleanNot);
3989
3990 private:
3991 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3992};
3993
Roland Levillaindff1f282014-11-05 14:15:05 +00003994class HTypeConversion : public HExpression<1> {
3995 public:
3996 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003997 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003998 : HExpression(result_type,
3999 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4000 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004001 SetRawInputAt(0, input);
4002 DCHECK_NE(input->GetType(), result_type);
4003 }
4004
4005 HInstruction* GetInput() const { return InputAt(0); }
4006 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4007 Primitive::Type GetResultType() const { return GetType(); }
4008
Roland Levillain624279f2014-12-04 11:54:28 +00004009 // Required by the x86 and ARM code generators when producing calls
4010 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004011
Roland Levillaindff1f282014-11-05 14:15:05 +00004012 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004013 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004014
Mark Mendelle82549b2015-05-06 10:55:34 -04004015 // Try to statically evaluate the conversion and return a HConstant
4016 // containing the result. If the input cannot be converted, return nullptr.
4017 HConstant* TryStaticEvaluation() const;
4018
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004019 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4020 Primitive::Type result_type) {
4021 // Some architectures may not require the 'GC' side effects, but at this point
4022 // in the compilation process we do not know what architecture we will
4023 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004024 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4025 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004026 return SideEffects::CanTriggerGC();
4027 }
4028 return SideEffects::None();
4029 }
4030
Roland Levillaindff1f282014-11-05 14:15:05 +00004031 DECLARE_INSTRUCTION(TypeConversion);
4032
4033 private:
4034 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4035};
4036
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004037static constexpr uint32_t kNoRegNumber = -1;
4038
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004039class HPhi : public HInstruction {
4040 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004041 HPhi(ArenaAllocator* arena,
4042 uint32_t reg_number,
4043 size_t number_of_inputs,
4044 Primitive::Type type,
4045 uint32_t dex_pc = kNoDexPc)
4046 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004047 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004048 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004049 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004050 is_live_(false),
4051 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004052 }
4053
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004054 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4055 static Primitive::Type ToPhiType(Primitive::Type type) {
4056 switch (type) {
4057 case Primitive::kPrimBoolean:
4058 case Primitive::kPrimByte:
4059 case Primitive::kPrimShort:
4060 case Primitive::kPrimChar:
4061 return Primitive::kPrimInt;
4062 default:
4063 return type;
4064 }
4065 }
4066
David Brazdilffee3d32015-07-06 11:48:53 +01004067 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4068
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004069 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004070
4071 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004072 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004073
Calin Juravle10e244f2015-01-26 18:54:32 +00004074 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004075 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004076
Calin Juravle10e244f2015-01-26 18:54:32 +00004077 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4078 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4079
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004080 uint32_t GetRegNumber() const { return reg_number_; }
4081
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004082 void SetDead() { is_live_ = false; }
4083 void SetLive() { is_live_ = true; }
4084 bool IsDead() const { return !is_live_; }
4085 bool IsLive() const { return is_live_; }
4086
David Brazdil77a48ae2015-09-15 12:34:04 +00004087 bool IsVRegEquivalentOf(HInstruction* other) const {
4088 return other != nullptr
4089 && other->IsPhi()
4090 && other->AsPhi()->GetBlock() == GetBlock()
4091 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4092 }
4093
Calin Juravlea4f88312015-04-16 12:57:19 +01004094 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4095 // An equivalent phi is a phi having the same dex register and type.
4096 // It assumes that phis with the same dex register are adjacent.
4097 HPhi* GetNextEquivalentPhiWithSameType() {
4098 HInstruction* next = GetNext();
4099 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4100 if (next->GetType() == GetType()) {
4101 return next->AsPhi();
4102 }
4103 next = next->GetNext();
4104 }
4105 return nullptr;
4106 }
4107
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004108 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004109
David Brazdil1abb4192015-02-17 18:33:36 +00004110 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004111 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004112 return inputs_[index];
4113 }
David Brazdil1abb4192015-02-17 18:33:36 +00004114
4115 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004116 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004117 }
4118
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004119 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004120 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004121 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004122 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004123 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004124 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004125
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004126 DISALLOW_COPY_AND_ASSIGN(HPhi);
4127};
4128
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004129class HNullCheck : public HExpression<1> {
4130 public:
4131 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004132 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004133 SetRawInputAt(0, value);
4134 }
4135
Calin Juravle10e244f2015-01-26 18:54:32 +00004136 bool CanBeMoved() const OVERRIDE { return true; }
4137 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004138 UNUSED(other);
4139 return true;
4140 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004141
Calin Juravle10e244f2015-01-26 18:54:32 +00004142 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004143
Calin Juravle10e244f2015-01-26 18:54:32 +00004144 bool CanThrow() const OVERRIDE { return true; }
4145
4146 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004147
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004148
4149 DECLARE_INSTRUCTION(NullCheck);
4150
4151 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004152 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4153};
4154
4155class FieldInfo : public ValueObject {
4156 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004157 FieldInfo(MemberOffset field_offset,
4158 Primitive::Type field_type,
4159 bool is_volatile,
4160 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004161 const DexFile& dex_file,
4162 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004163 : field_offset_(field_offset),
4164 field_type_(field_type),
4165 is_volatile_(is_volatile),
4166 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004167 dex_file_(dex_file),
4168 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004169
4170 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004171 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004172 uint32_t GetFieldIndex() const { return index_; }
4173 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004174 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004175 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004176
4177 private:
4178 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004179 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004180 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004181 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004182 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004183 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004184};
4185
4186class HInstanceFieldGet : public HExpression<1> {
4187 public:
4188 HInstanceFieldGet(HInstruction* value,
4189 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004190 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004191 bool is_volatile,
4192 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004193 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004194 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004195 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004196 : HExpression(
4197 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004198 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004199 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004200 SetRawInputAt(0, value);
4201 }
4202
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004203 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004204
4205 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4206 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4207 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004208 }
4209
Calin Juravle641547a2015-04-21 22:08:51 +01004210 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4211 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004212 }
4213
4214 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004215 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4216 }
4217
Calin Juravle52c48962014-12-16 17:02:57 +00004218 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004219 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004220 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004221 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004222
4223 DECLARE_INSTRUCTION(InstanceFieldGet);
4224
4225 private:
4226 const FieldInfo field_info_;
4227
4228 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4229};
4230
4231class HInstanceFieldSet : public HTemplateInstruction<2> {
4232 public:
4233 HInstanceFieldSet(HInstruction* object,
4234 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004235 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004236 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004237 bool is_volatile,
4238 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004239 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004240 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004241 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004242 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004243 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004244 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004245 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004246 SetRawInputAt(0, object);
4247 SetRawInputAt(1, value);
4248 }
4249
Calin Juravle641547a2015-04-21 22:08:51 +01004250 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4251 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004252 }
4253
Calin Juravle52c48962014-12-16 17:02:57 +00004254 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004255 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004256 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004257 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004258 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004259 bool GetValueCanBeNull() const { return value_can_be_null_; }
4260 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004261
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004262 DECLARE_INSTRUCTION(InstanceFieldSet);
4263
4264 private:
4265 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004266 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004267
4268 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4269};
4270
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004271class HArrayGet : public HExpression<2> {
4272 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004273 HArrayGet(HInstruction* array,
4274 HInstruction* index,
4275 Primitive::Type type,
Calin Juravle154746b2015-10-06 15:46:54 +01004276 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004277 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004278 SetRawInputAt(0, array);
4279 SetRawInputAt(1, index);
4280 }
4281
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004282 bool CanBeMoved() const OVERRIDE { return true; }
4283 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004284 UNUSED(other);
4285 return true;
4286 }
Calin Juravle641547a2015-04-21 22:08:51 +01004287 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4288 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004289 // TODO: We can be smarter here.
4290 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4291 // which generates the implicit null check. There are cases when these can be removed
4292 // to produce better code. If we ever add optimizations to do so we should allow an
4293 // implicit check here (as long as the address falls in the first page).
4294 return false;
4295 }
4296
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004297 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004298
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004299 HInstruction* GetArray() const { return InputAt(0); }
4300 HInstruction* GetIndex() const { return InputAt(1); }
4301
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004302 DECLARE_INSTRUCTION(ArrayGet);
4303
4304 private:
4305 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4306};
4307
4308class HArraySet : public HTemplateInstruction<3> {
4309 public:
4310 HArraySet(HInstruction* array,
4311 HInstruction* index,
4312 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004313 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004314 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004315 : HTemplateInstruction(
4316 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004317 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004318 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004319 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004320 value_can_be_null_(true),
4321 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004322 SetRawInputAt(0, array);
4323 SetRawInputAt(1, index);
4324 SetRawInputAt(2, value);
4325 }
4326
Calin Juravle77520bc2015-01-12 18:45:46 +00004327 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004328 // We currently always call a runtime method to catch array store
4329 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004330 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004331 }
4332
Mingyao Yang81014cb2015-06-02 03:16:27 -07004333 // Can throw ArrayStoreException.
4334 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4335
Calin Juravle641547a2015-04-21 22:08:51 +01004336 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4337 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004338 // TODO: Same as for ArrayGet.
4339 return false;
4340 }
4341
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004342 void ClearNeedsTypeCheck() {
4343 needs_type_check_ = false;
4344 }
4345
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004346 void ClearValueCanBeNull() {
4347 value_can_be_null_ = false;
4348 }
4349
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004350 void SetStaticTypeOfArrayIsObjectArray() {
4351 static_type_of_array_is_object_array_ = true;
4352 }
4353
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004354 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004355 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004356 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004357
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004358 HInstruction* GetArray() const { return InputAt(0); }
4359 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004360 HInstruction* GetValue() const { return InputAt(2); }
4361
4362 Primitive::Type GetComponentType() const {
4363 // The Dex format does not type floating point index operations. Since the
4364 // `expected_component_type_` is set during building and can therefore not
4365 // be correct, we also check what is the value type. If it is a floating
4366 // point type, we must use that type.
4367 Primitive::Type value_type = GetValue()->GetType();
4368 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4369 ? value_type
4370 : expected_component_type_;
4371 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004372
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004373 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4374 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4375 }
4376
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004377 DECLARE_INSTRUCTION(ArraySet);
4378
4379 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004380 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004381 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004382 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004383 // Cached information for the reference_type_info_ so that codegen
4384 // does not need to inspect the static type.
4385 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004386
4387 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4388};
4389
4390class HArrayLength : public HExpression<1> {
4391 public:
Calin Juravle154746b2015-10-06 15:46:54 +01004392 explicit HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004393 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004394 // Note that arrays do not change length, so the instruction does not
4395 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004396 SetRawInputAt(0, array);
4397 }
4398
Calin Juravle77520bc2015-01-12 18:45:46 +00004399 bool CanBeMoved() const OVERRIDE { return true; }
4400 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004401 UNUSED(other);
4402 return true;
4403 }
Calin Juravle641547a2015-04-21 22:08:51 +01004404 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4405 return obj == InputAt(0);
4406 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004407
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004408 DECLARE_INSTRUCTION(ArrayLength);
4409
4410 private:
4411 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4412};
4413
4414class HBoundsCheck : public HExpression<2> {
4415 public:
4416 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004417 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004418 DCHECK(index->GetType() == Primitive::kPrimInt);
4419 SetRawInputAt(0, index);
4420 SetRawInputAt(1, length);
4421 }
4422
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004423 bool CanBeMoved() const OVERRIDE { return true; }
4424 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004425 UNUSED(other);
4426 return true;
4427 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004428
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004429 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004430
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004431 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004432
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004433
4434 DECLARE_INSTRUCTION(BoundsCheck);
4435
4436 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004437 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4438};
4439
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004440/**
4441 * Some DEX instructions are folded into multiple HInstructions that need
4442 * to stay live until the last HInstruction. This class
4443 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004444 * HInstruction stays live. `index` represents the stack location index of the
4445 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004446 */
4447class HTemporary : public HTemplateInstruction<0> {
4448 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004449 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4450 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004451
4452 size_t GetIndex() const { return index_; }
4453
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004454 Primitive::Type GetType() const OVERRIDE {
4455 // The previous instruction is the one that will be stored in the temporary location.
4456 DCHECK(GetPrevious() != nullptr);
4457 return GetPrevious()->GetType();
4458 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004459
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004460 DECLARE_INSTRUCTION(Temporary);
4461
4462 private:
4463 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004464 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4465};
4466
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004467class HSuspendCheck : public HTemplateInstruction<0> {
4468 public:
4469 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004470 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004471
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004472 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004473 return true;
4474 }
4475
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004476 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4477 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004478
4479 DECLARE_INSTRUCTION(SuspendCheck);
4480
4481 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004482 // Only used for code generation, in order to share the same slow path between back edges
4483 // of a same loop.
4484 SlowPathCode* slow_path_;
4485
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004486 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4487};
4488
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004489/**
4490 * Instruction to load a Class object.
4491 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004492class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004493 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004494 HLoadClass(HCurrentMethod* current_method,
4495 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004496 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004497 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004498 uint32_t dex_pc,
4499 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004500 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004501 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004502 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004503 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004504 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004505 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004506 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004507 SetRawInputAt(0, current_method);
4508 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004509
4510 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004511
4512 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004513 // Note that we don't need to test for generate_clinit_check_.
4514 // Whether or not we need to generate the clinit check is processed in
4515 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004516 return other->AsLoadClass()->type_index_ == type_index_ &&
4517 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004518 }
4519
4520 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4521
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004522 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004523 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004524 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004525
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004526 bool NeedsEnvironment() const OVERRIDE {
4527 // Will call runtime and load the class if the class is not loaded yet.
4528 // TODO: finer grain decision.
Calin Juravle98893e12015-10-02 21:05:03 +01004529 return !is_referrers_class_ || needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004530 }
4531
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004532 bool MustGenerateClinitCheck() const {
4533 return generate_clinit_check_;
4534 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004535 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4536 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004537 }
4538
4539 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004540 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4541 }
4542
4543 bool NeedsAccessCheck() const {
4544 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004545 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004546
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004547 bool CanThrow() const OVERRIDE {
4548 // May call runtime and and therefore can throw.
4549 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004550 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004551 }
4552
Calin Juravleacf735c2015-02-12 15:25:22 +00004553 ReferenceTypeInfo GetLoadedClassRTI() {
4554 return loaded_class_rti_;
4555 }
4556
4557 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4558 // Make sure we only set exact types (the loaded class should never be merged).
4559 DCHECK(rti.IsExact());
4560 loaded_class_rti_ = rti;
4561 }
4562
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004563 const DexFile& GetDexFile() { return dex_file_; }
4564
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004565 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4566
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004567 static SideEffects SideEffectsForArchRuntimeCalls() {
4568 return SideEffects::CanTriggerGC();
4569 }
4570
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004571 DECLARE_INSTRUCTION(LoadClass);
4572
4573 private:
4574 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004575 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004576 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004577 // Whether this instruction must generate the initialization check.
4578 // Used for code generation.
4579 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004580 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004581
Calin Juravleacf735c2015-02-12 15:25:22 +00004582 ReferenceTypeInfo loaded_class_rti_;
4583
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004584 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4585};
4586
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004587class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004588 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004589 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004590 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4591 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004592 SetRawInputAt(0, current_method);
4593 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004594
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004595 bool CanBeMoved() const OVERRIDE { return true; }
4596
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004597 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4598 return other->AsLoadString()->string_index_ == string_index_;
4599 }
4600
4601 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4602
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004603 uint32_t GetStringIndex() const { return string_index_; }
4604
4605 // TODO: Can we deopt or debug when we resolve a string?
4606 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004607 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004608 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004609
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004610 static SideEffects SideEffectsForArchRuntimeCalls() {
4611 return SideEffects::CanTriggerGC();
4612 }
4613
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004614 DECLARE_INSTRUCTION(LoadString);
4615
4616 private:
4617 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004618
4619 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4620};
4621
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004622/**
4623 * Performs an initialization check on its Class object input.
4624 */
4625class HClinitCheck : public HExpression<1> {
4626 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004627 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004628 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004629 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004630 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4631 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004632 SetRawInputAt(0, constant);
4633 }
4634
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004635 bool CanBeMoved() const OVERRIDE { return true; }
4636 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4637 UNUSED(other);
4638 return true;
4639 }
4640
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004641 bool NeedsEnvironment() const OVERRIDE {
4642 // May call runtime to initialize the class.
4643 return true;
4644 }
4645
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004646
4647 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4648
4649 DECLARE_INSTRUCTION(ClinitCheck);
4650
4651 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004652 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4653};
4654
4655class HStaticFieldGet : public HExpression<1> {
4656 public:
4657 HStaticFieldGet(HInstruction* cls,
4658 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004659 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004660 bool is_volatile,
4661 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004662 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004663 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004664 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004665 : HExpression(
4666 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004667 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004668 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004669 SetRawInputAt(0, cls);
4670 }
4671
Calin Juravle52c48962014-12-16 17:02:57 +00004672
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004673 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004674
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004675 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004676 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4677 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004678 }
4679
4680 size_t ComputeHashCode() const OVERRIDE {
4681 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4682 }
4683
Calin Juravle52c48962014-12-16 17:02:57 +00004684 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004685 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4686 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004687 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004688
4689 DECLARE_INSTRUCTION(StaticFieldGet);
4690
4691 private:
4692 const FieldInfo field_info_;
4693
4694 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4695};
4696
4697class HStaticFieldSet : public HTemplateInstruction<2> {
4698 public:
4699 HStaticFieldSet(HInstruction* cls,
4700 HInstruction* value,
4701 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004702 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004703 bool is_volatile,
4704 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004705 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004706 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004707 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004708 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004709 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004710 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004711 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004712 SetRawInputAt(0, cls);
4713 SetRawInputAt(1, value);
4714 }
4715
Calin Juravle52c48962014-12-16 17:02:57 +00004716 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004717 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4718 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004719 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004720
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004721 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004722 bool GetValueCanBeNull() const { return value_can_be_null_; }
4723 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004724
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004725 DECLARE_INSTRUCTION(StaticFieldSet);
4726
4727 private:
4728 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004729 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004730
4731 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4732};
4733
Calin Juravlee460d1d2015-09-29 04:52:17 +01004734class HUnresolvedInstanceFieldGet : public HExpression<1> {
4735 public:
4736 HUnresolvedInstanceFieldGet(HInstruction* obj,
4737 Primitive::Type field_type,
4738 uint32_t field_index,
4739 uint32_t dex_pc)
4740 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4741 field_index_(field_index) {
4742 SetRawInputAt(0, obj);
4743 }
4744
4745 bool NeedsEnvironment() const OVERRIDE { return true; }
4746 bool CanThrow() const OVERRIDE { return true; }
4747
4748 Primitive::Type GetFieldType() const { return GetType(); }
4749 uint32_t GetFieldIndex() const { return field_index_; }
4750
4751 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4752
4753 private:
4754 const uint32_t field_index_;
4755
4756 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4757};
4758
4759class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4760 public:
4761 HUnresolvedInstanceFieldSet(HInstruction* obj,
4762 HInstruction* value,
4763 Primitive::Type field_type,
4764 uint32_t field_index,
4765 uint32_t dex_pc)
4766 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4767 field_type_(field_type),
4768 field_index_(field_index) {
4769 DCHECK_EQ(field_type, value->GetType());
4770 SetRawInputAt(0, obj);
4771 SetRawInputAt(1, value);
4772 }
4773
4774 bool NeedsEnvironment() const OVERRIDE { return true; }
4775 bool CanThrow() const OVERRIDE { return true; }
4776
4777 Primitive::Type GetFieldType() const { return field_type_; }
4778 uint32_t GetFieldIndex() const { return field_index_; }
4779
4780 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
4781
4782 private:
4783 const Primitive::Type field_type_;
4784 const uint32_t field_index_;
4785
4786 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
4787};
4788
4789class HUnresolvedStaticFieldGet : public HExpression<0> {
4790 public:
4791 HUnresolvedStaticFieldGet(Primitive::Type field_type,
4792 uint32_t field_index,
4793 uint32_t dex_pc)
4794 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4795 field_index_(field_index) {
4796 }
4797
4798 bool NeedsEnvironment() const OVERRIDE { return true; }
4799 bool CanThrow() const OVERRIDE { return true; }
4800
4801 Primitive::Type GetFieldType() const { return GetType(); }
4802 uint32_t GetFieldIndex() const { return field_index_; }
4803
4804 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
4805
4806 private:
4807 const uint32_t field_index_;
4808
4809 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
4810};
4811
4812class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
4813 public:
4814 HUnresolvedStaticFieldSet(HInstruction* value,
4815 Primitive::Type field_type,
4816 uint32_t field_index,
4817 uint32_t dex_pc)
4818 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4819 field_type_(field_type),
4820 field_index_(field_index) {
4821 DCHECK_EQ(field_type, value->GetType());
4822 SetRawInputAt(0, value);
4823 }
4824
4825 bool NeedsEnvironment() const OVERRIDE { return true; }
4826 bool CanThrow() const OVERRIDE { return true; }
4827
4828 Primitive::Type GetFieldType() const { return field_type_; }
4829 uint32_t GetFieldIndex() const { return field_index_; }
4830
4831 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
4832
4833 private:
4834 const Primitive::Type field_type_;
4835 const uint32_t field_index_;
4836
4837 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
4838};
4839
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004840// Implement the move-exception DEX instruction.
4841class HLoadException : public HExpression<0> {
4842 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004843 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4844 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004845
David Brazdilbbd733e2015-08-18 17:48:17 +01004846 bool CanBeNull() const OVERRIDE { return false; }
4847
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004848 DECLARE_INSTRUCTION(LoadException);
4849
4850 private:
4851 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4852};
4853
David Brazdilcb1c0552015-08-04 16:22:25 +01004854// Implicit part of move-exception which clears thread-local exception storage.
4855// Must not be removed because the runtime expects the TLS to get cleared.
4856class HClearException : public HTemplateInstruction<0> {
4857 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004858 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4859 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004860
4861 DECLARE_INSTRUCTION(ClearException);
4862
4863 private:
4864 DISALLOW_COPY_AND_ASSIGN(HClearException);
4865};
4866
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004867class HThrow : public HTemplateInstruction<1> {
4868 public:
4869 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004870 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004871 SetRawInputAt(0, exception);
4872 }
4873
4874 bool IsControlFlow() const OVERRIDE { return true; }
4875
4876 bool NeedsEnvironment() const OVERRIDE { return true; }
4877
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004878 bool CanThrow() const OVERRIDE { return true; }
4879
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004880
4881 DECLARE_INSTRUCTION(Throw);
4882
4883 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004884 DISALLOW_COPY_AND_ASSIGN(HThrow);
4885};
4886
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004887/**
4888 * Implementation strategies for the code generator of a HInstanceOf
4889 * or `HCheckCast`.
4890 */
4891enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01004892 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004893 kExactCheck, // Can do a single class compare.
4894 kClassHierarchyCheck, // Can just walk the super class chain.
4895 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
4896 kInterfaceCheck, // No optimization yet when checking against an interface.
4897 kArrayObjectCheck, // Can just check if the array is not primitive.
4898 kArrayCheck // No optimization yet when checking against a generic array.
4899};
4900
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004901class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004902 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004903 HInstanceOf(HInstruction* object,
4904 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004905 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004906 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004907 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004908 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004909 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004910 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004911 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004912 SetRawInputAt(0, object);
4913 SetRawInputAt(1, constant);
4914 }
4915
4916 bool CanBeMoved() const OVERRIDE { return true; }
4917
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004918 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004919 return true;
4920 }
4921
4922 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004923 return false;
4924 }
4925
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004926 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
4927
4928 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004929
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004930 // Used only in code generation.
4931 bool MustDoNullCheck() const { return must_do_null_check_; }
4932 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4933
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004934 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
4935 return (check_kind == TypeCheckKind::kExactCheck)
4936 ? SideEffects::None()
4937 // Mips currently does runtime calls for any other checks.
4938 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004939 }
4940
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004941 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004942
4943 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004944 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004945 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004946
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004947 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4948};
4949
Calin Juravleb1498f62015-02-16 13:13:29 +00004950class HBoundType : public HExpression<1> {
4951 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004952 // Constructs an HBoundType with the given upper_bound.
4953 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004954 HBoundType(HInstruction* input,
4955 ReferenceTypeInfo upper_bound,
4956 bool upper_can_be_null,
4957 uint32_t dex_pc = kNoDexPc)
4958 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004959 upper_bound_(upper_bound),
4960 upper_can_be_null_(upper_can_be_null),
4961 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004962 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004963 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004964 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004965 }
4966
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004967 // GetUpper* should only be used in reference type propagation.
4968 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4969 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004970
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004971 void SetCanBeNull(bool can_be_null) {
4972 DCHECK(upper_can_be_null_ || !can_be_null);
4973 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004974 }
4975
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004976 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4977
Calin Juravleb1498f62015-02-16 13:13:29 +00004978 DECLARE_INSTRUCTION(BoundType);
4979
4980 private:
4981 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004982 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4983 // It is used to bound the type in cases like:
4984 // if (x instanceof ClassX) {
4985 // // uper_bound_ will be ClassX
4986 // }
4987 const ReferenceTypeInfo upper_bound_;
4988 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4989 // is false then can_be_null_ cannot be true).
4990 const bool upper_can_be_null_;
4991 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004992
4993 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4994};
4995
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004996class HCheckCast : public HTemplateInstruction<2> {
4997 public:
4998 HCheckCast(HInstruction* object,
4999 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005000 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005001 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005002 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005003 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005004 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005005 SetRawInputAt(0, object);
5006 SetRawInputAt(1, constant);
5007 }
5008
5009 bool CanBeMoved() const OVERRIDE { return true; }
5010
5011 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5012 return true;
5013 }
5014
5015 bool NeedsEnvironment() const OVERRIDE {
5016 // Instruction may throw a CheckCastError.
5017 return true;
5018 }
5019
5020 bool CanThrow() const OVERRIDE { return true; }
5021
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005022 bool MustDoNullCheck() const { return must_do_null_check_; }
5023 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005024 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005025
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005026 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005027
5028 DECLARE_INSTRUCTION(CheckCast);
5029
5030 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005031 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005032 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005033
5034 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005035};
5036
Calin Juravle27df7582015-04-17 19:12:31 +01005037class HMemoryBarrier : public HTemplateInstruction<0> {
5038 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005039 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005040 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005041 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005042 barrier_kind_(barrier_kind) {}
5043
5044 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5045
5046 DECLARE_INSTRUCTION(MemoryBarrier);
5047
5048 private:
5049 const MemBarrierKind barrier_kind_;
5050
5051 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5052};
5053
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005054class HMonitorOperation : public HTemplateInstruction<1> {
5055 public:
5056 enum OperationKind {
5057 kEnter,
5058 kExit,
5059 };
5060
5061 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005062 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005063 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5064 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005065 SetRawInputAt(0, object);
5066 }
5067
5068 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005069 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5070
5071 bool CanThrow() const OVERRIDE {
5072 // Verifier guarantees that monitor-exit cannot throw.
5073 // This is important because it allows the HGraphBuilder to remove
5074 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5075 return IsEnter();
5076 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005077
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005078
5079 bool IsEnter() const { return kind_ == kEnter; }
5080
5081 DECLARE_INSTRUCTION(MonitorOperation);
5082
Calin Juravle52c48962014-12-16 17:02:57 +00005083 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005084 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005085
5086 private:
5087 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5088};
5089
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005090/**
5091 * A HInstruction used as a marker for the replacement of new + <init>
5092 * of a String to a call to a StringFactory. Only baseline will see
5093 * the node at code generation, where it will be be treated as null.
5094 * When compiling non-baseline, `HFakeString` instructions are being removed
5095 * in the instruction simplifier.
5096 */
5097class HFakeString : public HTemplateInstruction<0> {
5098 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005099 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5100 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005101
5102 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5103
5104 DECLARE_INSTRUCTION(FakeString);
5105
5106 private:
5107 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5108};
5109
Vladimir Markof9f64412015-09-02 14:05:49 +01005110class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005111 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005112 MoveOperands(Location source,
5113 Location destination,
5114 Primitive::Type type,
5115 HInstruction* instruction)
5116 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005117
5118 Location GetSource() const { return source_; }
5119 Location GetDestination() const { return destination_; }
5120
5121 void SetSource(Location value) { source_ = value; }
5122 void SetDestination(Location value) { destination_ = value; }
5123
5124 // The parallel move resolver marks moves as "in-progress" by clearing the
5125 // destination (but not the source).
5126 Location MarkPending() {
5127 DCHECK(!IsPending());
5128 Location dest = destination_;
5129 destination_ = Location::NoLocation();
5130 return dest;
5131 }
5132
5133 void ClearPending(Location dest) {
5134 DCHECK(IsPending());
5135 destination_ = dest;
5136 }
5137
5138 bool IsPending() const {
5139 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5140 return destination_.IsInvalid() && !source_.IsInvalid();
5141 }
5142
5143 // True if this blocks a move from the given location.
5144 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005145 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005146 }
5147
5148 // A move is redundant if it's been eliminated, if its source and
5149 // destination are the same, or if its destination is unneeded.
5150 bool IsRedundant() const {
5151 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5152 }
5153
5154 // We clear both operands to indicate move that's been eliminated.
5155 void Eliminate() {
5156 source_ = destination_ = Location::NoLocation();
5157 }
5158
5159 bool IsEliminated() const {
5160 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5161 return source_.IsInvalid();
5162 }
5163
Alexey Frunze4dda3372015-06-01 18:31:49 -07005164 Primitive::Type GetType() const { return type_; }
5165
Nicolas Geoffray90218252015-04-15 11:56:51 +01005166 bool Is64BitMove() const {
5167 return Primitive::Is64BitType(type_);
5168 }
5169
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005170 HInstruction* GetInstruction() const { return instruction_; }
5171
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005172 private:
5173 Location source_;
5174 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005175 // The type this move is for.
5176 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005177 // The instruction this move is assocatied with. Null when this move is
5178 // for moving an input in the expected locations of user (including a phi user).
5179 // This is only used in debug mode, to ensure we do not connect interval siblings
5180 // in the same parallel move.
5181 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005182};
5183
5184static constexpr size_t kDefaultNumberOfMoves = 4;
5185
5186class HParallelMove : public HTemplateInstruction<0> {
5187 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005188 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005189 : HTemplateInstruction(SideEffects::None(), dex_pc),
5190 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5191 moves_.reserve(kDefaultNumberOfMoves);
5192 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005193
Nicolas Geoffray90218252015-04-15 11:56:51 +01005194 void AddMove(Location source,
5195 Location destination,
5196 Primitive::Type type,
5197 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005198 DCHECK(source.IsValid());
5199 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005200 if (kIsDebugBuild) {
5201 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005202 for (const MoveOperands& move : moves_) {
5203 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005204 // Special case the situation where the move is for the spill slot
5205 // of the instruction.
5206 if ((GetPrevious() == instruction)
5207 || ((GetPrevious() == nullptr)
5208 && instruction->IsPhi()
5209 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005210 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005211 << "Doing parallel moves for the same instruction.";
5212 } else {
5213 DCHECK(false) << "Doing parallel moves for the same instruction.";
5214 }
5215 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005216 }
5217 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005218 for (const MoveOperands& move : moves_) {
5219 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005220 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005221 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005222 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005223 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005224 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005225 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005226 }
5227
Vladimir Marko225b6462015-09-28 12:17:40 +01005228 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005229 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005230 }
5231
Vladimir Marko225b6462015-09-28 12:17:40 +01005232 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005233
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005234 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005235
5236 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005237 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005238
5239 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5240};
5241
Mark Mendell0616ae02015-04-17 12:49:27 -04005242} // namespace art
5243
5244#ifdef ART_ENABLE_CODEGEN_x86
5245#include "nodes_x86.h"
5246#endif
5247
5248namespace art {
5249
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005250class HGraphVisitor : public ValueObject {
5251 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005252 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5253 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005254
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005255 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005256 virtual void VisitBasicBlock(HBasicBlock* block);
5257
Roland Levillain633021e2014-10-01 14:12:25 +01005258 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005259 void VisitInsertionOrder();
5260
Roland Levillain633021e2014-10-01 14:12:25 +01005261 // Visit the graph following dominator tree reverse post-order.
5262 void VisitReversePostOrder();
5263
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005264 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005265
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005266 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005267#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005268 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5269
5270 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5271
5272#undef DECLARE_VISIT_INSTRUCTION
5273
5274 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005275 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005276
5277 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5278};
5279
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005280class HGraphDelegateVisitor : public HGraphVisitor {
5281 public:
5282 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5283 virtual ~HGraphDelegateVisitor() {}
5284
5285 // Visit functions that delegate to to super class.
5286#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005287 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005288
5289 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5290
5291#undef DECLARE_VISIT_INSTRUCTION
5292
5293 private:
5294 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5295};
5296
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005297class HInsertionOrderIterator : public ValueObject {
5298 public:
5299 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5300
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005301 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005302 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005303 void Advance() { ++index_; }
5304
5305 private:
5306 const HGraph& graph_;
5307 size_t index_;
5308
5309 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5310};
5311
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005312class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005313 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005314 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5315 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005316 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005317 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005318
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005319 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5320 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005321 void Advance() { ++index_; }
5322
5323 private:
5324 const HGraph& graph_;
5325 size_t index_;
5326
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005327 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005328};
5329
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005330class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005331 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005332 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005333 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005334 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005335 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005336 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005337
5338 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005339 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005340 void Advance() { --index_; }
5341
5342 private:
5343 const HGraph& graph_;
5344 size_t index_;
5345
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005346 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005347};
5348
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005349class HLinearPostOrderIterator : public ValueObject {
5350 public:
5351 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005352 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005353
5354 bool Done() const { return index_ == 0; }
5355
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005356 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005357
5358 void Advance() {
5359 --index_;
5360 DCHECK_GE(index_, 0U);
5361 }
5362
5363 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005364 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005365 size_t index_;
5366
5367 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5368};
5369
5370class HLinearOrderIterator : public ValueObject {
5371 public:
5372 explicit HLinearOrderIterator(const HGraph& graph)
5373 : order_(graph.GetLinearOrder()), index_(0) {}
5374
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005375 bool Done() const { return index_ == order_.size(); }
5376 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005377 void Advance() { ++index_; }
5378
5379 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005380 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005381 size_t index_;
5382
5383 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5384};
5385
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005386// Iterator over the blocks that art part of the loop. Includes blocks part
5387// of an inner loop. The order in which the blocks are iterated is on their
5388// block id.
5389class HBlocksInLoopIterator : public ValueObject {
5390 public:
5391 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5392 : blocks_in_loop_(info.GetBlocks()),
5393 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5394 index_(0) {
5395 if (!blocks_in_loop_.IsBitSet(index_)) {
5396 Advance();
5397 }
5398 }
5399
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005400 bool Done() const { return index_ == blocks_.size(); }
5401 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005402 void Advance() {
5403 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005404 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005405 if (blocks_in_loop_.IsBitSet(index_)) {
5406 break;
5407 }
5408 }
5409 }
5410
5411 private:
5412 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005413 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005414 size_t index_;
5415
5416 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5417};
5418
Mingyao Yang3584bce2015-05-19 16:01:59 -07005419// Iterator over the blocks that art part of the loop. Includes blocks part
5420// of an inner loop. The order in which the blocks are iterated is reverse
5421// post order.
5422class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5423 public:
5424 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5425 : blocks_in_loop_(info.GetBlocks()),
5426 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5427 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005428 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005429 Advance();
5430 }
5431 }
5432
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005433 bool Done() const { return index_ == blocks_.size(); }
5434 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005435 void Advance() {
5436 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005437 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5438 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005439 break;
5440 }
5441 }
5442 }
5443
5444 private:
5445 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005446 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005447 size_t index_;
5448
5449 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5450};
5451
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005452inline int64_t Int64FromConstant(HConstant* constant) {
5453 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5454 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5455 : constant->AsLongConstant()->GetValue();
5456}
5457
Vladimir Marko58155012015-08-19 12:49:41 +00005458inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5459 // For the purposes of the compiler, the dex files must actually be the same object
5460 // if we want to safely treat them as the same. This is especially important for JIT
5461 // as custom class loaders can open the same underlying file (or memory) multiple
5462 // times and provide different class resolution but no two class loaders should ever
5463 // use the same DexFile object - doing so is an unsupported hack that can lead to
5464 // all sorts of weird failures.
5465 return &lhs == &rhs;
5466}
5467
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005468} // namespace art
5469
5470#endif // ART_COMPILER_OPTIMIZING_NODES_H_