blob: 7ac39d1a8e0855cd785596df7063bb42ae8e04c9 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
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);
Mingyao Yang8df69d42015-10-22 15:40:58 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070085 // All types.
86 kCondEQ, // ==
87 kCondNE, // !=
88 // Signed integers and floating-point numbers.
89 kCondLT, // <
90 kCondLE, // <=
91 kCondGT, // >
92 kCondGE, // >=
93 // Unsigned integers.
94 kCondB, // <
95 kCondBE, // <=
96 kCondA, // >
97 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070098};
99
Vladimir Markof9f64412015-09-02 14:05:49 +0100100class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100101 public:
102 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
103
104 void AddInstruction(HInstruction* instruction);
105 void RemoveInstruction(HInstruction* instruction);
106
David Brazdilc3d743f2015-04-22 13:40:50 +0100107 // Insert `instruction` before/after an existing instruction `cursor`.
108 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
109 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
110
Roland Levillain6b469232014-09-25 10:10:38 +0100111 // Return true if this list contains `instruction`.
112 bool Contains(HInstruction* instruction) const;
113
Roland Levillainccc07a92014-09-16 14:48:16 +0100114 // Return true if `instruction1` is found before `instruction2` in
115 // this instruction list and false otherwise. Abort if none
116 // of these instructions is found.
117 bool FoundBefore(const HInstruction* instruction1,
118 const HInstruction* instruction2) const;
119
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000120 bool IsEmpty() const { return first_instruction_ == nullptr; }
121 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
122
123 // Update the block of all instructions to be `block`.
124 void SetBlockOfInstructions(HBasicBlock* block) const;
125
126 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
127 void Add(const HInstructionList& instruction_list);
128
David Brazdil2d7352b2015-04-20 14:52:42 +0100129 // Return the number of instructions in the list. This is an expensive operation.
130 size_t CountSize() const;
131
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100132 private:
133 HInstruction* first_instruction_;
134 HInstruction* last_instruction_;
135
136 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000137 friend class HGraph;
138 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100140 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100141
142 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
143};
144
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000145// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100146class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100148 HGraph(ArenaAllocator* arena,
149 const DexFile& dex_file,
150 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100151 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700152 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100153 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100154 bool debuggable = false,
155 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000156 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100157 blocks_(arena->Adapter(kArenaAllocBlockList)),
158 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
159 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700160 entry_block_(nullptr),
161 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100162 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100163 number_of_vregs_(0),
164 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000165 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400166 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000167 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000168 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000169 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100170 dex_file_(dex_file),
171 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100172 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100173 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100174 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700175 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000176 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100177 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
178 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
179 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
180 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
181 cached_current_method_(nullptr) {
182 blocks_.reserve(kDefaultNumberOfBlocks);
183 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000184
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000185 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100186 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
187
David Brazdil69ba7b72015-06-23 18:27:30 +0100188 bool IsInSsaForm() const { return in_ssa_form_; }
189
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000190 HBasicBlock* GetEntryBlock() const { return entry_block_; }
191 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100192 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000193
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000194 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
195 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000196
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000197 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 // Try building the SSA form of this graph, with dominance computation and loop
200 // recognition. Returns whether it was successful in doing all these steps.
201 bool TryBuildingSsa() {
202 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000203 // The SSA builder requires loops to all be natural. Specifically, the dead phi
204 // elimination phase checks the consistency of the graph when doing a post-order
205 // visit for eliminating dead phis: a dead phi can only have loop header phi
206 // users remaining when being visited.
207 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100208 // Precompute per-block try membership before entering the SSA builder,
209 // which needs the information to build catch block phis from values of
210 // locals at throwing instructions inside try blocks.
211 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100213 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000214 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 }
216
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100217 void ComputeDominanceInformation();
218 void ClearDominanceInformation();
219
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000220 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100222 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100223 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000224
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000225 // Analyze all natural loops in this graph. Returns false if one
226 // loop is not natural, that is the header does not dominate the
227 // back edge.
228 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100229
David Brazdilffee3d32015-07-06 11:48:53 +0100230 // Iterate over blocks to compute try block membership. Needs reverse post
231 // order and loop information.
232 void ComputeTryBlockInformation();
233
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000234 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000235 // Returns the instruction used to replace the invoke expression or null if the
236 // invoke is for a void method.
237 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238
Mingyao Yang3584bce2015-05-19 16:01:59 -0700239 // Need to add a couple of blocks to test if the loop body is entered and
240 // put deoptimization instructions, etc.
241 void TransformLoopHeaderForBCE(HBasicBlock* header);
242
David Brazdil2d7352b2015-04-20 14:52:42 +0100243 // Removes `block` from the graph.
244 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000245
David Brazdilfc6a86a2015-06-26 10:33:45 +0000246 // Splits the edge between `block` and `successor` while preserving the
247 // indices in the predecessor/successor lists. If there are multiple edges
248 // between the blocks, the lowest indices are used.
249 // Returns the new block which is empty and has the same dex pc as `successor`.
250 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
251
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100252 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
253 void SimplifyLoop(HBasicBlock* header);
254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000255 int32_t GetNextInstructionId() {
256 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000257 return current_instruction_id_++;
258 }
259
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000260 int32_t GetCurrentInstructionId() const {
261 return current_instruction_id_;
262 }
263
264 void SetCurrentInstructionId(int32_t id) {
265 current_instruction_id_ = id;
266 }
267
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100268 uint16_t GetMaximumNumberOfOutVRegs() const {
269 return maximum_number_of_out_vregs_;
270 }
271
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000272 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
273 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100274 }
275
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100276 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
277 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
278 }
279
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000280 void UpdateTemporariesVRegSlots(size_t slots) {
281 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100282 }
283
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000284 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100285 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000286 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100287 }
288
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100289 void SetNumberOfVRegs(uint16_t number_of_vregs) {
290 number_of_vregs_ = number_of_vregs;
291 }
292
293 uint16_t GetNumberOfVRegs() const {
294 return number_of_vregs_;
295 }
296
297 void SetNumberOfInVRegs(uint16_t value) {
298 number_of_in_vregs_ = value;
299 }
300
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100301 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100302 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100303 return number_of_vregs_ - number_of_in_vregs_;
304 }
305
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100306 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100307 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100308 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100309
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100311 return linear_order_;
312 }
313
Mark Mendell1152c922015-04-24 17:06:35 -0400314 bool HasBoundsChecks() const {
315 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800316 }
317
Mark Mendell1152c922015-04-24 17:06:35 -0400318 void SetHasBoundsChecks(bool value) {
319 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800320 }
321
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100322 bool ShouldGenerateConstructorBarrier() const {
323 return should_generate_constructor_barrier_;
324 }
325
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000326 bool IsDebuggable() const { return debuggable_; }
327
David Brazdil8d5b8b22015-03-24 10:51:52 +0000328 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000329 // already, it is created and inserted into the graph. This method is only for
330 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600331 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000332
333 // TODO: This is problematic for the consistency of reference type propagation
334 // because it can be created anytime after the pass and thus it will be left
335 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600336 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000337
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600338 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
339 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000340 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600341 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
342 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000343 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600344 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
345 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000346 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600347 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
348 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000349 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000350
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100351 HCurrentMethod* GetCurrentMethod();
352
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000353 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100354
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100355 const DexFile& GetDexFile() const {
356 return dex_file_;
357 }
358
359 uint32_t GetMethodIdx() const {
360 return method_idx_;
361 }
362
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100363 InvokeType GetInvokeType() const {
364 return invoke_type_;
365 }
366
Mark Mendellc4701932015-04-10 13:18:51 -0400367 InstructionSet GetInstructionSet() const {
368 return instruction_set_;
369 }
370
David Brazdil77a48ae2015-09-15 12:34:04 +0000371 bool HasTryCatch() const { return has_try_catch_; }
372 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100373
David Brazdil2d7352b2015-04-20 14:52:42 +0100374 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100375 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000376 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100377 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000378
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000379 template <class InstructionType, typename ValueType>
380 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600381 ArenaSafeMap<ValueType, InstructionType*>* cache,
382 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000383 // Try to find an existing constant of the given value.
384 InstructionType* constant = nullptr;
385 auto cached_constant = cache->find(value);
386 if (cached_constant != cache->end()) {
387 constant = cached_constant->second;
388 }
389
390 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100391 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000392 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600393 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000394 cache->Overwrite(value, constant);
395 InsertConstant(constant);
396 }
397 return constant;
398 }
399
David Brazdil8d5b8b22015-03-24 10:51:52 +0000400 void InsertConstant(HConstant* instruction);
401
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000402 // Cache a float constant into the graph. This method should only be
403 // called by the SsaBuilder when creating "equivalent" instructions.
404 void CacheFloatConstant(HFloatConstant* constant);
405
406 // See CacheFloatConstant comment.
407 void CacheDoubleConstant(HDoubleConstant* constant);
408
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000409 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000410
411 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100412 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100414 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100415 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000416
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100417 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100418 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100419
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000420 HBasicBlock* entry_block_;
421 HBasicBlock* exit_block_;
422
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100423 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100424 uint16_t maximum_number_of_out_vregs_;
425
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100426 // The number of virtual registers in this method. Contains the parameters.
427 uint16_t number_of_vregs_;
428
429 // The number of virtual registers used by parameters of this method.
430 uint16_t number_of_in_vregs_;
431
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000432 // Number of vreg size slots that the temporaries use (used in baseline compiler).
433 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100434
Mark Mendell1152c922015-04-24 17:06:35 -0400435 // Has bounds checks. We can totally skip BCE if it's false.
436 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800437
David Brazdil77a48ae2015-09-15 12:34:04 +0000438 // Flag whether there are any try/catch blocks in the graph. We will skip
439 // try/catch-related passes if false.
440 bool has_try_catch_;
441
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000442 // Indicates whether the graph should be compiled in a way that
443 // ensures full debuggability. If false, we can apply more
444 // aggressive optimizations that may limit the level of debugging.
445 const bool debuggable_;
446
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000447 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000448 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000449
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100450 // The dex file from which the method is from.
451 const DexFile& dex_file_;
452
453 // The method index in the dex file.
454 const uint32_t method_idx_;
455
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100456 // If inlined, this encodes how the callee is being invoked.
457 const InvokeType invoke_type_;
458
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100459 // Whether the graph has been transformed to SSA form. Only used
460 // in debug mode to ensure we are not using properties only valid
461 // for non-SSA form (like the number of temporaries).
462 bool in_ssa_form_;
463
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100464 const bool should_generate_constructor_barrier_;
465
Mathieu Chartiere401d142015-04-22 13:56:20 -0700466 const InstructionSet instruction_set_;
467
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000468 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000469 HNullConstant* cached_null_constant_;
470 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000471 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000472 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000473 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000474
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100475 HCurrentMethod* cached_current_method_;
476
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000477 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100478 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000479 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000480 DISALLOW_COPY_AND_ASSIGN(HGraph);
481};
482
Vladimir Markof9f64412015-09-02 14:05:49 +0100483class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 public:
485 HLoopInformation(HBasicBlock* header, HGraph* graph)
486 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100487 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100488 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100489 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100490 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
491 back_edges_.reserve(kDefaultNumberOfBackEdges);
492 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493
494 HBasicBlock* GetHeader() const {
495 return header_;
496 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000497
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000498 void SetHeader(HBasicBlock* block) {
499 header_ = block;
500 }
501
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100502 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
503 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
504 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
505
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000506 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100507 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000508 }
509
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100510 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100511 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100512 }
513
David Brazdil46e2a392015-03-16 17:31:52 +0000514 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100515 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100516 }
517
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000518 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100519 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000520 }
521
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100523
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100524 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100525 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100526 }
527
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100528 // Returns the lifetime position of the back edge that has the
529 // greatest lifetime position.
530 size_t GetLifetimeEnd() const;
531
532 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100533 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100534 }
535
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100536 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100537 // that is the header dominates the back edge.
538 bool Populate();
539
David Brazdila4b8c212015-05-07 09:59:30 +0100540 // Reanalyzes the loop by removing loop info from its blocks and re-running
541 // Populate(). If there are no back edges left, the loop info is completely
542 // removed as well as its SuspendCheck instruction. It must be run on nested
543 // inner loops first.
544 void Update();
545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100546 // Returns whether this loop information contains `block`.
547 // Note that this loop information *must* be populated before entering this function.
548 bool Contains(const HBasicBlock& block) const;
549
550 // Returns whether this loop information is an inner loop of `other`.
551 // Note that `other` *must* be populated before entering this function.
552 bool IsIn(const HLoopInformation& other) const;
553
554 const ArenaBitVector& GetBlocks() const { return blocks_; }
555
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000556 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000557 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000558
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000559 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 // Internal recursive implementation of `Populate`.
561 void PopulateRecursive(HBasicBlock* block);
562
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100564 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100565 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100566 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567
568 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
569};
570
David Brazdilec16f792015-08-19 15:04:01 +0100571// Stores try/catch information for basic blocks.
572// Note that HGraph is constructed so that catch blocks cannot simultaneously
573// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100574class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100575 public:
576 // Try block information constructor.
577 explicit TryCatchInformation(const HTryBoundary& try_entry)
578 : try_entry_(&try_entry),
579 catch_dex_file_(nullptr),
580 catch_type_index_(DexFile::kDexNoIndex16) {
581 DCHECK(try_entry_ != nullptr);
582 }
583
584 // Catch block information constructor.
585 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
586 : try_entry_(nullptr),
587 catch_dex_file_(&dex_file),
588 catch_type_index_(catch_type_index) {}
589
590 bool IsTryBlock() const { return try_entry_ != nullptr; }
591
592 const HTryBoundary& GetTryEntry() const {
593 DCHECK(IsTryBlock());
594 return *try_entry_;
595 }
596
597 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
598
599 bool IsCatchAllTypeIndex() const {
600 DCHECK(IsCatchBlock());
601 return catch_type_index_ == DexFile::kDexNoIndex16;
602 }
603
604 uint16_t GetCatchTypeIndex() const {
605 DCHECK(IsCatchBlock());
606 return catch_type_index_;
607 }
608
609 const DexFile& GetCatchDexFile() const {
610 DCHECK(IsCatchBlock());
611 return *catch_dex_file_;
612 }
613
614 private:
615 // One of possibly several TryBoundary instructions entering the block's try.
616 // Only set for try blocks.
617 const HTryBoundary* try_entry_;
618
619 // Exception type information. Only set for catch blocks.
620 const DexFile* catch_dex_file_;
621 const uint16_t catch_type_index_;
622};
623
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100624static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100625static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100626
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000627// A block in a method. Contains the list of instructions represented
628// as a double linked list. Each block knows its predecessors and
629// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100630
Vladimir Markof9f64412015-09-02 14:05:49 +0100631class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600633 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000634 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000635 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
636 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000637 loop_information_(nullptr),
638 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000639 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100640 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100641 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100642 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000643 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000644 try_catch_information_(nullptr) {
645 predecessors_.reserve(kDefaultNumberOfPredecessors);
646 successors_.reserve(kDefaultNumberOfSuccessors);
647 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
648 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000649
Vladimir Marko60584552015-09-03 13:35:12 +0000650 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100651 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652 }
653
Vladimir Marko60584552015-09-03 13:35:12 +0000654 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100655 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656 }
657
Vladimir Marko60584552015-09-03 13:35:12 +0000658 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
659 return ContainsElement(successors_, block, start_from);
660 }
661
662 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100663 return dominated_blocks_;
664 }
665
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100666 bool IsEntryBlock() const {
667 return graph_->GetEntryBlock() == this;
668 }
669
670 bool IsExitBlock() const {
671 return graph_->GetExitBlock() == this;
672 }
673
David Brazdil46e2a392015-03-16 17:31:52 +0000674 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000675 bool IsSingleTryBoundary() const;
676
677 // Returns true if this block emits nothing but a jump.
678 bool IsSingleJump() const {
679 HLoopInformation* loop_info = GetLoopInformation();
680 return (IsSingleGoto() || IsSingleTryBoundary())
681 // Back edges generate a suspend check.
682 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
683 }
David Brazdil46e2a392015-03-16 17:31:52 +0000684
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000685 void AddBackEdge(HBasicBlock* back_edge) {
686 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000687 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000688 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100689 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000690 loop_information_->AddBackEdge(back_edge);
691 }
692
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000693 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000694 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000695
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100696 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000697 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600698 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000699
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000700 HBasicBlock* GetDominator() const { return dominator_; }
701 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000702 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
703
704 void RemoveDominatedBlock(HBasicBlock* block) {
705 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100706 }
Vladimir Marko60584552015-09-03 13:35:12 +0000707
708 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
709 ReplaceElement(dominated_blocks_, existing, new_block);
710 }
711
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100712 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000713
714 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100715 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000716 }
717
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100718 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
719 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100720 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100721 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100722 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
723 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000724
725 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000726 successors_.push_back(block);
727 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000728 }
729
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100730 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
731 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100732 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000733 new_block->predecessors_.push_back(this);
734 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000735 }
736
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000737 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
738 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000739 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000740 new_block->successors_.push_back(this);
741 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000742 }
743
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100744 // Insert `this` between `predecessor` and `successor. This method
745 // preserves the indicies, and will update the first edge found between
746 // `predecessor` and `successor`.
747 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
748 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100749 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000750 successor->predecessors_[predecessor_index] = this;
751 predecessor->successors_[successor_index] = this;
752 successors_.push_back(successor);
753 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100754 }
755
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100756 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000757 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100758 }
759
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000760 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000761 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000762 }
763
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000765 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100766 }
767
768 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000769 predecessors_.push_back(block);
770 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100771 }
772
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100773 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000774 DCHECK_EQ(predecessors_.size(), 2u);
775 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100776 }
777
David Brazdil769c9e52015-04-27 13:54:09 +0100778 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000779 DCHECK_EQ(successors_.size(), 2u);
780 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100781 }
782
David Brazdilfc6a86a2015-06-26 10:33:45 +0000783 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000784 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100785 }
786
David Brazdilfc6a86a2015-06-26 10:33:45 +0000787 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100789 }
790
David Brazdilfc6a86a2015-06-26 10:33:45 +0000791 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000792 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100793 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000794 }
795
796 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000797 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100798 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000799 }
800
801 // Returns whether the first occurrence of `predecessor` in the list of
802 // predecessors is at index `idx`.
803 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100804 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805 return GetPredecessorIndexOf(predecessor) == idx;
806 }
807
David Brazdilffee3d32015-07-06 11:48:53 +0100808 // Returns the number of non-exceptional successors. SsaChecker ensures that
809 // these are stored at the beginning of the successor list.
810 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000811 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100812 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813
David Brazdild7558da2015-09-22 13:04:14 +0100814 // Create a new block between this block and its predecessors. The new block
815 // is added to the graph, all predecessor edges are relinked to it and an edge
816 // is created to `this`. Returns the new empty block. Reverse post order or
817 // loop and try/catch information are not updated.
818 HBasicBlock* CreateImmediateDominator();
819
David Brazdilfc6a86a2015-06-26 10:33:45 +0000820 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100821 // created, latter block. Note that this method will add the block to the
822 // graph, create a Goto at the end of the former block and will create an edge
823 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100824 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000825 HBasicBlock* SplitBefore(HInstruction* cursor);
826
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000827 // Split the block into two blocks just after `cursor`. Returns the newly
828 // created block. Note that this method just updates raw block information,
829 // like predecessors, successors, dominators, and instruction list. It does not
830 // update the graph, reverse post order, loop information, nor make sure the
831 // blocks are consistent (for example ending with a control flow instruction).
832 HBasicBlock* SplitAfter(HInstruction* cursor);
833
834 // Merge `other` at the end of `this`. Successors and dominated blocks of
835 // `other` are changed to be successors and dominated blocks of `this`. Note
836 // that this method does not update the graph, reverse post order, loop
837 // information, nor make sure the blocks are consistent (for example ending
838 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100839 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000840
841 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
842 // of `this` are moved to `other`.
843 // Note that this method does not update the graph, reverse post order, loop
844 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000845 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846 void ReplaceWith(HBasicBlock* other);
847
David Brazdil2d7352b2015-04-20 14:52:42 +0100848 // Merge `other` at the end of `this`. This method updates loops, reverse post
849 // order, links to predecessors, successors, dominators and deletes the block
850 // from the graph. The two blocks must be successive, i.e. `this` the only
851 // predecessor of `other` and vice versa.
852 void MergeWith(HBasicBlock* other);
853
854 // Disconnects `this` from all its predecessors, successors and dominator,
855 // removes it from all loops it is included in and eventually from the graph.
856 // The block must not dominate any other block. Predecessors and successors
857 // are safely updated.
858 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000859
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000860 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100861 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100862 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100863 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100864 // Replace instruction `initial` with `replacement` within this block.
865 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
866 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100867 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100868 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000869 // RemoveInstruction and RemovePhi delete a given instruction from the respective
870 // instruction list. With 'ensure_safety' set to true, it verifies that the
871 // instruction is not in use and removes it from the use lists of its inputs.
872 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
873 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100874 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100875
876 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100877 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100878 }
879
Roland Levillain6b879dd2014-09-22 17:13:44 +0100880 bool IsLoopPreHeaderFirstPredecessor() const {
881 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100882 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100883 }
884
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100885 HLoopInformation* GetLoopInformation() const {
886 return loop_information_;
887 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000888
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000889 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100890 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000891 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100892 void SetInLoop(HLoopInformation* info) {
893 if (IsLoopHeader()) {
894 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100895 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100896 loop_information_ = info;
897 } else if (loop_information_->Contains(*info->GetHeader())) {
898 // Block is currently part of an outer loop. Make it part of this inner loop.
899 // Note that a non loop header having a loop information means this loop information
900 // has already been populated
901 loop_information_ = info;
902 } else {
903 // Block is part of an inner loop. Do not update the loop information.
904 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
905 // at this point, because this method is being called while populating `info`.
906 }
907 }
908
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000909 // Raw update of the loop information.
910 void SetLoopInformation(HLoopInformation* info) {
911 loop_information_ = info;
912 }
913
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100914 bool IsInLoop() const { return loop_information_ != nullptr; }
915
David Brazdilec16f792015-08-19 15:04:01 +0100916 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
917
918 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
919 try_catch_information_ = try_catch_information;
920 }
921
922 bool IsTryBlock() const {
923 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
924 }
925
926 bool IsCatchBlock() const {
927 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
928 }
David Brazdilffee3d32015-07-06 11:48:53 +0100929
930 // Returns the try entry that this block's successors should have. They will
931 // be in the same try, unless the block ends in a try boundary. In that case,
932 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100933 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100934
David Brazdild7558da2015-09-22 13:04:14 +0100935 bool HasThrowingInstructions() const;
936
David Brazdila4b8c212015-05-07 09:59:30 +0100937 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100938 bool Dominates(HBasicBlock* block) const;
939
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100940 size_t GetLifetimeStart() const { return lifetime_start_; }
941 size_t GetLifetimeEnd() const { return lifetime_end_; }
942
943 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
944 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
945
David Brazdil8d5b8b22015-03-24 10:51:52 +0000946 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000947 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100948 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000949 bool HasSinglePhi() const;
950
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000951 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000952 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000953 ArenaVector<HBasicBlock*> predecessors_;
954 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100955 HInstructionList instructions_;
956 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000957 HLoopInformation* loop_information_;
958 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000959 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100960 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100961 // The dex program counter of the first instruction of this block.
962 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100963 size_t lifetime_start_;
964 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100965 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100966
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000967 friend class HGraph;
968 friend class HInstruction;
969
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
971};
972
David Brazdilb2bd1c52015-03-25 11:17:37 +0000973// Iterates over the LoopInformation of all loops which contain 'block'
974// from the innermost to the outermost.
975class HLoopInformationOutwardIterator : public ValueObject {
976 public:
977 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
978 : current_(block.GetLoopInformation()) {}
979
980 bool Done() const { return current_ == nullptr; }
981
982 void Advance() {
983 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100984 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000985 }
986
987 HLoopInformation* Current() const {
988 DCHECK(!Done());
989 return current_;
990 }
991
992 private:
993 HLoopInformation* current_;
994
995 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
996};
997
Alexandre Ramesef20f712015-06-09 10:29:30 +0100998#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -0700999 M(Above, Condition) \
1000 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001001 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001002 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001003 M(ArrayGet, Instruction) \
1004 M(ArrayLength, Instruction) \
1005 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001006 M(Below, Condition) \
1007 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001008 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001009 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001010 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001011 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001012 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001013 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001014 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001015 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001016 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001017 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001018 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001019 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001020 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001021 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001022 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001023 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001024 M(FloatConstant, Constant) \
1025 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001026 M(GreaterThan, Condition) \
1027 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001028 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001029 M(InstanceFieldGet, Instruction) \
1030 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001031 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001033 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001034 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001035 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001036 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001037 M(LessThan, Condition) \
1038 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001039 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001040 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001041 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001042 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001043 M(Local, Instruction) \
1044 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001045 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001046 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001047 M(Mul, BinaryOperation) \
1048 M(Neg, UnaryOperation) \
1049 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001050 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001051 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001052 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001053 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001054 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001055 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001056 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001057 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001058 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001059 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001060 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001061 M(Return, Instruction) \
1062 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001063 M(Shl, BinaryOperation) \
1064 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001065 M(StaticFieldGet, Instruction) \
1066 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001067 M(UnresolvedInstanceFieldGet, Instruction) \
1068 M(UnresolvedInstanceFieldSet, Instruction) \
1069 M(UnresolvedStaticFieldGet, Instruction) \
1070 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001071 M(StoreLocal, Instruction) \
1072 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001073 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001074 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001075 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001076 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001077 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001078 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001079 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001080
Alexandre Ramesef20f712015-06-09 10:29:30 +01001081#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1082
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001083#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001084#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001085#else
1086#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1087 M(Arm64IntermediateAddress, Instruction)
1088#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001089
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001090#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1091
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001092#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1093
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001094#ifndef ART_ENABLE_CODEGEN_x86
1095#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1096#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001097#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1098 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001099 M(X86LoadFromConstantTable, Instruction) \
1100 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001101#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001102
1103#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1104
1105#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1106 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1107 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1108 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001109 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001110 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001111 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1112 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1113
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001114#define FOR_EACH_INSTRUCTION(M) \
1115 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1116 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001117 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001118 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001119 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001120
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001121#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001122FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1123#undef FORWARD_DECLARATION
1124
Roland Levillainccc07a92014-09-16 14:48:16 +01001125#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001126 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1127 const char* DebugName() const OVERRIDE { return #type; } \
1128 const H##type* As##type() const OVERRIDE { return this; } \
1129 H##type* As##type() OVERRIDE { return this; } \
1130 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001131 return other->Is##type(); \
1132 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001133 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001134
David Brazdiled596192015-01-23 10:39:45 +00001135template <typename T> class HUseList;
1136
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001137template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001138class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001139 public:
David Brazdiled596192015-01-23 10:39:45 +00001140 HUseListNode* GetPrevious() const { return prev_; }
1141 HUseListNode* GetNext() const { return next_; }
1142 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001143 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001144 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001145
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001146 private:
David Brazdiled596192015-01-23 10:39:45 +00001147 HUseListNode(T user, size_t index)
1148 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1149
1150 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001151 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001152 HUseListNode<T>* prev_;
1153 HUseListNode<T>* next_;
1154
1155 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001156
1157 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1158};
1159
David Brazdiled596192015-01-23 10:39:45 +00001160template <typename T>
1161class HUseList : public ValueObject {
1162 public:
1163 HUseList() : first_(nullptr) {}
1164
1165 void Clear() {
1166 first_ = nullptr;
1167 }
1168
1169 // Adds a new entry at the beginning of the use list and returns
1170 // the newly created node.
1171 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001172 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001173 if (IsEmpty()) {
1174 first_ = new_node;
1175 } else {
1176 first_->prev_ = new_node;
1177 new_node->next_ = first_;
1178 first_ = new_node;
1179 }
1180 return new_node;
1181 }
1182
1183 HUseListNode<T>* GetFirst() const {
1184 return first_;
1185 }
1186
1187 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001188 DCHECK(node != nullptr);
1189 DCHECK(Contains(node));
1190
David Brazdiled596192015-01-23 10:39:45 +00001191 if (node->prev_ != nullptr) {
1192 node->prev_->next_ = node->next_;
1193 }
1194 if (node->next_ != nullptr) {
1195 node->next_->prev_ = node->prev_;
1196 }
1197 if (node == first_) {
1198 first_ = node->next_;
1199 }
1200 }
1201
David Brazdil1abb4192015-02-17 18:33:36 +00001202 bool Contains(const HUseListNode<T>* node) const {
1203 if (node == nullptr) {
1204 return false;
1205 }
1206 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1207 if (current == node) {
1208 return true;
1209 }
1210 }
1211 return false;
1212 }
1213
David Brazdiled596192015-01-23 10:39:45 +00001214 bool IsEmpty() const {
1215 return first_ == nullptr;
1216 }
1217
1218 bool HasOnlyOneUse() const {
1219 return first_ != nullptr && first_->next_ == nullptr;
1220 }
1221
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001222 size_t SizeSlow() const {
1223 size_t count = 0;
1224 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1225 ++count;
1226 }
1227 return count;
1228 }
1229
David Brazdiled596192015-01-23 10:39:45 +00001230 private:
1231 HUseListNode<T>* first_;
1232};
1233
1234template<typename T>
1235class HUseIterator : public ValueObject {
1236 public:
1237 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1238
1239 bool Done() const { return current_ == nullptr; }
1240
1241 void Advance() {
1242 DCHECK(!Done());
1243 current_ = current_->GetNext();
1244 }
1245
1246 HUseListNode<T>* Current() const {
1247 DCHECK(!Done());
1248 return current_;
1249 }
1250
1251 private:
1252 HUseListNode<T>* current_;
1253
1254 friend class HValue;
1255};
1256
David Brazdil1abb4192015-02-17 18:33:36 +00001257// This class is used by HEnvironment and HInstruction classes to record the
1258// instructions they use and pointers to the corresponding HUseListNodes kept
1259// by the used instructions.
1260template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001261class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001262 public:
1263 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1264 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1265
1266 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1267 : instruction_(old_record.instruction_), use_node_(use_node) {
1268 DCHECK(instruction_ != nullptr);
1269 DCHECK(use_node_ != nullptr);
1270 DCHECK(old_record.use_node_ == nullptr);
1271 }
1272
1273 HInstruction* GetInstruction() const { return instruction_; }
1274 HUseListNode<T>* GetUseNode() const { return use_node_; }
1275
1276 private:
1277 // Instruction used by the user.
1278 HInstruction* instruction_;
1279
1280 // Corresponding entry in the use list kept by 'instruction_'.
1281 HUseListNode<T>* use_node_;
1282};
1283
Aart Bik854a02b2015-07-14 16:07:00 -07001284/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001285 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001286 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001287 * For write/read dependences on fields/arrays, the dependence analysis uses
1288 * type disambiguation (e.g. a float field write cannot modify the value of an
1289 * integer field read) and the access type (e.g. a reference array write cannot
1290 * modify the value of a reference field read [although it may modify the
1291 * reference fetch prior to reading the field, which is represented by its own
1292 * write/read dependence]). The analysis makes conservative points-to
1293 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1294 * the same, and any reference read depends on any reference read without
1295 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001296 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001297 * The internal representation uses 38-bit and is described in the table below.
1298 * The first line indicates the side effect, and for field/array accesses the
1299 * second line indicates the type of the access (in the order of the
1300 * Primitive::Type enum).
1301 * The two numbered lines below indicate the bit position in the bitfield (read
1302 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001303 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001304 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1305 * +-------------+---------+---------+--------------+---------+---------+
1306 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1307 * | 3 |333333322|222222221| 1 |111111110|000000000|
1308 * | 7 |654321098|765432109| 8 |765432109|876543210|
1309 *
1310 * Note that, to ease the implementation, 'changes' bits are least significant
1311 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001312 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001313class SideEffects : public ValueObject {
1314 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001315 SideEffects() : flags_(0) {}
1316
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001317 static SideEffects None() {
1318 return SideEffects(0);
1319 }
1320
1321 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001322 return SideEffects(kAllChangeBits | kAllDependOnBits);
1323 }
1324
1325 static SideEffects AllChanges() {
1326 return SideEffects(kAllChangeBits);
1327 }
1328
1329 static SideEffects AllDependencies() {
1330 return SideEffects(kAllDependOnBits);
1331 }
1332
1333 static SideEffects AllExceptGCDependency() {
1334 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1335 }
1336
1337 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001338 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001339 }
1340
Aart Bik34c3ba92015-07-20 14:08:59 -07001341 static SideEffects AllWrites() {
1342 return SideEffects(kAllWrites);
1343 }
1344
1345 static SideEffects AllReads() {
1346 return SideEffects(kAllReads);
1347 }
1348
1349 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1350 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001351 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001352 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001353 }
1354
Aart Bik854a02b2015-07-14 16:07:00 -07001355 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1356 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001357 }
1358
Aart Bik34c3ba92015-07-20 14:08:59 -07001359 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1360 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001361 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001362 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001363 }
1364
1365 static SideEffects ArrayReadOfType(Primitive::Type type) {
1366 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1367 }
1368
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001369 static SideEffects CanTriggerGC() {
1370 return SideEffects(1ULL << kCanTriggerGCBit);
1371 }
1372
1373 static SideEffects DependsOnGC() {
1374 return SideEffects(1ULL << kDependsOnGCBit);
1375 }
1376
Aart Bik854a02b2015-07-14 16:07:00 -07001377 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001378 SideEffects Union(SideEffects other) const {
1379 return SideEffects(flags_ | other.flags_);
1380 }
1381
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001382 SideEffects Exclusion(SideEffects other) const {
1383 return SideEffects(flags_ & ~other.flags_);
1384 }
1385
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001386 void Add(SideEffects other) {
1387 flags_ |= other.flags_;
1388 }
1389
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001390 bool Includes(SideEffects other) const {
1391 return (other.flags_ & flags_) == other.flags_;
1392 }
1393
1394 bool HasSideEffects() const {
1395 return (flags_ & kAllChangeBits);
1396 }
1397
1398 bool HasDependencies() const {
1399 return (flags_ & kAllDependOnBits);
1400 }
1401
1402 // Returns true if there are no side effects or dependencies.
1403 bool DoesNothing() const {
1404 return flags_ == 0;
1405 }
1406
Aart Bik854a02b2015-07-14 16:07:00 -07001407 // Returns true if something is written.
1408 bool DoesAnyWrite() const {
1409 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001410 }
1411
Aart Bik854a02b2015-07-14 16:07:00 -07001412 // Returns true if something is read.
1413 bool DoesAnyRead() const {
1414 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001415 }
1416
Aart Bik854a02b2015-07-14 16:07:00 -07001417 // Returns true if potentially everything is written and read
1418 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001419 bool DoesAllReadWrite() const {
1420 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1421 }
1422
Aart Bik854a02b2015-07-14 16:07:00 -07001423 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001424 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001425 }
1426
1427 // Returns true if this may read something written by other.
1428 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001429 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1430 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001431 }
1432
1433 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001434 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001435 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001436 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001437 for (int s = kLastBit; s >= 0; s--) {
1438 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1439 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1440 // This is a bit for the GC side effect.
1441 if (current_bit_is_set) {
1442 flags += "GC";
1443 }
Aart Bik854a02b2015-07-14 16:07:00 -07001444 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001445 } else {
1446 // This is a bit for the array/field analysis.
1447 // The underscore character stands for the 'can trigger GC' bit.
1448 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1449 if (current_bit_is_set) {
1450 flags += kDebug[s];
1451 }
1452 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1453 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1454 flags += "|";
1455 }
1456 }
Aart Bik854a02b2015-07-14 16:07:00 -07001457 }
1458 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001459 }
1460
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001461 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001462
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001463 private:
1464 static constexpr int kFieldArrayAnalysisBits = 9;
1465
1466 static constexpr int kFieldWriteOffset = 0;
1467 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1468 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1469 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1470
1471 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1472
1473 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1474 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1475 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1476 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1477
1478 static constexpr int kLastBit = kDependsOnGCBit;
1479 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1480
1481 // Aliases.
1482
1483 static_assert(kChangeBits == kDependOnBits,
1484 "the 'change' bits should match the 'depend on' bits.");
1485
1486 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1487 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1488 static constexpr uint64_t kAllWrites =
1489 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1490 static constexpr uint64_t kAllReads =
1491 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001492
Aart Bik854a02b2015-07-14 16:07:00 -07001493 // Work around the fact that HIR aliases I/F and J/D.
1494 // TODO: remove this interceptor once HIR types are clean
1495 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1496 switch (type) {
1497 case Primitive::kPrimInt:
1498 case Primitive::kPrimFloat:
1499 return TypeFlag(Primitive::kPrimInt, offset) |
1500 TypeFlag(Primitive::kPrimFloat, offset);
1501 case Primitive::kPrimLong:
1502 case Primitive::kPrimDouble:
1503 return TypeFlag(Primitive::kPrimLong, offset) |
1504 TypeFlag(Primitive::kPrimDouble, offset);
1505 default:
1506 return TypeFlag(type, offset);
1507 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001508 }
1509
Aart Bik854a02b2015-07-14 16:07:00 -07001510 // Translates type to bit flag.
1511 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1512 CHECK_NE(type, Primitive::kPrimVoid);
1513 const uint64_t one = 1;
1514 const int shift = type; // 0-based consecutive enum
1515 DCHECK_LE(kFieldWriteOffset, shift);
1516 DCHECK_LT(shift, kArrayWriteOffset);
1517 return one << (type + offset);
1518 }
1519
1520 // Private constructor on direct flags value.
1521 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1522
1523 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001524};
1525
David Brazdiled596192015-01-23 10:39:45 +00001526// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001527class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001528 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001529 HEnvironment(ArenaAllocator* arena,
1530 size_t number_of_vregs,
1531 const DexFile& dex_file,
1532 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001533 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001534 InvokeType invoke_type,
1535 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001536 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1537 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001538 parent_(nullptr),
1539 dex_file_(dex_file),
1540 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001541 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001542 invoke_type_(invoke_type),
1543 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001544 }
1545
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001546 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001547 : HEnvironment(arena,
1548 to_copy.Size(),
1549 to_copy.GetDexFile(),
1550 to_copy.GetMethodIdx(),
1551 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001552 to_copy.GetInvokeType(),
1553 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001554
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001555 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001556 if (parent_ != nullptr) {
1557 parent_->SetAndCopyParentChain(allocator, parent);
1558 } else {
1559 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1560 parent_->CopyFrom(parent);
1561 if (parent->GetParent() != nullptr) {
1562 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1563 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001564 }
David Brazdiled596192015-01-23 10:39:45 +00001565 }
1566
Vladimir Marko71bf8092015-09-15 15:33:14 +01001567 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001568 void CopyFrom(HEnvironment* environment);
1569
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001570 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1571 // input to the loop phi instead. This is for inserting instructions that
1572 // require an environment (like HDeoptimization) in the loop pre-header.
1573 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001574
1575 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001576 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001577 }
1578
1579 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001580 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001581 }
1582
David Brazdil1abb4192015-02-17 18:33:36 +00001583 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001584
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001585 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001586
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001587 HEnvironment* GetParent() const { return parent_; }
1588
1589 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001590 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001591 }
1592
1593 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001594 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001595 }
1596
1597 uint32_t GetDexPc() const {
1598 return dex_pc_;
1599 }
1600
1601 uint32_t GetMethodIdx() const {
1602 return method_idx_;
1603 }
1604
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001605 InvokeType GetInvokeType() const {
1606 return invoke_type_;
1607 }
1608
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001609 const DexFile& GetDexFile() const {
1610 return dex_file_;
1611 }
1612
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001613 HInstruction* GetHolder() const {
1614 return holder_;
1615 }
1616
David Brazdiled596192015-01-23 10:39:45 +00001617 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001618 // Record instructions' use entries of this environment for constant-time removal.
1619 // It should only be called by HInstruction when a new environment use is added.
1620 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1621 DCHECK(env_use->GetUser() == this);
1622 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001623 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001624 }
David Brazdiled596192015-01-23 10:39:45 +00001625
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001626 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1627 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001628 HEnvironment* parent_;
1629 const DexFile& dex_file_;
1630 const uint32_t method_idx_;
1631 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001632 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001633
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001634 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001635 HInstruction* const holder_;
1636
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001637 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001638
1639 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1640};
1641
Calin Juravleacf735c2015-02-12 15:25:22 +00001642class ReferenceTypeInfo : ValueObject {
1643 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001644 typedef Handle<mirror::Class> TypeHandle;
1645
Calin Juravle2e768302015-07-28 14:41:11 +00001646 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1647 // The constructor will check that the type_handle is valid.
1648 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001649 }
1650
Calin Juravle2e768302015-07-28 14:41:11 +00001651 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1652
1653 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1654 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001655 }
1656
Calin Juravle2e768302015-07-28 14:41:11 +00001657 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1658 return IsValidHandle(type_handle_);
1659 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001660
Calin Juravleacf735c2015-02-12 15:25:22 +00001661 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001662
1663 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1664 DCHECK(IsValid());
1665 return GetTypeHandle()->IsObjectClass();
1666 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001667
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001668 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001669 DCHECK(IsValid());
1670 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001671 }
1672
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001673 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1674 DCHECK(IsValid());
1675 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1676 }
1677
Mathieu Chartier90443472015-07-16 20:32:27 -07001678 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001679 DCHECK(IsValid());
1680 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001681 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001682
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001683 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001684 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001685 return GetTypeHandle()->IsArrayClass();
1686 }
1687
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001688 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1689 DCHECK(IsValid());
1690 return GetTypeHandle()->IsPrimitiveArray();
1691 }
1692
1693 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1694 DCHECK(IsValid());
1695 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1696 }
1697
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001698 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001699 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001700 if (!IsExact()) return false;
1701 if (!IsArrayClass()) return false;
1702 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1703 }
1704
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001705 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1706 DCHECK(IsValid());
1707 if (!IsExact()) return false;
1708 if (!IsArrayClass()) return false;
1709 if (!rti.IsArrayClass()) return false;
1710 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1711 rti.GetTypeHandle()->GetComponentType());
1712 }
1713
Calin Juravleacf735c2015-02-12 15:25:22 +00001714 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1715
Mathieu Chartier90443472015-07-16 20:32:27 -07001716 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001717 DCHECK(IsValid());
1718 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001719 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1720 }
1721
1722 // Returns true if the type information provide the same amount of details.
1723 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001724 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001725 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001726 if (!IsValid() && !rti.IsValid()) {
1727 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001728 return true;
1729 }
Calin Juravle2e768302015-07-28 14:41:11 +00001730 if (!IsValid() || !rti.IsValid()) {
1731 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001732 return false;
1733 }
Calin Juravle2e768302015-07-28 14:41:11 +00001734 return IsExact() == rti.IsExact()
1735 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001736 }
1737
1738 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001739 ReferenceTypeInfo();
1740 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001741
Calin Juravleacf735c2015-02-12 15:25:22 +00001742 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001743 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001744 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001745 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001746 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001747};
1748
1749std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1750
Vladimir Markof9f64412015-09-02 14:05:49 +01001751class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001752 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001753 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001754 : previous_(nullptr),
1755 next_(nullptr),
1756 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001757 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001758 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001759 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001760 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001761 locations_(nullptr),
1762 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001763 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001764 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001765 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001766
Dave Allison20dfc792014-06-16 20:44:29 -07001767 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001768
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001769#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001770 enum InstructionKind {
1771 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1772 };
1773#undef DECLARE_KIND
1774
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001775 HInstruction* GetNext() const { return next_; }
1776 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001777
Calin Juravle77520bc2015-01-12 18:45:46 +00001778 HInstruction* GetNextDisregardingMoves() const;
1779 HInstruction* GetPreviousDisregardingMoves() const;
1780
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001781 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001782 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001783 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001784 bool IsInBlock() const { return block_ != nullptr; }
1785 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001786 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001787
Roland Levillain6b879dd2014-09-22 17:13:44 +01001788 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001789 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001790
1791 virtual void Accept(HGraphVisitor* visitor) = 0;
1792 virtual const char* DebugName() const = 0;
1793
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001794 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001795 void SetRawInputAt(size_t index, HInstruction* input) {
1796 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1797 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001798
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001799 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001800
1801 uint32_t GetDexPc() const { return dex_pc_; }
1802
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001803 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001804
Roland Levillaine161a2a2014-10-03 12:45:18 +01001805 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001806 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001807
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001808 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001809 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001810
Calin Juravle10e244f2015-01-26 18:54:32 +00001811 // Does not apply for all instructions, but having this at top level greatly
1812 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001813 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001814 virtual bool CanBeNull() const {
1815 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1816 return true;
1817 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001818
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001819 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001820 return false;
1821 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001822
Calin Juravle2e768302015-07-28 14:41:11 +00001823 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001824
Calin Juravle61d544b2015-02-23 16:46:57 +00001825 ReferenceTypeInfo GetReferenceTypeInfo() const {
1826 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1827 return reference_type_info_;
1828 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001829
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001830 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001831 DCHECK(user != nullptr);
1832 HUseListNode<HInstruction*>* use =
1833 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1834 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001835 }
1836
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001837 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001838 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001839 HUseListNode<HEnvironment*>* env_use =
1840 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1841 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001842 }
1843
David Brazdil1abb4192015-02-17 18:33:36 +00001844 void RemoveAsUserOfInput(size_t input) {
1845 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1846 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1847 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001848
David Brazdil1abb4192015-02-17 18:33:36 +00001849 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1850 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001851
David Brazdiled596192015-01-23 10:39:45 +00001852 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1853 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001854 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001855 bool HasOnlyOneNonEnvironmentUse() const {
1856 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1857 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858
Roland Levillain6c82d402014-10-13 16:10:27 +01001859 // Does this instruction strictly dominate `other_instruction`?
1860 // Returns false if this instruction and `other_instruction` are the same.
1861 // Aborts if this instruction and `other_instruction` are both phis.
1862 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001863
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001864 int GetId() const { return id_; }
1865 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001866
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001867 int GetSsaIndex() const { return ssa_index_; }
1868 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1869 bool HasSsaIndex() const { return ssa_index_ != -1; }
1870
1871 bool HasEnvironment() const { return environment_ != nullptr; }
1872 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001873 // Set the `environment_` field. Raw because this method does not
1874 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001875 void SetRawEnvironment(HEnvironment* environment) {
1876 DCHECK(environment_ == nullptr);
1877 DCHECK_EQ(environment->GetHolder(), this);
1878 environment_ = environment;
1879 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001880
1881 // Set the environment of this instruction, copying it from `environment`. While
1882 // copying, the uses lists are being updated.
1883 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001884 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001885 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001886 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001887 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001888 if (environment->GetParent() != nullptr) {
1889 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1890 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001891 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001892
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001893 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1894 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001895 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001896 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001897 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001898 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001899 if (environment->GetParent() != nullptr) {
1900 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1901 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001902 }
1903
Nicolas Geoffray39468442014-09-02 15:17:15 +01001904 // Returns the number of entries in the environment. Typically, that is the
1905 // number of dex registers in a method. It could be more in case of inlining.
1906 size_t EnvironmentSize() const;
1907
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001908 LocationSummary* GetLocations() const { return locations_; }
1909 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001910
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001912 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913
Alexandre Rames188d4312015-04-09 18:30:21 +01001914 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1915 // uses of this instruction by `other` are *not* updated.
1916 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1917 ReplaceWith(other);
1918 other->ReplaceInput(this, use_index);
1919 }
1920
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001921 // Move `this` instruction before `cursor`.
1922 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001923
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001924#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001925 bool Is##type() const { return (As##type() != nullptr); } \
1926 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001927 virtual H##type* As##type() { return nullptr; }
1928
1929 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1930#undef INSTRUCTION_TYPE_CHECK
1931
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001932 // Returns whether the instruction can be moved within the graph.
1933 virtual bool CanBeMoved() const { return false; }
1934
1935 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001936 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001937 return false;
1938 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001939
1940 // Returns whether any data encoded in the two instructions is equal.
1941 // This method does not look at the inputs. Both instructions must be
1942 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001943 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001944 return false;
1945 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001946
1947 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001948 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001949 // 2) Their inputs are identical.
1950 bool Equals(HInstruction* other) const;
1951
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001952 virtual InstructionKind GetKind() const = 0;
1953
1954 virtual size_t ComputeHashCode() const {
1955 size_t result = GetKind();
1956 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1957 result = (result * 31) + InputAt(i)->GetId();
1958 }
1959 return result;
1960 }
1961
1962 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001963 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001964
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001965 size_t GetLifetimePosition() const { return lifetime_position_; }
1966 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1967 LiveInterval* GetLiveInterval() const { return live_interval_; }
1968 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1969 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1970
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001971 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1972
1973 // Returns whether the code generation of the instruction will require to have access
1974 // to the current method. Such instructions are:
1975 // (1): Instructions that require an environment, as calling the runtime requires
1976 // to walk the stack and have the current method stored at a specific stack address.
1977 // (2): Object literals like classes and strings, that are loaded from the dex cache
1978 // fields of the current method.
1979 bool NeedsCurrentMethod() const {
1980 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1981 }
1982
Vladimir Markodc151b22015-10-15 18:02:30 +01001983 // Returns whether the code generation of the instruction will require to have access
1984 // to the dex cache of the current method's declaring class via the current method.
1985 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001986
Mark Mendellc4701932015-04-10 13:18:51 -04001987 // Does this instruction have any use in an environment before
1988 // control flow hits 'other'?
1989 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1990
1991 // Remove all references to environment uses of this instruction.
1992 // The caller must ensure that this is safe to do.
1993 void RemoveEnvironmentUsers();
1994
David Brazdil1abb4192015-02-17 18:33:36 +00001995 protected:
1996 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1997 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1998
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001999 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002000 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2001
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002002 HInstruction* previous_;
2003 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002004 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002005 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002006
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002007 // An instruction gets an id when it is added to the graph.
2008 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002009 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002010 int id_;
2011
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002012 // When doing liveness analysis, instructions that have uses get an SSA index.
2013 int ssa_index_;
2014
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002015 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002016 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002017
2018 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002019 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002020
Nicolas Geoffray39468442014-09-02 15:17:15 +01002021 // The environment associated with this instruction. Not null if the instruction
2022 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002023 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002025 // Set by the code generator.
2026 LocationSummary* locations_;
2027
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002028 // Set by the liveness analysis.
2029 LiveInterval* live_interval_;
2030
2031 // Set by the liveness analysis, this is the position in a linear
2032 // order of blocks where this instruction's live interval start.
2033 size_t lifetime_position_;
2034
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002035 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002036
Calin Juravleacf735c2015-02-12 15:25:22 +00002037 // TODO: for primitive types this should be marked as invalid.
2038 ReferenceTypeInfo reference_type_info_;
2039
David Brazdil1abb4192015-02-17 18:33:36 +00002040 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002041 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002042 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002043 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002044 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002045
2046 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2047};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002048std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002049
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002050class HInputIterator : public ValueObject {
2051 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002052 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002053
2054 bool Done() const { return index_ == instruction_->InputCount(); }
2055 HInstruction* Current() const { return instruction_->InputAt(index_); }
2056 void Advance() { index_++; }
2057
2058 private:
2059 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002060 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002061
2062 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2063};
2064
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002065class HInstructionIterator : public ValueObject {
2066 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002067 explicit HInstructionIterator(const HInstructionList& instructions)
2068 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002069 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002070 }
2071
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002072 bool Done() const { return instruction_ == nullptr; }
2073 HInstruction* Current() const { return instruction_; }
2074 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002075 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002076 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002077 }
2078
2079 private:
2080 HInstruction* instruction_;
2081 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002082
2083 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002084};
2085
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002086class HBackwardInstructionIterator : public ValueObject {
2087 public:
2088 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2089 : instruction_(instructions.last_instruction_) {
2090 next_ = Done() ? nullptr : instruction_->GetPrevious();
2091 }
2092
2093 bool Done() const { return instruction_ == nullptr; }
2094 HInstruction* Current() const { return instruction_; }
2095 void Advance() {
2096 instruction_ = next_;
2097 next_ = Done() ? nullptr : instruction_->GetPrevious();
2098 }
2099
2100 private:
2101 HInstruction* instruction_;
2102 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002103
2104 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002105};
2106
Vladimir Markof9f64412015-09-02 14:05:49 +01002107template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002108class HTemplateInstruction: public HInstruction {
2109 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002110 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002111 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002112 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002114 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002115
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002116 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002117 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2118 DCHECK_LT(i, N);
2119 return inputs_[i];
2120 }
David Brazdil1abb4192015-02-17 18:33:36 +00002121
2122 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002123 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002124 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002125 }
2126
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002128 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002129
2130 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002131};
2132
Vladimir Markof9f64412015-09-02 14:05:49 +01002133// HTemplateInstruction specialization for N=0.
2134template<>
2135class HTemplateInstruction<0>: public HInstruction {
2136 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002137 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002138 : HInstruction(side_effects, dex_pc) {}
2139
Vladimir Markof9f64412015-09-02 14:05:49 +01002140 virtual ~HTemplateInstruction() {}
2141
2142 size_t InputCount() const OVERRIDE { return 0; }
2143
2144 protected:
2145 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2146 LOG(FATAL) << "Unreachable";
2147 UNREACHABLE();
2148 }
2149
2150 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2151 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2152 LOG(FATAL) << "Unreachable";
2153 UNREACHABLE();
2154 }
2155
2156 private:
2157 friend class SsaBuilder;
2158};
2159
Dave Allison20dfc792014-06-16 20:44:29 -07002160template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002161class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002162 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002163 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002164 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002165 virtual ~HExpression() {}
2166
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002167 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002168
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002169 protected:
2170 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002171};
2172
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002173// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2174// instruction that branches to the exit block.
2175class HReturnVoid : public HTemplateInstruction<0> {
2176 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002177 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2178 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002179
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002180 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002182 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002183
2184 private:
2185 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2186};
2187
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002188// Represents dex's RETURN opcodes. A HReturn is a control flow
2189// instruction that branches to the exit block.
2190class HReturn : public HTemplateInstruction<1> {
2191 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002192 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2193 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002194 SetRawInputAt(0, value);
2195 }
2196
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002197 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002198
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002199 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002200
2201 private:
2202 DISALLOW_COPY_AND_ASSIGN(HReturn);
2203};
2204
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002205// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002206// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002207// exit block.
2208class HExit : public HTemplateInstruction<0> {
2209 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002210 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002211
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002212 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002214 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002215
2216 private:
2217 DISALLOW_COPY_AND_ASSIGN(HExit);
2218};
2219
2220// Jumps from one block to another.
2221class HGoto : public HTemplateInstruction<0> {
2222 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002223 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002224
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002225 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002227 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002228 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002229 }
2230
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002231 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232
2233 private:
2234 DISALLOW_COPY_AND_ASSIGN(HGoto);
2235};
2236
Roland Levillain9867bc72015-08-05 10:21:34 +01002237class HConstant : public HExpression<0> {
2238 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002239 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2240 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002241
2242 bool CanBeMoved() const OVERRIDE { return true; }
2243
2244 virtual bool IsMinusOne() const { return false; }
2245 virtual bool IsZero() const { return false; }
2246 virtual bool IsOne() const { return false; }
2247
David Brazdil77a48ae2015-09-15 12:34:04 +00002248 virtual uint64_t GetValueAsUint64() const = 0;
2249
Roland Levillain9867bc72015-08-05 10:21:34 +01002250 DECLARE_INSTRUCTION(Constant);
2251
2252 private:
2253 DISALLOW_COPY_AND_ASSIGN(HConstant);
2254};
2255
2256class HNullConstant : public HConstant {
2257 public:
2258 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2259 return true;
2260 }
2261
David Brazdil77a48ae2015-09-15 12:34:04 +00002262 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2263
Roland Levillain9867bc72015-08-05 10:21:34 +01002264 size_t ComputeHashCode() const OVERRIDE { return 0; }
2265
2266 DECLARE_INSTRUCTION(NullConstant);
2267
2268 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002269 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002270
2271 friend class HGraph;
2272 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2273};
2274
2275// Constants of the type int. Those can be from Dex instructions, or
2276// synthesized (for example with the if-eqz instruction).
2277class HIntConstant : public HConstant {
2278 public:
2279 int32_t GetValue() const { return value_; }
2280
David Brazdil9f389d42015-10-01 14:32:56 +01002281 uint64_t GetValueAsUint64() const OVERRIDE {
2282 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2283 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002284
Roland Levillain9867bc72015-08-05 10:21:34 +01002285 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2286 DCHECK(other->IsIntConstant());
2287 return other->AsIntConstant()->value_ == value_;
2288 }
2289
2290 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2291
2292 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2293 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2294 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2295
2296 DECLARE_INSTRUCTION(IntConstant);
2297
2298 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002299 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2300 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2301 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2302 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002303
2304 const int32_t value_;
2305
2306 friend class HGraph;
2307 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2308 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2309 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2310};
2311
2312class HLongConstant : public HConstant {
2313 public:
2314 int64_t GetValue() const { return value_; }
2315
David Brazdil77a48ae2015-09-15 12:34:04 +00002316 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2317
Roland Levillain9867bc72015-08-05 10:21:34 +01002318 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2319 DCHECK(other->IsLongConstant());
2320 return other->AsLongConstant()->value_ == value_;
2321 }
2322
2323 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2324
2325 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2326 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2327 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2328
2329 DECLARE_INSTRUCTION(LongConstant);
2330
2331 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002332 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2333 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002334
2335 const int64_t value_;
2336
2337 friend class HGraph;
2338 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2339};
Dave Allison20dfc792014-06-16 20:44:29 -07002340
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002341// Conditional branch. A block ending with an HIf instruction must have
2342// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002343class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002344 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002345 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2346 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002347 SetRawInputAt(0, input);
2348 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002349
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002350 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002351
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002352 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002353 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002354 }
2355
2356 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002357 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002358 }
2359
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002360 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002361
2362 private:
2363 DISALLOW_COPY_AND_ASSIGN(HIf);
2364};
2365
David Brazdilfc6a86a2015-06-26 10:33:45 +00002366
2367// Abstract instruction which marks the beginning and/or end of a try block and
2368// links it to the respective exception handlers. Behaves the same as a Goto in
2369// non-exceptional control flow.
2370// Normal-flow successor is stored at index zero, exception handlers under
2371// higher indices in no particular order.
2372class HTryBoundary : public HTemplateInstruction<0> {
2373 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002374 enum BoundaryKind {
2375 kEntry,
2376 kExit,
2377 };
2378
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002379 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2380 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002381
2382 bool IsControlFlow() const OVERRIDE { return true; }
2383
2384 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002385 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002386
2387 // Returns whether `handler` is among its exception handlers (non-zero index
2388 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002389 bool HasExceptionHandler(const HBasicBlock& handler) const {
2390 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002391 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002392 }
2393
2394 // If not present already, adds `handler` to its block's list of exception
2395 // handlers.
2396 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002397 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002398 GetBlock()->AddSuccessor(handler);
2399 }
2400 }
2401
David Brazdil56e1acc2015-06-30 15:41:36 +01002402 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002403
David Brazdilffee3d32015-07-06 11:48:53 +01002404 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2405
David Brazdilfc6a86a2015-06-26 10:33:45 +00002406 DECLARE_INSTRUCTION(TryBoundary);
2407
2408 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002409 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002410
2411 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2412};
2413
David Brazdilffee3d32015-07-06 11:48:53 +01002414// Iterator over exception handlers of a given HTryBoundary, i.e. over
2415// exceptional successors of its basic block.
2416class HExceptionHandlerIterator : public ValueObject {
2417 public:
2418 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2419 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2420
Vladimir Marko60584552015-09-03 13:35:12 +00002421 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002422 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002423 size_t CurrentSuccessorIndex() const { return index_; }
2424 void Advance() { ++index_; }
2425
2426 private:
2427 const HBasicBlock& block_;
2428 size_t index_;
2429
2430 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2431};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002432
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002433// Deoptimize to interpreter, upon checking a condition.
2434class HDeoptimize : public HTemplateInstruction<1> {
2435 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002436 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2437 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002438 SetRawInputAt(0, cond);
2439 }
2440
2441 bool NeedsEnvironment() const OVERRIDE { return true; }
2442 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002443
2444 DECLARE_INSTRUCTION(Deoptimize);
2445
2446 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002447 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2448};
2449
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002450// Represents the ArtMethod that was passed as a first argument to
2451// the method. It is used by instructions that depend on it, like
2452// instructions that work with the dex cache.
2453class HCurrentMethod : public HExpression<0> {
2454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002455 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2456 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002457
2458 DECLARE_INSTRUCTION(CurrentMethod);
2459
2460 private:
2461 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2462};
2463
Mark Mendellfe57faa2015-09-18 09:26:15 -04002464// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2465// have one successor for each entry in the switch table, and the final successor
2466// will be the block containing the next Dex opcode.
2467class HPackedSwitch : public HTemplateInstruction<1> {
2468 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002469 HPackedSwitch(int32_t start_value,
2470 uint32_t num_entries,
2471 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002472 uint32_t dex_pc = kNoDexPc)
2473 : HTemplateInstruction(SideEffects::None(), dex_pc),
2474 start_value_(start_value),
2475 num_entries_(num_entries) {
2476 SetRawInputAt(0, input);
2477 }
2478
2479 bool IsControlFlow() const OVERRIDE { return true; }
2480
2481 int32_t GetStartValue() const { return start_value_; }
2482
Vladimir Marko211c2112015-09-24 16:52:33 +01002483 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002484
2485 HBasicBlock* GetDefaultBlock() const {
2486 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002487 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002488 }
2489 DECLARE_INSTRUCTION(PackedSwitch);
2490
2491 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002492 const int32_t start_value_;
2493 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002494
2495 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2496};
2497
Roland Levillain88cb1752014-10-20 16:36:47 +01002498class HUnaryOperation : public HExpression<1> {
2499 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002500 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2501 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002502 SetRawInputAt(0, input);
2503 }
2504
2505 HInstruction* GetInput() const { return InputAt(0); }
2506 Primitive::Type GetResultType() const { return GetType(); }
2507
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002508 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002509 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002510 return true;
2511 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002512
Roland Levillain9240d6a2014-10-20 16:47:04 +01002513 // Try to statically evaluate `operation` and return a HConstant
2514 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002515 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002516 HConstant* TryStaticEvaluation() const;
2517
2518 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002519 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2520 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002521
Roland Levillain88cb1752014-10-20 16:36:47 +01002522 DECLARE_INSTRUCTION(UnaryOperation);
2523
2524 private:
2525 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2526};
2527
Dave Allison20dfc792014-06-16 20:44:29 -07002528class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002529 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002530 HBinaryOperation(Primitive::Type result_type,
2531 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002532 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002533 SideEffects side_effects = SideEffects::None(),
2534 uint32_t dex_pc = kNoDexPc)
2535 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002536 SetRawInputAt(0, left);
2537 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002538 }
2539
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002540 HInstruction* GetLeft() const { return InputAt(0); }
2541 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002542 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002543
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002544 virtual bool IsCommutative() const { return false; }
2545
2546 // Put constant on the right.
2547 // Returns whether order is changed.
2548 bool OrderInputsWithConstantOnTheRight() {
2549 HInstruction* left = InputAt(0);
2550 HInstruction* right = InputAt(1);
2551 if (left->IsConstant() && !right->IsConstant()) {
2552 ReplaceInput(right, 0);
2553 ReplaceInput(left, 1);
2554 return true;
2555 }
2556 return false;
2557 }
2558
2559 // Order inputs by instruction id, but favor constant on the right side.
2560 // This helps GVN for commutative ops.
2561 void OrderInputs() {
2562 DCHECK(IsCommutative());
2563 HInstruction* left = InputAt(0);
2564 HInstruction* right = InputAt(1);
2565 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2566 return;
2567 }
2568 if (OrderInputsWithConstantOnTheRight()) {
2569 return;
2570 }
2571 // Order according to instruction id.
2572 if (left->GetId() > right->GetId()) {
2573 ReplaceInput(right, 0);
2574 ReplaceInput(left, 1);
2575 }
2576 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002577
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002578 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002579 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002580 return true;
2581 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002582
Roland Levillain9240d6a2014-10-20 16:47:04 +01002583 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002584 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002585 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002586 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002587
2588 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002589 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2590 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2591 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2592 HLongConstant* y ATTRIBUTE_UNUSED) const {
2593 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2594 return nullptr;
2595 }
2596 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2597 HIntConstant* y ATTRIBUTE_UNUSED) const {
2598 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2599 return nullptr;
2600 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002601
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002602 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002603 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002604 HConstant* GetConstantRight() const;
2605
2606 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002607 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002608 HInstruction* GetLeastConstantLeft() const;
2609
Roland Levillainccc07a92014-09-16 14:48:16 +01002610 DECLARE_INSTRUCTION(BinaryOperation);
2611
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002612 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002613 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2614};
2615
Mark Mendellc4701932015-04-10 13:18:51 -04002616// The comparison bias applies for floating point operations and indicates how NaN
2617// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002618enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002619 kNoBias, // bias is not applicable (i.e. for long operation)
2620 kGtBias, // return 1 for NaN comparisons
2621 kLtBias, // return -1 for NaN comparisons
2622};
2623
Dave Allison20dfc792014-06-16 20:44:29 -07002624class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002625 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002626 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2627 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002628 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002629 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002630
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002631 bool NeedsMaterialization() const { return needs_materialization_; }
2632 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002633
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002634 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002635 // `instruction`, and disregard moves in between.
2636 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002637
Dave Allison20dfc792014-06-16 20:44:29 -07002638 DECLARE_INSTRUCTION(Condition);
2639
2640 virtual IfCondition GetCondition() const = 0;
2641
Mark Mendellc4701932015-04-10 13:18:51 -04002642 virtual IfCondition GetOppositeCondition() const = 0;
2643
Roland Levillain4fa13f62015-07-06 18:11:54 +01002644 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002645
2646 void SetBias(ComparisonBias bias) { bias_ = bias; }
2647
2648 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2649 return bias_ == other->AsCondition()->bias_;
2650 }
2651
Roland Levillain4fa13f62015-07-06 18:11:54 +01002652 bool IsFPConditionTrueIfNaN() const {
2653 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2654 IfCondition if_cond = GetCondition();
2655 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2656 }
2657
2658 bool IsFPConditionFalseIfNaN() const {
2659 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2660 IfCondition if_cond = GetCondition();
2661 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2662 }
2663
Dave Allison20dfc792014-06-16 20:44:29 -07002664 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002665 // For register allocation purposes, returns whether this instruction needs to be
2666 // materialized (that is, not just be in the processor flags).
2667 bool needs_materialization_;
2668
Mark Mendellc4701932015-04-10 13:18:51 -04002669 // Needed if we merge a HCompare into a HCondition.
2670 ComparisonBias bias_;
2671
Dave Allison20dfc792014-06-16 20:44:29 -07002672 DISALLOW_COPY_AND_ASSIGN(HCondition);
2673};
2674
2675// Instruction to check if two inputs are equal to each other.
2676class HEqual : public HCondition {
2677 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002678 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2679 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002680
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002681 bool IsCommutative() const OVERRIDE { return true; }
2682
Roland Levillain9867bc72015-08-05 10:21:34 +01002683 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002684 return GetBlock()->GetGraph()->GetIntConstant(
2685 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002686 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002687 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002688 return GetBlock()->GetGraph()->GetIntConstant(
2689 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002690 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002691
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002692 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002693
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002694 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002695 return kCondEQ;
2696 }
2697
Mark Mendellc4701932015-04-10 13:18:51 -04002698 IfCondition GetOppositeCondition() const OVERRIDE {
2699 return kCondNE;
2700 }
2701
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002702 private:
Aart Bike9f37602015-10-09 11:15:55 -07002703 template <typename T> bool Compute(T x, T y) const { return x == y; }
2704
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002705 DISALLOW_COPY_AND_ASSIGN(HEqual);
2706};
2707
Dave Allison20dfc792014-06-16 20:44:29 -07002708class HNotEqual : public HCondition {
2709 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002710 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2711 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002712
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002713 bool IsCommutative() const OVERRIDE { return true; }
2714
Roland Levillain9867bc72015-08-05 10:21:34 +01002715 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002716 return GetBlock()->GetGraph()->GetIntConstant(
2717 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002718 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002720 return GetBlock()->GetGraph()->GetIntConstant(
2721 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002722 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002723
Dave Allison20dfc792014-06-16 20:44:29 -07002724 DECLARE_INSTRUCTION(NotEqual);
2725
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002726 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002727 return kCondNE;
2728 }
2729
Mark Mendellc4701932015-04-10 13:18:51 -04002730 IfCondition GetOppositeCondition() const OVERRIDE {
2731 return kCondEQ;
2732 }
2733
Dave Allison20dfc792014-06-16 20:44:29 -07002734 private:
Aart Bike9f37602015-10-09 11:15:55 -07002735 template <typename T> bool Compute(T x, T y) const { return x != y; }
2736
Dave Allison20dfc792014-06-16 20:44:29 -07002737 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2738};
2739
2740class HLessThan : public HCondition {
2741 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002742 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2743 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002744
Roland Levillain9867bc72015-08-05 10:21:34 +01002745 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002746 return GetBlock()->GetGraph()->GetIntConstant(
2747 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002748 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002749 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002750 return GetBlock()->GetGraph()->GetIntConstant(
2751 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002752 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002753
Dave Allison20dfc792014-06-16 20:44:29 -07002754 DECLARE_INSTRUCTION(LessThan);
2755
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002756 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002757 return kCondLT;
2758 }
2759
Mark Mendellc4701932015-04-10 13:18:51 -04002760 IfCondition GetOppositeCondition() const OVERRIDE {
2761 return kCondGE;
2762 }
2763
Dave Allison20dfc792014-06-16 20:44:29 -07002764 private:
Aart Bike9f37602015-10-09 11:15:55 -07002765 template <typename T> bool Compute(T x, T y) const { return x < y; }
2766
Dave Allison20dfc792014-06-16 20:44:29 -07002767 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2768};
2769
2770class HLessThanOrEqual : public HCondition {
2771 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002772 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2773 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002774
Roland Levillain9867bc72015-08-05 10:21:34 +01002775 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002776 return GetBlock()->GetGraph()->GetIntConstant(
2777 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002778 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002779 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002780 return GetBlock()->GetGraph()->GetIntConstant(
2781 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002782 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002783
Dave Allison20dfc792014-06-16 20:44:29 -07002784 DECLARE_INSTRUCTION(LessThanOrEqual);
2785
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002786 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002787 return kCondLE;
2788 }
2789
Mark Mendellc4701932015-04-10 13:18:51 -04002790 IfCondition GetOppositeCondition() const OVERRIDE {
2791 return kCondGT;
2792 }
2793
Dave Allison20dfc792014-06-16 20:44:29 -07002794 private:
Aart Bike9f37602015-10-09 11:15:55 -07002795 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2796
Dave Allison20dfc792014-06-16 20:44:29 -07002797 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2798};
2799
2800class HGreaterThan : public HCondition {
2801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002802 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2803 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002804
Roland Levillain9867bc72015-08-05 10:21:34 +01002805 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002806 return GetBlock()->GetGraph()->GetIntConstant(
2807 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002808 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002809 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002810 return GetBlock()->GetGraph()->GetIntConstant(
2811 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002812 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002813
Dave Allison20dfc792014-06-16 20:44:29 -07002814 DECLARE_INSTRUCTION(GreaterThan);
2815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002816 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002817 return kCondGT;
2818 }
2819
Mark Mendellc4701932015-04-10 13:18:51 -04002820 IfCondition GetOppositeCondition() const OVERRIDE {
2821 return kCondLE;
2822 }
2823
Dave Allison20dfc792014-06-16 20:44:29 -07002824 private:
Aart Bike9f37602015-10-09 11:15:55 -07002825 template <typename T> bool Compute(T x, T y) const { return x > y; }
2826
Dave Allison20dfc792014-06-16 20:44:29 -07002827 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2828};
2829
2830class HGreaterThanOrEqual : public HCondition {
2831 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002832 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2833 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002834
Roland Levillain9867bc72015-08-05 10:21:34 +01002835 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002836 return GetBlock()->GetGraph()->GetIntConstant(
2837 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002838 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002839 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002840 return GetBlock()->GetGraph()->GetIntConstant(
2841 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002842 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002843
Dave Allison20dfc792014-06-16 20:44:29 -07002844 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2845
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002846 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002847 return kCondGE;
2848 }
2849
Mark Mendellc4701932015-04-10 13:18:51 -04002850 IfCondition GetOppositeCondition() const OVERRIDE {
2851 return kCondLT;
2852 }
2853
Dave Allison20dfc792014-06-16 20:44:29 -07002854 private:
Aart Bike9f37602015-10-09 11:15:55 -07002855 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2856
Dave Allison20dfc792014-06-16 20:44:29 -07002857 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2858};
2859
Aart Bike9f37602015-10-09 11:15:55 -07002860class HBelow : public HCondition {
2861 public:
2862 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2863 : HCondition(first, second, dex_pc) {}
2864
2865 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2866 return GetBlock()->GetGraph()->GetIntConstant(
2867 Compute(static_cast<uint32_t>(x->GetValue()),
2868 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2869 }
2870 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2871 return GetBlock()->GetGraph()->GetIntConstant(
2872 Compute(static_cast<uint64_t>(x->GetValue()),
2873 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2874 }
2875
2876 DECLARE_INSTRUCTION(Below);
2877
2878 IfCondition GetCondition() const OVERRIDE {
2879 return kCondB;
2880 }
2881
2882 IfCondition GetOppositeCondition() const OVERRIDE {
2883 return kCondAE;
2884 }
2885
2886 private:
2887 template <typename T> bool Compute(T x, T y) const { return x < y; }
2888
2889 DISALLOW_COPY_AND_ASSIGN(HBelow);
2890};
2891
2892class HBelowOrEqual : public HCondition {
2893 public:
2894 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2895 : HCondition(first, second, dex_pc) {}
2896
2897 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2898 return GetBlock()->GetGraph()->GetIntConstant(
2899 Compute(static_cast<uint32_t>(x->GetValue()),
2900 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2901 }
2902 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2903 return GetBlock()->GetGraph()->GetIntConstant(
2904 Compute(static_cast<uint64_t>(x->GetValue()),
2905 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2906 }
2907
2908 DECLARE_INSTRUCTION(BelowOrEqual);
2909
2910 IfCondition GetCondition() const OVERRIDE {
2911 return kCondBE;
2912 }
2913
2914 IfCondition GetOppositeCondition() const OVERRIDE {
2915 return kCondA;
2916 }
2917
2918 private:
2919 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2920
2921 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2922};
2923
2924class HAbove : public HCondition {
2925 public:
2926 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2927 : HCondition(first, second, dex_pc) {}
2928
2929 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2930 return GetBlock()->GetGraph()->GetIntConstant(
2931 Compute(static_cast<uint32_t>(x->GetValue()),
2932 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2933 }
2934 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2935 return GetBlock()->GetGraph()->GetIntConstant(
2936 Compute(static_cast<uint64_t>(x->GetValue()),
2937 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2938 }
2939
2940 DECLARE_INSTRUCTION(Above);
2941
2942 IfCondition GetCondition() const OVERRIDE {
2943 return kCondA;
2944 }
2945
2946 IfCondition GetOppositeCondition() const OVERRIDE {
2947 return kCondBE;
2948 }
2949
2950 private:
2951 template <typename T> bool Compute(T x, T y) const { return x > y; }
2952
2953 DISALLOW_COPY_AND_ASSIGN(HAbove);
2954};
2955
2956class HAboveOrEqual : public HCondition {
2957 public:
2958 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2959 : HCondition(first, second, dex_pc) {}
2960
2961 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2962 return GetBlock()->GetGraph()->GetIntConstant(
2963 Compute(static_cast<uint32_t>(x->GetValue()),
2964 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2965 }
2966 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2967 return GetBlock()->GetGraph()->GetIntConstant(
2968 Compute(static_cast<uint64_t>(x->GetValue()),
2969 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2970 }
2971
2972 DECLARE_INSTRUCTION(AboveOrEqual);
2973
2974 IfCondition GetCondition() const OVERRIDE {
2975 return kCondAE;
2976 }
2977
2978 IfCondition GetOppositeCondition() const OVERRIDE {
2979 return kCondB;
2980 }
2981
2982 private:
2983 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2984
2985 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
2986};
Dave Allison20dfc792014-06-16 20:44:29 -07002987
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002988// Instruction to check how two inputs compare to each other.
2989// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2990class HCompare : public HBinaryOperation {
2991 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002992 HCompare(Primitive::Type type,
2993 HInstruction* first,
2994 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002995 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002996 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002997 : HBinaryOperation(Primitive::kPrimInt,
2998 first,
2999 second,
3000 SideEffectsForArchRuntimeCalls(type),
3001 dex_pc),
3002 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003003 DCHECK_EQ(type, first->GetType());
3004 DCHECK_EQ(type, second->GetType());
3005 }
3006
Roland Levillain9867bc72015-08-05 10:21:34 +01003007 template <typename T>
3008 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003009
Roland Levillain9867bc72015-08-05 10:21:34 +01003010 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003011 return GetBlock()->GetGraph()->GetIntConstant(
3012 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003013 }
3014 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003015 return GetBlock()->GetGraph()->GetIntConstant(
3016 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003017 }
3018
Calin Juravleddb7df22014-11-25 20:56:51 +00003019 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3020 return bias_ == other->AsCompare()->bias_;
3021 }
3022
Mark Mendellc4701932015-04-10 13:18:51 -04003023 ComparisonBias GetBias() const { return bias_; }
3024
Roland Levillain4fa13f62015-07-06 18:11:54 +01003025 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003026
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003027
3028 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3029 // MIPS64 uses a runtime call for FP comparisons.
3030 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3031 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003032
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003033 DECLARE_INSTRUCTION(Compare);
3034
3035 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003036 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003037
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003038 DISALLOW_COPY_AND_ASSIGN(HCompare);
3039};
3040
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003041// A local in the graph. Corresponds to a Dex register.
3042class HLocal : public HTemplateInstruction<0> {
3043 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003044 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003045 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003046
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003047 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003048
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003049 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003050
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003051 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003052 // The Dex register number.
3053 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003054
3055 DISALLOW_COPY_AND_ASSIGN(HLocal);
3056};
3057
3058// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003059class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003060 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003061 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3062 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003063 SetRawInputAt(0, local);
3064 }
3065
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003066 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3067
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003068 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003069
3070 private:
3071 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3072};
3073
3074// Store a value in a given local. This instruction has two inputs: the value
3075// and the local.
3076class HStoreLocal : public HTemplateInstruction<2> {
3077 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003078 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3079 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003080 SetRawInputAt(0, local);
3081 SetRawInputAt(1, value);
3082 }
3083
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003084 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3085
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003086 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003087
3088 private:
3089 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3090};
3091
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003092class HFloatConstant : public HConstant {
3093 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003094 float GetValue() const { return value_; }
3095
David Brazdil77a48ae2015-09-15 12:34:04 +00003096 uint64_t GetValueAsUint64() const OVERRIDE {
3097 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3098 }
3099
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003100 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003101 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003102 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003103 }
3104
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003105 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003106
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003107 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003108 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003109 }
3110 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003111 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003112 }
3113 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003114 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3115 }
3116 bool IsNaN() const {
3117 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003118 }
3119
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003120 DECLARE_INSTRUCTION(FloatConstant);
3121
3122 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003123 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3124 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3125 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3126 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003127
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003128 const float value_;
3129
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003130 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003131 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003132 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003133 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3134};
3135
3136class HDoubleConstant : public HConstant {
3137 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003138 double GetValue() const { return value_; }
3139
David Brazdil77a48ae2015-09-15 12:34:04 +00003140 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3141
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003142 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003143 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003144 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003145 }
3146
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003147 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003148
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003149 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003150 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003151 }
3152 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003153 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003154 }
3155 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003156 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3157 }
3158 bool IsNaN() const {
3159 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003160 }
3161
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003162 DECLARE_INSTRUCTION(DoubleConstant);
3163
3164 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003165 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3166 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3167 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3168 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003169
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003170 const double value_;
3171
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003172 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003173 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003174 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003175 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3176};
3177
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003178enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003179#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003180#include "intrinsics_list.h"
3181 kNone,
3182 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3183#undef INTRINSICS_LIST
3184#undef OPTIMIZING_INTRINSICS
3185};
3186std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3187
Agi Csaki05f20562015-08-19 14:58:14 -07003188enum IntrinsicNeedsEnvironmentOrCache {
3189 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3190 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003191};
3192
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003193class HInvoke : public HInstruction {
3194 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003195 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003196
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003197 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003198
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003199 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003200 SetRawInputAt(index, argument);
3201 }
3202
Roland Levillain3e3d7332015-04-28 11:00:54 +01003203 // Return the number of arguments. This number can be lower than
3204 // the number of inputs returned by InputCount(), as some invoke
3205 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3206 // inputs at the end of their list of inputs.
3207 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3208
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003209 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003210
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003211
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003212 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003213 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003214
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003215 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3216
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003217 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003218 return intrinsic_;
3219 }
3220
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003221 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003222
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003223 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003224 return GetEnvironment()->GetParent() != nullptr;
3225 }
3226
3227 bool CanThrow() const OVERRIDE { return true; }
3228
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003229 uint32_t* GetIntrinsicOptimizations() {
3230 return &intrinsic_optimizations_;
3231 }
3232
3233 const uint32_t* GetIntrinsicOptimizations() const {
3234 return &intrinsic_optimizations_;
3235 }
3236
3237 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3238
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003239 DECLARE_INSTRUCTION(Invoke);
3240
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003241 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003242 HInvoke(ArenaAllocator* arena,
3243 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003244 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003245 Primitive::Type return_type,
3246 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003247 uint32_t dex_method_index,
3248 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003249 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003250 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003251 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003252 inputs_(number_of_arguments + number_of_other_inputs,
3253 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003254 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003255 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003256 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003257 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003258 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003259 }
3260
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003261 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003262 return inputs_[index];
3263 }
3264
David Brazdil1abb4192015-02-17 18:33:36 +00003265 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003266 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003267 }
3268
Roland Levillain3e3d7332015-04-28 11:00:54 +01003269 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003270 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003271 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003272 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003273 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003274 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003275
3276 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3277 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003278
3279 private:
3280 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3281};
3282
Calin Juravle175dc732015-08-25 15:42:32 +01003283class HInvokeUnresolved : public HInvoke {
3284 public:
3285 HInvokeUnresolved(ArenaAllocator* arena,
3286 uint32_t number_of_arguments,
3287 Primitive::Type return_type,
3288 uint32_t dex_pc,
3289 uint32_t dex_method_index,
3290 InvokeType invoke_type)
3291 : HInvoke(arena,
3292 number_of_arguments,
3293 0u /* number_of_other_inputs */,
3294 return_type,
3295 dex_pc,
3296 dex_method_index,
3297 invoke_type) {
3298 }
3299
3300 DECLARE_INSTRUCTION(InvokeUnresolved);
3301
3302 private:
3303 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3304};
3305
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003306class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003307 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003308 // Requirements of this method call regarding the class
3309 // initialization (clinit) check of its declaring class.
3310 enum class ClinitCheckRequirement {
3311 kNone, // Class already initialized.
3312 kExplicit, // Static call having explicit clinit check as last input.
3313 kImplicit, // Static call implicitly requiring a clinit check.
3314 };
3315
Vladimir Marko58155012015-08-19 12:49:41 +00003316 // Determines how to load the target ArtMethod*.
3317 enum class MethodLoadKind {
3318 // Use a String init ArtMethod* loaded from Thread entrypoints.
3319 kStringInit,
3320
3321 // Use the method's own ArtMethod* loaded by the register allocator.
3322 kRecursive,
3323
3324 // Use ArtMethod* at a known address, embed the direct address in the code.
3325 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3326 kDirectAddress,
3327
3328 // Use ArtMethod* at an address that will be known at link time, embed the direct
3329 // address in the code. If the image is relocatable, emit .patch_oat entry.
3330 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3331 // the image relocatable or not.
3332 kDirectAddressWithFixup,
3333
3334 // Load from resoved methods array in the dex cache using a PC-relative load.
3335 // Used when we need to use the dex cache, for example for invoke-static that
3336 // may cause class initialization (the entry may point to a resolution method),
3337 // and we know that we can access the dex cache arrays using a PC-relative load.
3338 kDexCachePcRelative,
3339
3340 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3341 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3342 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3343 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3344 kDexCacheViaMethod,
3345 };
3346
3347 // Determines the location of the code pointer.
3348 enum class CodePtrLocation {
3349 // Recursive call, use local PC-relative call instruction.
3350 kCallSelf,
3351
3352 // Use PC-relative call instruction patched at link time.
3353 // Used for calls within an oat file, boot->boot or app->app.
3354 kCallPCRelative,
3355
3356 // Call to a known target address, embed the direct address in code.
3357 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3358 kCallDirect,
3359
3360 // Call to a target address that will be known at link time, embed the direct
3361 // address in code. If the image is relocatable, emit .patch_oat entry.
3362 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3363 // the image relocatable or not.
3364 kCallDirectWithFixup,
3365
3366 // Use code pointer from the ArtMethod*.
3367 // Used when we don't know the target code. This is also the last-resort-kind used when
3368 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3369 kCallArtMethod,
3370 };
3371
3372 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003373 MethodLoadKind method_load_kind;
3374 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003375 // The method load data holds
3376 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3377 // Note that there are multiple string init methods, each having its own offset.
3378 // - the method address for kDirectAddress
3379 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003380 uint64_t method_load_data;
3381 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003382 };
3383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003384 HInvokeStaticOrDirect(ArenaAllocator* arena,
3385 uint32_t number_of_arguments,
3386 Primitive::Type return_type,
3387 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003388 uint32_t method_index,
3389 MethodReference target_method,
3390 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003391 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003392 InvokeType invoke_type,
3393 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003394 : HInvoke(arena,
3395 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003396 // There is one extra argument for the HCurrentMethod node, and
3397 // potentially one other if the clinit check is explicit, and one other
3398 // if the method is a string factory.
3399 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003400 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003401 return_type,
3402 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003403 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003404 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003405 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003406 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003407 target_method_(target_method),
3408 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003409
Vladimir Markodc151b22015-10-15 18:02:30 +01003410 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
3411 dispatch_info_ = dispatch_info;
3412 }
3413
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003414 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003415 // We access the method via the dex cache so we can't do an implicit null check.
3416 // TODO: for intrinsics we can generate implicit null checks.
3417 return false;
3418 }
3419
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003420 bool CanBeNull() const OVERRIDE {
3421 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3422 }
3423
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003424 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003425 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3426 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3427 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003428 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003429 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003430 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003431 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003432 bool HasPcRelDexCache() const {
3433 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3434 }
Vladimir Marko58155012015-08-19 12:49:41 +00003435 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3436 MethodReference GetTargetMethod() const { return target_method_; }
3437
3438 int32_t GetStringInitOffset() const {
3439 DCHECK(IsStringInit());
3440 return dispatch_info_.method_load_data;
3441 }
3442
3443 uint64_t GetMethodAddress() const {
3444 DCHECK(HasMethodAddress());
3445 return dispatch_info_.method_load_data;
3446 }
3447
3448 uint32_t GetDexCacheArrayOffset() const {
3449 DCHECK(HasPcRelDexCache());
3450 return dispatch_info_.method_load_data;
3451 }
3452
3453 uint64_t GetDirectCodePtr() const {
3454 DCHECK(HasDirectCodePtr());
3455 return dispatch_info_.direct_code_ptr;
3456 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003457
Calin Juravle68ad6492015-08-18 17:08:12 +01003458 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3459
Roland Levillain4c0eb422015-04-24 16:43:49 +01003460 // Is this instruction a call to a static method?
3461 bool IsStatic() const {
3462 return GetInvokeType() == kStatic;
3463 }
3464
Roland Levillain3e3d7332015-04-28 11:00:54 +01003465 // Remove the art::HLoadClass instruction set as last input by
3466 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3467 // the initial art::HClinitCheck instruction (only relevant for
3468 // static calls with explicit clinit check).
3469 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003470 DCHECK(IsStaticWithExplicitClinitCheck());
3471 size_t last_input_index = InputCount() - 1;
3472 HInstruction* last_input = InputAt(last_input_index);
3473 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003474 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003475 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003476 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003477 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3478 DCHECK(IsStaticWithImplicitClinitCheck());
3479 }
3480
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003481 bool IsStringFactoryFor(HFakeString* str) const {
3482 if (!IsStringInit()) return false;
3483 // +1 for the current method.
3484 if (InputCount() == (number_of_arguments_ + 1)) return false;
3485 return InputAt(InputCount() - 1)->AsFakeString() == str;
3486 }
3487
3488 void RemoveFakeStringArgumentAsLastInput() {
3489 DCHECK(IsStringInit());
3490 size_t last_input_index = InputCount() - 1;
3491 HInstruction* last_input = InputAt(last_input_index);
3492 DCHECK(last_input != nullptr);
3493 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3494 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003495 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003496 }
3497
Roland Levillain4c0eb422015-04-24 16:43:49 +01003498 // Is this a call to a static method whose declaring class has an
3499 // explicit intialization check in the graph?
3500 bool IsStaticWithExplicitClinitCheck() const {
3501 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3502 }
3503
3504 // Is this a call to a static method whose declaring class has an
3505 // implicit intialization check requirement?
3506 bool IsStaticWithImplicitClinitCheck() const {
3507 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3508 }
3509
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003510 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003511
Roland Levillain4c0eb422015-04-24 16:43:49 +01003512 protected:
3513 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3514 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3515 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3516 HInstruction* input = input_record.GetInstruction();
3517 // `input` is the last input of a static invoke marked as having
3518 // an explicit clinit check. It must either be:
3519 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3520 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3521 DCHECK(input != nullptr);
3522 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3523 }
3524 return input_record;
3525 }
3526
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003527 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003528 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003529 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003530 // The target method may refer to different dex file or method index than the original
3531 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3532 // in derived class to increase visibility.
3533 MethodReference target_method_;
3534 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003535
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003536 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003537};
3538
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003539class HInvokeVirtual : public HInvoke {
3540 public:
3541 HInvokeVirtual(ArenaAllocator* arena,
3542 uint32_t number_of_arguments,
3543 Primitive::Type return_type,
3544 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003545 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003546 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003547 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003548 vtable_index_(vtable_index) {}
3549
Calin Juravle641547a2015-04-21 22:08:51 +01003550 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003551 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003552 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003553 }
3554
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003555 uint32_t GetVTableIndex() const { return vtable_index_; }
3556
3557 DECLARE_INSTRUCTION(InvokeVirtual);
3558
3559 private:
3560 const uint32_t vtable_index_;
3561
3562 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3563};
3564
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003565class HInvokeInterface : public HInvoke {
3566 public:
3567 HInvokeInterface(ArenaAllocator* arena,
3568 uint32_t number_of_arguments,
3569 Primitive::Type return_type,
3570 uint32_t dex_pc,
3571 uint32_t dex_method_index,
3572 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003573 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003574 imt_index_(imt_index) {}
3575
Calin Juravle641547a2015-04-21 22:08:51 +01003576 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003577 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003578 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003579 }
3580
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003581 uint32_t GetImtIndex() const { return imt_index_; }
3582 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3583
3584 DECLARE_INSTRUCTION(InvokeInterface);
3585
3586 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003587 const uint32_t imt_index_;
3588
3589 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3590};
3591
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003592class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003593 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003594 HNewInstance(HCurrentMethod* current_method,
3595 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003596 uint16_t type_index,
3597 const DexFile& dex_file,
Mingyao Yang7f43a3d2015-10-27 16:38:33 -07003598 bool can_throw,
3599 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003600 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003601 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003602 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003603 dex_file_(dex_file),
Mingyao Yang7f43a3d2015-10-27 16:38:33 -07003604 can_throw_(can_throw),
3605 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003606 entrypoint_(entrypoint) {
3607 SetRawInputAt(0, current_method);
3608 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003609
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003610 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003611 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003612
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003613 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003614 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yang7f43a3d2015-10-27 16:38:33 -07003615
3616 // It may throw when called on type that's not instantiable/accessible.
3617 bool CanThrow() const OVERRIDE { return can_throw_; }
3618
3619 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003620
Calin Juravle10e244f2015-01-26 18:54:32 +00003621 bool CanBeNull() const OVERRIDE { return false; }
3622
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003623 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3624
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003625 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003626
3627 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003628 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003629 const DexFile& dex_file_;
Mingyao Yang7f43a3d2015-10-27 16:38:33 -07003630 const bool can_throw_;
3631 const bool finalizable_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003632 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003633
3634 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3635};
3636
Roland Levillain88cb1752014-10-20 16:36:47 +01003637class HNeg : public HUnaryOperation {
3638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003639 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3640 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003641
Roland Levillain9867bc72015-08-05 10:21:34 +01003642 template <typename T> T Compute(T x) const { return -x; }
3643
3644 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003645 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003646 }
3647 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003648 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003649 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003650
Roland Levillain88cb1752014-10-20 16:36:47 +01003651 DECLARE_INSTRUCTION(Neg);
3652
3653 private:
3654 DISALLOW_COPY_AND_ASSIGN(HNeg);
3655};
3656
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003657class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003658 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003659 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003660 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003661 uint32_t dex_pc,
3662 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003663 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003664 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003665 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003666 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003667 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003668 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003669 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003670 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003671 }
3672
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003673 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003674 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003675
3676 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003677 bool NeedsEnvironment() const OVERRIDE { return true; }
3678
Mingyao Yang0c365e62015-03-31 15:09:29 -07003679 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3680 bool CanThrow() const OVERRIDE { return true; }
3681
Calin Juravle10e244f2015-01-26 18:54:32 +00003682 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003683
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003684 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3685
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003686 DECLARE_INSTRUCTION(NewArray);
3687
3688 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003689 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003690 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003691 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003692
3693 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3694};
3695
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003696class HAdd : public HBinaryOperation {
3697 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003698 HAdd(Primitive::Type result_type,
3699 HInstruction* left,
3700 HInstruction* right,
3701 uint32_t dex_pc = kNoDexPc)
3702 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003703
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003704 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003705
Roland Levillain9867bc72015-08-05 10:21:34 +01003706 template <typename T> T Compute(T x, T y) const { return x + y; }
3707
3708 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003709 return GetBlock()->GetGraph()->GetIntConstant(
3710 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003711 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003712 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003713 return GetBlock()->GetGraph()->GetLongConstant(
3714 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003715 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003716
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003717 DECLARE_INSTRUCTION(Add);
3718
3719 private:
3720 DISALLOW_COPY_AND_ASSIGN(HAdd);
3721};
3722
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003723class HSub : public HBinaryOperation {
3724 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003725 HSub(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) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003730
Roland Levillain9867bc72015-08-05 10:21:34 +01003731 template <typename T> T Compute(T x, T y) const { return x - y; }
3732
3733 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003734 return GetBlock()->GetGraph()->GetIntConstant(
3735 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003736 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003737 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003738 return GetBlock()->GetGraph()->GetLongConstant(
3739 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003740 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003741
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003742 DECLARE_INSTRUCTION(Sub);
3743
3744 private:
3745 DISALLOW_COPY_AND_ASSIGN(HSub);
3746};
3747
Calin Juravle34bacdf2014-10-07 20:23:36 +01003748class HMul : public HBinaryOperation {
3749 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003750 HMul(Primitive::Type result_type,
3751 HInstruction* left,
3752 HInstruction* right,
3753 uint32_t dex_pc = kNoDexPc)
3754 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003755
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003756 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003757
Roland Levillain9867bc72015-08-05 10:21:34 +01003758 template <typename T> T Compute(T x, T y) const { return x * y; }
3759
3760 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003761 return GetBlock()->GetGraph()->GetIntConstant(
3762 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003763 }
3764 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003765 return GetBlock()->GetGraph()->GetLongConstant(
3766 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003767 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003768
3769 DECLARE_INSTRUCTION(Mul);
3770
3771 private:
3772 DISALLOW_COPY_AND_ASSIGN(HMul);
3773};
3774
Calin Juravle7c4954d2014-10-28 16:57:40 +00003775class HDiv : public HBinaryOperation {
3776 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003777 HDiv(Primitive::Type result_type,
3778 HInstruction* left,
3779 HInstruction* right,
3780 uint32_t dex_pc)
3781 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003782
Roland Levillain9867bc72015-08-05 10:21:34 +01003783 template <typename T>
3784 T Compute(T x, T y) const {
3785 // Our graph structure ensures we never have 0 for `y` during
3786 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003787 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003788 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003789 return (y == -1) ? -x : x / y;
3790 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003791
Roland Levillain9867bc72015-08-05 10:21:34 +01003792 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003793 return GetBlock()->GetGraph()->GetIntConstant(
3794 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003795 }
3796 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003797 return GetBlock()->GetGraph()->GetLongConstant(
3798 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003799 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003800
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003801 static SideEffects SideEffectsForArchRuntimeCalls() {
3802 // The generated code can use a runtime call.
3803 return SideEffects::CanTriggerGC();
3804 }
3805
Calin Juravle7c4954d2014-10-28 16:57:40 +00003806 DECLARE_INSTRUCTION(Div);
3807
3808 private:
3809 DISALLOW_COPY_AND_ASSIGN(HDiv);
3810};
3811
Calin Juravlebacfec32014-11-14 15:54:36 +00003812class HRem : public HBinaryOperation {
3813 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003814 HRem(Primitive::Type result_type,
3815 HInstruction* left,
3816 HInstruction* right,
3817 uint32_t dex_pc)
3818 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003819
Roland Levillain9867bc72015-08-05 10:21:34 +01003820 template <typename T>
3821 T Compute(T x, T y) const {
3822 // Our graph structure ensures we never have 0 for `y` during
3823 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003824 DCHECK_NE(y, 0);
3825 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3826 return (y == -1) ? 0 : x % y;
3827 }
3828
Roland Levillain9867bc72015-08-05 10:21:34 +01003829 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003830 return GetBlock()->GetGraph()->GetIntConstant(
3831 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003832 }
3833 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003834 return GetBlock()->GetGraph()->GetLongConstant(
3835 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003836 }
3837
Calin Juravlebacfec32014-11-14 15:54:36 +00003838
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003839 static SideEffects SideEffectsForArchRuntimeCalls() {
3840 return SideEffects::CanTriggerGC();
3841 }
3842
Calin Juravlebacfec32014-11-14 15:54:36 +00003843 DECLARE_INSTRUCTION(Rem);
3844
3845 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003846 DISALLOW_COPY_AND_ASSIGN(HRem);
3847};
3848
Calin Juravled0d48522014-11-04 16:40:20 +00003849class HDivZeroCheck : public HExpression<1> {
3850 public:
3851 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003852 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003853 SetRawInputAt(0, value);
3854 }
3855
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003856 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3857
Calin Juravled0d48522014-11-04 16:40:20 +00003858 bool CanBeMoved() const OVERRIDE { return true; }
3859
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003860 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003861 return true;
3862 }
3863
3864 bool NeedsEnvironment() const OVERRIDE { return true; }
3865 bool CanThrow() const OVERRIDE { return true; }
3866
Calin Juravled0d48522014-11-04 16:40:20 +00003867 DECLARE_INSTRUCTION(DivZeroCheck);
3868
3869 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003870 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3871};
3872
Calin Juravle9aec02f2014-11-18 23:06:35 +00003873class HShl : public HBinaryOperation {
3874 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003875 HShl(Primitive::Type result_type,
3876 HInstruction* left,
3877 HInstruction* right,
3878 uint32_t dex_pc = kNoDexPc)
3879 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003880
Roland Levillain9867bc72015-08-05 10:21:34 +01003881 template <typename T, typename U, typename V>
3882 T Compute(T x, U y, V max_shift_value) const {
3883 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3884 "V is not the unsigned integer type corresponding to T");
3885 return x << (y & max_shift_value);
3886 }
3887
3888 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3889 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003890 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003891 }
3892 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3893 // case is handled as `x << static_cast<int>(y)`.
3894 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3895 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003896 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003897 }
3898 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3899 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003900 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003901 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003902
3903 DECLARE_INSTRUCTION(Shl);
3904
3905 private:
3906 DISALLOW_COPY_AND_ASSIGN(HShl);
3907};
3908
3909class HShr : public HBinaryOperation {
3910 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003911 HShr(Primitive::Type result_type,
3912 HInstruction* left,
3913 HInstruction* right,
3914 uint32_t dex_pc = kNoDexPc)
3915 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003916
Roland Levillain9867bc72015-08-05 10:21:34 +01003917 template <typename T, typename U, typename V>
3918 T Compute(T x, U y, V max_shift_value) const {
3919 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3920 "V is not the unsigned integer type corresponding to T");
3921 return x >> (y & max_shift_value);
3922 }
3923
3924 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3925 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003926 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003927 }
3928 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3929 // case is handled as `x >> static_cast<int>(y)`.
3930 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3931 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003932 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003933 }
3934 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3935 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003936 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003937 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003938
3939 DECLARE_INSTRUCTION(Shr);
3940
3941 private:
3942 DISALLOW_COPY_AND_ASSIGN(HShr);
3943};
3944
3945class HUShr : public HBinaryOperation {
3946 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003947 HUShr(Primitive::Type result_type,
3948 HInstruction* left,
3949 HInstruction* right,
3950 uint32_t dex_pc = kNoDexPc)
3951 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003952
Roland Levillain9867bc72015-08-05 10:21:34 +01003953 template <typename T, typename U, typename V>
3954 T Compute(T x, U y, V max_shift_value) const {
3955 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3956 "V is not the unsigned integer type corresponding to T");
3957 V ux = static_cast<V>(x);
3958 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003959 }
3960
Roland Levillain9867bc72015-08-05 10:21:34 +01003961 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3962 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003963 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003964 }
3965 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3966 // case is handled as `x >>> static_cast<int>(y)`.
3967 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3968 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003969 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003970 }
3971 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3972 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003973 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003974 }
3975
3976 DECLARE_INSTRUCTION(UShr);
3977
3978 private:
3979 DISALLOW_COPY_AND_ASSIGN(HUShr);
3980};
3981
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003982class HAnd : public HBinaryOperation {
3983 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003984 HAnd(Primitive::Type result_type,
3985 HInstruction* left,
3986 HInstruction* right,
3987 uint32_t dex_pc = kNoDexPc)
3988 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003989
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003990 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003991
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 template <typename T, typename U>
3993 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3994
3995 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003996 return GetBlock()->GetGraph()->GetIntConstant(
3997 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003998 }
3999 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004000 return GetBlock()->GetGraph()->GetLongConstant(
4001 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004002 }
4003 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004004 return GetBlock()->GetGraph()->GetLongConstant(
4005 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004006 }
4007 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004008 return GetBlock()->GetGraph()->GetLongConstant(
4009 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004010 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004011
4012 DECLARE_INSTRUCTION(And);
4013
4014 private:
4015 DISALLOW_COPY_AND_ASSIGN(HAnd);
4016};
4017
4018class HOr : public HBinaryOperation {
4019 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004020 HOr(Primitive::Type result_type,
4021 HInstruction* left,
4022 HInstruction* right,
4023 uint32_t dex_pc = kNoDexPc)
4024 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004025
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004026 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004027
Roland Levillain9867bc72015-08-05 10:21:34 +01004028 template <typename T, typename U>
4029 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4030
4031 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004032 return GetBlock()->GetGraph()->GetIntConstant(
4033 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004034 }
4035 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004036 return GetBlock()->GetGraph()->GetLongConstant(
4037 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004038 }
4039 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004040 return GetBlock()->GetGraph()->GetLongConstant(
4041 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004042 }
4043 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004044 return GetBlock()->GetGraph()->GetLongConstant(
4045 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004046 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004047
4048 DECLARE_INSTRUCTION(Or);
4049
4050 private:
4051 DISALLOW_COPY_AND_ASSIGN(HOr);
4052};
4053
4054class HXor : public HBinaryOperation {
4055 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004056 HXor(Primitive::Type result_type,
4057 HInstruction* left,
4058 HInstruction* right,
4059 uint32_t dex_pc = kNoDexPc)
4060 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004061
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004062 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004063
Roland Levillain9867bc72015-08-05 10:21:34 +01004064 template <typename T, typename U>
4065 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4066
4067 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004068 return GetBlock()->GetGraph()->GetIntConstant(
4069 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004070 }
4071 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004072 return GetBlock()->GetGraph()->GetLongConstant(
4073 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004074 }
4075 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004076 return GetBlock()->GetGraph()->GetLongConstant(
4077 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004078 }
4079 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004080 return GetBlock()->GetGraph()->GetLongConstant(
4081 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004082 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004083
4084 DECLARE_INSTRUCTION(Xor);
4085
4086 private:
4087 DISALLOW_COPY_AND_ASSIGN(HXor);
4088};
4089
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004090// The value of a parameter in this method. Its location depends on
4091// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004092class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004093 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004094 HParameterValue(const DexFile& dex_file,
4095 uint16_t type_index,
4096 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004097 Primitive::Type parameter_type,
4098 bool is_this = false)
4099 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004100 dex_file_(dex_file),
4101 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004102 index_(index),
4103 is_this_(is_this),
4104 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004105
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004106 const DexFile& GetDexFile() const { return dex_file_; }
4107 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004108 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004109 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004110
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004111 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4112 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004113
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004114 DECLARE_INSTRUCTION(ParameterValue);
4115
4116 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004117 const DexFile& dex_file_;
4118 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004119 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004120 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004121 const uint8_t index_;
4122
Calin Juravle10e244f2015-01-26 18:54:32 +00004123 // Whether or not the parameter value corresponds to 'this' argument.
4124 const bool is_this_;
4125
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004126 bool can_be_null_;
4127
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004128 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4129};
4130
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004131class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004132 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004133 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4134 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004135
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004136 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004137 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004138 return true;
4139 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004140
Roland Levillain9867bc72015-08-05 10:21:34 +01004141 template <typename T> T Compute(T x) const { return ~x; }
4142
4143 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004144 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004145 }
4146 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004147 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004148 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004149
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004150 DECLARE_INSTRUCTION(Not);
4151
4152 private:
4153 DISALLOW_COPY_AND_ASSIGN(HNot);
4154};
4155
David Brazdil66d126e2015-04-03 16:02:44 +01004156class HBooleanNot : public HUnaryOperation {
4157 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004158 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4159 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004160
4161 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004162 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004163 return true;
4164 }
4165
Roland Levillain9867bc72015-08-05 10:21:34 +01004166 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004167 DCHECK(IsUint<1>(x));
4168 return !x;
4169 }
4170
Roland Levillain9867bc72015-08-05 10:21:34 +01004171 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004172 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004173 }
4174 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4175 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004176 UNREACHABLE();
4177 }
4178
4179 DECLARE_INSTRUCTION(BooleanNot);
4180
4181 private:
4182 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4183};
4184
Roland Levillaindff1f282014-11-05 14:15:05 +00004185class HTypeConversion : public HExpression<1> {
4186 public:
4187 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004188 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004189 : HExpression(result_type,
4190 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4191 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004192 SetRawInputAt(0, input);
4193 DCHECK_NE(input->GetType(), result_type);
4194 }
4195
4196 HInstruction* GetInput() const { return InputAt(0); }
4197 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4198 Primitive::Type GetResultType() const { return GetType(); }
4199
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004200 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004201 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004202
Roland Levillaindff1f282014-11-05 14:15:05 +00004203 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004204 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004205
Mark Mendelle82549b2015-05-06 10:55:34 -04004206 // Try to statically evaluate the conversion and return a HConstant
4207 // containing the result. If the input cannot be converted, return nullptr.
4208 HConstant* TryStaticEvaluation() const;
4209
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004210 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4211 Primitive::Type result_type) {
4212 // Some architectures may not require the 'GC' side effects, but at this point
4213 // in the compilation process we do not know what architecture we will
4214 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004215 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4216 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004217 return SideEffects::CanTriggerGC();
4218 }
4219 return SideEffects::None();
4220 }
4221
Roland Levillaindff1f282014-11-05 14:15:05 +00004222 DECLARE_INSTRUCTION(TypeConversion);
4223
4224 private:
4225 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4226};
4227
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004228static constexpr uint32_t kNoRegNumber = -1;
4229
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004230class HPhi : public HInstruction {
4231 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004232 HPhi(ArenaAllocator* arena,
4233 uint32_t reg_number,
4234 size_t number_of_inputs,
4235 Primitive::Type type,
4236 uint32_t dex_pc = kNoDexPc)
4237 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004238 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004239 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004240 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004241 is_live_(false),
4242 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004243 }
4244
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004245 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4246 static Primitive::Type ToPhiType(Primitive::Type type) {
4247 switch (type) {
4248 case Primitive::kPrimBoolean:
4249 case Primitive::kPrimByte:
4250 case Primitive::kPrimShort:
4251 case Primitive::kPrimChar:
4252 return Primitive::kPrimInt;
4253 default:
4254 return type;
4255 }
4256 }
4257
David Brazdilffee3d32015-07-06 11:48:53 +01004258 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4259
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004260 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004261
4262 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004263 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004264
Calin Juravle10e244f2015-01-26 18:54:32 +00004265 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004266 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004267
Calin Juravle10e244f2015-01-26 18:54:32 +00004268 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4269 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4270
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004271 uint32_t GetRegNumber() const { return reg_number_; }
4272
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004273 void SetDead() { is_live_ = false; }
4274 void SetLive() { is_live_ = true; }
4275 bool IsDead() const { return !is_live_; }
4276 bool IsLive() const { return is_live_; }
4277
David Brazdil77a48ae2015-09-15 12:34:04 +00004278 bool IsVRegEquivalentOf(HInstruction* other) const {
4279 return other != nullptr
4280 && other->IsPhi()
4281 && other->AsPhi()->GetBlock() == GetBlock()
4282 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4283 }
4284
Calin Juravlea4f88312015-04-16 12:57:19 +01004285 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4286 // An equivalent phi is a phi having the same dex register and type.
4287 // It assumes that phis with the same dex register are adjacent.
4288 HPhi* GetNextEquivalentPhiWithSameType() {
4289 HInstruction* next = GetNext();
4290 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4291 if (next->GetType() == GetType()) {
4292 return next->AsPhi();
4293 }
4294 next = next->GetNext();
4295 }
4296 return nullptr;
4297 }
4298
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004299 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004300
David Brazdil1abb4192015-02-17 18:33:36 +00004301 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004302 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004303 return inputs_[index];
4304 }
David Brazdil1abb4192015-02-17 18:33:36 +00004305
4306 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004307 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004308 }
4309
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004310 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004311 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004312 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004313 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004314 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004315 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004316
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004317 DISALLOW_COPY_AND_ASSIGN(HPhi);
4318};
4319
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004320class HNullCheck : public HExpression<1> {
4321 public:
4322 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004323 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004324 SetRawInputAt(0, value);
4325 }
4326
Calin Juravle10e244f2015-01-26 18:54:32 +00004327 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004328 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004329 return true;
4330 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004331
Calin Juravle10e244f2015-01-26 18:54:32 +00004332 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004333
Calin Juravle10e244f2015-01-26 18:54:32 +00004334 bool CanThrow() const OVERRIDE { return true; }
4335
4336 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004337
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004338
4339 DECLARE_INSTRUCTION(NullCheck);
4340
4341 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004342 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4343};
4344
4345class FieldInfo : public ValueObject {
4346 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004347 FieldInfo(MemberOffset field_offset,
4348 Primitive::Type field_type,
4349 bool is_volatile,
4350 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004351 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004352 const DexFile& dex_file,
4353 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004354 : field_offset_(field_offset),
4355 field_type_(field_type),
4356 is_volatile_(is_volatile),
4357 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004358 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004359 dex_file_(dex_file),
4360 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004361
4362 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004363 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004364 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004365 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004366 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004367 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004368 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004369
4370 private:
4371 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004372 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004373 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004374 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004375 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004376 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004377 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004378};
4379
4380class HInstanceFieldGet : public HExpression<1> {
4381 public:
4382 HInstanceFieldGet(HInstruction* value,
4383 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004384 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004385 bool is_volatile,
4386 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004387 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004388 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004389 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004390 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004391 : HExpression(field_type,
4392 SideEffects::FieldReadOfType(field_type, is_volatile),
4393 dex_pc),
4394 field_info_(field_offset,
4395 field_type,
4396 is_volatile,
4397 field_idx,
4398 declaring_class_def_index,
4399 dex_file,
4400 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004401 SetRawInputAt(0, value);
4402 }
4403
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004404 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004405
4406 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4407 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4408 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004409 }
4410
Calin Juravle641547a2015-04-21 22:08:51 +01004411 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4412 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004413 }
4414
4415 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004416 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4417 }
4418
Calin Juravle52c48962014-12-16 17:02:57 +00004419 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004420 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004421 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004422 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004423
4424 DECLARE_INSTRUCTION(InstanceFieldGet);
4425
4426 private:
4427 const FieldInfo field_info_;
4428
4429 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4430};
4431
4432class HInstanceFieldSet : public HTemplateInstruction<2> {
4433 public:
4434 HInstanceFieldSet(HInstruction* object,
4435 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004436 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004437 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004438 bool is_volatile,
4439 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004440 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004441 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004442 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004443 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004444 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4445 dex_pc),
4446 field_info_(field_offset,
4447 field_type,
4448 is_volatile,
4449 field_idx,
4450 declaring_class_def_index,
4451 dex_file,
4452 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004453 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004454 SetRawInputAt(0, object);
4455 SetRawInputAt(1, value);
4456 }
4457
Calin Juravle641547a2015-04-21 22:08:51 +01004458 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4459 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004460 }
4461
Calin Juravle52c48962014-12-16 17:02:57 +00004462 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004463 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004464 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004465 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004466 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004467 bool GetValueCanBeNull() const { return value_can_be_null_; }
4468 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004469
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004470 DECLARE_INSTRUCTION(InstanceFieldSet);
4471
4472 private:
4473 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004474 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004475
4476 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4477};
4478
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004479class HArrayGet : public HExpression<2> {
4480 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004481 HArrayGet(HInstruction* array,
4482 HInstruction* index,
4483 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004484 uint32_t dex_pc,
4485 SideEffects additional_side_effects = SideEffects::None())
4486 : HExpression(type,
4487 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
4488 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004489 SetRawInputAt(0, array);
4490 SetRawInputAt(1, index);
4491 }
4492
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004493 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004494 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004495 return true;
4496 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004497 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004498 // TODO: We can be smarter here.
4499 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4500 // which generates the implicit null check. There are cases when these can be removed
4501 // to produce better code. If we ever add optimizations to do so we should allow an
4502 // implicit check here (as long as the address falls in the first page).
4503 return false;
4504 }
4505
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004506 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004507
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004508 HInstruction* GetArray() const { return InputAt(0); }
4509 HInstruction* GetIndex() const { return InputAt(1); }
4510
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004511 DECLARE_INSTRUCTION(ArrayGet);
4512
4513 private:
4514 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4515};
4516
4517class HArraySet : public HTemplateInstruction<3> {
4518 public:
4519 HArraySet(HInstruction* array,
4520 HInstruction* index,
4521 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004522 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004523 uint32_t dex_pc,
4524 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004525 : HTemplateInstruction(
4526 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004527 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4528 additional_side_effects),
4529 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004530 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004531 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004532 value_can_be_null_(true),
4533 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004534 SetRawInputAt(0, array);
4535 SetRawInputAt(1, index);
4536 SetRawInputAt(2, value);
4537 }
4538
Calin Juravle77520bc2015-01-12 18:45:46 +00004539 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004540 // We currently always call a runtime method to catch array store
4541 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004542 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004543 }
4544
Mingyao Yang81014cb2015-06-02 03:16:27 -07004545 // Can throw ArrayStoreException.
4546 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4547
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004548 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004549 // TODO: Same as for ArrayGet.
4550 return false;
4551 }
4552
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004553 void ClearNeedsTypeCheck() {
4554 needs_type_check_ = false;
4555 }
4556
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004557 void ClearValueCanBeNull() {
4558 value_can_be_null_ = false;
4559 }
4560
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004561 void SetStaticTypeOfArrayIsObjectArray() {
4562 static_type_of_array_is_object_array_ = true;
4563 }
4564
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004565 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004566 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004567 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004568
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004569 HInstruction* GetArray() const { return InputAt(0); }
4570 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004571 HInstruction* GetValue() const { return InputAt(2); }
4572
4573 Primitive::Type GetComponentType() const {
4574 // The Dex format does not type floating point index operations. Since the
4575 // `expected_component_type_` is set during building and can therefore not
4576 // be correct, we also check what is the value type. If it is a floating
4577 // point type, we must use that type.
4578 Primitive::Type value_type = GetValue()->GetType();
4579 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4580 ? value_type
4581 : expected_component_type_;
4582 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004583
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004584 Primitive::Type GetRawExpectedComponentType() const {
4585 return expected_component_type_;
4586 }
4587
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004588 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4589 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4590 }
4591
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004592 DECLARE_INSTRUCTION(ArraySet);
4593
4594 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004595 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004596 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004597 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004598 // Cached information for the reference_type_info_ so that codegen
4599 // does not need to inspect the static type.
4600 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004601
4602 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4603};
4604
4605class HArrayLength : public HExpression<1> {
4606 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004607 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004608 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004609 // Note that arrays do not change length, so the instruction does not
4610 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004611 SetRawInputAt(0, array);
4612 }
4613
Calin Juravle77520bc2015-01-12 18:45:46 +00004614 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004615 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004616 return true;
4617 }
Calin Juravle641547a2015-04-21 22:08:51 +01004618 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4619 return obj == InputAt(0);
4620 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004621
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004622 DECLARE_INSTRUCTION(ArrayLength);
4623
4624 private:
4625 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4626};
4627
4628class HBoundsCheck : public HExpression<2> {
4629 public:
4630 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004631 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004632 DCHECK(index->GetType() == Primitive::kPrimInt);
4633 SetRawInputAt(0, index);
4634 SetRawInputAt(1, length);
4635 }
4636
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004637 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004638 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004639 return true;
4640 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004641
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004642 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004643
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004644 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004645
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004646 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004647
4648 DECLARE_INSTRUCTION(BoundsCheck);
4649
4650 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004651 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4652};
4653
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004654/**
4655 * Some DEX instructions are folded into multiple HInstructions that need
4656 * to stay live until the last HInstruction. This class
4657 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004658 * HInstruction stays live. `index` represents the stack location index of the
4659 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004660 */
4661class HTemporary : public HTemplateInstruction<0> {
4662 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004663 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4664 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004665
4666 size_t GetIndex() const { return index_; }
4667
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004668 Primitive::Type GetType() const OVERRIDE {
4669 // The previous instruction is the one that will be stored in the temporary location.
4670 DCHECK(GetPrevious() != nullptr);
4671 return GetPrevious()->GetType();
4672 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004673
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004674 DECLARE_INSTRUCTION(Temporary);
4675
4676 private:
4677 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004678 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4679};
4680
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004681class HSuspendCheck : public HTemplateInstruction<0> {
4682 public:
4683 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004684 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004685
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004686 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004687 return true;
4688 }
4689
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004690 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4691 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004692
4693 DECLARE_INSTRUCTION(SuspendCheck);
4694
4695 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004696 // Only used for code generation, in order to share the same slow path between back edges
4697 // of a same loop.
4698 SlowPathCode* slow_path_;
4699
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004700 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4701};
4702
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004703/**
4704 * Instruction to load a Class object.
4705 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004706class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004707 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004708 HLoadClass(HCurrentMethod* current_method,
4709 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004710 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004711 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004712 uint32_t dex_pc,
4713 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004714 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004715 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004716 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004717 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004718 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004719 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004720 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004721 // Referrers class should not need access check. We never inline unverified
4722 // methods so we can't possibly end up in this situation.
4723 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004724 SetRawInputAt(0, current_method);
4725 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004726
4727 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004728
4729 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004730 // Note that we don't need to test for generate_clinit_check_.
4731 // Whether or not we need to generate the clinit check is processed in
4732 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004733 return other->AsLoadClass()->type_index_ == type_index_ &&
4734 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004735 }
4736
4737 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4738
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004739 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004740 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004741 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004742
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004743 bool NeedsEnvironment() const OVERRIDE {
4744 // Will call runtime and load the class if the class is not loaded yet.
4745 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004746 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004747 }
4748
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004749 bool MustGenerateClinitCheck() const {
4750 return generate_clinit_check_;
4751 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004752 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4753 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004754 }
4755
4756 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004757 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4758 }
4759
4760 bool NeedsAccessCheck() const {
4761 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004762 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004763
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004764 bool CanThrow() const OVERRIDE {
4765 // May call runtime and and therefore can throw.
4766 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004767 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004768 }
4769
Calin Juravleacf735c2015-02-12 15:25:22 +00004770 ReferenceTypeInfo GetLoadedClassRTI() {
4771 return loaded_class_rti_;
4772 }
4773
4774 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4775 // Make sure we only set exact types (the loaded class should never be merged).
4776 DCHECK(rti.IsExact());
4777 loaded_class_rti_ = rti;
4778 }
4779
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004780 const DexFile& GetDexFile() { return dex_file_; }
4781
Vladimir Markodc151b22015-10-15 18:02:30 +01004782 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004783
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004784 static SideEffects SideEffectsForArchRuntimeCalls() {
4785 return SideEffects::CanTriggerGC();
4786 }
4787
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004788 DECLARE_INSTRUCTION(LoadClass);
4789
4790 private:
4791 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004792 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004793 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004794 // Whether this instruction must generate the initialization check.
4795 // Used for code generation.
4796 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004797 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004798
Calin Juravleacf735c2015-02-12 15:25:22 +00004799 ReferenceTypeInfo loaded_class_rti_;
4800
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004801 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4802};
4803
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004804class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004805 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004806 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004807 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4808 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004809 SetRawInputAt(0, current_method);
4810 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004811
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004812 bool CanBeMoved() const OVERRIDE { return true; }
4813
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004814 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4815 return other->AsLoadString()->string_index_ == string_index_;
4816 }
4817
4818 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4819
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004820 uint32_t GetStringIndex() const { return string_index_; }
4821
4822 // TODO: Can we deopt or debug when we resolve a string?
4823 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004824 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004825 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004826
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004827 static SideEffects SideEffectsForArchRuntimeCalls() {
4828 return SideEffects::CanTriggerGC();
4829 }
4830
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004831 DECLARE_INSTRUCTION(LoadString);
4832
4833 private:
4834 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004835
4836 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4837};
4838
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004839/**
4840 * Performs an initialization check on its Class object input.
4841 */
4842class HClinitCheck : public HExpression<1> {
4843 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004844 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004845 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004846 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004847 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4848 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004849 SetRawInputAt(0, constant);
4850 }
4851
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004852 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004853 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004854 return true;
4855 }
4856
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004857 bool NeedsEnvironment() const OVERRIDE {
4858 // May call runtime to initialize the class.
4859 return true;
4860 }
4861
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004862
4863 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4864
4865 DECLARE_INSTRUCTION(ClinitCheck);
4866
4867 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004868 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4869};
4870
4871class HStaticFieldGet : public HExpression<1> {
4872 public:
4873 HStaticFieldGet(HInstruction* cls,
4874 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004875 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004876 bool is_volatile,
4877 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004878 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004879 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004880 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004881 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004882 : HExpression(field_type,
4883 SideEffects::FieldReadOfType(field_type, is_volatile),
4884 dex_pc),
4885 field_info_(field_offset,
4886 field_type,
4887 is_volatile,
4888 field_idx,
4889 declaring_class_def_index,
4890 dex_file,
4891 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004892 SetRawInputAt(0, cls);
4893 }
4894
Calin Juravle52c48962014-12-16 17:02:57 +00004895
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004896 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004897
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004898 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004899 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4900 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004901 }
4902
4903 size_t ComputeHashCode() const OVERRIDE {
4904 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4905 }
4906
Calin Juravle52c48962014-12-16 17:02:57 +00004907 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004908 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4909 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004910 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004911
4912 DECLARE_INSTRUCTION(StaticFieldGet);
4913
4914 private:
4915 const FieldInfo field_info_;
4916
4917 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4918};
4919
4920class HStaticFieldSet : public HTemplateInstruction<2> {
4921 public:
4922 HStaticFieldSet(HInstruction* cls,
4923 HInstruction* value,
4924 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004925 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004926 bool is_volatile,
4927 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004928 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004929 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004930 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004931 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004932 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4933 dex_pc),
4934 field_info_(field_offset,
4935 field_type,
4936 is_volatile,
4937 field_idx,
4938 declaring_class_def_index,
4939 dex_file,
4940 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004941 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004942 SetRawInputAt(0, cls);
4943 SetRawInputAt(1, value);
4944 }
4945
Calin Juravle52c48962014-12-16 17:02:57 +00004946 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004947 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4948 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004949 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004950
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004951 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004952 bool GetValueCanBeNull() const { return value_can_be_null_; }
4953 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004954
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004955 DECLARE_INSTRUCTION(StaticFieldSet);
4956
4957 private:
4958 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004959 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004960
4961 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4962};
4963
Calin Juravlee460d1d2015-09-29 04:52:17 +01004964class HUnresolvedInstanceFieldGet : public HExpression<1> {
4965 public:
4966 HUnresolvedInstanceFieldGet(HInstruction* obj,
4967 Primitive::Type field_type,
4968 uint32_t field_index,
4969 uint32_t dex_pc)
4970 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4971 field_index_(field_index) {
4972 SetRawInputAt(0, obj);
4973 }
4974
4975 bool NeedsEnvironment() const OVERRIDE { return true; }
4976 bool CanThrow() const OVERRIDE { return true; }
4977
4978 Primitive::Type GetFieldType() const { return GetType(); }
4979 uint32_t GetFieldIndex() const { return field_index_; }
4980
4981 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4982
4983 private:
4984 const uint32_t field_index_;
4985
4986 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4987};
4988
4989class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4990 public:
4991 HUnresolvedInstanceFieldSet(HInstruction* obj,
4992 HInstruction* value,
4993 Primitive::Type field_type,
4994 uint32_t field_index,
4995 uint32_t dex_pc)
4996 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4997 field_type_(field_type),
4998 field_index_(field_index) {
4999 DCHECK_EQ(field_type, value->GetType());
5000 SetRawInputAt(0, obj);
5001 SetRawInputAt(1, value);
5002 }
5003
5004 bool NeedsEnvironment() const OVERRIDE { return true; }
5005 bool CanThrow() const OVERRIDE { return true; }
5006
5007 Primitive::Type GetFieldType() const { return field_type_; }
5008 uint32_t GetFieldIndex() const { return field_index_; }
5009
5010 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5011
5012 private:
5013 const Primitive::Type field_type_;
5014 const uint32_t field_index_;
5015
5016 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5017};
5018
5019class HUnresolvedStaticFieldGet : public HExpression<0> {
5020 public:
5021 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5022 uint32_t field_index,
5023 uint32_t dex_pc)
5024 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5025 field_index_(field_index) {
5026 }
5027
5028 bool NeedsEnvironment() const OVERRIDE { return true; }
5029 bool CanThrow() const OVERRIDE { return true; }
5030
5031 Primitive::Type GetFieldType() const { return GetType(); }
5032 uint32_t GetFieldIndex() const { return field_index_; }
5033
5034 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5035
5036 private:
5037 const uint32_t field_index_;
5038
5039 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5040};
5041
5042class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5043 public:
5044 HUnresolvedStaticFieldSet(HInstruction* value,
5045 Primitive::Type field_type,
5046 uint32_t field_index,
5047 uint32_t dex_pc)
5048 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5049 field_type_(field_type),
5050 field_index_(field_index) {
5051 DCHECK_EQ(field_type, value->GetType());
5052 SetRawInputAt(0, value);
5053 }
5054
5055 bool NeedsEnvironment() const OVERRIDE { return true; }
5056 bool CanThrow() const OVERRIDE { return true; }
5057
5058 Primitive::Type GetFieldType() const { return field_type_; }
5059 uint32_t GetFieldIndex() const { return field_index_; }
5060
5061 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5062
5063 private:
5064 const Primitive::Type field_type_;
5065 const uint32_t field_index_;
5066
5067 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5068};
5069
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005070// Implement the move-exception DEX instruction.
5071class HLoadException : public HExpression<0> {
5072 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005073 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5074 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005075
David Brazdilbbd733e2015-08-18 17:48:17 +01005076 bool CanBeNull() const OVERRIDE { return false; }
5077
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005078 DECLARE_INSTRUCTION(LoadException);
5079
5080 private:
5081 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5082};
5083
David Brazdilcb1c0552015-08-04 16:22:25 +01005084// Implicit part of move-exception which clears thread-local exception storage.
5085// Must not be removed because the runtime expects the TLS to get cleared.
5086class HClearException : public HTemplateInstruction<0> {
5087 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005088 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5089 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005090
5091 DECLARE_INSTRUCTION(ClearException);
5092
5093 private:
5094 DISALLOW_COPY_AND_ASSIGN(HClearException);
5095};
5096
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005097class HThrow : public HTemplateInstruction<1> {
5098 public:
5099 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005100 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005101 SetRawInputAt(0, exception);
5102 }
5103
5104 bool IsControlFlow() const OVERRIDE { return true; }
5105
5106 bool NeedsEnvironment() const OVERRIDE { return true; }
5107
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005108 bool CanThrow() const OVERRIDE { return true; }
5109
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005110
5111 DECLARE_INSTRUCTION(Throw);
5112
5113 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005114 DISALLOW_COPY_AND_ASSIGN(HThrow);
5115};
5116
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005117/**
5118 * Implementation strategies for the code generator of a HInstanceOf
5119 * or `HCheckCast`.
5120 */
5121enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005122 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005123 kExactCheck, // Can do a single class compare.
5124 kClassHierarchyCheck, // Can just walk the super class chain.
5125 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5126 kInterfaceCheck, // No optimization yet when checking against an interface.
5127 kArrayObjectCheck, // Can just check if the array is not primitive.
5128 kArrayCheck // No optimization yet when checking against a generic array.
5129};
5130
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005131class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005132 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005133 HInstanceOf(HInstruction* object,
5134 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005135 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005136 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005137 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005138 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005139 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005140 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005141 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005142 SetRawInputAt(0, object);
5143 SetRawInputAt(1, constant);
5144 }
5145
5146 bool CanBeMoved() const OVERRIDE { return true; }
5147
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005148 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005149 return true;
5150 }
5151
5152 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005153 return false;
5154 }
5155
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005156 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5157
5158 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005159
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005160 // Used only in code generation.
5161 bool MustDoNullCheck() const { return must_do_null_check_; }
5162 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5163
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005164 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5165 return (check_kind == TypeCheckKind::kExactCheck)
5166 ? SideEffects::None()
5167 // Mips currently does runtime calls for any other checks.
5168 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005169 }
5170
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005171 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005172
5173 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005174 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005175 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005176
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005177 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5178};
5179
Calin Juravleb1498f62015-02-16 13:13:29 +00005180class HBoundType : public HExpression<1> {
5181 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005182 // Constructs an HBoundType with the given upper_bound.
5183 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005184 HBoundType(HInstruction* input,
5185 ReferenceTypeInfo upper_bound,
5186 bool upper_can_be_null,
5187 uint32_t dex_pc = kNoDexPc)
5188 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005189 upper_bound_(upper_bound),
5190 upper_can_be_null_(upper_can_be_null),
5191 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005192 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005193 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005194 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005195 }
5196
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005197 // GetUpper* should only be used in reference type propagation.
5198 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5199 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005200
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005201 void SetCanBeNull(bool can_be_null) {
5202 DCHECK(upper_can_be_null_ || !can_be_null);
5203 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005204 }
5205
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005206 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5207
Calin Juravleb1498f62015-02-16 13:13:29 +00005208 DECLARE_INSTRUCTION(BoundType);
5209
5210 private:
5211 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005212 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5213 // It is used to bound the type in cases like:
5214 // if (x instanceof ClassX) {
5215 // // uper_bound_ will be ClassX
5216 // }
5217 const ReferenceTypeInfo upper_bound_;
5218 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5219 // is false then can_be_null_ cannot be true).
5220 const bool upper_can_be_null_;
5221 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005222
5223 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5224};
5225
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005226class HCheckCast : public HTemplateInstruction<2> {
5227 public:
5228 HCheckCast(HInstruction* object,
5229 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005230 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005231 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005232 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005233 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005234 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005235 SetRawInputAt(0, object);
5236 SetRawInputAt(1, constant);
5237 }
5238
5239 bool CanBeMoved() const OVERRIDE { return true; }
5240
5241 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5242 return true;
5243 }
5244
5245 bool NeedsEnvironment() const OVERRIDE {
5246 // Instruction may throw a CheckCastError.
5247 return true;
5248 }
5249
5250 bool CanThrow() const OVERRIDE { return true; }
5251
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005252 bool MustDoNullCheck() const { return must_do_null_check_; }
5253 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005254 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005255
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005256 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005257
5258 DECLARE_INSTRUCTION(CheckCast);
5259
5260 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005261 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005262 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005263
5264 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005265};
5266
Calin Juravle27df7582015-04-17 19:12:31 +01005267class HMemoryBarrier : public HTemplateInstruction<0> {
5268 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005269 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005270 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005271 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005272 barrier_kind_(barrier_kind) {}
5273
5274 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5275
5276 DECLARE_INSTRUCTION(MemoryBarrier);
5277
5278 private:
5279 const MemBarrierKind barrier_kind_;
5280
5281 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5282};
5283
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005284class HMonitorOperation : public HTemplateInstruction<1> {
5285 public:
5286 enum OperationKind {
5287 kEnter,
5288 kExit,
5289 };
5290
5291 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005292 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005293 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5294 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005295 SetRawInputAt(0, object);
5296 }
5297
5298 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005299 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5300
5301 bool CanThrow() const OVERRIDE {
5302 // Verifier guarantees that monitor-exit cannot throw.
5303 // This is important because it allows the HGraphBuilder to remove
5304 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5305 return IsEnter();
5306 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005307
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005308
5309 bool IsEnter() const { return kind_ == kEnter; }
5310
5311 DECLARE_INSTRUCTION(MonitorOperation);
5312
Calin Juravle52c48962014-12-16 17:02:57 +00005313 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005314 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005315
5316 private:
5317 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5318};
5319
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005320/**
5321 * A HInstruction used as a marker for the replacement of new + <init>
5322 * of a String to a call to a StringFactory. Only baseline will see
5323 * the node at code generation, where it will be be treated as null.
5324 * When compiling non-baseline, `HFakeString` instructions are being removed
5325 * in the instruction simplifier.
5326 */
5327class HFakeString : public HTemplateInstruction<0> {
5328 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005329 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5330 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005331
5332 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5333
5334 DECLARE_INSTRUCTION(FakeString);
5335
5336 private:
5337 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5338};
5339
Vladimir Markof9f64412015-09-02 14:05:49 +01005340class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005341 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005342 MoveOperands(Location source,
5343 Location destination,
5344 Primitive::Type type,
5345 HInstruction* instruction)
5346 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005347
5348 Location GetSource() const { return source_; }
5349 Location GetDestination() const { return destination_; }
5350
5351 void SetSource(Location value) { source_ = value; }
5352 void SetDestination(Location value) { destination_ = value; }
5353
5354 // The parallel move resolver marks moves as "in-progress" by clearing the
5355 // destination (but not the source).
5356 Location MarkPending() {
5357 DCHECK(!IsPending());
5358 Location dest = destination_;
5359 destination_ = Location::NoLocation();
5360 return dest;
5361 }
5362
5363 void ClearPending(Location dest) {
5364 DCHECK(IsPending());
5365 destination_ = dest;
5366 }
5367
5368 bool IsPending() const {
5369 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5370 return destination_.IsInvalid() && !source_.IsInvalid();
5371 }
5372
5373 // True if this blocks a move from the given location.
5374 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005375 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005376 }
5377
5378 // A move is redundant if it's been eliminated, if its source and
5379 // destination are the same, or if its destination is unneeded.
5380 bool IsRedundant() const {
5381 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5382 }
5383
5384 // We clear both operands to indicate move that's been eliminated.
5385 void Eliminate() {
5386 source_ = destination_ = Location::NoLocation();
5387 }
5388
5389 bool IsEliminated() const {
5390 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5391 return source_.IsInvalid();
5392 }
5393
Alexey Frunze4dda3372015-06-01 18:31:49 -07005394 Primitive::Type GetType() const { return type_; }
5395
Nicolas Geoffray90218252015-04-15 11:56:51 +01005396 bool Is64BitMove() const {
5397 return Primitive::Is64BitType(type_);
5398 }
5399
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005400 HInstruction* GetInstruction() const { return instruction_; }
5401
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005402 private:
5403 Location source_;
5404 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005405 // The type this move is for.
5406 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005407 // The instruction this move is assocatied with. Null when this move is
5408 // for moving an input in the expected locations of user (including a phi user).
5409 // This is only used in debug mode, to ensure we do not connect interval siblings
5410 // in the same parallel move.
5411 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005412};
5413
5414static constexpr size_t kDefaultNumberOfMoves = 4;
5415
5416class HParallelMove : public HTemplateInstruction<0> {
5417 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005418 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005419 : HTemplateInstruction(SideEffects::None(), dex_pc),
5420 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5421 moves_.reserve(kDefaultNumberOfMoves);
5422 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005423
Nicolas Geoffray90218252015-04-15 11:56:51 +01005424 void AddMove(Location source,
5425 Location destination,
5426 Primitive::Type type,
5427 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005428 DCHECK(source.IsValid());
5429 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005430 if (kIsDebugBuild) {
5431 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005432 for (const MoveOperands& move : moves_) {
5433 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005434 // Special case the situation where the move is for the spill slot
5435 // of the instruction.
5436 if ((GetPrevious() == instruction)
5437 || ((GetPrevious() == nullptr)
5438 && instruction->IsPhi()
5439 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005440 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005441 << "Doing parallel moves for the same instruction.";
5442 } else {
5443 DCHECK(false) << "Doing parallel moves for the same instruction.";
5444 }
5445 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005446 }
5447 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005448 for (const MoveOperands& move : moves_) {
5449 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005450 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005451 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005452 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005453 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005454 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005455 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005456 }
5457
Vladimir Marko225b6462015-09-28 12:17:40 +01005458 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005459 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005460 }
5461
Vladimir Marko225b6462015-09-28 12:17:40 +01005462 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005463
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005464 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005465
5466 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005467 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005468
5469 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5470};
5471
Mark Mendell0616ae02015-04-17 12:49:27 -04005472} // namespace art
5473
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005474#ifdef ART_ENABLE_CODEGEN_arm64
5475#include "nodes_arm64.h"
5476#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005477#ifdef ART_ENABLE_CODEGEN_x86
5478#include "nodes_x86.h"
5479#endif
5480
5481namespace art {
5482
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005483class HGraphVisitor : public ValueObject {
5484 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005485 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5486 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005487
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005488 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005489 virtual void VisitBasicBlock(HBasicBlock* block);
5490
Roland Levillain633021e2014-10-01 14:12:25 +01005491 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005492 void VisitInsertionOrder();
5493
Roland Levillain633021e2014-10-01 14:12:25 +01005494 // Visit the graph following dominator tree reverse post-order.
5495 void VisitReversePostOrder();
5496
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005497 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005498
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005499 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005500#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005501 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5502
5503 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5504
5505#undef DECLARE_VISIT_INSTRUCTION
5506
5507 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005508 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005509
5510 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5511};
5512
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005513class HGraphDelegateVisitor : public HGraphVisitor {
5514 public:
5515 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5516 virtual ~HGraphDelegateVisitor() {}
5517
5518 // Visit functions that delegate to to super class.
5519#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005520 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005521
5522 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5523
5524#undef DECLARE_VISIT_INSTRUCTION
5525
5526 private:
5527 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5528};
5529
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005530class HInsertionOrderIterator : public ValueObject {
5531 public:
5532 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5533
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005534 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005535 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005536 void Advance() { ++index_; }
5537
5538 private:
5539 const HGraph& graph_;
5540 size_t index_;
5541
5542 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5543};
5544
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005545class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005546 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005547 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5548 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005549 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005550 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005551
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005552 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5553 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005554 void Advance() { ++index_; }
5555
5556 private:
5557 const HGraph& graph_;
5558 size_t index_;
5559
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005560 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005561};
5562
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005563class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005564 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005565 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005566 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005567 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005568 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005569 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005570
5571 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005572 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005573 void Advance() { --index_; }
5574
5575 private:
5576 const HGraph& graph_;
5577 size_t index_;
5578
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005579 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005580};
5581
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005582class HLinearPostOrderIterator : public ValueObject {
5583 public:
5584 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005585 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005586
5587 bool Done() const { return index_ == 0; }
5588
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005589 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005590
5591 void Advance() {
5592 --index_;
5593 DCHECK_GE(index_, 0U);
5594 }
5595
5596 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005597 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005598 size_t index_;
5599
5600 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5601};
5602
5603class HLinearOrderIterator : public ValueObject {
5604 public:
5605 explicit HLinearOrderIterator(const HGraph& graph)
5606 : order_(graph.GetLinearOrder()), index_(0) {}
5607
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005608 bool Done() const { return index_ == order_.size(); }
5609 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005610 void Advance() { ++index_; }
5611
5612 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005613 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005614 size_t index_;
5615
5616 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5617};
5618
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005619// Iterator over the blocks that art part of the loop. Includes blocks part
5620// of an inner loop. The order in which the blocks are iterated is on their
5621// block id.
5622class HBlocksInLoopIterator : public ValueObject {
5623 public:
5624 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5625 : blocks_in_loop_(info.GetBlocks()),
5626 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5627 index_(0) {
5628 if (!blocks_in_loop_.IsBitSet(index_)) {
5629 Advance();
5630 }
5631 }
5632
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005633 bool Done() const { return index_ == blocks_.size(); }
5634 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005635 void Advance() {
5636 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005637 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005638 if (blocks_in_loop_.IsBitSet(index_)) {
5639 break;
5640 }
5641 }
5642 }
5643
5644 private:
5645 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005646 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005647 size_t index_;
5648
5649 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5650};
5651
Mingyao Yang3584bce2015-05-19 16:01:59 -07005652// Iterator over the blocks that art part of the loop. Includes blocks part
5653// of an inner loop. The order in which the blocks are iterated is reverse
5654// post order.
5655class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5656 public:
5657 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5658 : blocks_in_loop_(info.GetBlocks()),
5659 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5660 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005661 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005662 Advance();
5663 }
5664 }
5665
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005666 bool Done() const { return index_ == blocks_.size(); }
5667 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005668 void Advance() {
5669 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005670 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5671 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005672 break;
5673 }
5674 }
5675 }
5676
5677 private:
5678 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005679 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005680 size_t index_;
5681
5682 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5683};
5684
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005685inline int64_t Int64FromConstant(HConstant* constant) {
5686 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5687 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5688 : constant->AsLongConstant()->GetValue();
5689}
5690
Vladimir Marko58155012015-08-19 12:49:41 +00005691inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5692 // For the purposes of the compiler, the dex files must actually be the same object
5693 // if we want to safely treat them as the same. This is especially important for JIT
5694 // as custom class loaders can open the same underlying file (or memory) multiple
5695 // times and provide different class resolution but no two class loaders should ever
5696 // use the same DexFile object - doing so is an unsupported hack that can lead to
5697 // all sorts of weird failures.
5698 return &lhs == &rhs;
5699}
5700
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005701} // namespace art
5702
5703#endif // ART_COMPILER_OPTIMIZING_NODES_H_