blob: 0f2c1cffee0deb409767c1ee79cfbf1a5f0ba462 [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
Aart Bik73f1f3b2015-10-28 15:28:08 -0700554 // Returns true if instruction is not defined within this loop or any loop nested inside
555 // this loop. If must_dominate is set, only definitions that actually dominate the loop
556 // header can be invariant. Otherwise, any definition outside the loop, including
557 // definitions that appear after the loop, is invariant.
558 bool IsLoopInvariant(HInstruction* instruction, bool must_dominate) const;
559
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 const ArenaBitVector& GetBlocks() const { return blocks_; }
561
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000562 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000563 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000564
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000565 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100566 // Internal recursive implementation of `Populate`.
567 void PopulateRecursive(HBasicBlock* block);
568
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000569 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100570 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100571 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100572 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000573
574 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
575};
576
David Brazdilec16f792015-08-19 15:04:01 +0100577// Stores try/catch information for basic blocks.
578// Note that HGraph is constructed so that catch blocks cannot simultaneously
579// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100580class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100581 public:
582 // Try block information constructor.
583 explicit TryCatchInformation(const HTryBoundary& try_entry)
584 : try_entry_(&try_entry),
585 catch_dex_file_(nullptr),
586 catch_type_index_(DexFile::kDexNoIndex16) {
587 DCHECK(try_entry_ != nullptr);
588 }
589
590 // Catch block information constructor.
591 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
592 : try_entry_(nullptr),
593 catch_dex_file_(&dex_file),
594 catch_type_index_(catch_type_index) {}
595
596 bool IsTryBlock() const { return try_entry_ != nullptr; }
597
598 const HTryBoundary& GetTryEntry() const {
599 DCHECK(IsTryBlock());
600 return *try_entry_;
601 }
602
603 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
604
605 bool IsCatchAllTypeIndex() const {
606 DCHECK(IsCatchBlock());
607 return catch_type_index_ == DexFile::kDexNoIndex16;
608 }
609
610 uint16_t GetCatchTypeIndex() const {
611 DCHECK(IsCatchBlock());
612 return catch_type_index_;
613 }
614
615 const DexFile& GetCatchDexFile() const {
616 DCHECK(IsCatchBlock());
617 return *catch_dex_file_;
618 }
619
620 private:
621 // One of possibly several TryBoundary instructions entering the block's try.
622 // Only set for try blocks.
623 const HTryBoundary* try_entry_;
624
625 // Exception type information. Only set for catch blocks.
626 const DexFile* catch_dex_file_;
627 const uint16_t catch_type_index_;
628};
629
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100630static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100631static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100632
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000633// A block in a method. Contains the list of instructions represented
634// as a double linked list. Each block knows its predecessors and
635// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100636
Vladimir Markof9f64412015-09-02 14:05:49 +0100637class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600639 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000640 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000641 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
642 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 loop_information_(nullptr),
644 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000645 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100646 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100647 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100648 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000649 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000650 try_catch_information_(nullptr) {
651 predecessors_.reserve(kDefaultNumberOfPredecessors);
652 successors_.reserve(kDefaultNumberOfSuccessors);
653 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
654 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000655
Vladimir Marko60584552015-09-03 13:35:12 +0000656 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100657 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000658 }
659
Vladimir Marko60584552015-09-03 13:35:12 +0000660 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100661 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000662 }
663
Vladimir Marko60584552015-09-03 13:35:12 +0000664 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
665 return ContainsElement(successors_, block, start_from);
666 }
667
668 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100669 return dominated_blocks_;
670 }
671
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100672 bool IsEntryBlock() const {
673 return graph_->GetEntryBlock() == this;
674 }
675
676 bool IsExitBlock() const {
677 return graph_->GetExitBlock() == this;
678 }
679
David Brazdil46e2a392015-03-16 17:31:52 +0000680 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000681 bool IsSingleTryBoundary() const;
682
683 // Returns true if this block emits nothing but a jump.
684 bool IsSingleJump() const {
685 HLoopInformation* loop_info = GetLoopInformation();
686 return (IsSingleGoto() || IsSingleTryBoundary())
687 // Back edges generate a suspend check.
688 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
689 }
David Brazdil46e2a392015-03-16 17:31:52 +0000690
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000691 void AddBackEdge(HBasicBlock* back_edge) {
692 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000693 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000694 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100695 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000696 loop_information_->AddBackEdge(back_edge);
697 }
698
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000699 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000700 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000701
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100702 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000703 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600704 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000705
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000706 HBasicBlock* GetDominator() const { return dominator_; }
707 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000708 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
709
710 void RemoveDominatedBlock(HBasicBlock* block) {
711 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100712 }
Vladimir Marko60584552015-09-03 13:35:12 +0000713
714 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
715 ReplaceElement(dominated_blocks_, existing, new_block);
716 }
717
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100718 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000719
720 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100721 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000722 }
723
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100724 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
725 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100726 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100727 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100728 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
729 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000730
731 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000732 successors_.push_back(block);
733 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000734 }
735
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100736 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
737 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100738 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000739 new_block->predecessors_.push_back(this);
740 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000741 }
742
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000743 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
744 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000745 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000746 new_block->successors_.push_back(this);
747 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000748 }
749
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100750 // Insert `this` between `predecessor` and `successor. This method
751 // preserves the indicies, and will update the first edge found between
752 // `predecessor` and `successor`.
753 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
754 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100755 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000756 successor->predecessors_[predecessor_index] = this;
757 predecessor->successors_[successor_index] = this;
758 successors_.push_back(successor);
759 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100760 }
761
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100762 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000763 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 }
765
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000766 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000767 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000768 }
769
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100770 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000771 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100772 }
773
774 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000775 predecessors_.push_back(block);
776 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100777 }
778
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100779 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000780 DCHECK_EQ(predecessors_.size(), 2u);
781 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100782 }
783
David Brazdil769c9e52015-04-27 13:54:09 +0100784 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000785 DCHECK_EQ(successors_.size(), 2u);
786 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100787 }
788
David Brazdilfc6a86a2015-06-26 10:33:45 +0000789 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000790 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100791 }
792
David Brazdilfc6a86a2015-06-26 10:33:45 +0000793 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000794 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100795 }
796
David Brazdilfc6a86a2015-06-26 10:33:45 +0000797 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000798 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100799 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000800 }
801
802 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000803 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100804 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805 }
806
807 // Returns whether the first occurrence of `predecessor` in the list of
808 // predecessors is at index `idx`.
809 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100810 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000811 return GetPredecessorIndexOf(predecessor) == idx;
812 }
813
David Brazdilffee3d32015-07-06 11:48:53 +0100814 // Returns the number of non-exceptional successors. SsaChecker ensures that
815 // these are stored at the beginning of the successor list.
816 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000817 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100818 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000819
David Brazdild7558da2015-09-22 13:04:14 +0100820 // Create a new block between this block and its predecessors. The new block
821 // is added to the graph, all predecessor edges are relinked to it and an edge
822 // is created to `this`. Returns the new empty block. Reverse post order or
823 // loop and try/catch information are not updated.
824 HBasicBlock* CreateImmediateDominator();
825
David Brazdilfc6a86a2015-06-26 10:33:45 +0000826 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100827 // created, latter block. Note that this method will add the block to the
828 // graph, create a Goto at the end of the former block and will create an edge
829 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100830 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000831 HBasicBlock* SplitBefore(HInstruction* cursor);
832
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000833 // Split the block into two blocks just after `cursor`. Returns the newly
834 // created block. Note that this method just updates raw block information,
835 // like predecessors, successors, dominators, and instruction list. It does not
836 // update the graph, reverse post order, loop information, nor make sure the
837 // blocks are consistent (for example ending with a control flow instruction).
838 HBasicBlock* SplitAfter(HInstruction* cursor);
839
840 // Merge `other` at the end of `this`. Successors and dominated blocks of
841 // `other` are changed to be successors and dominated blocks of `this`. Note
842 // that this method does not update the graph, reverse post order, loop
843 // information, nor make sure the blocks are consistent (for example ending
844 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100845 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846
847 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
848 // of `this` are moved to `other`.
849 // Note that this method does not update the graph, reverse post order, loop
850 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000851 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000852 void ReplaceWith(HBasicBlock* other);
853
David Brazdil2d7352b2015-04-20 14:52:42 +0100854 // Merge `other` at the end of `this`. This method updates loops, reverse post
855 // order, links to predecessors, successors, dominators and deletes the block
856 // from the graph. The two blocks must be successive, i.e. `this` the only
857 // predecessor of `other` and vice versa.
858 void MergeWith(HBasicBlock* other);
859
860 // Disconnects `this` from all its predecessors, successors and dominator,
861 // removes it from all loops it is included in and eventually from the graph.
862 // The block must not dominate any other block. Predecessors and successors
863 // are safely updated.
864 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000865
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000866 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100867 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100868 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100869 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100870 // Replace instruction `initial` with `replacement` within this block.
871 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
872 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100873 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100874 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000875 // RemoveInstruction and RemovePhi delete a given instruction from the respective
876 // instruction list. With 'ensure_safety' set to true, it verifies that the
877 // instruction is not in use and removes it from the use lists of its inputs.
878 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
879 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100880 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100881
882 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100883 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100884 }
885
Roland Levillain6b879dd2014-09-22 17:13:44 +0100886 bool IsLoopPreHeaderFirstPredecessor() const {
887 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100888 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100889 }
890
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100891 HLoopInformation* GetLoopInformation() const {
892 return loop_information_;
893 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000895 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100896 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000897 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 void SetInLoop(HLoopInformation* info) {
899 if (IsLoopHeader()) {
900 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100901 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100902 loop_information_ = info;
903 } else if (loop_information_->Contains(*info->GetHeader())) {
904 // Block is currently part of an outer loop. Make it part of this inner loop.
905 // Note that a non loop header having a loop information means this loop information
906 // has already been populated
907 loop_information_ = info;
908 } else {
909 // Block is part of an inner loop. Do not update the loop information.
910 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
911 // at this point, because this method is being called while populating `info`.
912 }
913 }
914
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000915 // Raw update of the loop information.
916 void SetLoopInformation(HLoopInformation* info) {
917 loop_information_ = info;
918 }
919
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100920 bool IsInLoop() const { return loop_information_ != nullptr; }
921
David Brazdilec16f792015-08-19 15:04:01 +0100922 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
923
924 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
925 try_catch_information_ = try_catch_information;
926 }
927
928 bool IsTryBlock() const {
929 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
930 }
931
932 bool IsCatchBlock() const {
933 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
934 }
David Brazdilffee3d32015-07-06 11:48:53 +0100935
936 // Returns the try entry that this block's successors should have. They will
937 // be in the same try, unless the block ends in a try boundary. In that case,
938 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100939 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100940
David Brazdild7558da2015-09-22 13:04:14 +0100941 bool HasThrowingInstructions() const;
942
David Brazdila4b8c212015-05-07 09:59:30 +0100943 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100944 bool Dominates(HBasicBlock* block) const;
945
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100946 size_t GetLifetimeStart() const { return lifetime_start_; }
947 size_t GetLifetimeEnd() const { return lifetime_end_; }
948
949 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
950 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
951
David Brazdil8d5b8b22015-03-24 10:51:52 +0000952 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000953 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100954 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000955 bool HasSinglePhi() const;
956
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000958 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000959 ArenaVector<HBasicBlock*> predecessors_;
960 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100961 HInstructionList instructions_;
962 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000963 HLoopInformation* loop_information_;
964 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000965 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100966 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100967 // The dex program counter of the first instruction of this block.
968 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100969 size_t lifetime_start_;
970 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100971 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100972
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000973 friend class HGraph;
974 friend class HInstruction;
975
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000976 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
977};
978
David Brazdilb2bd1c52015-03-25 11:17:37 +0000979// Iterates over the LoopInformation of all loops which contain 'block'
980// from the innermost to the outermost.
981class HLoopInformationOutwardIterator : public ValueObject {
982 public:
983 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
984 : current_(block.GetLoopInformation()) {}
985
986 bool Done() const { return current_ == nullptr; }
987
988 void Advance() {
989 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100990 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000991 }
992
993 HLoopInformation* Current() const {
994 DCHECK(!Done());
995 return current_;
996 }
997
998 private:
999 HLoopInformation* current_;
1000
1001 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1002};
1003
Alexandre Ramesef20f712015-06-09 10:29:30 +01001004#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001005 M(Above, Condition) \
1006 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001007 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001008 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001009 M(ArrayGet, Instruction) \
1010 M(ArrayLength, Instruction) \
1011 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001012 M(Below, Condition) \
1013 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001014 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001015 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001016 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001017 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001018 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001019 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001020 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001021 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001022 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001023 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001024 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001025 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001026 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001027 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001028 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001029 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001030 M(FloatConstant, Constant) \
1031 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(GreaterThan, Condition) \
1033 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001034 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001035 M(InstanceFieldGet, Instruction) \
1036 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001037 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001039 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001040 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001041 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001042 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001043 M(LessThan, Condition) \
1044 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001045 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001046 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001047 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001048 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(Local, Instruction) \
1050 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001051 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001052 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001053 M(Mul, BinaryOperation) \
1054 M(Neg, UnaryOperation) \
1055 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001056 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001057 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001058 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001059 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001060 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001061 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001062 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001063 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001064 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001065 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001066 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001067 M(Return, Instruction) \
1068 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001069 M(Shl, BinaryOperation) \
1070 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001071 M(StaticFieldGet, Instruction) \
1072 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001073 M(UnresolvedInstanceFieldGet, Instruction) \
1074 M(UnresolvedInstanceFieldSet, Instruction) \
1075 M(UnresolvedStaticFieldGet, Instruction) \
1076 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001077 M(StoreLocal, Instruction) \
1078 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001079 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001080 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001081 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001082 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001083 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001084 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001085 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001086
Alexandre Ramesef20f712015-06-09 10:29:30 +01001087#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1088
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001089#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001090#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001091#else
1092#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1093 M(Arm64IntermediateAddress, Instruction)
1094#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001095
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001096#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1097
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001098#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1099
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001100#ifndef ART_ENABLE_CODEGEN_x86
1101#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1102#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001103#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1104 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001105 M(X86LoadFromConstantTable, Instruction) \
1106 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001107#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001108
1109#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1110
1111#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1112 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1113 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1114 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001115 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001116 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001117 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1118 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1119
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001120#define FOR_EACH_INSTRUCTION(M) \
1121 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1122 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001123 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001124 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001125 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001126
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001127#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001128FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1129#undef FORWARD_DECLARATION
1130
Roland Levillainccc07a92014-09-16 14:48:16 +01001131#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001132 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1133 const char* DebugName() const OVERRIDE { return #type; } \
1134 const H##type* As##type() const OVERRIDE { return this; } \
1135 H##type* As##type() OVERRIDE { return this; } \
1136 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001137 return other->Is##type(); \
1138 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001139 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001140
David Brazdiled596192015-01-23 10:39:45 +00001141template <typename T> class HUseList;
1142
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001143template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001144class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001145 public:
David Brazdiled596192015-01-23 10:39:45 +00001146 HUseListNode* GetPrevious() const { return prev_; }
1147 HUseListNode* GetNext() const { return next_; }
1148 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001149 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001150 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001151
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001152 private:
David Brazdiled596192015-01-23 10:39:45 +00001153 HUseListNode(T user, size_t index)
1154 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1155
1156 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001157 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001158 HUseListNode<T>* prev_;
1159 HUseListNode<T>* next_;
1160
1161 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001162
1163 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1164};
1165
David Brazdiled596192015-01-23 10:39:45 +00001166template <typename T>
1167class HUseList : public ValueObject {
1168 public:
1169 HUseList() : first_(nullptr) {}
1170
1171 void Clear() {
1172 first_ = nullptr;
1173 }
1174
1175 // Adds a new entry at the beginning of the use list and returns
1176 // the newly created node.
1177 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001178 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001179 if (IsEmpty()) {
1180 first_ = new_node;
1181 } else {
1182 first_->prev_ = new_node;
1183 new_node->next_ = first_;
1184 first_ = new_node;
1185 }
1186 return new_node;
1187 }
1188
1189 HUseListNode<T>* GetFirst() const {
1190 return first_;
1191 }
1192
1193 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001194 DCHECK(node != nullptr);
1195 DCHECK(Contains(node));
1196
David Brazdiled596192015-01-23 10:39:45 +00001197 if (node->prev_ != nullptr) {
1198 node->prev_->next_ = node->next_;
1199 }
1200 if (node->next_ != nullptr) {
1201 node->next_->prev_ = node->prev_;
1202 }
1203 if (node == first_) {
1204 first_ = node->next_;
1205 }
1206 }
1207
David Brazdil1abb4192015-02-17 18:33:36 +00001208 bool Contains(const HUseListNode<T>* node) const {
1209 if (node == nullptr) {
1210 return false;
1211 }
1212 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1213 if (current == node) {
1214 return true;
1215 }
1216 }
1217 return false;
1218 }
1219
David Brazdiled596192015-01-23 10:39:45 +00001220 bool IsEmpty() const {
1221 return first_ == nullptr;
1222 }
1223
1224 bool HasOnlyOneUse() const {
1225 return first_ != nullptr && first_->next_ == nullptr;
1226 }
1227
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001228 size_t SizeSlow() const {
1229 size_t count = 0;
1230 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1231 ++count;
1232 }
1233 return count;
1234 }
1235
David Brazdiled596192015-01-23 10:39:45 +00001236 private:
1237 HUseListNode<T>* first_;
1238};
1239
1240template<typename T>
1241class HUseIterator : public ValueObject {
1242 public:
1243 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1244
1245 bool Done() const { return current_ == nullptr; }
1246
1247 void Advance() {
1248 DCHECK(!Done());
1249 current_ = current_->GetNext();
1250 }
1251
1252 HUseListNode<T>* Current() const {
1253 DCHECK(!Done());
1254 return current_;
1255 }
1256
1257 private:
1258 HUseListNode<T>* current_;
1259
1260 friend class HValue;
1261};
1262
David Brazdil1abb4192015-02-17 18:33:36 +00001263// This class is used by HEnvironment and HInstruction classes to record the
1264// instructions they use and pointers to the corresponding HUseListNodes kept
1265// by the used instructions.
1266template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001267class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001268 public:
1269 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1270 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1271
1272 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1273 : instruction_(old_record.instruction_), use_node_(use_node) {
1274 DCHECK(instruction_ != nullptr);
1275 DCHECK(use_node_ != nullptr);
1276 DCHECK(old_record.use_node_ == nullptr);
1277 }
1278
1279 HInstruction* GetInstruction() const { return instruction_; }
1280 HUseListNode<T>* GetUseNode() const { return use_node_; }
1281
1282 private:
1283 // Instruction used by the user.
1284 HInstruction* instruction_;
1285
1286 // Corresponding entry in the use list kept by 'instruction_'.
1287 HUseListNode<T>* use_node_;
1288};
1289
Aart Bik854a02b2015-07-14 16:07:00 -07001290/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001291 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001292 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001293 * For write/read dependences on fields/arrays, the dependence analysis uses
1294 * type disambiguation (e.g. a float field write cannot modify the value of an
1295 * integer field read) and the access type (e.g. a reference array write cannot
1296 * modify the value of a reference field read [although it may modify the
1297 * reference fetch prior to reading the field, which is represented by its own
1298 * write/read dependence]). The analysis makes conservative points-to
1299 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1300 * the same, and any reference read depends on any reference read without
1301 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001302 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001303 * The internal representation uses 38-bit and is described in the table below.
1304 * The first line indicates the side effect, and for field/array accesses the
1305 * second line indicates the type of the access (in the order of the
1306 * Primitive::Type enum).
1307 * The two numbered lines below indicate the bit position in the bitfield (read
1308 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001309 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001310 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1311 * +-------------+---------+---------+--------------+---------+---------+
1312 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1313 * | 3 |333333322|222222221| 1 |111111110|000000000|
1314 * | 7 |654321098|765432109| 8 |765432109|876543210|
1315 *
1316 * Note that, to ease the implementation, 'changes' bits are least significant
1317 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001318 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001319class SideEffects : public ValueObject {
1320 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001321 SideEffects() : flags_(0) {}
1322
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001323 static SideEffects None() {
1324 return SideEffects(0);
1325 }
1326
1327 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001328 return SideEffects(kAllChangeBits | kAllDependOnBits);
1329 }
1330
1331 static SideEffects AllChanges() {
1332 return SideEffects(kAllChangeBits);
1333 }
1334
1335 static SideEffects AllDependencies() {
1336 return SideEffects(kAllDependOnBits);
1337 }
1338
1339 static SideEffects AllExceptGCDependency() {
1340 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1341 }
1342
1343 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001344 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001345 }
1346
Aart Bik34c3ba92015-07-20 14:08:59 -07001347 static SideEffects AllWrites() {
1348 return SideEffects(kAllWrites);
1349 }
1350
1351 static SideEffects AllReads() {
1352 return SideEffects(kAllReads);
1353 }
1354
1355 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1356 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001357 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001358 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001359 }
1360
Aart Bik854a02b2015-07-14 16:07:00 -07001361 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1362 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001363 }
1364
Aart Bik34c3ba92015-07-20 14:08:59 -07001365 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1366 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001367 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001368 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001369 }
1370
1371 static SideEffects ArrayReadOfType(Primitive::Type type) {
1372 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1373 }
1374
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001375 static SideEffects CanTriggerGC() {
1376 return SideEffects(1ULL << kCanTriggerGCBit);
1377 }
1378
1379 static SideEffects DependsOnGC() {
1380 return SideEffects(1ULL << kDependsOnGCBit);
1381 }
1382
Aart Bik854a02b2015-07-14 16:07:00 -07001383 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001384 SideEffects Union(SideEffects other) const {
1385 return SideEffects(flags_ | other.flags_);
1386 }
1387
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001388 SideEffects Exclusion(SideEffects other) const {
1389 return SideEffects(flags_ & ~other.flags_);
1390 }
1391
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001392 void Add(SideEffects other) {
1393 flags_ |= other.flags_;
1394 }
1395
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001396 bool Includes(SideEffects other) const {
1397 return (other.flags_ & flags_) == other.flags_;
1398 }
1399
1400 bool HasSideEffects() const {
1401 return (flags_ & kAllChangeBits);
1402 }
1403
1404 bool HasDependencies() const {
1405 return (flags_ & kAllDependOnBits);
1406 }
1407
1408 // Returns true if there are no side effects or dependencies.
1409 bool DoesNothing() const {
1410 return flags_ == 0;
1411 }
1412
Aart Bik854a02b2015-07-14 16:07:00 -07001413 // Returns true if something is written.
1414 bool DoesAnyWrite() const {
1415 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001416 }
1417
Aart Bik854a02b2015-07-14 16:07:00 -07001418 // Returns true if something is read.
1419 bool DoesAnyRead() const {
1420 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001421 }
1422
Aart Bik854a02b2015-07-14 16:07:00 -07001423 // Returns true if potentially everything is written and read
1424 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001425 bool DoesAllReadWrite() const {
1426 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1427 }
1428
Aart Bik854a02b2015-07-14 16:07:00 -07001429 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001430 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001431 }
1432
1433 // Returns true if this may read something written by other.
1434 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001435 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1436 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001437 }
1438
1439 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001440 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001441 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001442 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001443 for (int s = kLastBit; s >= 0; s--) {
1444 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1445 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1446 // This is a bit for the GC side effect.
1447 if (current_bit_is_set) {
1448 flags += "GC";
1449 }
Aart Bik854a02b2015-07-14 16:07:00 -07001450 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001451 } else {
1452 // This is a bit for the array/field analysis.
1453 // The underscore character stands for the 'can trigger GC' bit.
1454 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1455 if (current_bit_is_set) {
1456 flags += kDebug[s];
1457 }
1458 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1459 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1460 flags += "|";
1461 }
1462 }
Aart Bik854a02b2015-07-14 16:07:00 -07001463 }
1464 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001465 }
1466
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001467 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001468
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001469 private:
1470 static constexpr int kFieldArrayAnalysisBits = 9;
1471
1472 static constexpr int kFieldWriteOffset = 0;
1473 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1474 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1475 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1476
1477 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1478
1479 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1480 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1481 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1482 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1483
1484 static constexpr int kLastBit = kDependsOnGCBit;
1485 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1486
1487 // Aliases.
1488
1489 static_assert(kChangeBits == kDependOnBits,
1490 "the 'change' bits should match the 'depend on' bits.");
1491
1492 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1493 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1494 static constexpr uint64_t kAllWrites =
1495 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1496 static constexpr uint64_t kAllReads =
1497 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001498
Aart Bik854a02b2015-07-14 16:07:00 -07001499 // Work around the fact that HIR aliases I/F and J/D.
1500 // TODO: remove this interceptor once HIR types are clean
1501 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1502 switch (type) {
1503 case Primitive::kPrimInt:
1504 case Primitive::kPrimFloat:
1505 return TypeFlag(Primitive::kPrimInt, offset) |
1506 TypeFlag(Primitive::kPrimFloat, offset);
1507 case Primitive::kPrimLong:
1508 case Primitive::kPrimDouble:
1509 return TypeFlag(Primitive::kPrimLong, offset) |
1510 TypeFlag(Primitive::kPrimDouble, offset);
1511 default:
1512 return TypeFlag(type, offset);
1513 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001514 }
1515
Aart Bik854a02b2015-07-14 16:07:00 -07001516 // Translates type to bit flag.
1517 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1518 CHECK_NE(type, Primitive::kPrimVoid);
1519 const uint64_t one = 1;
1520 const int shift = type; // 0-based consecutive enum
1521 DCHECK_LE(kFieldWriteOffset, shift);
1522 DCHECK_LT(shift, kArrayWriteOffset);
1523 return one << (type + offset);
1524 }
1525
1526 // Private constructor on direct flags value.
1527 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1528
1529 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530};
1531
David Brazdiled596192015-01-23 10:39:45 +00001532// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001533class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001534 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001535 HEnvironment(ArenaAllocator* arena,
1536 size_t number_of_vregs,
1537 const DexFile& dex_file,
1538 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001539 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001540 InvokeType invoke_type,
1541 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001542 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1543 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001544 parent_(nullptr),
1545 dex_file_(dex_file),
1546 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001547 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001548 invoke_type_(invoke_type),
1549 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001550 }
1551
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001552 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001553 : HEnvironment(arena,
1554 to_copy.Size(),
1555 to_copy.GetDexFile(),
1556 to_copy.GetMethodIdx(),
1557 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001558 to_copy.GetInvokeType(),
1559 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001560
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001561 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001562 if (parent_ != nullptr) {
1563 parent_->SetAndCopyParentChain(allocator, parent);
1564 } else {
1565 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1566 parent_->CopyFrom(parent);
1567 if (parent->GetParent() != nullptr) {
1568 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1569 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001570 }
David Brazdiled596192015-01-23 10:39:45 +00001571 }
1572
Vladimir Marko71bf8092015-09-15 15:33:14 +01001573 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001574 void CopyFrom(HEnvironment* environment);
1575
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001576 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1577 // input to the loop phi instead. This is for inserting instructions that
1578 // require an environment (like HDeoptimization) in the loop pre-header.
1579 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001580
1581 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001582 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001583 }
1584
1585 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001586 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001587 }
1588
David Brazdil1abb4192015-02-17 18:33:36 +00001589 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001590
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001591 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001592
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001593 HEnvironment* GetParent() const { return parent_; }
1594
1595 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001596 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001597 }
1598
1599 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001600 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001601 }
1602
1603 uint32_t GetDexPc() const {
1604 return dex_pc_;
1605 }
1606
1607 uint32_t GetMethodIdx() const {
1608 return method_idx_;
1609 }
1610
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001611 InvokeType GetInvokeType() const {
1612 return invoke_type_;
1613 }
1614
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001615 const DexFile& GetDexFile() const {
1616 return dex_file_;
1617 }
1618
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001619 HInstruction* GetHolder() const {
1620 return holder_;
1621 }
1622
David Brazdiled596192015-01-23 10:39:45 +00001623 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001624 // Record instructions' use entries of this environment for constant-time removal.
1625 // It should only be called by HInstruction when a new environment use is added.
1626 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1627 DCHECK(env_use->GetUser() == this);
1628 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001629 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001630 }
David Brazdiled596192015-01-23 10:39:45 +00001631
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001632 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1633 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001634 HEnvironment* parent_;
1635 const DexFile& dex_file_;
1636 const uint32_t method_idx_;
1637 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001638 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001639
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001640 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001641 HInstruction* const holder_;
1642
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001643 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001644
1645 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1646};
1647
Calin Juravleacf735c2015-02-12 15:25:22 +00001648class ReferenceTypeInfo : ValueObject {
1649 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001650 typedef Handle<mirror::Class> TypeHandle;
1651
Calin Juravle2e768302015-07-28 14:41:11 +00001652 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1653 // The constructor will check that the type_handle is valid.
1654 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001655 }
1656
Calin Juravle2e768302015-07-28 14:41:11 +00001657 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1658
1659 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1660 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001661 }
1662
Calin Juravle2e768302015-07-28 14:41:11 +00001663 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1664 return IsValidHandle(type_handle_);
1665 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001666
Calin Juravleacf735c2015-02-12 15:25:22 +00001667 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001668
1669 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1670 DCHECK(IsValid());
1671 return GetTypeHandle()->IsObjectClass();
1672 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001673
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001674 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001675 DCHECK(IsValid());
1676 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001677 }
1678
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001679 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1680 DCHECK(IsValid());
1681 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1682 }
1683
Mathieu Chartier90443472015-07-16 20:32:27 -07001684 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001685 DCHECK(IsValid());
1686 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001687 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001688
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001689 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001690 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001691 return GetTypeHandle()->IsArrayClass();
1692 }
1693
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001694 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1695 DCHECK(IsValid());
1696 return GetTypeHandle()->IsPrimitiveArray();
1697 }
1698
1699 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1700 DCHECK(IsValid());
1701 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1702 }
1703
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001704 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001705 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001706 if (!IsExact()) return false;
1707 if (!IsArrayClass()) return false;
1708 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1709 }
1710
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001711 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1712 DCHECK(IsValid());
1713 if (!IsExact()) return false;
1714 if (!IsArrayClass()) return false;
1715 if (!rti.IsArrayClass()) return false;
1716 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1717 rti.GetTypeHandle()->GetComponentType());
1718 }
1719
Calin Juravleacf735c2015-02-12 15:25:22 +00001720 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1721
Mathieu Chartier90443472015-07-16 20:32:27 -07001722 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001723 DCHECK(IsValid());
1724 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001725 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1726 }
1727
1728 // Returns true if the type information provide the same amount of details.
1729 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001730 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001731 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001732 if (!IsValid() && !rti.IsValid()) {
1733 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001734 return true;
1735 }
Calin Juravle2e768302015-07-28 14:41:11 +00001736 if (!IsValid() || !rti.IsValid()) {
1737 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001738 return false;
1739 }
Calin Juravle2e768302015-07-28 14:41:11 +00001740 return IsExact() == rti.IsExact()
1741 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001742 }
1743
1744 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001745 ReferenceTypeInfo();
1746 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001747
Calin Juravleacf735c2015-02-12 15:25:22 +00001748 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001749 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001750 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001751 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001752 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001753};
1754
1755std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1756
Vladimir Markof9f64412015-09-02 14:05:49 +01001757class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001758 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001759 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001760 : previous_(nullptr),
1761 next_(nullptr),
1762 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001763 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001764 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001765 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001766 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001767 locations_(nullptr),
1768 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001769 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001770 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001771 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001772
Dave Allison20dfc792014-06-16 20:44:29 -07001773 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001774
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001775#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001776 enum InstructionKind {
1777 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1778 };
1779#undef DECLARE_KIND
1780
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001781 HInstruction* GetNext() const { return next_; }
1782 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001783
Calin Juravle77520bc2015-01-12 18:45:46 +00001784 HInstruction* GetNextDisregardingMoves() const;
1785 HInstruction* GetPreviousDisregardingMoves() const;
1786
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001787 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001788 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001789 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001790 bool IsInBlock() const { return block_ != nullptr; }
1791 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001792 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001793
Roland Levillain6b879dd2014-09-22 17:13:44 +01001794 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001795 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001796
1797 virtual void Accept(HGraphVisitor* visitor) = 0;
1798 virtual const char* DebugName() const = 0;
1799
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001800 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001801 void SetRawInputAt(size_t index, HInstruction* input) {
1802 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1803 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001804
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001805 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001806
1807 uint32_t GetDexPc() const { return dex_pc_; }
1808
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001809 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001810
Roland Levillaine161a2a2014-10-03 12:45:18 +01001811 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001812 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001813
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001814 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001815 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001816
Calin Juravle10e244f2015-01-26 18:54:32 +00001817 // Does not apply for all instructions, but having this at top level greatly
1818 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001819 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001820 virtual bool CanBeNull() const {
1821 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1822 return true;
1823 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001824
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001825 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001826 return false;
1827 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001828
Calin Juravle2e768302015-07-28 14:41:11 +00001829 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001830
Calin Juravle61d544b2015-02-23 16:46:57 +00001831 ReferenceTypeInfo GetReferenceTypeInfo() const {
1832 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1833 return reference_type_info_;
1834 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001835
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001836 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001837 DCHECK(user != nullptr);
1838 HUseListNode<HInstruction*>* use =
1839 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1840 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001841 }
1842
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001843 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001844 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001845 HUseListNode<HEnvironment*>* env_use =
1846 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1847 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001848 }
1849
David Brazdil1abb4192015-02-17 18:33:36 +00001850 void RemoveAsUserOfInput(size_t input) {
1851 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1852 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1853 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001854
David Brazdil1abb4192015-02-17 18:33:36 +00001855 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1856 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001857
David Brazdiled596192015-01-23 10:39:45 +00001858 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1859 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001860 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001861 bool HasOnlyOneNonEnvironmentUse() const {
1862 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1863 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001864
Roland Levillain6c82d402014-10-13 16:10:27 +01001865 // Does this instruction strictly dominate `other_instruction`?
1866 // Returns false if this instruction and `other_instruction` are the same.
1867 // Aborts if this instruction and `other_instruction` are both phis.
1868 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001869
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001870 int GetId() const { return id_; }
1871 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001872
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001873 int GetSsaIndex() const { return ssa_index_; }
1874 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1875 bool HasSsaIndex() const { return ssa_index_ != -1; }
1876
1877 bool HasEnvironment() const { return environment_ != nullptr; }
1878 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001879 // Set the `environment_` field. Raw because this method does not
1880 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001881 void SetRawEnvironment(HEnvironment* environment) {
1882 DCHECK(environment_ == nullptr);
1883 DCHECK_EQ(environment->GetHolder(), this);
1884 environment_ = environment;
1885 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001886
1887 // Set the environment of this instruction, copying it from `environment`. While
1888 // copying, the uses lists are being updated.
1889 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001890 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001891 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001892 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001893 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001894 if (environment->GetParent() != nullptr) {
1895 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1896 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001897 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001898
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001899 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1900 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001901 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001902 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001903 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001904 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001905 if (environment->GetParent() != nullptr) {
1906 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1907 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001908 }
1909
Nicolas Geoffray39468442014-09-02 15:17:15 +01001910 // Returns the number of entries in the environment. Typically, that is the
1911 // number of dex registers in a method. It could be more in case of inlining.
1912 size_t EnvironmentSize() const;
1913
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001914 LocationSummary* GetLocations() const { return locations_; }
1915 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001916
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001917 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001918 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001919
Alexandre Rames188d4312015-04-09 18:30:21 +01001920 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1921 // uses of this instruction by `other` are *not* updated.
1922 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1923 ReplaceWith(other);
1924 other->ReplaceInput(this, use_index);
1925 }
1926
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001927 // Move `this` instruction before `cursor`.
1928 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001929
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001930#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001931 bool Is##type() const { return (As##type() != nullptr); } \
1932 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001933 virtual H##type* As##type() { return nullptr; }
1934
1935 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1936#undef INSTRUCTION_TYPE_CHECK
1937
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001938 // Returns whether the instruction can be moved within the graph.
1939 virtual bool CanBeMoved() const { return false; }
1940
1941 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001942 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001943 return false;
1944 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001945
1946 // Returns whether any data encoded in the two instructions is equal.
1947 // This method does not look at the inputs. Both instructions must be
1948 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001949 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001950 return false;
1951 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001952
1953 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001954 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001955 // 2) Their inputs are identical.
1956 bool Equals(HInstruction* other) const;
1957
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001958 virtual InstructionKind GetKind() const = 0;
1959
1960 virtual size_t ComputeHashCode() const {
1961 size_t result = GetKind();
1962 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1963 result = (result * 31) + InputAt(i)->GetId();
1964 }
1965 return result;
1966 }
1967
1968 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001969 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001970
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001971 size_t GetLifetimePosition() const { return lifetime_position_; }
1972 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1973 LiveInterval* GetLiveInterval() const { return live_interval_; }
1974 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1975 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1976
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001977 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1978
1979 // Returns whether the code generation of the instruction will require to have access
1980 // to the current method. Such instructions are:
1981 // (1): Instructions that require an environment, as calling the runtime requires
1982 // to walk the stack and have the current method stored at a specific stack address.
1983 // (2): Object literals like classes and strings, that are loaded from the dex cache
1984 // fields of the current method.
1985 bool NeedsCurrentMethod() const {
1986 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1987 }
1988
Vladimir Markodc151b22015-10-15 18:02:30 +01001989 // Returns whether the code generation of the instruction will require to have access
1990 // to the dex cache of the current method's declaring class via the current method.
1991 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001992
Mark Mendellc4701932015-04-10 13:18:51 -04001993 // Does this instruction have any use in an environment before
1994 // control flow hits 'other'?
1995 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1996
1997 // Remove all references to environment uses of this instruction.
1998 // The caller must ensure that this is safe to do.
1999 void RemoveEnvironmentUsers();
2000
David Brazdil1abb4192015-02-17 18:33:36 +00002001 protected:
2002 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2003 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2004
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002005 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002006 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2007
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002008 HInstruction* previous_;
2009 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002010 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002011 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002012
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002013 // An instruction gets an id when it is added to the graph.
2014 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002015 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002016 int id_;
2017
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002018 // When doing liveness analysis, instructions that have uses get an SSA index.
2019 int ssa_index_;
2020
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002021 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002022 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002023
2024 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002025 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002026
Nicolas Geoffray39468442014-09-02 15:17:15 +01002027 // The environment associated with this instruction. Not null if the instruction
2028 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002029 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002030
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002031 // Set by the code generator.
2032 LocationSummary* locations_;
2033
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002034 // Set by the liveness analysis.
2035 LiveInterval* live_interval_;
2036
2037 // Set by the liveness analysis, this is the position in a linear
2038 // order of blocks where this instruction's live interval start.
2039 size_t lifetime_position_;
2040
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002041 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002042
Calin Juravleacf735c2015-02-12 15:25:22 +00002043 // TODO: for primitive types this should be marked as invalid.
2044 ReferenceTypeInfo reference_type_info_;
2045
David Brazdil1abb4192015-02-17 18:33:36 +00002046 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002047 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002048 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002049 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002050 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002051
2052 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2053};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002054std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002056class HInputIterator : public ValueObject {
2057 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002058 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002059
2060 bool Done() const { return index_ == instruction_->InputCount(); }
2061 HInstruction* Current() const { return instruction_->InputAt(index_); }
2062 void Advance() { index_++; }
2063
2064 private:
2065 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002066 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002067
2068 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2069};
2070
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002071class HInstructionIterator : public ValueObject {
2072 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002073 explicit HInstructionIterator(const HInstructionList& instructions)
2074 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002075 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002076 }
2077
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002078 bool Done() const { return instruction_ == nullptr; }
2079 HInstruction* Current() const { return instruction_; }
2080 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002081 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002082 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002083 }
2084
2085 private:
2086 HInstruction* instruction_;
2087 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002088
2089 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002090};
2091
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002092class HBackwardInstructionIterator : public ValueObject {
2093 public:
2094 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2095 : instruction_(instructions.last_instruction_) {
2096 next_ = Done() ? nullptr : instruction_->GetPrevious();
2097 }
2098
2099 bool Done() const { return instruction_ == nullptr; }
2100 HInstruction* Current() const { return instruction_; }
2101 void Advance() {
2102 instruction_ = next_;
2103 next_ = Done() ? nullptr : instruction_->GetPrevious();
2104 }
2105
2106 private:
2107 HInstruction* instruction_;
2108 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002109
2110 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002111};
2112
Vladimir Markof9f64412015-09-02 14:05:49 +01002113template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114class HTemplateInstruction: public HInstruction {
2115 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002116 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002117 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002118 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002120 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002122 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002123 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2124 DCHECK_LT(i, N);
2125 return inputs_[i];
2126 }
David Brazdil1abb4192015-02-17 18:33:36 +00002127
2128 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002129 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002130 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002131 }
2132
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002133 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002134 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002135
2136 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002137};
2138
Vladimir Markof9f64412015-09-02 14:05:49 +01002139// HTemplateInstruction specialization for N=0.
2140template<>
2141class HTemplateInstruction<0>: public HInstruction {
2142 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002143 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002144 : HInstruction(side_effects, dex_pc) {}
2145
Vladimir Markof9f64412015-09-02 14:05:49 +01002146 virtual ~HTemplateInstruction() {}
2147
2148 size_t InputCount() const OVERRIDE { return 0; }
2149
2150 protected:
2151 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2152 LOG(FATAL) << "Unreachable";
2153 UNREACHABLE();
2154 }
2155
2156 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2157 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2158 LOG(FATAL) << "Unreachable";
2159 UNREACHABLE();
2160 }
2161
2162 private:
2163 friend class SsaBuilder;
2164};
2165
Dave Allison20dfc792014-06-16 20:44:29 -07002166template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002167class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002168 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002169 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002170 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002171 virtual ~HExpression() {}
2172
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002173 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002174
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002175 protected:
2176 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002177};
2178
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002179// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2180// instruction that branches to the exit block.
2181class HReturnVoid : public HTemplateInstruction<0> {
2182 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002183 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2184 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002185
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002186 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002187
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002188 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002189
2190 private:
2191 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2192};
2193
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002194// Represents dex's RETURN opcodes. A HReturn is a control flow
2195// instruction that branches to the exit block.
2196class HReturn : public HTemplateInstruction<1> {
2197 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002198 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2199 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002200 SetRawInputAt(0, value);
2201 }
2202
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002203 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002204
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002205 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002206
2207 private:
2208 DISALLOW_COPY_AND_ASSIGN(HReturn);
2209};
2210
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002211// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002212// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213// exit block.
2214class HExit : public HTemplateInstruction<0> {
2215 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002216 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002218 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002219
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002220 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221
2222 private:
2223 DISALLOW_COPY_AND_ASSIGN(HExit);
2224};
2225
2226// Jumps from one block to another.
2227class HGoto : public HTemplateInstruction<0> {
2228 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002229 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002230
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002231 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002233 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002234 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002235 }
2236
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002237 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002238
2239 private:
2240 DISALLOW_COPY_AND_ASSIGN(HGoto);
2241};
2242
Roland Levillain9867bc72015-08-05 10:21:34 +01002243class HConstant : public HExpression<0> {
2244 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002245 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2246 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002247
2248 bool CanBeMoved() const OVERRIDE { return true; }
2249
2250 virtual bool IsMinusOne() const { return false; }
2251 virtual bool IsZero() const { return false; }
2252 virtual bool IsOne() const { return false; }
2253
David Brazdil77a48ae2015-09-15 12:34:04 +00002254 virtual uint64_t GetValueAsUint64() const = 0;
2255
Roland Levillain9867bc72015-08-05 10:21:34 +01002256 DECLARE_INSTRUCTION(Constant);
2257
2258 private:
2259 DISALLOW_COPY_AND_ASSIGN(HConstant);
2260};
2261
2262class HNullConstant : public HConstant {
2263 public:
2264 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2265 return true;
2266 }
2267
David Brazdil77a48ae2015-09-15 12:34:04 +00002268 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2269
Roland Levillain9867bc72015-08-05 10:21:34 +01002270 size_t ComputeHashCode() const OVERRIDE { return 0; }
2271
2272 DECLARE_INSTRUCTION(NullConstant);
2273
2274 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002275 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002276
2277 friend class HGraph;
2278 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2279};
2280
2281// Constants of the type int. Those can be from Dex instructions, or
2282// synthesized (for example with the if-eqz instruction).
2283class HIntConstant : public HConstant {
2284 public:
2285 int32_t GetValue() const { return value_; }
2286
David Brazdil9f389d42015-10-01 14:32:56 +01002287 uint64_t GetValueAsUint64() const OVERRIDE {
2288 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2289 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002290
Roland Levillain9867bc72015-08-05 10:21:34 +01002291 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2292 DCHECK(other->IsIntConstant());
2293 return other->AsIntConstant()->value_ == value_;
2294 }
2295
2296 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2297
2298 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2299 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2300 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2301
2302 DECLARE_INSTRUCTION(IntConstant);
2303
2304 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002305 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2306 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2307 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2308 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002309
2310 const int32_t value_;
2311
2312 friend class HGraph;
2313 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2314 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2315 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2316};
2317
2318class HLongConstant : public HConstant {
2319 public:
2320 int64_t GetValue() const { return value_; }
2321
David Brazdil77a48ae2015-09-15 12:34:04 +00002322 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2323
Roland Levillain9867bc72015-08-05 10:21:34 +01002324 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2325 DCHECK(other->IsLongConstant());
2326 return other->AsLongConstant()->value_ == value_;
2327 }
2328
2329 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2330
2331 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2332 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2333 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2334
2335 DECLARE_INSTRUCTION(LongConstant);
2336
2337 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002338 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2339 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002340
2341 const int64_t value_;
2342
2343 friend class HGraph;
2344 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2345};
Dave Allison20dfc792014-06-16 20:44:29 -07002346
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002347// Conditional branch. A block ending with an HIf instruction must have
2348// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002349class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002350 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002351 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2352 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002353 SetRawInputAt(0, input);
2354 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002355
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002356 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002357
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002358 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002359 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002360 }
2361
2362 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002363 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002364 }
2365
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002366 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002367
2368 private:
2369 DISALLOW_COPY_AND_ASSIGN(HIf);
2370};
2371
David Brazdilfc6a86a2015-06-26 10:33:45 +00002372
2373// Abstract instruction which marks the beginning and/or end of a try block and
2374// links it to the respective exception handlers. Behaves the same as a Goto in
2375// non-exceptional control flow.
2376// Normal-flow successor is stored at index zero, exception handlers under
2377// higher indices in no particular order.
2378class HTryBoundary : public HTemplateInstruction<0> {
2379 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002380 enum BoundaryKind {
2381 kEntry,
2382 kExit,
2383 };
2384
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002385 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2386 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002387
2388 bool IsControlFlow() const OVERRIDE { return true; }
2389
2390 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002391 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002392
2393 // Returns whether `handler` is among its exception handlers (non-zero index
2394 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002395 bool HasExceptionHandler(const HBasicBlock& handler) const {
2396 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002397 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002398 }
2399
2400 // If not present already, adds `handler` to its block's list of exception
2401 // handlers.
2402 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002403 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002404 GetBlock()->AddSuccessor(handler);
2405 }
2406 }
2407
David Brazdil56e1acc2015-06-30 15:41:36 +01002408 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002409
David Brazdilffee3d32015-07-06 11:48:53 +01002410 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2411
David Brazdilfc6a86a2015-06-26 10:33:45 +00002412 DECLARE_INSTRUCTION(TryBoundary);
2413
2414 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002415 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002416
2417 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2418};
2419
David Brazdilffee3d32015-07-06 11:48:53 +01002420// Iterator over exception handlers of a given HTryBoundary, i.e. over
2421// exceptional successors of its basic block.
2422class HExceptionHandlerIterator : public ValueObject {
2423 public:
2424 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2425 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2426
Vladimir Marko60584552015-09-03 13:35:12 +00002427 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002428 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002429 size_t CurrentSuccessorIndex() const { return index_; }
2430 void Advance() { ++index_; }
2431
2432 private:
2433 const HBasicBlock& block_;
2434 size_t index_;
2435
2436 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2437};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002438
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002439// Deoptimize to interpreter, upon checking a condition.
2440class HDeoptimize : public HTemplateInstruction<1> {
2441 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002442 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2443 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002444 SetRawInputAt(0, cond);
2445 }
2446
2447 bool NeedsEnvironment() const OVERRIDE { return true; }
2448 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002449
2450 DECLARE_INSTRUCTION(Deoptimize);
2451
2452 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002453 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2454};
2455
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002456// Represents the ArtMethod that was passed as a first argument to
2457// the method. It is used by instructions that depend on it, like
2458// instructions that work with the dex cache.
2459class HCurrentMethod : public HExpression<0> {
2460 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002461 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2462 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002463
2464 DECLARE_INSTRUCTION(CurrentMethod);
2465
2466 private:
2467 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2468};
2469
Mark Mendellfe57faa2015-09-18 09:26:15 -04002470// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2471// have one successor for each entry in the switch table, and the final successor
2472// will be the block containing the next Dex opcode.
2473class HPackedSwitch : public HTemplateInstruction<1> {
2474 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002475 HPackedSwitch(int32_t start_value,
2476 uint32_t num_entries,
2477 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002478 uint32_t dex_pc = kNoDexPc)
2479 : HTemplateInstruction(SideEffects::None(), dex_pc),
2480 start_value_(start_value),
2481 num_entries_(num_entries) {
2482 SetRawInputAt(0, input);
2483 }
2484
2485 bool IsControlFlow() const OVERRIDE { return true; }
2486
2487 int32_t GetStartValue() const { return start_value_; }
2488
Vladimir Marko211c2112015-09-24 16:52:33 +01002489 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002490
2491 HBasicBlock* GetDefaultBlock() const {
2492 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002493 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002494 }
2495 DECLARE_INSTRUCTION(PackedSwitch);
2496
2497 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002498 const int32_t start_value_;
2499 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002500
2501 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2502};
2503
Roland Levillain88cb1752014-10-20 16:36:47 +01002504class HUnaryOperation : public HExpression<1> {
2505 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002506 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2507 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002508 SetRawInputAt(0, input);
2509 }
2510
2511 HInstruction* GetInput() const { return InputAt(0); }
2512 Primitive::Type GetResultType() const { return GetType(); }
2513
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002514 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002515 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002516 return true;
2517 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002518
Roland Levillain9240d6a2014-10-20 16:47:04 +01002519 // Try to statically evaluate `operation` and return a HConstant
2520 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002521 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002522 HConstant* TryStaticEvaluation() const;
2523
2524 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002525 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2526 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002527
Roland Levillain88cb1752014-10-20 16:36:47 +01002528 DECLARE_INSTRUCTION(UnaryOperation);
2529
2530 private:
2531 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2532};
2533
Dave Allison20dfc792014-06-16 20:44:29 -07002534class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002535 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002536 HBinaryOperation(Primitive::Type result_type,
2537 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002538 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002539 SideEffects side_effects = SideEffects::None(),
2540 uint32_t dex_pc = kNoDexPc)
2541 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002542 SetRawInputAt(0, left);
2543 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002544 }
2545
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002546 HInstruction* GetLeft() const { return InputAt(0); }
2547 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002548 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002549
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002550 virtual bool IsCommutative() const { return false; }
2551
2552 // Put constant on the right.
2553 // Returns whether order is changed.
2554 bool OrderInputsWithConstantOnTheRight() {
2555 HInstruction* left = InputAt(0);
2556 HInstruction* right = InputAt(1);
2557 if (left->IsConstant() && !right->IsConstant()) {
2558 ReplaceInput(right, 0);
2559 ReplaceInput(left, 1);
2560 return true;
2561 }
2562 return false;
2563 }
2564
2565 // Order inputs by instruction id, but favor constant on the right side.
2566 // This helps GVN for commutative ops.
2567 void OrderInputs() {
2568 DCHECK(IsCommutative());
2569 HInstruction* left = InputAt(0);
2570 HInstruction* right = InputAt(1);
2571 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2572 return;
2573 }
2574 if (OrderInputsWithConstantOnTheRight()) {
2575 return;
2576 }
2577 // Order according to instruction id.
2578 if (left->GetId() > right->GetId()) {
2579 ReplaceInput(right, 0);
2580 ReplaceInput(left, 1);
2581 }
2582 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002583
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002584 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002585 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002586 return true;
2587 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002588
Roland Levillain9240d6a2014-10-20 16:47:04 +01002589 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002590 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002591 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002592 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002593
2594 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002595 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2596 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2597 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2598 HLongConstant* y ATTRIBUTE_UNUSED) const {
2599 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2600 return nullptr;
2601 }
2602 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2603 HIntConstant* y ATTRIBUTE_UNUSED) const {
2604 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2605 return nullptr;
2606 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002607
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002608 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002609 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002610 HConstant* GetConstantRight() const;
2611
2612 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002613 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002614 HInstruction* GetLeastConstantLeft() const;
2615
Roland Levillainccc07a92014-09-16 14:48:16 +01002616 DECLARE_INSTRUCTION(BinaryOperation);
2617
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002618 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002619 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2620};
2621
Mark Mendellc4701932015-04-10 13:18:51 -04002622// The comparison bias applies for floating point operations and indicates how NaN
2623// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002624enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002625 kNoBias, // bias is not applicable (i.e. for long operation)
2626 kGtBias, // return 1 for NaN comparisons
2627 kLtBias, // return -1 for NaN comparisons
2628};
2629
Dave Allison20dfc792014-06-16 20:44:29 -07002630class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002631 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002632 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2633 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002634 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002635 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002636
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002637 bool NeedsMaterialization() const { return needs_materialization_; }
2638 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002639
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002640 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002641 // `instruction`, and disregard moves in between.
2642 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002643
Dave Allison20dfc792014-06-16 20:44:29 -07002644 DECLARE_INSTRUCTION(Condition);
2645
2646 virtual IfCondition GetCondition() const = 0;
2647
Mark Mendellc4701932015-04-10 13:18:51 -04002648 virtual IfCondition GetOppositeCondition() const = 0;
2649
Roland Levillain4fa13f62015-07-06 18:11:54 +01002650 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002651
2652 void SetBias(ComparisonBias bias) { bias_ = bias; }
2653
2654 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2655 return bias_ == other->AsCondition()->bias_;
2656 }
2657
Roland Levillain4fa13f62015-07-06 18:11:54 +01002658 bool IsFPConditionTrueIfNaN() const {
2659 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2660 IfCondition if_cond = GetCondition();
2661 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2662 }
2663
2664 bool IsFPConditionFalseIfNaN() const {
2665 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2666 IfCondition if_cond = GetCondition();
2667 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2668 }
2669
Dave Allison20dfc792014-06-16 20:44:29 -07002670 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002671 // For register allocation purposes, returns whether this instruction needs to be
2672 // materialized (that is, not just be in the processor flags).
2673 bool needs_materialization_;
2674
Mark Mendellc4701932015-04-10 13:18:51 -04002675 // Needed if we merge a HCompare into a HCondition.
2676 ComparisonBias bias_;
2677
Dave Allison20dfc792014-06-16 20:44:29 -07002678 DISALLOW_COPY_AND_ASSIGN(HCondition);
2679};
2680
2681// Instruction to check if two inputs are equal to each other.
2682class HEqual : public HCondition {
2683 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002684 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2685 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002686
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002687 bool IsCommutative() const OVERRIDE { return true; }
2688
Roland Levillain9867bc72015-08-05 10:21:34 +01002689 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002690 return GetBlock()->GetGraph()->GetIntConstant(
2691 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002692 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002693 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002694 return GetBlock()->GetGraph()->GetIntConstant(
2695 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002696 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002697
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002698 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002699
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002700 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002701 return kCondEQ;
2702 }
2703
Mark Mendellc4701932015-04-10 13:18:51 -04002704 IfCondition GetOppositeCondition() const OVERRIDE {
2705 return kCondNE;
2706 }
2707
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002708 private:
Aart Bike9f37602015-10-09 11:15:55 -07002709 template <typename T> bool Compute(T x, T y) const { return x == y; }
2710
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002711 DISALLOW_COPY_AND_ASSIGN(HEqual);
2712};
2713
Dave Allison20dfc792014-06-16 20:44:29 -07002714class HNotEqual : public HCondition {
2715 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002716 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2717 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002718
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002719 bool IsCommutative() const OVERRIDE { return true; }
2720
Roland Levillain9867bc72015-08-05 10:21:34 +01002721 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002722 return GetBlock()->GetGraph()->GetIntConstant(
2723 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002724 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002725 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002726 return GetBlock()->GetGraph()->GetIntConstant(
2727 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002728 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002729
Dave Allison20dfc792014-06-16 20:44:29 -07002730 DECLARE_INSTRUCTION(NotEqual);
2731
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002732 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002733 return kCondNE;
2734 }
2735
Mark Mendellc4701932015-04-10 13:18:51 -04002736 IfCondition GetOppositeCondition() const OVERRIDE {
2737 return kCondEQ;
2738 }
2739
Dave Allison20dfc792014-06-16 20:44:29 -07002740 private:
Aart Bike9f37602015-10-09 11:15:55 -07002741 template <typename T> bool Compute(T x, T y) const { return x != y; }
2742
Dave Allison20dfc792014-06-16 20:44:29 -07002743 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2744};
2745
2746class HLessThan : public HCondition {
2747 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002748 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2749 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002750
Roland Levillain9867bc72015-08-05 10:21:34 +01002751 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002752 return GetBlock()->GetGraph()->GetIntConstant(
2753 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002754 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002755 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 return GetBlock()->GetGraph()->GetIntConstant(
2757 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002758 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002759
Dave Allison20dfc792014-06-16 20:44:29 -07002760 DECLARE_INSTRUCTION(LessThan);
2761
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002762 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002763 return kCondLT;
2764 }
2765
Mark Mendellc4701932015-04-10 13:18:51 -04002766 IfCondition GetOppositeCondition() const OVERRIDE {
2767 return kCondGE;
2768 }
2769
Dave Allison20dfc792014-06-16 20:44:29 -07002770 private:
Aart Bike9f37602015-10-09 11:15:55 -07002771 template <typename T> bool Compute(T x, T y) const { return x < y; }
2772
Dave Allison20dfc792014-06-16 20:44:29 -07002773 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2774};
2775
2776class HLessThanOrEqual : public HCondition {
2777 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002778 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2779 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002780
Roland Levillain9867bc72015-08-05 10:21:34 +01002781 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002782 return GetBlock()->GetGraph()->GetIntConstant(
2783 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002784 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002785 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002786 return GetBlock()->GetGraph()->GetIntConstant(
2787 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002788 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002789
Dave Allison20dfc792014-06-16 20:44:29 -07002790 DECLARE_INSTRUCTION(LessThanOrEqual);
2791
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002792 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002793 return kCondLE;
2794 }
2795
Mark Mendellc4701932015-04-10 13:18:51 -04002796 IfCondition GetOppositeCondition() const OVERRIDE {
2797 return kCondGT;
2798 }
2799
Dave Allison20dfc792014-06-16 20:44:29 -07002800 private:
Aart Bike9f37602015-10-09 11:15:55 -07002801 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2802
Dave Allison20dfc792014-06-16 20:44:29 -07002803 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2804};
2805
2806class HGreaterThan : public HCondition {
2807 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002808 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2809 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002810
Roland Levillain9867bc72015-08-05 10:21:34 +01002811 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002812 return GetBlock()->GetGraph()->GetIntConstant(
2813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002814 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002815 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 return GetBlock()->GetGraph()->GetIntConstant(
2817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002818 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002819
Dave Allison20dfc792014-06-16 20:44:29 -07002820 DECLARE_INSTRUCTION(GreaterThan);
2821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002822 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002823 return kCondGT;
2824 }
2825
Mark Mendellc4701932015-04-10 13:18:51 -04002826 IfCondition GetOppositeCondition() const OVERRIDE {
2827 return kCondLE;
2828 }
2829
Dave Allison20dfc792014-06-16 20:44:29 -07002830 private:
Aart Bike9f37602015-10-09 11:15:55 -07002831 template <typename T> bool Compute(T x, T y) const { return x > y; }
2832
Dave Allison20dfc792014-06-16 20:44:29 -07002833 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2834};
2835
2836class HGreaterThanOrEqual : public HCondition {
2837 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002838 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2839 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002840
Roland Levillain9867bc72015-08-05 10:21:34 +01002841 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002842 return GetBlock()->GetGraph()->GetIntConstant(
2843 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002844 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002845 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002846 return GetBlock()->GetGraph()->GetIntConstant(
2847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002848 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002849
Dave Allison20dfc792014-06-16 20:44:29 -07002850 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2851
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002852 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002853 return kCondGE;
2854 }
2855
Mark Mendellc4701932015-04-10 13:18:51 -04002856 IfCondition GetOppositeCondition() const OVERRIDE {
2857 return kCondLT;
2858 }
2859
Dave Allison20dfc792014-06-16 20:44:29 -07002860 private:
Aart Bike9f37602015-10-09 11:15:55 -07002861 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2862
Dave Allison20dfc792014-06-16 20:44:29 -07002863 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2864};
2865
Aart Bike9f37602015-10-09 11:15:55 -07002866class HBelow : public HCondition {
2867 public:
2868 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2869 : HCondition(first, second, dex_pc) {}
2870
2871 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2872 return GetBlock()->GetGraph()->GetIntConstant(
2873 Compute(static_cast<uint32_t>(x->GetValue()),
2874 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2875 }
2876 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2877 return GetBlock()->GetGraph()->GetIntConstant(
2878 Compute(static_cast<uint64_t>(x->GetValue()),
2879 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2880 }
2881
2882 DECLARE_INSTRUCTION(Below);
2883
2884 IfCondition GetCondition() const OVERRIDE {
2885 return kCondB;
2886 }
2887
2888 IfCondition GetOppositeCondition() const OVERRIDE {
2889 return kCondAE;
2890 }
2891
2892 private:
2893 template <typename T> bool Compute(T x, T y) const { return x < y; }
2894
2895 DISALLOW_COPY_AND_ASSIGN(HBelow);
2896};
2897
2898class HBelowOrEqual : public HCondition {
2899 public:
2900 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2901 : HCondition(first, second, dex_pc) {}
2902
2903 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2904 return GetBlock()->GetGraph()->GetIntConstant(
2905 Compute(static_cast<uint32_t>(x->GetValue()),
2906 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2907 }
2908 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2909 return GetBlock()->GetGraph()->GetIntConstant(
2910 Compute(static_cast<uint64_t>(x->GetValue()),
2911 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2912 }
2913
2914 DECLARE_INSTRUCTION(BelowOrEqual);
2915
2916 IfCondition GetCondition() const OVERRIDE {
2917 return kCondBE;
2918 }
2919
2920 IfCondition GetOppositeCondition() const OVERRIDE {
2921 return kCondA;
2922 }
2923
2924 private:
2925 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2926
2927 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2928};
2929
2930class HAbove : public HCondition {
2931 public:
2932 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2933 : HCondition(first, second, dex_pc) {}
2934
2935 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2936 return GetBlock()->GetGraph()->GetIntConstant(
2937 Compute(static_cast<uint32_t>(x->GetValue()),
2938 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2939 }
2940 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2941 return GetBlock()->GetGraph()->GetIntConstant(
2942 Compute(static_cast<uint64_t>(x->GetValue()),
2943 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2944 }
2945
2946 DECLARE_INSTRUCTION(Above);
2947
2948 IfCondition GetCondition() const OVERRIDE {
2949 return kCondA;
2950 }
2951
2952 IfCondition GetOppositeCondition() const OVERRIDE {
2953 return kCondBE;
2954 }
2955
2956 private:
2957 template <typename T> bool Compute(T x, T y) const { return x > y; }
2958
2959 DISALLOW_COPY_AND_ASSIGN(HAbove);
2960};
2961
2962class HAboveOrEqual : public HCondition {
2963 public:
2964 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2965 : HCondition(first, second, dex_pc) {}
2966
2967 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2968 return GetBlock()->GetGraph()->GetIntConstant(
2969 Compute(static_cast<uint32_t>(x->GetValue()),
2970 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2971 }
2972 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2973 return GetBlock()->GetGraph()->GetIntConstant(
2974 Compute(static_cast<uint64_t>(x->GetValue()),
2975 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2976 }
2977
2978 DECLARE_INSTRUCTION(AboveOrEqual);
2979
2980 IfCondition GetCondition() const OVERRIDE {
2981 return kCondAE;
2982 }
2983
2984 IfCondition GetOppositeCondition() const OVERRIDE {
2985 return kCondB;
2986 }
2987
2988 private:
2989 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2990
2991 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
2992};
Dave Allison20dfc792014-06-16 20:44:29 -07002993
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002994// Instruction to check how two inputs compare to each other.
2995// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2996class HCompare : public HBinaryOperation {
2997 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002998 HCompare(Primitive::Type type,
2999 HInstruction* first,
3000 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003001 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003002 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003003 : HBinaryOperation(Primitive::kPrimInt,
3004 first,
3005 second,
3006 SideEffectsForArchRuntimeCalls(type),
3007 dex_pc),
3008 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003009 DCHECK_EQ(type, first->GetType());
3010 DCHECK_EQ(type, second->GetType());
3011 }
3012
Roland Levillain9867bc72015-08-05 10:21:34 +01003013 template <typename T>
3014 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003015
Roland Levillain9867bc72015-08-05 10:21:34 +01003016 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003017 return GetBlock()->GetGraph()->GetIntConstant(
3018 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003019 }
3020 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003021 return GetBlock()->GetGraph()->GetIntConstant(
3022 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003023 }
3024
Calin Juravleddb7df22014-11-25 20:56:51 +00003025 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3026 return bias_ == other->AsCompare()->bias_;
3027 }
3028
Mark Mendellc4701932015-04-10 13:18:51 -04003029 ComparisonBias GetBias() const { return bias_; }
3030
Roland Levillain4fa13f62015-07-06 18:11:54 +01003031 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003032
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003033
3034 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3035 // MIPS64 uses a runtime call for FP comparisons.
3036 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3037 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003038
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003039 DECLARE_INSTRUCTION(Compare);
3040
3041 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003042 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003043
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003044 DISALLOW_COPY_AND_ASSIGN(HCompare);
3045};
3046
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003047// A local in the graph. Corresponds to a Dex register.
3048class HLocal : public HTemplateInstruction<0> {
3049 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003050 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003051 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003052
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003053 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003054
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003055 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003056
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003057 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003058 // The Dex register number.
3059 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003060
3061 DISALLOW_COPY_AND_ASSIGN(HLocal);
3062};
3063
3064// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003065class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003066 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003067 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3068 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003069 SetRawInputAt(0, local);
3070 }
3071
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003072 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3073
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003074 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003075
3076 private:
3077 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3078};
3079
3080// Store a value in a given local. This instruction has two inputs: the value
3081// and the local.
3082class HStoreLocal : public HTemplateInstruction<2> {
3083 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003084 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3085 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003086 SetRawInputAt(0, local);
3087 SetRawInputAt(1, value);
3088 }
3089
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003090 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3091
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003092 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003093
3094 private:
3095 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3096};
3097
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003098class HFloatConstant : public HConstant {
3099 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003100 float GetValue() const { return value_; }
3101
David Brazdil77a48ae2015-09-15 12:34:04 +00003102 uint64_t GetValueAsUint64() const OVERRIDE {
3103 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3104 }
3105
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003106 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003107 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003108 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003109 }
3110
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003111 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003112
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003113 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003114 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003115 }
3116 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003117 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003118 }
3119 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003120 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3121 }
3122 bool IsNaN() const {
3123 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003124 }
3125
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003126 DECLARE_INSTRUCTION(FloatConstant);
3127
3128 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003129 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3130 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3131 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3132 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003133
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003134 const float value_;
3135
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003136 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003137 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003138 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003139 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3140};
3141
3142class HDoubleConstant : public HConstant {
3143 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003144 double GetValue() const { return value_; }
3145
David Brazdil77a48ae2015-09-15 12:34:04 +00003146 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3147
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003148 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003149 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003150 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003151 }
3152
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003153 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003154
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003155 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003156 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003157 }
3158 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003159 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003160 }
3161 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003162 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3163 }
3164 bool IsNaN() const {
3165 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003166 }
3167
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003168 DECLARE_INSTRUCTION(DoubleConstant);
3169
3170 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003171 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3172 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3173 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3174 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003175
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003176 const double value_;
3177
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003178 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003179 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003180 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003181 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3182};
3183
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003184enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003185#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003186#include "intrinsics_list.h"
3187 kNone,
3188 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3189#undef INTRINSICS_LIST
3190#undef OPTIMIZING_INTRINSICS
3191};
3192std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3193
Agi Csaki05f20562015-08-19 14:58:14 -07003194enum IntrinsicNeedsEnvironmentOrCache {
3195 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3196 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003197};
3198
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003199class HInvoke : public HInstruction {
3200 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003201 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003202
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003203 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003204
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003205 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003206 SetRawInputAt(index, argument);
3207 }
3208
Roland Levillain3e3d7332015-04-28 11:00:54 +01003209 // Return the number of arguments. This number can be lower than
3210 // the number of inputs returned by InputCount(), as some invoke
3211 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3212 // inputs at the end of their list of inputs.
3213 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3214
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003215 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003216
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003217
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003218 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003219 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003220
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003221 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3222
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003223 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003224 return intrinsic_;
3225 }
3226
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003227 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003228
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003229 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003230 return GetEnvironment()->GetParent() != nullptr;
3231 }
3232
3233 bool CanThrow() const OVERRIDE { return true; }
3234
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003235 uint32_t* GetIntrinsicOptimizations() {
3236 return &intrinsic_optimizations_;
3237 }
3238
3239 const uint32_t* GetIntrinsicOptimizations() const {
3240 return &intrinsic_optimizations_;
3241 }
3242
3243 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3244
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003245 DECLARE_INSTRUCTION(Invoke);
3246
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003247 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003248 HInvoke(ArenaAllocator* arena,
3249 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003250 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003251 Primitive::Type return_type,
3252 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003253 uint32_t dex_method_index,
3254 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003255 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003256 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003257 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003258 inputs_(number_of_arguments + number_of_other_inputs,
3259 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003260 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003261 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003262 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003263 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003264 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003265 }
3266
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003267 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003268 return inputs_[index];
3269 }
3270
David Brazdil1abb4192015-02-17 18:33:36 +00003271 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003272 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003273 }
3274
Roland Levillain3e3d7332015-04-28 11:00:54 +01003275 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003276 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003277 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003278 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003279 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003280 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003281
3282 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3283 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003284
3285 private:
3286 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3287};
3288
Calin Juravle175dc732015-08-25 15:42:32 +01003289class HInvokeUnresolved : public HInvoke {
3290 public:
3291 HInvokeUnresolved(ArenaAllocator* arena,
3292 uint32_t number_of_arguments,
3293 Primitive::Type return_type,
3294 uint32_t dex_pc,
3295 uint32_t dex_method_index,
3296 InvokeType invoke_type)
3297 : HInvoke(arena,
3298 number_of_arguments,
3299 0u /* number_of_other_inputs */,
3300 return_type,
3301 dex_pc,
3302 dex_method_index,
3303 invoke_type) {
3304 }
3305
3306 DECLARE_INSTRUCTION(InvokeUnresolved);
3307
3308 private:
3309 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3310};
3311
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003312class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003313 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003314 // Requirements of this method call regarding the class
3315 // initialization (clinit) check of its declaring class.
3316 enum class ClinitCheckRequirement {
3317 kNone, // Class already initialized.
3318 kExplicit, // Static call having explicit clinit check as last input.
3319 kImplicit, // Static call implicitly requiring a clinit check.
3320 };
3321
Vladimir Marko58155012015-08-19 12:49:41 +00003322 // Determines how to load the target ArtMethod*.
3323 enum class MethodLoadKind {
3324 // Use a String init ArtMethod* loaded from Thread entrypoints.
3325 kStringInit,
3326
3327 // Use the method's own ArtMethod* loaded by the register allocator.
3328 kRecursive,
3329
3330 // Use ArtMethod* at a known address, embed the direct address in the code.
3331 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3332 kDirectAddress,
3333
3334 // Use ArtMethod* at an address that will be known at link time, embed the direct
3335 // address in the code. If the image is relocatable, emit .patch_oat entry.
3336 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3337 // the image relocatable or not.
3338 kDirectAddressWithFixup,
3339
3340 // Load from resoved methods array in the dex cache using a PC-relative load.
3341 // Used when we need to use the dex cache, for example for invoke-static that
3342 // may cause class initialization (the entry may point to a resolution method),
3343 // and we know that we can access the dex cache arrays using a PC-relative load.
3344 kDexCachePcRelative,
3345
3346 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3347 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3348 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3349 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3350 kDexCacheViaMethod,
3351 };
3352
3353 // Determines the location of the code pointer.
3354 enum class CodePtrLocation {
3355 // Recursive call, use local PC-relative call instruction.
3356 kCallSelf,
3357
3358 // Use PC-relative call instruction patched at link time.
3359 // Used for calls within an oat file, boot->boot or app->app.
3360 kCallPCRelative,
3361
3362 // Call to a known target address, embed the direct address in code.
3363 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3364 kCallDirect,
3365
3366 // Call to a target address that will be known at link time, embed the direct
3367 // address in code. If the image is relocatable, emit .patch_oat entry.
3368 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3369 // the image relocatable or not.
3370 kCallDirectWithFixup,
3371
3372 // Use code pointer from the ArtMethod*.
3373 // Used when we don't know the target code. This is also the last-resort-kind used when
3374 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3375 kCallArtMethod,
3376 };
3377
3378 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003379 MethodLoadKind method_load_kind;
3380 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003381 // The method load data holds
3382 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3383 // Note that there are multiple string init methods, each having its own offset.
3384 // - the method address for kDirectAddress
3385 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003386 uint64_t method_load_data;
3387 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003388 };
3389
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003390 HInvokeStaticOrDirect(ArenaAllocator* arena,
3391 uint32_t number_of_arguments,
3392 Primitive::Type return_type,
3393 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003394 uint32_t method_index,
3395 MethodReference target_method,
3396 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003397 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003398 InvokeType invoke_type,
3399 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003400 : HInvoke(arena,
3401 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003402 // There is one extra argument for the HCurrentMethod node, and
3403 // potentially one other if the clinit check is explicit, and one other
3404 // if the method is a string factory.
3405 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003406 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003407 return_type,
3408 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003409 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003410 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003411 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003412 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003413 target_method_(target_method),
3414 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003415
Vladimir Markodc151b22015-10-15 18:02:30 +01003416 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
3417 dispatch_info_ = dispatch_info;
3418 }
3419
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003420 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003421 // We access the method via the dex cache so we can't do an implicit null check.
3422 // TODO: for intrinsics we can generate implicit null checks.
3423 return false;
3424 }
3425
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003426 bool CanBeNull() const OVERRIDE {
3427 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3428 }
3429
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003430 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003431 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3432 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3433 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003434 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003435 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003436 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003437 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003438 bool HasPcRelDexCache() const {
3439 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3440 }
Vladimir Marko58155012015-08-19 12:49:41 +00003441 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3442 MethodReference GetTargetMethod() const { return target_method_; }
3443
3444 int32_t GetStringInitOffset() const {
3445 DCHECK(IsStringInit());
3446 return dispatch_info_.method_load_data;
3447 }
3448
3449 uint64_t GetMethodAddress() const {
3450 DCHECK(HasMethodAddress());
3451 return dispatch_info_.method_load_data;
3452 }
3453
3454 uint32_t GetDexCacheArrayOffset() const {
3455 DCHECK(HasPcRelDexCache());
3456 return dispatch_info_.method_load_data;
3457 }
3458
3459 uint64_t GetDirectCodePtr() const {
3460 DCHECK(HasDirectCodePtr());
3461 return dispatch_info_.direct_code_ptr;
3462 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003463
Calin Juravle68ad6492015-08-18 17:08:12 +01003464 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3465
Roland Levillain4c0eb422015-04-24 16:43:49 +01003466 // Is this instruction a call to a static method?
3467 bool IsStatic() const {
3468 return GetInvokeType() == kStatic;
3469 }
3470
Roland Levillain3e3d7332015-04-28 11:00:54 +01003471 // Remove the art::HLoadClass instruction set as last input by
3472 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3473 // the initial art::HClinitCheck instruction (only relevant for
3474 // static calls with explicit clinit check).
3475 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003476 DCHECK(IsStaticWithExplicitClinitCheck());
3477 size_t last_input_index = InputCount() - 1;
3478 HInstruction* last_input = InputAt(last_input_index);
3479 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003480 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003481 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003482 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003483 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3484 DCHECK(IsStaticWithImplicitClinitCheck());
3485 }
3486
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003487 bool IsStringFactoryFor(HFakeString* str) const {
3488 if (!IsStringInit()) return false;
3489 // +1 for the current method.
3490 if (InputCount() == (number_of_arguments_ + 1)) return false;
3491 return InputAt(InputCount() - 1)->AsFakeString() == str;
3492 }
3493
3494 void RemoveFakeStringArgumentAsLastInput() {
3495 DCHECK(IsStringInit());
3496 size_t last_input_index = InputCount() - 1;
3497 HInstruction* last_input = InputAt(last_input_index);
3498 DCHECK(last_input != nullptr);
3499 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3500 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003501 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003502 }
3503
Roland Levillain4c0eb422015-04-24 16:43:49 +01003504 // Is this a call to a static method whose declaring class has an
3505 // explicit intialization check in the graph?
3506 bool IsStaticWithExplicitClinitCheck() const {
3507 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3508 }
3509
3510 // Is this a call to a static method whose declaring class has an
3511 // implicit intialization check requirement?
3512 bool IsStaticWithImplicitClinitCheck() const {
3513 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3514 }
3515
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003516 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003517
Roland Levillain4c0eb422015-04-24 16:43:49 +01003518 protected:
3519 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3520 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3521 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3522 HInstruction* input = input_record.GetInstruction();
3523 // `input` is the last input of a static invoke marked as having
3524 // an explicit clinit check. It must either be:
3525 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3526 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3527 DCHECK(input != nullptr);
3528 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3529 }
3530 return input_record;
3531 }
3532
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003533 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003534 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003535 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003536 // The target method may refer to different dex file or method index than the original
3537 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3538 // in derived class to increase visibility.
3539 MethodReference target_method_;
3540 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003541
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003542 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003543};
3544
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003545class HInvokeVirtual : public HInvoke {
3546 public:
3547 HInvokeVirtual(ArenaAllocator* arena,
3548 uint32_t number_of_arguments,
3549 Primitive::Type return_type,
3550 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003551 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003552 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003553 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003554 vtable_index_(vtable_index) {}
3555
Calin Juravle641547a2015-04-21 22:08:51 +01003556 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003557 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003558 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003559 }
3560
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003561 uint32_t GetVTableIndex() const { return vtable_index_; }
3562
3563 DECLARE_INSTRUCTION(InvokeVirtual);
3564
3565 private:
3566 const uint32_t vtable_index_;
3567
3568 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3569};
3570
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003571class HInvokeInterface : public HInvoke {
3572 public:
3573 HInvokeInterface(ArenaAllocator* arena,
3574 uint32_t number_of_arguments,
3575 Primitive::Type return_type,
3576 uint32_t dex_pc,
3577 uint32_t dex_method_index,
3578 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003579 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003580 imt_index_(imt_index) {}
3581
Calin Juravle641547a2015-04-21 22:08:51 +01003582 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003583 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003584 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003585 }
3586
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003587 uint32_t GetImtIndex() const { return imt_index_; }
3588 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3589
3590 DECLARE_INSTRUCTION(InvokeInterface);
3591
3592 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003593 const uint32_t imt_index_;
3594
3595 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3596};
3597
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003598class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003599 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003600 HNewInstance(HCurrentMethod* current_method,
3601 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003602 uint16_t type_index,
3603 const DexFile& dex_file,
3604 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003605 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003606 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003607 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003608 entrypoint_(entrypoint) {
3609 SetRawInputAt(0, current_method);
3610 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003611
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003612 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003613 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003614
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003615 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003616 bool NeedsEnvironment() const OVERRIDE { return true; }
Andreas Gampe55d02cf2015-10-29 02:59:50 +00003617 // It may throw when called on:
3618 // - interfaces
3619 // - abstract/innaccessible/unknown classes
3620 // TODO: optimize when possible.
3621 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003622
Calin Juravle10e244f2015-01-26 18:54:32 +00003623 bool CanBeNull() const OVERRIDE { return false; }
3624
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003625 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3626
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003627 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003628
3629 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003630 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003631 const DexFile& dex_file_;
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),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004241 is_live_(false),
Calin Juravle10e244f2015-01-26 18:54:32 +00004242 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_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004266 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),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004488 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
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004506 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) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004753 // The entrypoint the code generator is going to call does not do
4754 // clinit of the class.
4755 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004756 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004757 }
4758
4759 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004760 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4761 }
4762
4763 bool NeedsAccessCheck() const {
4764 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004765 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004766
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004767 bool CanThrow() const OVERRIDE {
4768 // May call runtime and and therefore can throw.
4769 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004770 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004771 }
4772
Calin Juravleacf735c2015-02-12 15:25:22 +00004773 ReferenceTypeInfo GetLoadedClassRTI() {
4774 return loaded_class_rti_;
4775 }
4776
4777 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4778 // Make sure we only set exact types (the loaded class should never be merged).
4779 DCHECK(rti.IsExact());
4780 loaded_class_rti_ = rti;
4781 }
4782
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004783 const DexFile& GetDexFile() { return dex_file_; }
4784
Vladimir Markodc151b22015-10-15 18:02:30 +01004785 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004786
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004787 static SideEffects SideEffectsForArchRuntimeCalls() {
4788 return SideEffects::CanTriggerGC();
4789 }
4790
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004791 DECLARE_INSTRUCTION(LoadClass);
4792
4793 private:
4794 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004795 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004796 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004797 // Whether this instruction must generate the initialization check.
4798 // Used for code generation.
4799 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004800 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004801
Calin Juravleacf735c2015-02-12 15:25:22 +00004802 ReferenceTypeInfo loaded_class_rti_;
4803
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004804 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4805};
4806
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004807class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004808 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004809 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004810 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4811 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004812 SetRawInputAt(0, current_method);
4813 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004814
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004815 bool CanBeMoved() const OVERRIDE { return true; }
4816
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004817 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4818 return other->AsLoadString()->string_index_ == string_index_;
4819 }
4820
4821 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4822
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004823 uint32_t GetStringIndex() const { return string_index_; }
4824
4825 // TODO: Can we deopt or debug when we resolve a string?
4826 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004827 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004828 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004829
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004830 static SideEffects SideEffectsForArchRuntimeCalls() {
4831 return SideEffects::CanTriggerGC();
4832 }
4833
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004834 DECLARE_INSTRUCTION(LoadString);
4835
4836 private:
4837 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004838
4839 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4840};
4841
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004842/**
4843 * Performs an initialization check on its Class object input.
4844 */
4845class HClinitCheck : public HExpression<1> {
4846 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004847 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004848 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004849 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004850 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4851 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004852 SetRawInputAt(0, constant);
4853 }
4854
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004855 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004856 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004857 return true;
4858 }
4859
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004860 bool NeedsEnvironment() const OVERRIDE {
4861 // May call runtime to initialize the class.
4862 return true;
4863 }
4864
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004865
4866 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4867
4868 DECLARE_INSTRUCTION(ClinitCheck);
4869
4870 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004871 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4872};
4873
4874class HStaticFieldGet : public HExpression<1> {
4875 public:
4876 HStaticFieldGet(HInstruction* cls,
4877 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004878 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004879 bool is_volatile,
4880 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004881 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004882 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004883 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004884 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004885 : HExpression(field_type,
4886 SideEffects::FieldReadOfType(field_type, is_volatile),
4887 dex_pc),
4888 field_info_(field_offset,
4889 field_type,
4890 is_volatile,
4891 field_idx,
4892 declaring_class_def_index,
4893 dex_file,
4894 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004895 SetRawInputAt(0, cls);
4896 }
4897
Calin Juravle52c48962014-12-16 17:02:57 +00004898
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004899 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004900
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004901 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004902 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4903 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004904 }
4905
4906 size_t ComputeHashCode() const OVERRIDE {
4907 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4908 }
4909
Calin Juravle52c48962014-12-16 17:02:57 +00004910 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004911 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4912 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004913 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004914
4915 DECLARE_INSTRUCTION(StaticFieldGet);
4916
4917 private:
4918 const FieldInfo field_info_;
4919
4920 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4921};
4922
4923class HStaticFieldSet : public HTemplateInstruction<2> {
4924 public:
4925 HStaticFieldSet(HInstruction* cls,
4926 HInstruction* value,
4927 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004928 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004929 bool is_volatile,
4930 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004931 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004932 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004933 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004934 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004935 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4936 dex_pc),
4937 field_info_(field_offset,
4938 field_type,
4939 is_volatile,
4940 field_idx,
4941 declaring_class_def_index,
4942 dex_file,
4943 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004944 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004945 SetRawInputAt(0, cls);
4946 SetRawInputAt(1, value);
4947 }
4948
Calin Juravle52c48962014-12-16 17:02:57 +00004949 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004950 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4951 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004952 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004953
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004954 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004955 bool GetValueCanBeNull() const { return value_can_be_null_; }
4956 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004957
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004958 DECLARE_INSTRUCTION(StaticFieldSet);
4959
4960 private:
4961 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004962 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004963
4964 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4965};
4966
Calin Juravlee460d1d2015-09-29 04:52:17 +01004967class HUnresolvedInstanceFieldGet : public HExpression<1> {
4968 public:
4969 HUnresolvedInstanceFieldGet(HInstruction* obj,
4970 Primitive::Type field_type,
4971 uint32_t field_index,
4972 uint32_t dex_pc)
4973 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4974 field_index_(field_index) {
4975 SetRawInputAt(0, obj);
4976 }
4977
4978 bool NeedsEnvironment() const OVERRIDE { return true; }
4979 bool CanThrow() const OVERRIDE { return true; }
4980
4981 Primitive::Type GetFieldType() const { return GetType(); }
4982 uint32_t GetFieldIndex() const { return field_index_; }
4983
4984 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4985
4986 private:
4987 const uint32_t field_index_;
4988
4989 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4990};
4991
4992class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4993 public:
4994 HUnresolvedInstanceFieldSet(HInstruction* obj,
4995 HInstruction* value,
4996 Primitive::Type field_type,
4997 uint32_t field_index,
4998 uint32_t dex_pc)
4999 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5000 field_type_(field_type),
5001 field_index_(field_index) {
5002 DCHECK_EQ(field_type, value->GetType());
5003 SetRawInputAt(0, obj);
5004 SetRawInputAt(1, value);
5005 }
5006
5007 bool NeedsEnvironment() const OVERRIDE { return true; }
5008 bool CanThrow() const OVERRIDE { return true; }
5009
5010 Primitive::Type GetFieldType() const { return field_type_; }
5011 uint32_t GetFieldIndex() const { return field_index_; }
5012
5013 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5014
5015 private:
5016 const Primitive::Type field_type_;
5017 const uint32_t field_index_;
5018
5019 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5020};
5021
5022class HUnresolvedStaticFieldGet : public HExpression<0> {
5023 public:
5024 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5025 uint32_t field_index,
5026 uint32_t dex_pc)
5027 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5028 field_index_(field_index) {
5029 }
5030
5031 bool NeedsEnvironment() const OVERRIDE { return true; }
5032 bool CanThrow() const OVERRIDE { return true; }
5033
5034 Primitive::Type GetFieldType() const { return GetType(); }
5035 uint32_t GetFieldIndex() const { return field_index_; }
5036
5037 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5038
5039 private:
5040 const uint32_t field_index_;
5041
5042 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5043};
5044
5045class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5046 public:
5047 HUnresolvedStaticFieldSet(HInstruction* value,
5048 Primitive::Type field_type,
5049 uint32_t field_index,
5050 uint32_t dex_pc)
5051 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5052 field_type_(field_type),
5053 field_index_(field_index) {
5054 DCHECK_EQ(field_type, value->GetType());
5055 SetRawInputAt(0, value);
5056 }
5057
5058 bool NeedsEnvironment() const OVERRIDE { return true; }
5059 bool CanThrow() const OVERRIDE { return true; }
5060
5061 Primitive::Type GetFieldType() const { return field_type_; }
5062 uint32_t GetFieldIndex() const { return field_index_; }
5063
5064 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5065
5066 private:
5067 const Primitive::Type field_type_;
5068 const uint32_t field_index_;
5069
5070 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5071};
5072
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005073// Implement the move-exception DEX instruction.
5074class HLoadException : public HExpression<0> {
5075 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005076 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5077 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005078
David Brazdilbbd733e2015-08-18 17:48:17 +01005079 bool CanBeNull() const OVERRIDE { return false; }
5080
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005081 DECLARE_INSTRUCTION(LoadException);
5082
5083 private:
5084 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5085};
5086
David Brazdilcb1c0552015-08-04 16:22:25 +01005087// Implicit part of move-exception which clears thread-local exception storage.
5088// Must not be removed because the runtime expects the TLS to get cleared.
5089class HClearException : public HTemplateInstruction<0> {
5090 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005091 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5092 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005093
5094 DECLARE_INSTRUCTION(ClearException);
5095
5096 private:
5097 DISALLOW_COPY_AND_ASSIGN(HClearException);
5098};
5099
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005100class HThrow : public HTemplateInstruction<1> {
5101 public:
5102 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005103 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005104 SetRawInputAt(0, exception);
5105 }
5106
5107 bool IsControlFlow() const OVERRIDE { return true; }
5108
5109 bool NeedsEnvironment() const OVERRIDE { return true; }
5110
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005111 bool CanThrow() const OVERRIDE { return true; }
5112
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005113
5114 DECLARE_INSTRUCTION(Throw);
5115
5116 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005117 DISALLOW_COPY_AND_ASSIGN(HThrow);
5118};
5119
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005120/**
5121 * Implementation strategies for the code generator of a HInstanceOf
5122 * or `HCheckCast`.
5123 */
5124enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005125 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005126 kExactCheck, // Can do a single class compare.
5127 kClassHierarchyCheck, // Can just walk the super class chain.
5128 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5129 kInterfaceCheck, // No optimization yet when checking against an interface.
5130 kArrayObjectCheck, // Can just check if the array is not primitive.
5131 kArrayCheck // No optimization yet when checking against a generic array.
5132};
5133
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005134class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005135 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005136 HInstanceOf(HInstruction* object,
5137 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005138 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005139 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005140 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005141 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005142 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005143 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005144 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005145 SetRawInputAt(0, object);
5146 SetRawInputAt(1, constant);
5147 }
5148
5149 bool CanBeMoved() const OVERRIDE { return true; }
5150
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005151 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005152 return true;
5153 }
5154
5155 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005156 return false;
5157 }
5158
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005159 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5160
5161 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005162
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005163 // Used only in code generation.
5164 bool MustDoNullCheck() const { return must_do_null_check_; }
5165 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5166
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005167 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5168 return (check_kind == TypeCheckKind::kExactCheck)
5169 ? SideEffects::None()
5170 // Mips currently does runtime calls for any other checks.
5171 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005172 }
5173
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005174 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005175
5176 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005177 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005178 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005179
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005180 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5181};
5182
Calin Juravleb1498f62015-02-16 13:13:29 +00005183class HBoundType : public HExpression<1> {
5184 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005185 // Constructs an HBoundType with the given upper_bound.
5186 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005187 HBoundType(HInstruction* input,
5188 ReferenceTypeInfo upper_bound,
5189 bool upper_can_be_null,
5190 uint32_t dex_pc = kNoDexPc)
5191 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005192 upper_bound_(upper_bound),
5193 upper_can_be_null_(upper_can_be_null),
5194 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005195 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005196 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005197 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005198 }
5199
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005200 // GetUpper* should only be used in reference type propagation.
5201 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5202 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005203
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005204 void SetCanBeNull(bool can_be_null) {
5205 DCHECK(upper_can_be_null_ || !can_be_null);
5206 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005207 }
5208
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005209 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5210
Calin Juravleb1498f62015-02-16 13:13:29 +00005211 DECLARE_INSTRUCTION(BoundType);
5212
5213 private:
5214 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005215 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5216 // It is used to bound the type in cases like:
5217 // if (x instanceof ClassX) {
5218 // // uper_bound_ will be ClassX
5219 // }
5220 const ReferenceTypeInfo upper_bound_;
5221 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5222 // is false then can_be_null_ cannot be true).
5223 const bool upper_can_be_null_;
5224 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005225
5226 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5227};
5228
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005229class HCheckCast : public HTemplateInstruction<2> {
5230 public:
5231 HCheckCast(HInstruction* object,
5232 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005233 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005234 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005235 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005236 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005237 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005238 SetRawInputAt(0, object);
5239 SetRawInputAt(1, constant);
5240 }
5241
5242 bool CanBeMoved() const OVERRIDE { return true; }
5243
5244 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5245 return true;
5246 }
5247
5248 bool NeedsEnvironment() const OVERRIDE {
5249 // Instruction may throw a CheckCastError.
5250 return true;
5251 }
5252
5253 bool CanThrow() const OVERRIDE { return true; }
5254
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005255 bool MustDoNullCheck() const { return must_do_null_check_; }
5256 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005257 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005258
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005259 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005260
5261 DECLARE_INSTRUCTION(CheckCast);
5262
5263 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005264 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005265 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005266
5267 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005268};
5269
Calin Juravle27df7582015-04-17 19:12:31 +01005270class HMemoryBarrier : public HTemplateInstruction<0> {
5271 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005272 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005273 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005274 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005275 barrier_kind_(barrier_kind) {}
5276
5277 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5278
5279 DECLARE_INSTRUCTION(MemoryBarrier);
5280
5281 private:
5282 const MemBarrierKind barrier_kind_;
5283
5284 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5285};
5286
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005287class HMonitorOperation : public HTemplateInstruction<1> {
5288 public:
5289 enum OperationKind {
5290 kEnter,
5291 kExit,
5292 };
5293
5294 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005295 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005296 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5297 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005298 SetRawInputAt(0, object);
5299 }
5300
5301 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005302 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5303
5304 bool CanThrow() const OVERRIDE {
5305 // Verifier guarantees that monitor-exit cannot throw.
5306 // This is important because it allows the HGraphBuilder to remove
5307 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5308 return IsEnter();
5309 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005310
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005311
5312 bool IsEnter() const { return kind_ == kEnter; }
5313
5314 DECLARE_INSTRUCTION(MonitorOperation);
5315
Calin Juravle52c48962014-12-16 17:02:57 +00005316 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005317 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005318
5319 private:
5320 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5321};
5322
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005323/**
5324 * A HInstruction used as a marker for the replacement of new + <init>
5325 * of a String to a call to a StringFactory. Only baseline will see
5326 * the node at code generation, where it will be be treated as null.
5327 * When compiling non-baseline, `HFakeString` instructions are being removed
5328 * in the instruction simplifier.
5329 */
5330class HFakeString : public HTemplateInstruction<0> {
5331 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005332 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5333 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005334
5335 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5336
5337 DECLARE_INSTRUCTION(FakeString);
5338
5339 private:
5340 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5341};
5342
Vladimir Markof9f64412015-09-02 14:05:49 +01005343class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005344 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005345 MoveOperands(Location source,
5346 Location destination,
5347 Primitive::Type type,
5348 HInstruction* instruction)
5349 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005350
5351 Location GetSource() const { return source_; }
5352 Location GetDestination() const { return destination_; }
5353
5354 void SetSource(Location value) { source_ = value; }
5355 void SetDestination(Location value) { destination_ = value; }
5356
5357 // The parallel move resolver marks moves as "in-progress" by clearing the
5358 // destination (but not the source).
5359 Location MarkPending() {
5360 DCHECK(!IsPending());
5361 Location dest = destination_;
5362 destination_ = Location::NoLocation();
5363 return dest;
5364 }
5365
5366 void ClearPending(Location dest) {
5367 DCHECK(IsPending());
5368 destination_ = dest;
5369 }
5370
5371 bool IsPending() const {
5372 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5373 return destination_.IsInvalid() && !source_.IsInvalid();
5374 }
5375
5376 // True if this blocks a move from the given location.
5377 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005378 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005379 }
5380
5381 // A move is redundant if it's been eliminated, if its source and
5382 // destination are the same, or if its destination is unneeded.
5383 bool IsRedundant() const {
5384 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5385 }
5386
5387 // We clear both operands to indicate move that's been eliminated.
5388 void Eliminate() {
5389 source_ = destination_ = Location::NoLocation();
5390 }
5391
5392 bool IsEliminated() const {
5393 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5394 return source_.IsInvalid();
5395 }
5396
Alexey Frunze4dda3372015-06-01 18:31:49 -07005397 Primitive::Type GetType() const { return type_; }
5398
Nicolas Geoffray90218252015-04-15 11:56:51 +01005399 bool Is64BitMove() const {
5400 return Primitive::Is64BitType(type_);
5401 }
5402
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005403 HInstruction* GetInstruction() const { return instruction_; }
5404
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005405 private:
5406 Location source_;
5407 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005408 // The type this move is for.
5409 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005410 // The instruction this move is assocatied with. Null when this move is
5411 // for moving an input in the expected locations of user (including a phi user).
5412 // This is only used in debug mode, to ensure we do not connect interval siblings
5413 // in the same parallel move.
5414 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005415};
5416
5417static constexpr size_t kDefaultNumberOfMoves = 4;
5418
5419class HParallelMove : public HTemplateInstruction<0> {
5420 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005421 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005422 : HTemplateInstruction(SideEffects::None(), dex_pc),
5423 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5424 moves_.reserve(kDefaultNumberOfMoves);
5425 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005426
Nicolas Geoffray90218252015-04-15 11:56:51 +01005427 void AddMove(Location source,
5428 Location destination,
5429 Primitive::Type type,
5430 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005431 DCHECK(source.IsValid());
5432 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005433 if (kIsDebugBuild) {
5434 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005435 for (const MoveOperands& move : moves_) {
5436 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005437 // Special case the situation where the move is for the spill slot
5438 // of the instruction.
5439 if ((GetPrevious() == instruction)
5440 || ((GetPrevious() == nullptr)
5441 && instruction->IsPhi()
5442 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005443 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005444 << "Doing parallel moves for the same instruction.";
5445 } else {
5446 DCHECK(false) << "Doing parallel moves for the same instruction.";
5447 }
5448 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005449 }
5450 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005451 for (const MoveOperands& move : moves_) {
5452 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005453 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005454 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005455 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005456 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005457 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005458 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005459 }
5460
Vladimir Marko225b6462015-09-28 12:17:40 +01005461 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005462 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005463 }
5464
Vladimir Marko225b6462015-09-28 12:17:40 +01005465 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005466
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005467 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005468
5469 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005470 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005471
5472 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5473};
5474
Mark Mendell0616ae02015-04-17 12:49:27 -04005475} // namespace art
5476
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005477#ifdef ART_ENABLE_CODEGEN_arm64
5478#include "nodes_arm64.h"
5479#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005480#ifdef ART_ENABLE_CODEGEN_x86
5481#include "nodes_x86.h"
5482#endif
5483
5484namespace art {
5485
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005486class HGraphVisitor : public ValueObject {
5487 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005488 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5489 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005490
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005491 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005492 virtual void VisitBasicBlock(HBasicBlock* block);
5493
Roland Levillain633021e2014-10-01 14:12:25 +01005494 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005495 void VisitInsertionOrder();
5496
Roland Levillain633021e2014-10-01 14:12:25 +01005497 // Visit the graph following dominator tree reverse post-order.
5498 void VisitReversePostOrder();
5499
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005500 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005501
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005502 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005503#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005504 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5505
5506 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5507
5508#undef DECLARE_VISIT_INSTRUCTION
5509
5510 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005511 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005512
5513 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5514};
5515
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005516class HGraphDelegateVisitor : public HGraphVisitor {
5517 public:
5518 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5519 virtual ~HGraphDelegateVisitor() {}
5520
5521 // Visit functions that delegate to to super class.
5522#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005523 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005524
5525 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5526
5527#undef DECLARE_VISIT_INSTRUCTION
5528
5529 private:
5530 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5531};
5532
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005533class HInsertionOrderIterator : public ValueObject {
5534 public:
5535 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5536
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005537 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005538 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005539 void Advance() { ++index_; }
5540
5541 private:
5542 const HGraph& graph_;
5543 size_t index_;
5544
5545 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5546};
5547
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005548class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005549 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005550 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5551 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005552 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005553 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005554
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005555 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5556 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005557 void Advance() { ++index_; }
5558
5559 private:
5560 const HGraph& graph_;
5561 size_t index_;
5562
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005563 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005564};
5565
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005566class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005567 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005568 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005569 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005570 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005571 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005572 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005573
5574 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005575 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005576 void Advance() { --index_; }
5577
5578 private:
5579 const HGraph& graph_;
5580 size_t index_;
5581
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005582 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005583};
5584
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005585class HLinearPostOrderIterator : public ValueObject {
5586 public:
5587 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005588 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005589
5590 bool Done() const { return index_ == 0; }
5591
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005592 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005593
5594 void Advance() {
5595 --index_;
5596 DCHECK_GE(index_, 0U);
5597 }
5598
5599 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005600 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005601 size_t index_;
5602
5603 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5604};
5605
5606class HLinearOrderIterator : public ValueObject {
5607 public:
5608 explicit HLinearOrderIterator(const HGraph& graph)
5609 : order_(graph.GetLinearOrder()), index_(0) {}
5610
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005611 bool Done() const { return index_ == order_.size(); }
5612 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005613 void Advance() { ++index_; }
5614
5615 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005616 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005617 size_t index_;
5618
5619 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5620};
5621
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005622// Iterator over the blocks that art part of the loop. Includes blocks part
5623// of an inner loop. The order in which the blocks are iterated is on their
5624// block id.
5625class HBlocksInLoopIterator : public ValueObject {
5626 public:
5627 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5628 : blocks_in_loop_(info.GetBlocks()),
5629 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5630 index_(0) {
5631 if (!blocks_in_loop_.IsBitSet(index_)) {
5632 Advance();
5633 }
5634 }
5635
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005636 bool Done() const { return index_ == blocks_.size(); }
5637 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005638 void Advance() {
5639 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005640 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005641 if (blocks_in_loop_.IsBitSet(index_)) {
5642 break;
5643 }
5644 }
5645 }
5646
5647 private:
5648 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005649 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005650 size_t index_;
5651
5652 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5653};
5654
Mingyao Yang3584bce2015-05-19 16:01:59 -07005655// Iterator over the blocks that art part of the loop. Includes blocks part
5656// of an inner loop. The order in which the blocks are iterated is reverse
5657// post order.
5658class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5659 public:
5660 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5661 : blocks_in_loop_(info.GetBlocks()),
5662 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5663 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005664 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005665 Advance();
5666 }
5667 }
5668
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005669 bool Done() const { return index_ == blocks_.size(); }
5670 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005671 void Advance() {
5672 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005673 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5674 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005675 break;
5676 }
5677 }
5678 }
5679
5680 private:
5681 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005682 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005683 size_t index_;
5684
5685 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5686};
5687
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005688inline int64_t Int64FromConstant(HConstant* constant) {
5689 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5690 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5691 : constant->AsLongConstant()->GetValue();
5692}
5693
Vladimir Marko58155012015-08-19 12:49:41 +00005694inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5695 // For the purposes of the compiler, the dex files must actually be the same object
5696 // if we want to safely treat them as the same. This is especially important for JIT
5697 // as custom class loaders can open the same underlying file (or memory) multiple
5698 // times and provide different class resolution but no two class loaders should ever
5699 // use the same DexFile object - doing so is an unsupported hack that can lead to
5700 // all sorts of weird failures.
5701 return &lhs == &rhs;
5702}
5703
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005704} // namespace art
5705
5706#endif // ART_COMPILER_OPTIMIZING_NODES_H_