blob: 4e8124894a18f4d93a611c840af4182f05f27021 [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
David Brazdil9bc43612015-11-05 21:25:24 +0000840 // Split catch block into two blocks after the original move-exception bytecode
841 // instruction, or at the beginning if not present. Returns the newly created,
842 // latter block, or nullptr if such block could not be created (must be dead
843 // in that case). Note that this method just updates raw block information,
844 // like predecessors, successors, dominators, and instruction list. It does not
845 // update the graph, reverse post order, loop information, nor make sure the
846 // blocks are consistent (for example ending with a control flow instruction).
847 HBasicBlock* SplitCatchBlockAfterMoveException();
848
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000849 // Merge `other` at the end of `this`. Successors and dominated blocks of
850 // `other` are changed to be successors and dominated blocks of `this`. Note
851 // that this method does not update the graph, reverse post order, loop
852 // information, nor make sure the blocks are consistent (for example ending
853 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100854 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000855
856 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
857 // of `this` are moved to `other`.
858 // Note that this method does not update the graph, reverse post order, loop
859 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000860 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000861 void ReplaceWith(HBasicBlock* other);
862
David Brazdil2d7352b2015-04-20 14:52:42 +0100863 // Merge `other` at the end of `this`. This method updates loops, reverse post
864 // order, links to predecessors, successors, dominators and deletes the block
865 // from the graph. The two blocks must be successive, i.e. `this` the only
866 // predecessor of `other` and vice versa.
867 void MergeWith(HBasicBlock* other);
868
869 // Disconnects `this` from all its predecessors, successors and dominator,
870 // removes it from all loops it is included in and eventually from the graph.
871 // The block must not dominate any other block. Predecessors and successors
872 // are safely updated.
873 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000874
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000875 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100876 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100877 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100878 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100879 // Replace instruction `initial` with `replacement` within this block.
880 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
881 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100882 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100883 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000884 // RemoveInstruction and RemovePhi delete a given instruction from the respective
885 // instruction list. With 'ensure_safety' set to true, it verifies that the
886 // instruction is not in use and removes it from the use lists of its inputs.
887 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
888 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100889 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100890
891 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100892 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100893 }
894
Roland Levillain6b879dd2014-09-22 17:13:44 +0100895 bool IsLoopPreHeaderFirstPredecessor() const {
896 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100897 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100898 }
899
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100900 HLoopInformation* GetLoopInformation() const {
901 return loop_information_;
902 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000903
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000904 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100905 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100907 void SetInLoop(HLoopInformation* info) {
908 if (IsLoopHeader()) {
909 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100910 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100911 loop_information_ = info;
912 } else if (loop_information_->Contains(*info->GetHeader())) {
913 // Block is currently part of an outer loop. Make it part of this inner loop.
914 // Note that a non loop header having a loop information means this loop information
915 // has already been populated
916 loop_information_ = info;
917 } else {
918 // Block is part of an inner loop. Do not update the loop information.
919 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
920 // at this point, because this method is being called while populating `info`.
921 }
922 }
923
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000924 // Raw update of the loop information.
925 void SetLoopInformation(HLoopInformation* info) {
926 loop_information_ = info;
927 }
928
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100929 bool IsInLoop() const { return loop_information_ != nullptr; }
930
David Brazdilec16f792015-08-19 15:04:01 +0100931 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
932
933 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
934 try_catch_information_ = try_catch_information;
935 }
936
937 bool IsTryBlock() const {
938 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
939 }
940
941 bool IsCatchBlock() const {
942 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
943 }
David Brazdilffee3d32015-07-06 11:48:53 +0100944
945 // Returns the try entry that this block's successors should have. They will
946 // be in the same try, unless the block ends in a try boundary. In that case,
947 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100948 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100949
David Brazdild7558da2015-09-22 13:04:14 +0100950 bool HasThrowingInstructions() const;
951
David Brazdila4b8c212015-05-07 09:59:30 +0100952 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100953 bool Dominates(HBasicBlock* block) const;
954
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100955 size_t GetLifetimeStart() const { return lifetime_start_; }
956 size_t GetLifetimeEnd() const { return lifetime_end_; }
957
958 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
959 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
960
David Brazdil8d5b8b22015-03-24 10:51:52 +0000961 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000962 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100963 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000964 bool HasSinglePhi() const;
965
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000966 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000967 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000968 ArenaVector<HBasicBlock*> predecessors_;
969 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100970 HInstructionList instructions_;
971 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000972 HLoopInformation* loop_information_;
973 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000974 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100975 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100976 // The dex program counter of the first instruction of this block.
977 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100978 size_t lifetime_start_;
979 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100980 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100981
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000982 friend class HGraph;
983 friend class HInstruction;
984
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000985 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
986};
987
David Brazdilb2bd1c52015-03-25 11:17:37 +0000988// Iterates over the LoopInformation of all loops which contain 'block'
989// from the innermost to the outermost.
990class HLoopInformationOutwardIterator : public ValueObject {
991 public:
992 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
993 : current_(block.GetLoopInformation()) {}
994
995 bool Done() const { return current_ == nullptr; }
996
997 void Advance() {
998 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100999 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001000 }
1001
1002 HLoopInformation* Current() const {
1003 DCHECK(!Done());
1004 return current_;
1005 }
1006
1007 private:
1008 HLoopInformation* current_;
1009
1010 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1011};
1012
Alexandre Ramesef20f712015-06-09 10:29:30 +01001013#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001014 M(Above, Condition) \
1015 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001016 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001017 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001018 M(ArrayGet, Instruction) \
1019 M(ArrayLength, Instruction) \
1020 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001021 M(Below, Condition) \
1022 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001023 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001024 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001025 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001026 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001027 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001028 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001029 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001030 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001031 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001032 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001033 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001034 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001035 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001036 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001037 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001038 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001039 M(FloatConstant, Constant) \
1040 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001041 M(GreaterThan, Condition) \
1042 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001043 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001044 M(InstanceFieldGet, Instruction) \
1045 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001046 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001047 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001048 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001049 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001050 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001051 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001052 M(LessThan, Condition) \
1053 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001054 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001055 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001056 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001057 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001058 M(Local, Instruction) \
1059 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001060 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001061 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001062 M(Mul, BinaryOperation) \
1063 M(Neg, UnaryOperation) \
1064 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001065 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001066 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001067 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001068 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001069 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001070 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001071 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001072 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001073 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001074 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001075 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001076 M(Return, Instruction) \
1077 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001078 M(Shl, BinaryOperation) \
1079 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001080 M(StaticFieldGet, Instruction) \
1081 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001082 M(UnresolvedInstanceFieldGet, Instruction) \
1083 M(UnresolvedInstanceFieldSet, Instruction) \
1084 M(UnresolvedStaticFieldGet, Instruction) \
1085 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001086 M(StoreLocal, Instruction) \
1087 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001088 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001089 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001090 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001091 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001092 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001093 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001094 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001095
Alexandre Ramesef20f712015-06-09 10:29:30 +01001096#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1097
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001098#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001099#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001100#else
1101#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1102 M(Arm64IntermediateAddress, Instruction)
1103#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001104
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001105#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1106
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001107#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1108
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001109#ifndef ART_ENABLE_CODEGEN_x86
1110#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1111#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001112#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1113 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001114 M(X86LoadFromConstantTable, Instruction) \
1115 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001116#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001117
1118#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1119
1120#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1121 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1122 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1123 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001124 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001125 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001126 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1127 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1128
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001129#define FOR_EACH_INSTRUCTION(M) \
1130 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1131 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001132 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001133 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001134 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001135
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001136#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001137FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1138#undef FORWARD_DECLARATION
1139
Roland Levillainccc07a92014-09-16 14:48:16 +01001140#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001141 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1142 const char* DebugName() const OVERRIDE { return #type; } \
1143 const H##type* As##type() const OVERRIDE { return this; } \
1144 H##type* As##type() OVERRIDE { return this; } \
1145 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001146 return other->Is##type(); \
1147 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001148 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001149
David Brazdiled596192015-01-23 10:39:45 +00001150template <typename T> class HUseList;
1151
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001152template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001153class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001154 public:
David Brazdiled596192015-01-23 10:39:45 +00001155 HUseListNode* GetPrevious() const { return prev_; }
1156 HUseListNode* GetNext() const { return next_; }
1157 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001158 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001159 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001160
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001161 private:
David Brazdiled596192015-01-23 10:39:45 +00001162 HUseListNode(T user, size_t index)
1163 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1164
1165 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001166 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001167 HUseListNode<T>* prev_;
1168 HUseListNode<T>* next_;
1169
1170 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001171
1172 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1173};
1174
David Brazdiled596192015-01-23 10:39:45 +00001175template <typename T>
1176class HUseList : public ValueObject {
1177 public:
1178 HUseList() : first_(nullptr) {}
1179
1180 void Clear() {
1181 first_ = nullptr;
1182 }
1183
1184 // Adds a new entry at the beginning of the use list and returns
1185 // the newly created node.
1186 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001187 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001188 if (IsEmpty()) {
1189 first_ = new_node;
1190 } else {
1191 first_->prev_ = new_node;
1192 new_node->next_ = first_;
1193 first_ = new_node;
1194 }
1195 return new_node;
1196 }
1197
1198 HUseListNode<T>* GetFirst() const {
1199 return first_;
1200 }
1201
1202 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001203 DCHECK(node != nullptr);
1204 DCHECK(Contains(node));
1205
David Brazdiled596192015-01-23 10:39:45 +00001206 if (node->prev_ != nullptr) {
1207 node->prev_->next_ = node->next_;
1208 }
1209 if (node->next_ != nullptr) {
1210 node->next_->prev_ = node->prev_;
1211 }
1212 if (node == first_) {
1213 first_ = node->next_;
1214 }
1215 }
1216
David Brazdil1abb4192015-02-17 18:33:36 +00001217 bool Contains(const HUseListNode<T>* node) const {
1218 if (node == nullptr) {
1219 return false;
1220 }
1221 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1222 if (current == node) {
1223 return true;
1224 }
1225 }
1226 return false;
1227 }
1228
David Brazdiled596192015-01-23 10:39:45 +00001229 bool IsEmpty() const {
1230 return first_ == nullptr;
1231 }
1232
1233 bool HasOnlyOneUse() const {
1234 return first_ != nullptr && first_->next_ == nullptr;
1235 }
1236
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001237 size_t SizeSlow() const {
1238 size_t count = 0;
1239 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1240 ++count;
1241 }
1242 return count;
1243 }
1244
David Brazdiled596192015-01-23 10:39:45 +00001245 private:
1246 HUseListNode<T>* first_;
1247};
1248
1249template<typename T>
1250class HUseIterator : public ValueObject {
1251 public:
1252 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1253
1254 bool Done() const { return current_ == nullptr; }
1255
1256 void Advance() {
1257 DCHECK(!Done());
1258 current_ = current_->GetNext();
1259 }
1260
1261 HUseListNode<T>* Current() const {
1262 DCHECK(!Done());
1263 return current_;
1264 }
1265
1266 private:
1267 HUseListNode<T>* current_;
1268
1269 friend class HValue;
1270};
1271
David Brazdil1abb4192015-02-17 18:33:36 +00001272// This class is used by HEnvironment and HInstruction classes to record the
1273// instructions they use and pointers to the corresponding HUseListNodes kept
1274// by the used instructions.
1275template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001276class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001277 public:
1278 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1279 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1280
1281 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1282 : instruction_(old_record.instruction_), use_node_(use_node) {
1283 DCHECK(instruction_ != nullptr);
1284 DCHECK(use_node_ != nullptr);
1285 DCHECK(old_record.use_node_ == nullptr);
1286 }
1287
1288 HInstruction* GetInstruction() const { return instruction_; }
1289 HUseListNode<T>* GetUseNode() const { return use_node_; }
1290
1291 private:
1292 // Instruction used by the user.
1293 HInstruction* instruction_;
1294
1295 // Corresponding entry in the use list kept by 'instruction_'.
1296 HUseListNode<T>* use_node_;
1297};
1298
Aart Bik854a02b2015-07-14 16:07:00 -07001299/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001300 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001301 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001302 * For write/read dependences on fields/arrays, the dependence analysis uses
1303 * type disambiguation (e.g. a float field write cannot modify the value of an
1304 * integer field read) and the access type (e.g. a reference array write cannot
1305 * modify the value of a reference field read [although it may modify the
1306 * reference fetch prior to reading the field, which is represented by its own
1307 * write/read dependence]). The analysis makes conservative points-to
1308 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1309 * the same, and any reference read depends on any reference read without
1310 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001311 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001312 * The internal representation uses 38-bit and is described in the table below.
1313 * The first line indicates the side effect, and for field/array accesses the
1314 * second line indicates the type of the access (in the order of the
1315 * Primitive::Type enum).
1316 * The two numbered lines below indicate the bit position in the bitfield (read
1317 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001318 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001319 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1320 * +-------------+---------+---------+--------------+---------+---------+
1321 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1322 * | 3 |333333322|222222221| 1 |111111110|000000000|
1323 * | 7 |654321098|765432109| 8 |765432109|876543210|
1324 *
1325 * Note that, to ease the implementation, 'changes' bits are least significant
1326 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001327 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001328class SideEffects : public ValueObject {
1329 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001330 SideEffects() : flags_(0) {}
1331
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001332 static SideEffects None() {
1333 return SideEffects(0);
1334 }
1335
1336 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001337 return SideEffects(kAllChangeBits | kAllDependOnBits);
1338 }
1339
1340 static SideEffects AllChanges() {
1341 return SideEffects(kAllChangeBits);
1342 }
1343
1344 static SideEffects AllDependencies() {
1345 return SideEffects(kAllDependOnBits);
1346 }
1347
1348 static SideEffects AllExceptGCDependency() {
1349 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1350 }
1351
1352 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001353 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001354 }
1355
Aart Bik34c3ba92015-07-20 14:08:59 -07001356 static SideEffects AllWrites() {
1357 return SideEffects(kAllWrites);
1358 }
1359
1360 static SideEffects AllReads() {
1361 return SideEffects(kAllReads);
1362 }
1363
1364 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1365 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001366 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001367 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001368 }
1369
Aart Bik854a02b2015-07-14 16:07:00 -07001370 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1371 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001372 }
1373
Aart Bik34c3ba92015-07-20 14:08:59 -07001374 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1375 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001376 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001377 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001378 }
1379
1380 static SideEffects ArrayReadOfType(Primitive::Type type) {
1381 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1382 }
1383
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001384 static SideEffects CanTriggerGC() {
1385 return SideEffects(1ULL << kCanTriggerGCBit);
1386 }
1387
1388 static SideEffects DependsOnGC() {
1389 return SideEffects(1ULL << kDependsOnGCBit);
1390 }
1391
Aart Bik854a02b2015-07-14 16:07:00 -07001392 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001393 SideEffects Union(SideEffects other) const {
1394 return SideEffects(flags_ | other.flags_);
1395 }
1396
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001397 SideEffects Exclusion(SideEffects other) const {
1398 return SideEffects(flags_ & ~other.flags_);
1399 }
1400
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001401 void Add(SideEffects other) {
1402 flags_ |= other.flags_;
1403 }
1404
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 bool Includes(SideEffects other) const {
1406 return (other.flags_ & flags_) == other.flags_;
1407 }
1408
1409 bool HasSideEffects() const {
1410 return (flags_ & kAllChangeBits);
1411 }
1412
1413 bool HasDependencies() const {
1414 return (flags_ & kAllDependOnBits);
1415 }
1416
1417 // Returns true if there are no side effects or dependencies.
1418 bool DoesNothing() const {
1419 return flags_ == 0;
1420 }
1421
Aart Bik854a02b2015-07-14 16:07:00 -07001422 // Returns true if something is written.
1423 bool DoesAnyWrite() const {
1424 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001425 }
1426
Aart Bik854a02b2015-07-14 16:07:00 -07001427 // Returns true if something is read.
1428 bool DoesAnyRead() const {
1429 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001430 }
1431
Aart Bik854a02b2015-07-14 16:07:00 -07001432 // Returns true if potentially everything is written and read
1433 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001434 bool DoesAllReadWrite() const {
1435 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1436 }
1437
Aart Bik854a02b2015-07-14 16:07:00 -07001438 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001439 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001440 }
1441
1442 // Returns true if this may read something written by other.
1443 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001444 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1445 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001446 }
1447
1448 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001449 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001450 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001451 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001452 for (int s = kLastBit; s >= 0; s--) {
1453 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1454 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1455 // This is a bit for the GC side effect.
1456 if (current_bit_is_set) {
1457 flags += "GC";
1458 }
Aart Bik854a02b2015-07-14 16:07:00 -07001459 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001460 } else {
1461 // This is a bit for the array/field analysis.
1462 // The underscore character stands for the 'can trigger GC' bit.
1463 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1464 if (current_bit_is_set) {
1465 flags += kDebug[s];
1466 }
1467 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1468 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1469 flags += "|";
1470 }
1471 }
Aart Bik854a02b2015-07-14 16:07:00 -07001472 }
1473 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001474 }
1475
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001476 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001477
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001478 private:
1479 static constexpr int kFieldArrayAnalysisBits = 9;
1480
1481 static constexpr int kFieldWriteOffset = 0;
1482 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1483 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1484 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1485
1486 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1487
1488 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1489 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1490 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1491 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1492
1493 static constexpr int kLastBit = kDependsOnGCBit;
1494 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1495
1496 // Aliases.
1497
1498 static_assert(kChangeBits == kDependOnBits,
1499 "the 'change' bits should match the 'depend on' bits.");
1500
1501 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1502 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1503 static constexpr uint64_t kAllWrites =
1504 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1505 static constexpr uint64_t kAllReads =
1506 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001507
Aart Bik854a02b2015-07-14 16:07:00 -07001508 // Work around the fact that HIR aliases I/F and J/D.
1509 // TODO: remove this interceptor once HIR types are clean
1510 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1511 switch (type) {
1512 case Primitive::kPrimInt:
1513 case Primitive::kPrimFloat:
1514 return TypeFlag(Primitive::kPrimInt, offset) |
1515 TypeFlag(Primitive::kPrimFloat, offset);
1516 case Primitive::kPrimLong:
1517 case Primitive::kPrimDouble:
1518 return TypeFlag(Primitive::kPrimLong, offset) |
1519 TypeFlag(Primitive::kPrimDouble, offset);
1520 default:
1521 return TypeFlag(type, offset);
1522 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001523 }
1524
Aart Bik854a02b2015-07-14 16:07:00 -07001525 // Translates type to bit flag.
1526 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1527 CHECK_NE(type, Primitive::kPrimVoid);
1528 const uint64_t one = 1;
1529 const int shift = type; // 0-based consecutive enum
1530 DCHECK_LE(kFieldWriteOffset, shift);
1531 DCHECK_LT(shift, kArrayWriteOffset);
1532 return one << (type + offset);
1533 }
1534
1535 // Private constructor on direct flags value.
1536 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1537
1538 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001539};
1540
David Brazdiled596192015-01-23 10:39:45 +00001541// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001542class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001543 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001544 HEnvironment(ArenaAllocator* arena,
1545 size_t number_of_vregs,
1546 const DexFile& dex_file,
1547 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001548 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001549 InvokeType invoke_type,
1550 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001551 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1552 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001553 parent_(nullptr),
1554 dex_file_(dex_file),
1555 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001556 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001557 invoke_type_(invoke_type),
1558 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001559 }
1560
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001561 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001562 : HEnvironment(arena,
1563 to_copy.Size(),
1564 to_copy.GetDexFile(),
1565 to_copy.GetMethodIdx(),
1566 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001567 to_copy.GetInvokeType(),
1568 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001569
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001570 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001571 if (parent_ != nullptr) {
1572 parent_->SetAndCopyParentChain(allocator, parent);
1573 } else {
1574 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1575 parent_->CopyFrom(parent);
1576 if (parent->GetParent() != nullptr) {
1577 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1578 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001579 }
David Brazdiled596192015-01-23 10:39:45 +00001580 }
1581
Vladimir Marko71bf8092015-09-15 15:33:14 +01001582 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001583 void CopyFrom(HEnvironment* environment);
1584
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001585 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1586 // input to the loop phi instead. This is for inserting instructions that
1587 // require an environment (like HDeoptimization) in the loop pre-header.
1588 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001589
1590 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001591 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001592 }
1593
1594 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001595 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001596 }
1597
David Brazdil1abb4192015-02-17 18:33:36 +00001598 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001599
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001600 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001601
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001602 HEnvironment* GetParent() const { return parent_; }
1603
1604 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001605 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001606 }
1607
1608 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001609 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001610 }
1611
1612 uint32_t GetDexPc() const {
1613 return dex_pc_;
1614 }
1615
1616 uint32_t GetMethodIdx() const {
1617 return method_idx_;
1618 }
1619
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001620 InvokeType GetInvokeType() const {
1621 return invoke_type_;
1622 }
1623
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001624 const DexFile& GetDexFile() const {
1625 return dex_file_;
1626 }
1627
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001628 HInstruction* GetHolder() const {
1629 return holder_;
1630 }
1631
David Brazdiled596192015-01-23 10:39:45 +00001632 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001633 // Record instructions' use entries of this environment for constant-time removal.
1634 // It should only be called by HInstruction when a new environment use is added.
1635 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1636 DCHECK(env_use->GetUser() == this);
1637 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001638 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001639 }
David Brazdiled596192015-01-23 10:39:45 +00001640
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001641 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1642 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001643 HEnvironment* parent_;
1644 const DexFile& dex_file_;
1645 const uint32_t method_idx_;
1646 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001647 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001648
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001649 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001650 HInstruction* const holder_;
1651
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001652 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001653
1654 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1655};
1656
Calin Juravleacf735c2015-02-12 15:25:22 +00001657class ReferenceTypeInfo : ValueObject {
1658 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001659 typedef Handle<mirror::Class> TypeHandle;
1660
Calin Juravle2e768302015-07-28 14:41:11 +00001661 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1662 // The constructor will check that the type_handle is valid.
1663 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001664 }
1665
Calin Juravle2e768302015-07-28 14:41:11 +00001666 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1667
1668 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1669 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001670 }
1671
Calin Juravle2e768302015-07-28 14:41:11 +00001672 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1673 return IsValidHandle(type_handle_);
1674 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001675
Calin Juravleacf735c2015-02-12 15:25:22 +00001676 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001677
1678 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1679 DCHECK(IsValid());
1680 return GetTypeHandle()->IsObjectClass();
1681 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001682
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001683 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001684 DCHECK(IsValid());
1685 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001686 }
1687
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001688 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1689 DCHECK(IsValid());
1690 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1691 }
1692
Mathieu Chartier90443472015-07-16 20:32:27 -07001693 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001694 DCHECK(IsValid());
1695 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001696 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001697
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001698 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001699 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001700 return GetTypeHandle()->IsArrayClass();
1701 }
1702
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001703 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1704 DCHECK(IsValid());
1705 return GetTypeHandle()->IsPrimitiveArray();
1706 }
1707
1708 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1709 DCHECK(IsValid());
1710 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1711 }
1712
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001713 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001714 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001715 if (!IsExact()) return false;
1716 if (!IsArrayClass()) return false;
1717 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1718 }
1719
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001720 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1721 DCHECK(IsValid());
1722 if (!IsExact()) return false;
1723 if (!IsArrayClass()) return false;
1724 if (!rti.IsArrayClass()) return false;
1725 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1726 rti.GetTypeHandle()->GetComponentType());
1727 }
1728
Calin Juravleacf735c2015-02-12 15:25:22 +00001729 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1730
Mathieu Chartier90443472015-07-16 20:32:27 -07001731 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001732 DCHECK(IsValid());
1733 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001734 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1735 }
1736
1737 // Returns true if the type information provide the same amount of details.
1738 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001739 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001740 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001741 if (!IsValid() && !rti.IsValid()) {
1742 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001743 return true;
1744 }
Calin Juravle2e768302015-07-28 14:41:11 +00001745 if (!IsValid() || !rti.IsValid()) {
1746 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001747 return false;
1748 }
Calin Juravle2e768302015-07-28 14:41:11 +00001749 return IsExact() == rti.IsExact()
1750 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001751 }
1752
1753 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001754 ReferenceTypeInfo();
1755 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001756
Calin Juravleacf735c2015-02-12 15:25:22 +00001757 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001758 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001759 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001760 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001761 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001762};
1763
1764std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1765
Vladimir Markof9f64412015-09-02 14:05:49 +01001766class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001767 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001768 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001769 : previous_(nullptr),
1770 next_(nullptr),
1771 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001772 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001773 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001774 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001775 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001776 locations_(nullptr),
1777 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001778 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001779 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001780 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001781
Dave Allison20dfc792014-06-16 20:44:29 -07001782 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001783
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001784#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001785 enum InstructionKind {
1786 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1787 };
1788#undef DECLARE_KIND
1789
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001790 HInstruction* GetNext() const { return next_; }
1791 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001792
Calin Juravle77520bc2015-01-12 18:45:46 +00001793 HInstruction* GetNextDisregardingMoves() const;
1794 HInstruction* GetPreviousDisregardingMoves() const;
1795
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001796 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001797 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001798 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001799 bool IsInBlock() const { return block_ != nullptr; }
1800 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001801 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001802
Roland Levillain6b879dd2014-09-22 17:13:44 +01001803 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001804 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001805
1806 virtual void Accept(HGraphVisitor* visitor) = 0;
1807 virtual const char* DebugName() const = 0;
1808
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001809 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001810 void SetRawInputAt(size_t index, HInstruction* input) {
1811 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1812 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001813
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001814 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001815
1816 uint32_t GetDexPc() const { return dex_pc_; }
1817
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001818 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001819
Roland Levillaine161a2a2014-10-03 12:45:18 +01001820 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001821 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001822
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001823 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001824 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001825
Calin Juravle10e244f2015-01-26 18:54:32 +00001826 // Does not apply for all instructions, but having this at top level greatly
1827 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001828 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001829 virtual bool CanBeNull() const {
1830 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1831 return true;
1832 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001833
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001834 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001835 return false;
1836 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001837
Calin Juravle2e768302015-07-28 14:41:11 +00001838 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001839
Calin Juravle61d544b2015-02-23 16:46:57 +00001840 ReferenceTypeInfo GetReferenceTypeInfo() const {
1841 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1842 return reference_type_info_;
1843 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001844
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001845 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001846 DCHECK(user != nullptr);
1847 HUseListNode<HInstruction*>* use =
1848 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1849 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001850 }
1851
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001852 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001853 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001854 HUseListNode<HEnvironment*>* env_use =
1855 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1856 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001857 }
1858
David Brazdil1abb4192015-02-17 18:33:36 +00001859 void RemoveAsUserOfInput(size_t input) {
1860 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1861 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1862 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001863
David Brazdil1abb4192015-02-17 18:33:36 +00001864 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1865 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001866
David Brazdiled596192015-01-23 10:39:45 +00001867 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1868 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001869 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001870 bool HasOnlyOneNonEnvironmentUse() const {
1871 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1872 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001873
Roland Levillain6c82d402014-10-13 16:10:27 +01001874 // Does this instruction strictly dominate `other_instruction`?
1875 // Returns false if this instruction and `other_instruction` are the same.
1876 // Aborts if this instruction and `other_instruction` are both phis.
1877 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001878
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001879 int GetId() const { return id_; }
1880 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001881
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001882 int GetSsaIndex() const { return ssa_index_; }
1883 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1884 bool HasSsaIndex() const { return ssa_index_ != -1; }
1885
1886 bool HasEnvironment() const { return environment_ != nullptr; }
1887 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001888 // Set the `environment_` field. Raw because this method does not
1889 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001890 void SetRawEnvironment(HEnvironment* environment) {
1891 DCHECK(environment_ == nullptr);
1892 DCHECK_EQ(environment->GetHolder(), this);
1893 environment_ = environment;
1894 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001895
1896 // Set the environment of this instruction, copying it from `environment`. While
1897 // copying, the uses lists are being updated.
1898 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001899 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001900 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001901 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001902 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001903 if (environment->GetParent() != nullptr) {
1904 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1905 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001906 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001907
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001908 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1909 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001910 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001911 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001912 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001913 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001914 if (environment->GetParent() != nullptr) {
1915 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1916 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001917 }
1918
Nicolas Geoffray39468442014-09-02 15:17:15 +01001919 // Returns the number of entries in the environment. Typically, that is the
1920 // number of dex registers in a method. It could be more in case of inlining.
1921 size_t EnvironmentSize() const;
1922
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001923 LocationSummary* GetLocations() const { return locations_; }
1924 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001925
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001926 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001927 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001928
Alexandre Rames188d4312015-04-09 18:30:21 +01001929 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1930 // uses of this instruction by `other` are *not* updated.
1931 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1932 ReplaceWith(other);
1933 other->ReplaceInput(this, use_index);
1934 }
1935
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001936 // Move `this` instruction before `cursor`.
1937 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001938
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001939#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001940 bool Is##type() const { return (As##type() != nullptr); } \
1941 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001942 virtual H##type* As##type() { return nullptr; }
1943
1944 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1945#undef INSTRUCTION_TYPE_CHECK
1946
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001947 // Returns whether the instruction can be moved within the graph.
1948 virtual bool CanBeMoved() const { return false; }
1949
1950 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001951 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001952 return false;
1953 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001954
1955 // Returns whether any data encoded in the two instructions is equal.
1956 // This method does not look at the inputs. Both instructions must be
1957 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001958 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001959 return false;
1960 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001961
1962 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001963 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001964 // 2) Their inputs are identical.
1965 bool Equals(HInstruction* other) const;
1966
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001967 virtual InstructionKind GetKind() const = 0;
1968
1969 virtual size_t ComputeHashCode() const {
1970 size_t result = GetKind();
1971 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1972 result = (result * 31) + InputAt(i)->GetId();
1973 }
1974 return result;
1975 }
1976
1977 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001978 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001979
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001980 size_t GetLifetimePosition() const { return lifetime_position_; }
1981 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1982 LiveInterval* GetLiveInterval() const { return live_interval_; }
1983 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1984 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1985
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001986 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1987
1988 // Returns whether the code generation of the instruction will require to have access
1989 // to the current method. Such instructions are:
1990 // (1): Instructions that require an environment, as calling the runtime requires
1991 // to walk the stack and have the current method stored at a specific stack address.
1992 // (2): Object literals like classes and strings, that are loaded from the dex cache
1993 // fields of the current method.
1994 bool NeedsCurrentMethod() const {
1995 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1996 }
1997
Vladimir Markodc151b22015-10-15 18:02:30 +01001998 // Returns whether the code generation of the instruction will require to have access
1999 // to the dex cache of the current method's declaring class via the current method.
2000 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002001
Mark Mendellc4701932015-04-10 13:18:51 -04002002 // Does this instruction have any use in an environment before
2003 // control flow hits 'other'?
2004 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2005
2006 // Remove all references to environment uses of this instruction.
2007 // The caller must ensure that this is safe to do.
2008 void RemoveEnvironmentUsers();
2009
David Brazdil1abb4192015-02-17 18:33:36 +00002010 protected:
2011 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2012 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2013
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002014 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002015 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2016
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002017 HInstruction* previous_;
2018 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002019 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002020 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002021
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002022 // An instruction gets an id when it is added to the graph.
2023 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002024 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002025 int id_;
2026
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002027 // When doing liveness analysis, instructions that have uses get an SSA index.
2028 int ssa_index_;
2029
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002030 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002031 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002032
2033 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002034 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002035
Nicolas Geoffray39468442014-09-02 15:17:15 +01002036 // The environment associated with this instruction. Not null if the instruction
2037 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002038 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002039
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002040 // Set by the code generator.
2041 LocationSummary* locations_;
2042
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002043 // Set by the liveness analysis.
2044 LiveInterval* live_interval_;
2045
2046 // Set by the liveness analysis, this is the position in a linear
2047 // order of blocks where this instruction's live interval start.
2048 size_t lifetime_position_;
2049
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002050 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002051
Calin Juravleacf735c2015-02-12 15:25:22 +00002052 // TODO: for primitive types this should be marked as invalid.
2053 ReferenceTypeInfo reference_type_info_;
2054
David Brazdil1abb4192015-02-17 18:33:36 +00002055 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002056 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002057 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002058 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002059 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002060
2061 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2062};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002063std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002064
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002065class HInputIterator : public ValueObject {
2066 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002067 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002068
2069 bool Done() const { return index_ == instruction_->InputCount(); }
2070 HInstruction* Current() const { return instruction_->InputAt(index_); }
2071 void Advance() { index_++; }
2072
2073 private:
2074 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002075 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002076
2077 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2078};
2079
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002080class HInstructionIterator : public ValueObject {
2081 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002082 explicit HInstructionIterator(const HInstructionList& instructions)
2083 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002084 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002085 }
2086
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002087 bool Done() const { return instruction_ == nullptr; }
2088 HInstruction* Current() const { return instruction_; }
2089 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002090 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002091 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002092 }
2093
2094 private:
2095 HInstruction* instruction_;
2096 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002097
2098 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002099};
2100
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002101class HBackwardInstructionIterator : public ValueObject {
2102 public:
2103 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2104 : instruction_(instructions.last_instruction_) {
2105 next_ = Done() ? nullptr : instruction_->GetPrevious();
2106 }
2107
2108 bool Done() const { return instruction_ == nullptr; }
2109 HInstruction* Current() const { return instruction_; }
2110 void Advance() {
2111 instruction_ = next_;
2112 next_ = Done() ? nullptr : instruction_->GetPrevious();
2113 }
2114
2115 private:
2116 HInstruction* instruction_;
2117 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002118
2119 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002120};
2121
Vladimir Markof9f64412015-09-02 14:05:49 +01002122template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002123class HTemplateInstruction: public HInstruction {
2124 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002125 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002126 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002127 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002128
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002129 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002130
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002131 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002132 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2133 DCHECK_LT(i, N);
2134 return inputs_[i];
2135 }
David Brazdil1abb4192015-02-17 18:33:36 +00002136
2137 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002138 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002139 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002140 }
2141
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002142 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002143 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002144
2145 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002146};
2147
Vladimir Markof9f64412015-09-02 14:05:49 +01002148// HTemplateInstruction specialization for N=0.
2149template<>
2150class HTemplateInstruction<0>: public HInstruction {
2151 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002152 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002153 : HInstruction(side_effects, dex_pc) {}
2154
Vladimir Markof9f64412015-09-02 14:05:49 +01002155 virtual ~HTemplateInstruction() {}
2156
2157 size_t InputCount() const OVERRIDE { return 0; }
2158
2159 protected:
2160 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2161 LOG(FATAL) << "Unreachable";
2162 UNREACHABLE();
2163 }
2164
2165 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2166 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2167 LOG(FATAL) << "Unreachable";
2168 UNREACHABLE();
2169 }
2170
2171 private:
2172 friend class SsaBuilder;
2173};
2174
Dave Allison20dfc792014-06-16 20:44:29 -07002175template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002176class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002177 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002178 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002179 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002180 virtual ~HExpression() {}
2181
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002182 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002183
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002184 protected:
2185 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002186};
2187
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002188// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2189// instruction that branches to the exit block.
2190class HReturnVoid : public HTemplateInstruction<0> {
2191 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002192 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2193 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002194
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002195 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002196
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002197 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002198
2199 private:
2200 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2201};
2202
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002203// Represents dex's RETURN opcodes. A HReturn is a control flow
2204// instruction that branches to the exit block.
2205class HReturn : public HTemplateInstruction<1> {
2206 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002207 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2208 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002209 SetRawInputAt(0, value);
2210 }
2211
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002212 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002213
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002214 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002215
2216 private:
2217 DISALLOW_COPY_AND_ASSIGN(HReturn);
2218};
2219
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002220// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002221// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002222// exit block.
2223class HExit : public HTemplateInstruction<0> {
2224 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002225 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002226
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002227 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002228
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002229 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002230
2231 private:
2232 DISALLOW_COPY_AND_ASSIGN(HExit);
2233};
2234
2235// Jumps from one block to another.
2236class HGoto : public HTemplateInstruction<0> {
2237 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002238 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002239
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002240 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002241
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002242 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002243 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002244 }
2245
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002246 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002247
2248 private:
2249 DISALLOW_COPY_AND_ASSIGN(HGoto);
2250};
2251
Roland Levillain9867bc72015-08-05 10:21:34 +01002252class HConstant : public HExpression<0> {
2253 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002254 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2255 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002256
2257 bool CanBeMoved() const OVERRIDE { return true; }
2258
2259 virtual bool IsMinusOne() const { return false; }
2260 virtual bool IsZero() const { return false; }
2261 virtual bool IsOne() const { return false; }
2262
David Brazdil77a48ae2015-09-15 12:34:04 +00002263 virtual uint64_t GetValueAsUint64() const = 0;
2264
Roland Levillain9867bc72015-08-05 10:21:34 +01002265 DECLARE_INSTRUCTION(Constant);
2266
2267 private:
2268 DISALLOW_COPY_AND_ASSIGN(HConstant);
2269};
2270
2271class HNullConstant : public HConstant {
2272 public:
2273 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2274 return true;
2275 }
2276
David Brazdil77a48ae2015-09-15 12:34:04 +00002277 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2278
Roland Levillain9867bc72015-08-05 10:21:34 +01002279 size_t ComputeHashCode() const OVERRIDE { return 0; }
2280
2281 DECLARE_INSTRUCTION(NullConstant);
2282
2283 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002284 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002285
2286 friend class HGraph;
2287 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2288};
2289
2290// Constants of the type int. Those can be from Dex instructions, or
2291// synthesized (for example with the if-eqz instruction).
2292class HIntConstant : public HConstant {
2293 public:
2294 int32_t GetValue() const { return value_; }
2295
David Brazdil9f389d42015-10-01 14:32:56 +01002296 uint64_t GetValueAsUint64() const OVERRIDE {
2297 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2298 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002299
Roland Levillain9867bc72015-08-05 10:21:34 +01002300 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2301 DCHECK(other->IsIntConstant());
2302 return other->AsIntConstant()->value_ == value_;
2303 }
2304
2305 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2306
2307 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2308 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2309 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2310
2311 DECLARE_INSTRUCTION(IntConstant);
2312
2313 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002314 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2315 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2316 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2317 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002318
2319 const int32_t value_;
2320
2321 friend class HGraph;
2322 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2323 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2324 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2325};
2326
2327class HLongConstant : public HConstant {
2328 public:
2329 int64_t GetValue() const { return value_; }
2330
David Brazdil77a48ae2015-09-15 12:34:04 +00002331 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2332
Roland Levillain9867bc72015-08-05 10:21:34 +01002333 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2334 DCHECK(other->IsLongConstant());
2335 return other->AsLongConstant()->value_ == value_;
2336 }
2337
2338 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2339
2340 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2341 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2342 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2343
2344 DECLARE_INSTRUCTION(LongConstant);
2345
2346 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002347 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2348 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002349
2350 const int64_t value_;
2351
2352 friend class HGraph;
2353 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2354};
Dave Allison20dfc792014-06-16 20:44:29 -07002355
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002356// Conditional branch. A block ending with an HIf instruction must have
2357// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002358class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002359 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002360 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2361 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002362 SetRawInputAt(0, input);
2363 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002364
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002365 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002366
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002367 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002368 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002369 }
2370
2371 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002372 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002373 }
2374
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002375 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002376
2377 private:
2378 DISALLOW_COPY_AND_ASSIGN(HIf);
2379};
2380
David Brazdilfc6a86a2015-06-26 10:33:45 +00002381
2382// Abstract instruction which marks the beginning and/or end of a try block and
2383// links it to the respective exception handlers. Behaves the same as a Goto in
2384// non-exceptional control flow.
2385// Normal-flow successor is stored at index zero, exception handlers under
2386// higher indices in no particular order.
2387class HTryBoundary : public HTemplateInstruction<0> {
2388 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002389 enum BoundaryKind {
2390 kEntry,
2391 kExit,
2392 };
2393
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002394 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2395 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002396
2397 bool IsControlFlow() const OVERRIDE { return true; }
2398
2399 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002400 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002401
2402 // Returns whether `handler` is among its exception handlers (non-zero index
2403 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002404 bool HasExceptionHandler(const HBasicBlock& handler) const {
2405 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002406 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002407 }
2408
2409 // If not present already, adds `handler` to its block's list of exception
2410 // handlers.
2411 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002412 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002413 GetBlock()->AddSuccessor(handler);
2414 }
2415 }
2416
David Brazdil56e1acc2015-06-30 15:41:36 +01002417 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002418
David Brazdilffee3d32015-07-06 11:48:53 +01002419 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2420
David Brazdilfc6a86a2015-06-26 10:33:45 +00002421 DECLARE_INSTRUCTION(TryBoundary);
2422
2423 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002424 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002425
2426 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2427};
2428
David Brazdilffee3d32015-07-06 11:48:53 +01002429// Iterator over exception handlers of a given HTryBoundary, i.e. over
2430// exceptional successors of its basic block.
2431class HExceptionHandlerIterator : public ValueObject {
2432 public:
2433 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2434 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2435
Vladimir Marko60584552015-09-03 13:35:12 +00002436 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002437 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002438 size_t CurrentSuccessorIndex() const { return index_; }
2439 void Advance() { ++index_; }
2440
2441 private:
2442 const HBasicBlock& block_;
2443 size_t index_;
2444
2445 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2446};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002447
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002448// Deoptimize to interpreter, upon checking a condition.
2449class HDeoptimize : public HTemplateInstruction<1> {
2450 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002451 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2452 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002453 SetRawInputAt(0, cond);
2454 }
2455
2456 bool NeedsEnvironment() const OVERRIDE { return true; }
2457 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002458
2459 DECLARE_INSTRUCTION(Deoptimize);
2460
2461 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002462 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2463};
2464
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002465// Represents the ArtMethod that was passed as a first argument to
2466// the method. It is used by instructions that depend on it, like
2467// instructions that work with the dex cache.
2468class HCurrentMethod : public HExpression<0> {
2469 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002470 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2471 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002472
2473 DECLARE_INSTRUCTION(CurrentMethod);
2474
2475 private:
2476 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2477};
2478
Mark Mendellfe57faa2015-09-18 09:26:15 -04002479// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2480// have one successor for each entry in the switch table, and the final successor
2481// will be the block containing the next Dex opcode.
2482class HPackedSwitch : public HTemplateInstruction<1> {
2483 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002484 HPackedSwitch(int32_t start_value,
2485 uint32_t num_entries,
2486 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002487 uint32_t dex_pc = kNoDexPc)
2488 : HTemplateInstruction(SideEffects::None(), dex_pc),
2489 start_value_(start_value),
2490 num_entries_(num_entries) {
2491 SetRawInputAt(0, input);
2492 }
2493
2494 bool IsControlFlow() const OVERRIDE { return true; }
2495
2496 int32_t GetStartValue() const { return start_value_; }
2497
Vladimir Marko211c2112015-09-24 16:52:33 +01002498 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002499
2500 HBasicBlock* GetDefaultBlock() const {
2501 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002502 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002503 }
2504 DECLARE_INSTRUCTION(PackedSwitch);
2505
2506 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002507 const int32_t start_value_;
2508 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002509
2510 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2511};
2512
Roland Levillain88cb1752014-10-20 16:36:47 +01002513class HUnaryOperation : public HExpression<1> {
2514 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002515 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2516 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002517 SetRawInputAt(0, input);
2518 }
2519
2520 HInstruction* GetInput() const { return InputAt(0); }
2521 Primitive::Type GetResultType() const { return GetType(); }
2522
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002523 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002524 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002525 return true;
2526 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002527
Roland Levillain9240d6a2014-10-20 16:47:04 +01002528 // Try to statically evaluate `operation` and return a HConstant
2529 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002530 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002531 HConstant* TryStaticEvaluation() const;
2532
2533 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002534 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2535 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002536
Roland Levillain88cb1752014-10-20 16:36:47 +01002537 DECLARE_INSTRUCTION(UnaryOperation);
2538
2539 private:
2540 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2541};
2542
Dave Allison20dfc792014-06-16 20:44:29 -07002543class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002544 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002545 HBinaryOperation(Primitive::Type result_type,
2546 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002547 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002548 SideEffects side_effects = SideEffects::None(),
2549 uint32_t dex_pc = kNoDexPc)
2550 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002551 SetRawInputAt(0, left);
2552 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002553 }
2554
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002555 HInstruction* GetLeft() const { return InputAt(0); }
2556 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002557 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002558
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002559 virtual bool IsCommutative() const { return false; }
2560
2561 // Put constant on the right.
2562 // Returns whether order is changed.
2563 bool OrderInputsWithConstantOnTheRight() {
2564 HInstruction* left = InputAt(0);
2565 HInstruction* right = InputAt(1);
2566 if (left->IsConstant() && !right->IsConstant()) {
2567 ReplaceInput(right, 0);
2568 ReplaceInput(left, 1);
2569 return true;
2570 }
2571 return false;
2572 }
2573
2574 // Order inputs by instruction id, but favor constant on the right side.
2575 // This helps GVN for commutative ops.
2576 void OrderInputs() {
2577 DCHECK(IsCommutative());
2578 HInstruction* left = InputAt(0);
2579 HInstruction* right = InputAt(1);
2580 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2581 return;
2582 }
2583 if (OrderInputsWithConstantOnTheRight()) {
2584 return;
2585 }
2586 // Order according to instruction id.
2587 if (left->GetId() > right->GetId()) {
2588 ReplaceInput(right, 0);
2589 ReplaceInput(left, 1);
2590 }
2591 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002592
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002593 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002594 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002595 return true;
2596 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002597
Roland Levillain9240d6a2014-10-20 16:47:04 +01002598 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002599 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002600 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002601 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002602
2603 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002604 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2605 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2606 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2607 HLongConstant* y ATTRIBUTE_UNUSED) const {
2608 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2609 return nullptr;
2610 }
2611 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2612 HIntConstant* y ATTRIBUTE_UNUSED) const {
2613 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2614 return nullptr;
2615 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002616
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002617 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002618 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002619 HConstant* GetConstantRight() const;
2620
2621 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002622 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002623 HInstruction* GetLeastConstantLeft() const;
2624
Roland Levillainccc07a92014-09-16 14:48:16 +01002625 DECLARE_INSTRUCTION(BinaryOperation);
2626
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002627 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002628 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2629};
2630
Mark Mendellc4701932015-04-10 13:18:51 -04002631// The comparison bias applies for floating point operations and indicates how NaN
2632// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002633enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002634 kNoBias, // bias is not applicable (i.e. for long operation)
2635 kGtBias, // return 1 for NaN comparisons
2636 kLtBias, // return -1 for NaN comparisons
2637};
2638
Dave Allison20dfc792014-06-16 20:44:29 -07002639class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002640 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002641 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2642 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002643 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002644 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002645
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002646 bool NeedsMaterialization() const { return needs_materialization_; }
2647 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002648
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002649 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002650 // `instruction`, and disregard moves in between.
2651 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002652
Dave Allison20dfc792014-06-16 20:44:29 -07002653 DECLARE_INSTRUCTION(Condition);
2654
2655 virtual IfCondition GetCondition() const = 0;
2656
Mark Mendellc4701932015-04-10 13:18:51 -04002657 virtual IfCondition GetOppositeCondition() const = 0;
2658
Roland Levillain4fa13f62015-07-06 18:11:54 +01002659 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002660
2661 void SetBias(ComparisonBias bias) { bias_ = bias; }
2662
2663 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2664 return bias_ == other->AsCondition()->bias_;
2665 }
2666
Roland Levillain4fa13f62015-07-06 18:11:54 +01002667 bool IsFPConditionTrueIfNaN() const {
2668 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2669 IfCondition if_cond = GetCondition();
2670 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2671 }
2672
2673 bool IsFPConditionFalseIfNaN() const {
2674 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2675 IfCondition if_cond = GetCondition();
2676 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2677 }
2678
Dave Allison20dfc792014-06-16 20:44:29 -07002679 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002680 // For register allocation purposes, returns whether this instruction needs to be
2681 // materialized (that is, not just be in the processor flags).
2682 bool needs_materialization_;
2683
Mark Mendellc4701932015-04-10 13:18:51 -04002684 // Needed if we merge a HCompare into a HCondition.
2685 ComparisonBias bias_;
2686
Dave Allison20dfc792014-06-16 20:44:29 -07002687 DISALLOW_COPY_AND_ASSIGN(HCondition);
2688};
2689
2690// Instruction to check if two inputs are equal to each other.
2691class HEqual : public HCondition {
2692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002693 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2694 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002695
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002696 bool IsCommutative() const OVERRIDE { return true; }
2697
Roland Levillain9867bc72015-08-05 10:21:34 +01002698 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002699 return GetBlock()->GetGraph()->GetIntConstant(
2700 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002701 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002702 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002703 return GetBlock()->GetGraph()->GetIntConstant(
2704 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002705 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002706
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002707 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002708
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002709 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002710 return kCondEQ;
2711 }
2712
Mark Mendellc4701932015-04-10 13:18:51 -04002713 IfCondition GetOppositeCondition() const OVERRIDE {
2714 return kCondNE;
2715 }
2716
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002717 private:
Aart Bike9f37602015-10-09 11:15:55 -07002718 template <typename T> bool Compute(T x, T y) const { return x == y; }
2719
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002720 DISALLOW_COPY_AND_ASSIGN(HEqual);
2721};
2722
Dave Allison20dfc792014-06-16 20:44:29 -07002723class HNotEqual : public HCondition {
2724 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002725 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2726 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002727
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002728 bool IsCommutative() const OVERRIDE { return true; }
2729
Roland Levillain9867bc72015-08-05 10:21:34 +01002730 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002731 return GetBlock()->GetGraph()->GetIntConstant(
2732 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002733 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002734 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002735 return GetBlock()->GetGraph()->GetIntConstant(
2736 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002737 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002738
Dave Allison20dfc792014-06-16 20:44:29 -07002739 DECLARE_INSTRUCTION(NotEqual);
2740
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002741 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002742 return kCondNE;
2743 }
2744
Mark Mendellc4701932015-04-10 13:18:51 -04002745 IfCondition GetOppositeCondition() const OVERRIDE {
2746 return kCondEQ;
2747 }
2748
Dave Allison20dfc792014-06-16 20:44:29 -07002749 private:
Aart Bike9f37602015-10-09 11:15:55 -07002750 template <typename T> bool Compute(T x, T y) const { return x != y; }
2751
Dave Allison20dfc792014-06-16 20:44:29 -07002752 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2753};
2754
2755class HLessThan : public HCondition {
2756 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002757 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2758 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002759
Roland Levillain9867bc72015-08-05 10:21:34 +01002760 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002761 return GetBlock()->GetGraph()->GetIntConstant(
2762 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002763 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002764 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002765 return GetBlock()->GetGraph()->GetIntConstant(
2766 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002767 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002768
Dave Allison20dfc792014-06-16 20:44:29 -07002769 DECLARE_INSTRUCTION(LessThan);
2770
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002771 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002772 return kCondLT;
2773 }
2774
Mark Mendellc4701932015-04-10 13:18:51 -04002775 IfCondition GetOppositeCondition() const OVERRIDE {
2776 return kCondGE;
2777 }
2778
Dave Allison20dfc792014-06-16 20:44:29 -07002779 private:
Aart Bike9f37602015-10-09 11:15:55 -07002780 template <typename T> bool Compute(T x, T y) const { return x < y; }
2781
Dave Allison20dfc792014-06-16 20:44:29 -07002782 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2783};
2784
2785class HLessThanOrEqual : public HCondition {
2786 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002787 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2788 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002789
Roland Levillain9867bc72015-08-05 10:21:34 +01002790 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002791 return GetBlock()->GetGraph()->GetIntConstant(
2792 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002793 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002794 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002795 return GetBlock()->GetGraph()->GetIntConstant(
2796 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002797 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002798
Dave Allison20dfc792014-06-16 20:44:29 -07002799 DECLARE_INSTRUCTION(LessThanOrEqual);
2800
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002801 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002802 return kCondLE;
2803 }
2804
Mark Mendellc4701932015-04-10 13:18:51 -04002805 IfCondition GetOppositeCondition() const OVERRIDE {
2806 return kCondGT;
2807 }
2808
Dave Allison20dfc792014-06-16 20:44:29 -07002809 private:
Aart Bike9f37602015-10-09 11:15:55 -07002810 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2811
Dave Allison20dfc792014-06-16 20:44:29 -07002812 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2813};
2814
2815class HGreaterThan : public HCondition {
2816 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002817 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2818 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002819
Roland Levillain9867bc72015-08-05 10:21:34 +01002820 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002821 return GetBlock()->GetGraph()->GetIntConstant(
2822 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002823 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002824 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002825 return GetBlock()->GetGraph()->GetIntConstant(
2826 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002827 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002828
Dave Allison20dfc792014-06-16 20:44:29 -07002829 DECLARE_INSTRUCTION(GreaterThan);
2830
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002831 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002832 return kCondGT;
2833 }
2834
Mark Mendellc4701932015-04-10 13:18:51 -04002835 IfCondition GetOppositeCondition() const OVERRIDE {
2836 return kCondLE;
2837 }
2838
Dave Allison20dfc792014-06-16 20:44:29 -07002839 private:
Aart Bike9f37602015-10-09 11:15:55 -07002840 template <typename T> bool Compute(T x, T y) const { return x > y; }
2841
Dave Allison20dfc792014-06-16 20:44:29 -07002842 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2843};
2844
2845class HGreaterThanOrEqual : public HCondition {
2846 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002847 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2848 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002849
Roland Levillain9867bc72015-08-05 10:21:34 +01002850 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002851 return GetBlock()->GetGraph()->GetIntConstant(
2852 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002853 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002854 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002855 return GetBlock()->GetGraph()->GetIntConstant(
2856 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002857 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002858
Dave Allison20dfc792014-06-16 20:44:29 -07002859 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2860
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002861 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002862 return kCondGE;
2863 }
2864
Mark Mendellc4701932015-04-10 13:18:51 -04002865 IfCondition GetOppositeCondition() const OVERRIDE {
2866 return kCondLT;
2867 }
2868
Dave Allison20dfc792014-06-16 20:44:29 -07002869 private:
Aart Bike9f37602015-10-09 11:15:55 -07002870 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2871
Dave Allison20dfc792014-06-16 20:44:29 -07002872 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2873};
2874
Aart Bike9f37602015-10-09 11:15:55 -07002875class HBelow : public HCondition {
2876 public:
2877 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2878 : HCondition(first, second, dex_pc) {}
2879
2880 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2881 return GetBlock()->GetGraph()->GetIntConstant(
2882 Compute(static_cast<uint32_t>(x->GetValue()),
2883 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2884 }
2885 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2886 return GetBlock()->GetGraph()->GetIntConstant(
2887 Compute(static_cast<uint64_t>(x->GetValue()),
2888 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2889 }
2890
2891 DECLARE_INSTRUCTION(Below);
2892
2893 IfCondition GetCondition() const OVERRIDE {
2894 return kCondB;
2895 }
2896
2897 IfCondition GetOppositeCondition() const OVERRIDE {
2898 return kCondAE;
2899 }
2900
2901 private:
2902 template <typename T> bool Compute(T x, T y) const { return x < y; }
2903
2904 DISALLOW_COPY_AND_ASSIGN(HBelow);
2905};
2906
2907class HBelowOrEqual : public HCondition {
2908 public:
2909 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2910 : HCondition(first, second, dex_pc) {}
2911
2912 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2913 return GetBlock()->GetGraph()->GetIntConstant(
2914 Compute(static_cast<uint32_t>(x->GetValue()),
2915 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2916 }
2917 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2918 return GetBlock()->GetGraph()->GetIntConstant(
2919 Compute(static_cast<uint64_t>(x->GetValue()),
2920 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2921 }
2922
2923 DECLARE_INSTRUCTION(BelowOrEqual);
2924
2925 IfCondition GetCondition() const OVERRIDE {
2926 return kCondBE;
2927 }
2928
2929 IfCondition GetOppositeCondition() const OVERRIDE {
2930 return kCondA;
2931 }
2932
2933 private:
2934 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2935
2936 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2937};
2938
2939class HAbove : public HCondition {
2940 public:
2941 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2942 : HCondition(first, second, dex_pc) {}
2943
2944 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2945 return GetBlock()->GetGraph()->GetIntConstant(
2946 Compute(static_cast<uint32_t>(x->GetValue()),
2947 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2948 }
2949 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2950 return GetBlock()->GetGraph()->GetIntConstant(
2951 Compute(static_cast<uint64_t>(x->GetValue()),
2952 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2953 }
2954
2955 DECLARE_INSTRUCTION(Above);
2956
2957 IfCondition GetCondition() const OVERRIDE {
2958 return kCondA;
2959 }
2960
2961 IfCondition GetOppositeCondition() const OVERRIDE {
2962 return kCondBE;
2963 }
2964
2965 private:
2966 template <typename T> bool Compute(T x, T y) const { return x > y; }
2967
2968 DISALLOW_COPY_AND_ASSIGN(HAbove);
2969};
2970
2971class HAboveOrEqual : public HCondition {
2972 public:
2973 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2974 : HCondition(first, second, dex_pc) {}
2975
2976 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2977 return GetBlock()->GetGraph()->GetIntConstant(
2978 Compute(static_cast<uint32_t>(x->GetValue()),
2979 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2980 }
2981 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2982 return GetBlock()->GetGraph()->GetIntConstant(
2983 Compute(static_cast<uint64_t>(x->GetValue()),
2984 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2985 }
2986
2987 DECLARE_INSTRUCTION(AboveOrEqual);
2988
2989 IfCondition GetCondition() const OVERRIDE {
2990 return kCondAE;
2991 }
2992
2993 IfCondition GetOppositeCondition() const OVERRIDE {
2994 return kCondB;
2995 }
2996
2997 private:
2998 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2999
3000 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3001};
Dave Allison20dfc792014-06-16 20:44:29 -07003002
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003003// Instruction to check how two inputs compare to each other.
3004// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3005class HCompare : public HBinaryOperation {
3006 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003007 HCompare(Primitive::Type type,
3008 HInstruction* first,
3009 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003010 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003011 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003012 : HBinaryOperation(Primitive::kPrimInt,
3013 first,
3014 second,
3015 SideEffectsForArchRuntimeCalls(type),
3016 dex_pc),
3017 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003018 DCHECK_EQ(type, first->GetType());
3019 DCHECK_EQ(type, second->GetType());
3020 }
3021
Roland Levillain9867bc72015-08-05 10:21:34 +01003022 template <typename T>
3023 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003024
Roland Levillain9867bc72015-08-05 10:21:34 +01003025 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003026 return GetBlock()->GetGraph()->GetIntConstant(
3027 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003028 }
3029 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003030 return GetBlock()->GetGraph()->GetIntConstant(
3031 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003032 }
3033
Calin Juravleddb7df22014-11-25 20:56:51 +00003034 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3035 return bias_ == other->AsCompare()->bias_;
3036 }
3037
Mark Mendellc4701932015-04-10 13:18:51 -04003038 ComparisonBias GetBias() const { return bias_; }
3039
Roland Levillain4fa13f62015-07-06 18:11:54 +01003040 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003041
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003042
3043 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3044 // MIPS64 uses a runtime call for FP comparisons.
3045 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3046 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003047
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003048 DECLARE_INSTRUCTION(Compare);
3049
3050 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003051 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003052
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003053 DISALLOW_COPY_AND_ASSIGN(HCompare);
3054};
3055
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003056// A local in the graph. Corresponds to a Dex register.
3057class HLocal : public HTemplateInstruction<0> {
3058 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003059 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003060 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003061
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003062 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003063
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003064 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003065
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003066 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003067 // The Dex register number.
3068 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003069
3070 DISALLOW_COPY_AND_ASSIGN(HLocal);
3071};
3072
3073// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003074class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003075 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003076 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3077 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003078 SetRawInputAt(0, local);
3079 }
3080
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003081 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3082
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003083 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003084
3085 private:
3086 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3087};
3088
3089// Store a value in a given local. This instruction has two inputs: the value
3090// and the local.
3091class HStoreLocal : public HTemplateInstruction<2> {
3092 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003093 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3094 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003095 SetRawInputAt(0, local);
3096 SetRawInputAt(1, value);
3097 }
3098
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003099 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3100
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003101 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003102
3103 private:
3104 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3105};
3106
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003107class HFloatConstant : public HConstant {
3108 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003109 float GetValue() const { return value_; }
3110
David Brazdil77a48ae2015-09-15 12:34:04 +00003111 uint64_t GetValueAsUint64() const OVERRIDE {
3112 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3113 }
3114
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003115 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003116 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003117 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003118 }
3119
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003120 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003121
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003122 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003123 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003124 }
3125 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003126 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003127 }
3128 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003129 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3130 }
3131 bool IsNaN() const {
3132 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003133 }
3134
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003135 DECLARE_INSTRUCTION(FloatConstant);
3136
3137 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003138 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3139 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3140 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3141 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003142
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003143 const float value_;
3144
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003145 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003146 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003147 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003148 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3149};
3150
3151class HDoubleConstant : public HConstant {
3152 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003153 double GetValue() const { return value_; }
3154
David Brazdil77a48ae2015-09-15 12:34:04 +00003155 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3156
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003157 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003158 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003159 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003160 }
3161
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003162 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003163
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003164 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003165 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003166 }
3167 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003168 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003169 }
3170 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003171 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3172 }
3173 bool IsNaN() const {
3174 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003175 }
3176
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003177 DECLARE_INSTRUCTION(DoubleConstant);
3178
3179 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003180 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3181 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3182 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3183 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003184
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003185 const double value_;
3186
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003187 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003188 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003189 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003190 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3191};
3192
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003193enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003194#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003195#include "intrinsics_list.h"
3196 kNone,
3197 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3198#undef INTRINSICS_LIST
3199#undef OPTIMIZING_INTRINSICS
3200};
3201std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3202
Agi Csaki05f20562015-08-19 14:58:14 -07003203enum IntrinsicNeedsEnvironmentOrCache {
3204 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3205 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003206};
3207
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003208class HInvoke : public HInstruction {
3209 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003210 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003211
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003212 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003213
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003214 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003215 SetRawInputAt(index, argument);
3216 }
3217
Roland Levillain3e3d7332015-04-28 11:00:54 +01003218 // Return the number of arguments. This number can be lower than
3219 // the number of inputs returned by InputCount(), as some invoke
3220 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3221 // inputs at the end of their list of inputs.
3222 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3223
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003224 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003225
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003226
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003227 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003228 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003229
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003230 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3231
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003232 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003233 return intrinsic_;
3234 }
3235
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003236 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003237
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003238 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003239 return GetEnvironment()->GetParent() != nullptr;
3240 }
3241
3242 bool CanThrow() const OVERRIDE { return true; }
3243
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003244 uint32_t* GetIntrinsicOptimizations() {
3245 return &intrinsic_optimizations_;
3246 }
3247
3248 const uint32_t* GetIntrinsicOptimizations() const {
3249 return &intrinsic_optimizations_;
3250 }
3251
3252 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3253
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003254 DECLARE_INSTRUCTION(Invoke);
3255
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003256 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003257 HInvoke(ArenaAllocator* arena,
3258 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003259 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003260 Primitive::Type return_type,
3261 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003262 uint32_t dex_method_index,
3263 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003264 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003265 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003266 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003267 inputs_(number_of_arguments + number_of_other_inputs,
3268 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003269 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003270 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003271 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003272 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003273 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003274 }
3275
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003276 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003277 return inputs_[index];
3278 }
3279
David Brazdil1abb4192015-02-17 18:33:36 +00003280 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003281 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003282 }
3283
Roland Levillain3e3d7332015-04-28 11:00:54 +01003284 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003285 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003286 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003287 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003288 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003289 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003290
3291 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3292 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003293
3294 private:
3295 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3296};
3297
Calin Juravle175dc732015-08-25 15:42:32 +01003298class HInvokeUnresolved : public HInvoke {
3299 public:
3300 HInvokeUnresolved(ArenaAllocator* arena,
3301 uint32_t number_of_arguments,
3302 Primitive::Type return_type,
3303 uint32_t dex_pc,
3304 uint32_t dex_method_index,
3305 InvokeType invoke_type)
3306 : HInvoke(arena,
3307 number_of_arguments,
3308 0u /* number_of_other_inputs */,
3309 return_type,
3310 dex_pc,
3311 dex_method_index,
3312 invoke_type) {
3313 }
3314
3315 DECLARE_INSTRUCTION(InvokeUnresolved);
3316
3317 private:
3318 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3319};
3320
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003321class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003322 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003323 // Requirements of this method call regarding the class
3324 // initialization (clinit) check of its declaring class.
3325 enum class ClinitCheckRequirement {
3326 kNone, // Class already initialized.
3327 kExplicit, // Static call having explicit clinit check as last input.
3328 kImplicit, // Static call implicitly requiring a clinit check.
3329 };
3330
Vladimir Marko58155012015-08-19 12:49:41 +00003331 // Determines how to load the target ArtMethod*.
3332 enum class MethodLoadKind {
3333 // Use a String init ArtMethod* loaded from Thread entrypoints.
3334 kStringInit,
3335
3336 // Use the method's own ArtMethod* loaded by the register allocator.
3337 kRecursive,
3338
3339 // Use ArtMethod* at a known address, embed the direct address in the code.
3340 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3341 kDirectAddress,
3342
3343 // Use ArtMethod* at an address that will be known at link time, embed the direct
3344 // address in the code. If the image is relocatable, emit .patch_oat entry.
3345 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3346 // the image relocatable or not.
3347 kDirectAddressWithFixup,
3348
3349 // Load from resoved methods array in the dex cache using a PC-relative load.
3350 // Used when we need to use the dex cache, for example for invoke-static that
3351 // may cause class initialization (the entry may point to a resolution method),
3352 // and we know that we can access the dex cache arrays using a PC-relative load.
3353 kDexCachePcRelative,
3354
3355 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3356 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3357 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3358 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3359 kDexCacheViaMethod,
3360 };
3361
3362 // Determines the location of the code pointer.
3363 enum class CodePtrLocation {
3364 // Recursive call, use local PC-relative call instruction.
3365 kCallSelf,
3366
3367 // Use PC-relative call instruction patched at link time.
3368 // Used for calls within an oat file, boot->boot or app->app.
3369 kCallPCRelative,
3370
3371 // Call to a known target address, embed the direct address in code.
3372 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3373 kCallDirect,
3374
3375 // Call to a target address that will be known at link time, embed the direct
3376 // address in code. If the image is relocatable, emit .patch_oat entry.
3377 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3378 // the image relocatable or not.
3379 kCallDirectWithFixup,
3380
3381 // Use code pointer from the ArtMethod*.
3382 // Used when we don't know the target code. This is also the last-resort-kind used when
3383 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3384 kCallArtMethod,
3385 };
3386
3387 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003388 MethodLoadKind method_load_kind;
3389 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003390 // The method load data holds
3391 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3392 // Note that there are multiple string init methods, each having its own offset.
3393 // - the method address for kDirectAddress
3394 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003395 uint64_t method_load_data;
3396 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003397 };
3398
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003399 HInvokeStaticOrDirect(ArenaAllocator* arena,
3400 uint32_t number_of_arguments,
3401 Primitive::Type return_type,
3402 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003403 uint32_t method_index,
3404 MethodReference target_method,
3405 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003406 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003407 InvokeType invoke_type,
3408 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003409 : HInvoke(arena,
3410 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003411 // There is potentially one extra argument for the HCurrentMethod node, and
3412 // potentially one other if the clinit check is explicit, and potentially
3413 // one other if the method is a string factory.
3414 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3415 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3416 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003417 return_type,
3418 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003419 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003420 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003421 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003422 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003423 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003424 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003425
Vladimir Markodc151b22015-10-15 18:02:30 +01003426 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003427 bool had_current_method_input = HasCurrentMethodInput();
3428 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3429
3430 // Using the current method is the default and once we find a better
3431 // method load kind, we should not go back to using the current method.
3432 DCHECK(had_current_method_input || !needs_current_method_input);
3433
3434 if (had_current_method_input && !needs_current_method_input) {
3435 DCHECK_EQ(InputAt(GetCurrentMethodInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3436 RemoveInputAt(GetCurrentMethodInputIndex());
3437 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003438 dispatch_info_ = dispatch_info;
3439 }
3440
Vladimir Markob554b5a2015-11-06 12:57:55 +00003441 void RemoveInputAt(size_t index);
3442
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003443 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003444 // We access the method via the dex cache so we can't do an implicit null check.
3445 // TODO: for intrinsics we can generate implicit null checks.
3446 return false;
3447 }
3448
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003449 bool CanBeNull() const OVERRIDE {
3450 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3451 }
3452
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003453 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003454 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3455 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3456 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003457 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003458 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003459 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003460 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003461 bool HasPcRelDexCache() const {
3462 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3463 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003464 bool HasCurrentMethodInput() const {
3465 // This function can be called only after the invoke has been fully initialized by the builder.
3466 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
3467 DCHECK(InputAt(GetCurrentMethodInputIndex())->IsCurrentMethod());
3468 return true;
3469 } else {
3470 DCHECK(InputCount() == GetCurrentMethodInputIndex() ||
3471 !InputAt(GetCurrentMethodInputIndex())->IsCurrentMethod());
3472 return false;
3473 }
3474 }
Vladimir Marko58155012015-08-19 12:49:41 +00003475 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3476 MethodReference GetTargetMethod() const { return target_method_; }
3477
3478 int32_t GetStringInitOffset() const {
3479 DCHECK(IsStringInit());
3480 return dispatch_info_.method_load_data;
3481 }
3482
3483 uint64_t GetMethodAddress() const {
3484 DCHECK(HasMethodAddress());
3485 return dispatch_info_.method_load_data;
3486 }
3487
3488 uint32_t GetDexCacheArrayOffset() const {
3489 DCHECK(HasPcRelDexCache());
3490 return dispatch_info_.method_load_data;
3491 }
3492
3493 uint64_t GetDirectCodePtr() const {
3494 DCHECK(HasDirectCodePtr());
3495 return dispatch_info_.direct_code_ptr;
3496 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003497
Calin Juravle68ad6492015-08-18 17:08:12 +01003498 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3499
Roland Levillain4c0eb422015-04-24 16:43:49 +01003500 // Is this instruction a call to a static method?
3501 bool IsStatic() const {
3502 return GetInvokeType() == kStatic;
3503 }
3504
Roland Levillain3e3d7332015-04-28 11:00:54 +01003505 // Remove the art::HLoadClass instruction set as last input by
3506 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3507 // the initial art::HClinitCheck instruction (only relevant for
3508 // static calls with explicit clinit check).
3509 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003510 DCHECK(IsStaticWithExplicitClinitCheck());
3511 size_t last_input_index = InputCount() - 1;
3512 HInstruction* last_input = InputAt(last_input_index);
3513 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003514 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003515 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003516 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003517 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3518 DCHECK(IsStaticWithImplicitClinitCheck());
3519 }
3520
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003521 bool IsStringFactoryFor(HFakeString* str) const {
3522 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003523 DCHECK(!HasCurrentMethodInput());
3524 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003525 return InputAt(InputCount() - 1)->AsFakeString() == str;
3526 }
3527
3528 void RemoveFakeStringArgumentAsLastInput() {
3529 DCHECK(IsStringInit());
3530 size_t last_input_index = InputCount() - 1;
3531 HInstruction* last_input = InputAt(last_input_index);
3532 DCHECK(last_input != nullptr);
3533 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3534 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003535 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003536 }
3537
Roland Levillain4c0eb422015-04-24 16:43:49 +01003538 // Is this a call to a static method whose declaring class has an
3539 // explicit intialization check in the graph?
3540 bool IsStaticWithExplicitClinitCheck() const {
3541 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3542 }
3543
3544 // Is this a call to a static method whose declaring class has an
3545 // implicit intialization check requirement?
3546 bool IsStaticWithImplicitClinitCheck() const {
3547 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3548 }
3549
Vladimir Markob554b5a2015-11-06 12:57:55 +00003550 // Does this method load kind need the current method as an input?
3551 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3552 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3553 }
3554
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003555 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003556
Roland Levillain4c0eb422015-04-24 16:43:49 +01003557 protected:
3558 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3559 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3560 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3561 HInstruction* input = input_record.GetInstruction();
3562 // `input` is the last input of a static invoke marked as having
3563 // an explicit clinit check. It must either be:
3564 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3565 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3566 DCHECK(input != nullptr);
3567 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3568 }
3569 return input_record;
3570 }
3571
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003572 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003573 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003574 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003575 // The target method may refer to different dex file or method index than the original
3576 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3577 // in derived class to increase visibility.
3578 MethodReference target_method_;
3579 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003580
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003581 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003582};
3583
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003584class HInvokeVirtual : public HInvoke {
3585 public:
3586 HInvokeVirtual(ArenaAllocator* arena,
3587 uint32_t number_of_arguments,
3588 Primitive::Type return_type,
3589 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003590 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003591 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003592 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003593 vtable_index_(vtable_index) {}
3594
Calin Juravle641547a2015-04-21 22:08:51 +01003595 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003596 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003597 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003598 }
3599
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003600 uint32_t GetVTableIndex() const { return vtable_index_; }
3601
3602 DECLARE_INSTRUCTION(InvokeVirtual);
3603
3604 private:
3605 const uint32_t vtable_index_;
3606
3607 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3608};
3609
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003610class HInvokeInterface : public HInvoke {
3611 public:
3612 HInvokeInterface(ArenaAllocator* arena,
3613 uint32_t number_of_arguments,
3614 Primitive::Type return_type,
3615 uint32_t dex_pc,
3616 uint32_t dex_method_index,
3617 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003618 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003619 imt_index_(imt_index) {}
3620
Calin Juravle641547a2015-04-21 22:08:51 +01003621 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003622 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003623 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003624 }
3625
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003626 uint32_t GetImtIndex() const { return imt_index_; }
3627 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3628
3629 DECLARE_INSTRUCTION(InvokeInterface);
3630
3631 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003632 const uint32_t imt_index_;
3633
3634 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3635};
3636
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003637class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003638 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003639 HNewInstance(HCurrentMethod* current_method,
3640 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003641 uint16_t type_index,
3642 const DexFile& dex_file,
3643 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003644 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003645 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003646 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003647 entrypoint_(entrypoint) {
3648 SetRawInputAt(0, current_method);
3649 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003650
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003651 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003652 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003653
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003654 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003655 bool NeedsEnvironment() const OVERRIDE { return true; }
Andreas Gampe55d02cf2015-10-29 02:59:50 +00003656 // It may throw when called on:
3657 // - interfaces
3658 // - abstract/innaccessible/unknown classes
3659 // TODO: optimize when possible.
3660 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003661
Calin Juravle10e244f2015-01-26 18:54:32 +00003662 bool CanBeNull() const OVERRIDE { return false; }
3663
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003664 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3665
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003666 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003667
3668 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003669 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003670 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003671 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003672
3673 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3674};
3675
Roland Levillain88cb1752014-10-20 16:36:47 +01003676class HNeg : public HUnaryOperation {
3677 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003678 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3679 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003680
Roland Levillain9867bc72015-08-05 10:21:34 +01003681 template <typename T> T Compute(T x) const { return -x; }
3682
3683 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003684 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003685 }
3686 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003687 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003688 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003689
Roland Levillain88cb1752014-10-20 16:36:47 +01003690 DECLARE_INSTRUCTION(Neg);
3691
3692 private:
3693 DISALLOW_COPY_AND_ASSIGN(HNeg);
3694};
3695
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003696class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003697 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003698 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003699 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003700 uint32_t dex_pc,
3701 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003702 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003703 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003704 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003705 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003706 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003707 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003708 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003709 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003710 }
3711
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003712 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003713 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003714
3715 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003716 bool NeedsEnvironment() const OVERRIDE { return true; }
3717
Mingyao Yang0c365e62015-03-31 15:09:29 -07003718 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3719 bool CanThrow() const OVERRIDE { return true; }
3720
Calin Juravle10e244f2015-01-26 18:54:32 +00003721 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003722
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003723 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3724
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003725 DECLARE_INSTRUCTION(NewArray);
3726
3727 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003728 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003729 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003730 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003731
3732 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3733};
3734
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003735class HAdd : public HBinaryOperation {
3736 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003737 HAdd(Primitive::Type result_type,
3738 HInstruction* left,
3739 HInstruction* right,
3740 uint32_t dex_pc = kNoDexPc)
3741 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003742
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003743 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003744
Roland Levillain9867bc72015-08-05 10:21:34 +01003745 template <typename T> T Compute(T x, T y) const { return x + y; }
3746
3747 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003748 return GetBlock()->GetGraph()->GetIntConstant(
3749 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003750 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003751 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003752 return GetBlock()->GetGraph()->GetLongConstant(
3753 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003754 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003755
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003756 DECLARE_INSTRUCTION(Add);
3757
3758 private:
3759 DISALLOW_COPY_AND_ASSIGN(HAdd);
3760};
3761
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003762class HSub : public HBinaryOperation {
3763 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003764 HSub(Primitive::Type result_type,
3765 HInstruction* left,
3766 HInstruction* right,
3767 uint32_t dex_pc = kNoDexPc)
3768 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003769
Roland Levillain9867bc72015-08-05 10:21:34 +01003770 template <typename T> T Compute(T x, T y) const { return x - y; }
3771
3772 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003773 return GetBlock()->GetGraph()->GetIntConstant(
3774 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003775 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003776 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003777 return GetBlock()->GetGraph()->GetLongConstant(
3778 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003779 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003780
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003781 DECLARE_INSTRUCTION(Sub);
3782
3783 private:
3784 DISALLOW_COPY_AND_ASSIGN(HSub);
3785};
3786
Calin Juravle34bacdf2014-10-07 20:23:36 +01003787class HMul : public HBinaryOperation {
3788 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003789 HMul(Primitive::Type result_type,
3790 HInstruction* left,
3791 HInstruction* right,
3792 uint32_t dex_pc = kNoDexPc)
3793 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003794
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003795 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003796
Roland Levillain9867bc72015-08-05 10:21:34 +01003797 template <typename T> T Compute(T x, T y) const { return x * y; }
3798
3799 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003800 return GetBlock()->GetGraph()->GetIntConstant(
3801 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003802 }
3803 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003804 return GetBlock()->GetGraph()->GetLongConstant(
3805 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003806 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003807
3808 DECLARE_INSTRUCTION(Mul);
3809
3810 private:
3811 DISALLOW_COPY_AND_ASSIGN(HMul);
3812};
3813
Calin Juravle7c4954d2014-10-28 16:57:40 +00003814class HDiv : public HBinaryOperation {
3815 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003816 HDiv(Primitive::Type result_type,
3817 HInstruction* left,
3818 HInstruction* right,
3819 uint32_t dex_pc)
3820 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003821
Roland Levillain9867bc72015-08-05 10:21:34 +01003822 template <typename T>
3823 T Compute(T x, T y) const {
3824 // Our graph structure ensures we never have 0 for `y` during
3825 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003826 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003827 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003828 return (y == -1) ? -x : x / y;
3829 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003830
Roland Levillain9867bc72015-08-05 10:21:34 +01003831 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003832 return GetBlock()->GetGraph()->GetIntConstant(
3833 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003834 }
3835 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003836 return GetBlock()->GetGraph()->GetLongConstant(
3837 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003838 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003839
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003840 static SideEffects SideEffectsForArchRuntimeCalls() {
3841 // The generated code can use a runtime call.
3842 return SideEffects::CanTriggerGC();
3843 }
3844
Calin Juravle7c4954d2014-10-28 16:57:40 +00003845 DECLARE_INSTRUCTION(Div);
3846
3847 private:
3848 DISALLOW_COPY_AND_ASSIGN(HDiv);
3849};
3850
Calin Juravlebacfec32014-11-14 15:54:36 +00003851class HRem : public HBinaryOperation {
3852 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003853 HRem(Primitive::Type result_type,
3854 HInstruction* left,
3855 HInstruction* right,
3856 uint32_t dex_pc)
3857 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003858
Roland Levillain9867bc72015-08-05 10:21:34 +01003859 template <typename T>
3860 T Compute(T x, T y) const {
3861 // Our graph structure ensures we never have 0 for `y` during
3862 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003863 DCHECK_NE(y, 0);
3864 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3865 return (y == -1) ? 0 : x % y;
3866 }
3867
Roland Levillain9867bc72015-08-05 10:21:34 +01003868 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003869 return GetBlock()->GetGraph()->GetIntConstant(
3870 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003871 }
3872 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003873 return GetBlock()->GetGraph()->GetLongConstant(
3874 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003875 }
3876
Calin Juravlebacfec32014-11-14 15:54:36 +00003877
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003878 static SideEffects SideEffectsForArchRuntimeCalls() {
3879 return SideEffects::CanTriggerGC();
3880 }
3881
Calin Juravlebacfec32014-11-14 15:54:36 +00003882 DECLARE_INSTRUCTION(Rem);
3883
3884 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003885 DISALLOW_COPY_AND_ASSIGN(HRem);
3886};
3887
Calin Juravled0d48522014-11-04 16:40:20 +00003888class HDivZeroCheck : public HExpression<1> {
3889 public:
3890 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003891 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003892 SetRawInputAt(0, value);
3893 }
3894
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003895 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3896
Calin Juravled0d48522014-11-04 16:40:20 +00003897 bool CanBeMoved() const OVERRIDE { return true; }
3898
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003899 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003900 return true;
3901 }
3902
3903 bool NeedsEnvironment() const OVERRIDE { return true; }
3904 bool CanThrow() const OVERRIDE { return true; }
3905
Calin Juravled0d48522014-11-04 16:40:20 +00003906 DECLARE_INSTRUCTION(DivZeroCheck);
3907
3908 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003909 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3910};
3911
Calin Juravle9aec02f2014-11-18 23:06:35 +00003912class HShl : public HBinaryOperation {
3913 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003914 HShl(Primitive::Type result_type,
3915 HInstruction* left,
3916 HInstruction* right,
3917 uint32_t dex_pc = kNoDexPc)
3918 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003919
Roland Levillain9867bc72015-08-05 10:21:34 +01003920 template <typename T, typename U, typename V>
3921 T Compute(T x, U y, V max_shift_value) const {
3922 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3923 "V is not the unsigned integer type corresponding to T");
3924 return x << (y & max_shift_value);
3925 }
3926
3927 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3928 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003929 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003930 }
3931 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3932 // case is handled as `x << static_cast<int>(y)`.
3933 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3934 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003935 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003936 }
3937 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3938 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003939 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003940 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003941
3942 DECLARE_INSTRUCTION(Shl);
3943
3944 private:
3945 DISALLOW_COPY_AND_ASSIGN(HShl);
3946};
3947
3948class HShr : public HBinaryOperation {
3949 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003950 HShr(Primitive::Type result_type,
3951 HInstruction* left,
3952 HInstruction* right,
3953 uint32_t dex_pc = kNoDexPc)
3954 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003955
Roland Levillain9867bc72015-08-05 10:21:34 +01003956 template <typename T, typename U, typename V>
3957 T Compute(T x, U y, V max_shift_value) const {
3958 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3959 "V is not the unsigned integer type corresponding to T");
3960 return x >> (y & max_shift_value);
3961 }
3962
3963 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3964 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003965 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003966 }
3967 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3968 // case is handled as `x >> static_cast<int>(y)`.
3969 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3970 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003971 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003972 }
3973 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3974 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003975 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003976 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003977
3978 DECLARE_INSTRUCTION(Shr);
3979
3980 private:
3981 DISALLOW_COPY_AND_ASSIGN(HShr);
3982};
3983
3984class HUShr : public HBinaryOperation {
3985 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003986 HUShr(Primitive::Type result_type,
3987 HInstruction* left,
3988 HInstruction* right,
3989 uint32_t dex_pc = kNoDexPc)
3990 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003991
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 template <typename T, typename U, typename V>
3993 T Compute(T x, U y, V max_shift_value) const {
3994 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3995 "V is not the unsigned integer type corresponding to T");
3996 V ux = static_cast<V>(x);
3997 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003998 }
3999
Roland Levillain9867bc72015-08-05 10:21:34 +01004000 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4001 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004002 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004003 }
4004 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4005 // case is handled as `x >>> static_cast<int>(y)`.
4006 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4007 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004008 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004009 }
4010 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4011 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004012 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004013 }
4014
4015 DECLARE_INSTRUCTION(UShr);
4016
4017 private:
4018 DISALLOW_COPY_AND_ASSIGN(HUShr);
4019};
4020
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004021class HAnd : public HBinaryOperation {
4022 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004023 HAnd(Primitive::Type result_type,
4024 HInstruction* left,
4025 HInstruction* right,
4026 uint32_t dex_pc = kNoDexPc)
4027 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004028
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004029 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004030
Roland Levillain9867bc72015-08-05 10:21:34 +01004031 template <typename T, typename U>
4032 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4033
4034 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004035 return GetBlock()->GetGraph()->GetIntConstant(
4036 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004037 }
4038 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004039 return GetBlock()->GetGraph()->GetLongConstant(
4040 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004041 }
4042 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004043 return GetBlock()->GetGraph()->GetLongConstant(
4044 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004045 }
4046 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004047 return GetBlock()->GetGraph()->GetLongConstant(
4048 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004049 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004050
4051 DECLARE_INSTRUCTION(And);
4052
4053 private:
4054 DISALLOW_COPY_AND_ASSIGN(HAnd);
4055};
4056
4057class HOr : public HBinaryOperation {
4058 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004059 HOr(Primitive::Type result_type,
4060 HInstruction* left,
4061 HInstruction* right,
4062 uint32_t dex_pc = kNoDexPc)
4063 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004064
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004065 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004066
Roland Levillain9867bc72015-08-05 10:21:34 +01004067 template <typename T, typename U>
4068 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4069
4070 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004071 return GetBlock()->GetGraph()->GetIntConstant(
4072 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004073 }
4074 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004075 return GetBlock()->GetGraph()->GetLongConstant(
4076 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004077 }
4078 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004079 return GetBlock()->GetGraph()->GetLongConstant(
4080 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004081 }
4082 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004083 return GetBlock()->GetGraph()->GetLongConstant(
4084 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004085 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004086
4087 DECLARE_INSTRUCTION(Or);
4088
4089 private:
4090 DISALLOW_COPY_AND_ASSIGN(HOr);
4091};
4092
4093class HXor : public HBinaryOperation {
4094 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004095 HXor(Primitive::Type result_type,
4096 HInstruction* left,
4097 HInstruction* right,
4098 uint32_t dex_pc = kNoDexPc)
4099 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004100
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004101 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004102
Roland Levillain9867bc72015-08-05 10:21:34 +01004103 template <typename T, typename U>
4104 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4105
4106 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004107 return GetBlock()->GetGraph()->GetIntConstant(
4108 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004109 }
4110 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004111 return GetBlock()->GetGraph()->GetLongConstant(
4112 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004113 }
4114 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004115 return GetBlock()->GetGraph()->GetLongConstant(
4116 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004117 }
4118 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004119 return GetBlock()->GetGraph()->GetLongConstant(
4120 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004121 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004122
4123 DECLARE_INSTRUCTION(Xor);
4124
4125 private:
4126 DISALLOW_COPY_AND_ASSIGN(HXor);
4127};
4128
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004129// The value of a parameter in this method. Its location depends on
4130// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004131class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004132 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004133 HParameterValue(const DexFile& dex_file,
4134 uint16_t type_index,
4135 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004136 Primitive::Type parameter_type,
4137 bool is_this = false)
4138 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004139 dex_file_(dex_file),
4140 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004141 index_(index),
4142 is_this_(is_this),
4143 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004144
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004145 const DexFile& GetDexFile() const { return dex_file_; }
4146 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004147 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004148 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004149
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004150 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4151 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004152
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004153 DECLARE_INSTRUCTION(ParameterValue);
4154
4155 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004156 const DexFile& dex_file_;
4157 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004158 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004159 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004160 const uint8_t index_;
4161
Calin Juravle10e244f2015-01-26 18:54:32 +00004162 // Whether or not the parameter value corresponds to 'this' argument.
4163 const bool is_this_;
4164
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004165 bool can_be_null_;
4166
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004167 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4168};
4169
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004170class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004171 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004172 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4173 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004174
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004175 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004176 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004177 return true;
4178 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004179
Roland Levillain9867bc72015-08-05 10:21:34 +01004180 template <typename T> T Compute(T x) const { return ~x; }
4181
4182 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004183 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004184 }
4185 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004186 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004187 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004188
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004189 DECLARE_INSTRUCTION(Not);
4190
4191 private:
4192 DISALLOW_COPY_AND_ASSIGN(HNot);
4193};
4194
David Brazdil66d126e2015-04-03 16:02:44 +01004195class HBooleanNot : public HUnaryOperation {
4196 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004197 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4198 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004199
4200 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004201 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004202 return true;
4203 }
4204
Roland Levillain9867bc72015-08-05 10:21:34 +01004205 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004206 DCHECK(IsUint<1>(x));
4207 return !x;
4208 }
4209
Roland Levillain9867bc72015-08-05 10:21:34 +01004210 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004211 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004212 }
4213 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4214 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004215 UNREACHABLE();
4216 }
4217
4218 DECLARE_INSTRUCTION(BooleanNot);
4219
4220 private:
4221 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4222};
4223
Roland Levillaindff1f282014-11-05 14:15:05 +00004224class HTypeConversion : public HExpression<1> {
4225 public:
4226 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004227 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004228 : HExpression(result_type,
4229 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4230 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004231 SetRawInputAt(0, input);
4232 DCHECK_NE(input->GetType(), result_type);
4233 }
4234
4235 HInstruction* GetInput() const { return InputAt(0); }
4236 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4237 Primitive::Type GetResultType() const { return GetType(); }
4238
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004239 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004240 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004241
Roland Levillaindff1f282014-11-05 14:15:05 +00004242 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004243 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004244
Mark Mendelle82549b2015-05-06 10:55:34 -04004245 // Try to statically evaluate the conversion and return a HConstant
4246 // containing the result. If the input cannot be converted, return nullptr.
4247 HConstant* TryStaticEvaluation() const;
4248
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004249 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4250 Primitive::Type result_type) {
4251 // Some architectures may not require the 'GC' side effects, but at this point
4252 // in the compilation process we do not know what architecture we will
4253 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004254 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4255 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004256 return SideEffects::CanTriggerGC();
4257 }
4258 return SideEffects::None();
4259 }
4260
Roland Levillaindff1f282014-11-05 14:15:05 +00004261 DECLARE_INSTRUCTION(TypeConversion);
4262
4263 private:
4264 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4265};
4266
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004267static constexpr uint32_t kNoRegNumber = -1;
4268
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004269class HPhi : public HInstruction {
4270 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004271 HPhi(ArenaAllocator* arena,
4272 uint32_t reg_number,
4273 size_t number_of_inputs,
4274 Primitive::Type type,
4275 uint32_t dex_pc = kNoDexPc)
4276 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004277 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004278 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004279 type_(type),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004280 is_live_(false),
Calin Juravle10e244f2015-01-26 18:54:32 +00004281 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004282 }
4283
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004284 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4285 static Primitive::Type ToPhiType(Primitive::Type type) {
4286 switch (type) {
4287 case Primitive::kPrimBoolean:
4288 case Primitive::kPrimByte:
4289 case Primitive::kPrimShort:
4290 case Primitive::kPrimChar:
4291 return Primitive::kPrimInt;
4292 default:
4293 return type;
4294 }
4295 }
4296
David Brazdilffee3d32015-07-06 11:48:53 +01004297 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4298
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004299 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004300
4301 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004302 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004303
Calin Juravle10e244f2015-01-26 18:54:32 +00004304 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004305 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004306
Calin Juravle10e244f2015-01-26 18:54:32 +00004307 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4308 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4309
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004310 uint32_t GetRegNumber() const { return reg_number_; }
4311
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004312 void SetDead() { is_live_ = false; }
4313 void SetLive() { is_live_ = true; }
4314 bool IsDead() const { return !is_live_; }
4315 bool IsLive() const { return is_live_; }
4316
David Brazdil77a48ae2015-09-15 12:34:04 +00004317 bool IsVRegEquivalentOf(HInstruction* other) const {
4318 return other != nullptr
4319 && other->IsPhi()
4320 && other->AsPhi()->GetBlock() == GetBlock()
4321 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4322 }
4323
Calin Juravlea4f88312015-04-16 12:57:19 +01004324 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4325 // An equivalent phi is a phi having the same dex register and type.
4326 // It assumes that phis with the same dex register are adjacent.
4327 HPhi* GetNextEquivalentPhiWithSameType() {
4328 HInstruction* next = GetNext();
4329 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4330 if (next->GetType() == GetType()) {
4331 return next->AsPhi();
4332 }
4333 next = next->GetNext();
4334 }
4335 return nullptr;
4336 }
4337
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004338 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004339
David Brazdil1abb4192015-02-17 18:33:36 +00004340 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004341 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004342 return inputs_[index];
4343 }
David Brazdil1abb4192015-02-17 18:33:36 +00004344
4345 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004346 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004347 }
4348
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004349 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004350 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004351 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004352 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004353 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004354 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004355
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004356 DISALLOW_COPY_AND_ASSIGN(HPhi);
4357};
4358
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004359class HNullCheck : public HExpression<1> {
4360 public:
4361 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004362 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004363 SetRawInputAt(0, value);
4364 }
4365
Calin Juravle10e244f2015-01-26 18:54:32 +00004366 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004367 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004368 return true;
4369 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004370
Calin Juravle10e244f2015-01-26 18:54:32 +00004371 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004372
Calin Juravle10e244f2015-01-26 18:54:32 +00004373 bool CanThrow() const OVERRIDE { return true; }
4374
4375 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004376
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004377
4378 DECLARE_INSTRUCTION(NullCheck);
4379
4380 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004381 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4382};
4383
4384class FieldInfo : public ValueObject {
4385 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004386 FieldInfo(MemberOffset field_offset,
4387 Primitive::Type field_type,
4388 bool is_volatile,
4389 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004390 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004391 const DexFile& dex_file,
4392 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004393 : field_offset_(field_offset),
4394 field_type_(field_type),
4395 is_volatile_(is_volatile),
4396 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004397 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004398 dex_file_(dex_file),
4399 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400
4401 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004402 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004403 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004404 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004405 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004406 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004407 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004408
4409 private:
4410 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004411 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004412 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004413 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004414 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004415 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004416 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004417};
4418
4419class HInstanceFieldGet : public HExpression<1> {
4420 public:
4421 HInstanceFieldGet(HInstruction* value,
4422 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004423 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004424 bool is_volatile,
4425 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004426 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004427 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004428 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004429 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004430 : HExpression(field_type,
4431 SideEffects::FieldReadOfType(field_type, is_volatile),
4432 dex_pc),
4433 field_info_(field_offset,
4434 field_type,
4435 is_volatile,
4436 field_idx,
4437 declaring_class_def_index,
4438 dex_file,
4439 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004440 SetRawInputAt(0, value);
4441 }
4442
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004443 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004444
4445 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4446 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4447 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004448 }
4449
Calin Juravle641547a2015-04-21 22:08:51 +01004450 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4451 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004452 }
4453
4454 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004455 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4456 }
4457
Calin Juravle52c48962014-12-16 17:02:57 +00004458 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004459 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004460 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004461 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004462
4463 DECLARE_INSTRUCTION(InstanceFieldGet);
4464
4465 private:
4466 const FieldInfo field_info_;
4467
4468 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4469};
4470
4471class HInstanceFieldSet : public HTemplateInstruction<2> {
4472 public:
4473 HInstanceFieldSet(HInstruction* object,
4474 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004475 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004476 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004477 bool is_volatile,
4478 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004479 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004480 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004481 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004482 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004483 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4484 dex_pc),
4485 field_info_(field_offset,
4486 field_type,
4487 is_volatile,
4488 field_idx,
4489 declaring_class_def_index,
4490 dex_file,
4491 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004492 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004493 SetRawInputAt(0, object);
4494 SetRawInputAt(1, value);
4495 }
4496
Calin Juravle641547a2015-04-21 22:08:51 +01004497 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4498 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004499 }
4500
Calin Juravle52c48962014-12-16 17:02:57 +00004501 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004502 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004503 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004504 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004505 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004506 bool GetValueCanBeNull() const { return value_can_be_null_; }
4507 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004508
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004509 DECLARE_INSTRUCTION(InstanceFieldSet);
4510
4511 private:
4512 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004513 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004514
4515 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4516};
4517
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004518class HArrayGet : public HExpression<2> {
4519 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004520 HArrayGet(HInstruction* array,
4521 HInstruction* index,
4522 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004523 uint32_t dex_pc,
4524 SideEffects additional_side_effects = SideEffects::None())
4525 : HExpression(type,
4526 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004527 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004528 SetRawInputAt(0, array);
4529 SetRawInputAt(1, index);
4530 }
4531
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004532 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004533 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004534 return true;
4535 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004536 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004537 // TODO: We can be smarter here.
4538 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4539 // which generates the implicit null check. There are cases when these can be removed
4540 // to produce better code. If we ever add optimizations to do so we should allow an
4541 // implicit check here (as long as the address falls in the first page).
4542 return false;
4543 }
4544
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004545 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004546
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004547 HInstruction* GetArray() const { return InputAt(0); }
4548 HInstruction* GetIndex() const { return InputAt(1); }
4549
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550 DECLARE_INSTRUCTION(ArrayGet);
4551
4552 private:
4553 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4554};
4555
4556class HArraySet : public HTemplateInstruction<3> {
4557 public:
4558 HArraySet(HInstruction* array,
4559 HInstruction* index,
4560 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004561 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004562 uint32_t dex_pc,
4563 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004564 : HTemplateInstruction(
4565 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004566 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4567 additional_side_effects),
4568 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004569 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004570 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004571 value_can_be_null_(true),
4572 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004573 SetRawInputAt(0, array);
4574 SetRawInputAt(1, index);
4575 SetRawInputAt(2, value);
4576 }
4577
Calin Juravle77520bc2015-01-12 18:45:46 +00004578 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004579 // We currently always call a runtime method to catch array store
4580 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004581 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004582 }
4583
Mingyao Yang81014cb2015-06-02 03:16:27 -07004584 // Can throw ArrayStoreException.
4585 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4586
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004587 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004588 // TODO: Same as for ArrayGet.
4589 return false;
4590 }
4591
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004592 void ClearNeedsTypeCheck() {
4593 needs_type_check_ = false;
4594 }
4595
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004596 void ClearValueCanBeNull() {
4597 value_can_be_null_ = false;
4598 }
4599
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004600 void SetStaticTypeOfArrayIsObjectArray() {
4601 static_type_of_array_is_object_array_ = true;
4602 }
4603
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004604 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004605 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004606 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004607
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004608 HInstruction* GetArray() const { return InputAt(0); }
4609 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004610 HInstruction* GetValue() const { return InputAt(2); }
4611
4612 Primitive::Type GetComponentType() const {
4613 // The Dex format does not type floating point index operations. Since the
4614 // `expected_component_type_` is set during building and can therefore not
4615 // be correct, we also check what is the value type. If it is a floating
4616 // point type, we must use that type.
4617 Primitive::Type value_type = GetValue()->GetType();
4618 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4619 ? value_type
4620 : expected_component_type_;
4621 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004622
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004623 Primitive::Type GetRawExpectedComponentType() const {
4624 return expected_component_type_;
4625 }
4626
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004627 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4628 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4629 }
4630
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004631 DECLARE_INSTRUCTION(ArraySet);
4632
4633 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004634 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004635 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004636 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004637 // Cached information for the reference_type_info_ so that codegen
4638 // does not need to inspect the static type.
4639 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004640
4641 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4642};
4643
4644class HArrayLength : public HExpression<1> {
4645 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004646 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004647 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004648 // Note that arrays do not change length, so the instruction does not
4649 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004650 SetRawInputAt(0, array);
4651 }
4652
Calin Juravle77520bc2015-01-12 18:45:46 +00004653 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004654 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004655 return true;
4656 }
Calin Juravle641547a2015-04-21 22:08:51 +01004657 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4658 return obj == InputAt(0);
4659 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004660
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004661 DECLARE_INSTRUCTION(ArrayLength);
4662
4663 private:
4664 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4665};
4666
4667class HBoundsCheck : public HExpression<2> {
4668 public:
4669 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004670 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004671 DCHECK(index->GetType() == Primitive::kPrimInt);
4672 SetRawInputAt(0, index);
4673 SetRawInputAt(1, length);
4674 }
4675
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004676 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004677 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004678 return true;
4679 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004680
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004681 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004682
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004683 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004684
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004685 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004686
4687 DECLARE_INSTRUCTION(BoundsCheck);
4688
4689 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004690 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4691};
4692
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004693/**
4694 * Some DEX instructions are folded into multiple HInstructions that need
4695 * to stay live until the last HInstruction. This class
4696 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004697 * HInstruction stays live. `index` represents the stack location index of the
4698 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004699 */
4700class HTemporary : public HTemplateInstruction<0> {
4701 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004702 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4703 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004704
4705 size_t GetIndex() const { return index_; }
4706
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004707 Primitive::Type GetType() const OVERRIDE {
4708 // The previous instruction is the one that will be stored in the temporary location.
4709 DCHECK(GetPrevious() != nullptr);
4710 return GetPrevious()->GetType();
4711 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004712
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004713 DECLARE_INSTRUCTION(Temporary);
4714
4715 private:
4716 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004717 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4718};
4719
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004720class HSuspendCheck : public HTemplateInstruction<0> {
4721 public:
4722 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004723 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004724
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004725 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004726 return true;
4727 }
4728
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004729 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4730 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004731
4732 DECLARE_INSTRUCTION(SuspendCheck);
4733
4734 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004735 // Only used for code generation, in order to share the same slow path between back edges
4736 // of a same loop.
4737 SlowPathCode* slow_path_;
4738
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004739 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4740};
4741
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004742/**
4743 * Instruction to load a Class object.
4744 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004745class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004746 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004747 HLoadClass(HCurrentMethod* current_method,
4748 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004749 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004750 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004751 uint32_t dex_pc,
4752 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004753 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004754 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004755 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004756 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004757 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004758 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004759 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004760 // Referrers class should not need access check. We never inline unverified
4761 // methods so we can't possibly end up in this situation.
4762 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004763 SetRawInputAt(0, current_method);
4764 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004765
4766 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004767
4768 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004769 // Note that we don't need to test for generate_clinit_check_.
4770 // Whether or not we need to generate the clinit check is processed in
4771 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004772 return other->AsLoadClass()->type_index_ == type_index_ &&
4773 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004774 }
4775
4776 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4777
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004778 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004779 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004780 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004781
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004782 bool NeedsEnvironment() const OVERRIDE {
4783 // Will call runtime and load the class if the class is not loaded yet.
4784 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004785 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004786 }
4787
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004788 bool MustGenerateClinitCheck() const {
4789 return generate_clinit_check_;
4790 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004791 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004792 // The entrypoint the code generator is going to call does not do
4793 // clinit of the class.
4794 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004795 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004796 }
4797
4798 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004799 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4800 }
4801
4802 bool NeedsAccessCheck() const {
4803 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004804 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004805
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004806 bool CanThrow() const OVERRIDE {
4807 // May call runtime and and therefore can throw.
4808 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004809 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004810 }
4811
Calin Juravleacf735c2015-02-12 15:25:22 +00004812 ReferenceTypeInfo GetLoadedClassRTI() {
4813 return loaded_class_rti_;
4814 }
4815
4816 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4817 // Make sure we only set exact types (the loaded class should never be merged).
4818 DCHECK(rti.IsExact());
4819 loaded_class_rti_ = rti;
4820 }
4821
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004822 const DexFile& GetDexFile() { return dex_file_; }
4823
Vladimir Markodc151b22015-10-15 18:02:30 +01004824 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004825
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004826 static SideEffects SideEffectsForArchRuntimeCalls() {
4827 return SideEffects::CanTriggerGC();
4828 }
4829
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004830 DECLARE_INSTRUCTION(LoadClass);
4831
4832 private:
4833 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004834 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004835 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004836 // Whether this instruction must generate the initialization check.
4837 // Used for code generation.
4838 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004839 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004840
Calin Juravleacf735c2015-02-12 15:25:22 +00004841 ReferenceTypeInfo loaded_class_rti_;
4842
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004843 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4844};
4845
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004846class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004847 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004848 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004849 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4850 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004851 SetRawInputAt(0, current_method);
4852 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004853
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004854 bool CanBeMoved() const OVERRIDE { return true; }
4855
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004856 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4857 return other->AsLoadString()->string_index_ == string_index_;
4858 }
4859
4860 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4861
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004862 uint32_t GetStringIndex() const { return string_index_; }
4863
4864 // TODO: Can we deopt or debug when we resolve a string?
4865 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004866 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004867 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004868
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004869 static SideEffects SideEffectsForArchRuntimeCalls() {
4870 return SideEffects::CanTriggerGC();
4871 }
4872
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004873 DECLARE_INSTRUCTION(LoadString);
4874
4875 private:
4876 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004877
4878 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4879};
4880
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004881/**
4882 * Performs an initialization check on its Class object input.
4883 */
4884class HClinitCheck : public HExpression<1> {
4885 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004886 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004887 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004888 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004889 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4890 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004891 SetRawInputAt(0, constant);
4892 }
4893
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004894 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004895 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004896 return true;
4897 }
4898
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004899 bool NeedsEnvironment() const OVERRIDE {
4900 // May call runtime to initialize the class.
4901 return true;
4902 }
4903
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004904
4905 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4906
4907 DECLARE_INSTRUCTION(ClinitCheck);
4908
4909 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004910 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4911};
4912
4913class HStaticFieldGet : public HExpression<1> {
4914 public:
4915 HStaticFieldGet(HInstruction* cls,
4916 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004917 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004918 bool is_volatile,
4919 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004920 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004921 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004922 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004923 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004924 : HExpression(field_type,
4925 SideEffects::FieldReadOfType(field_type, is_volatile),
4926 dex_pc),
4927 field_info_(field_offset,
4928 field_type,
4929 is_volatile,
4930 field_idx,
4931 declaring_class_def_index,
4932 dex_file,
4933 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004934 SetRawInputAt(0, cls);
4935 }
4936
Calin Juravle52c48962014-12-16 17:02:57 +00004937
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004938 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004939
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004940 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004941 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4942 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004943 }
4944
4945 size_t ComputeHashCode() const OVERRIDE {
4946 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
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
4954 DECLARE_INSTRUCTION(StaticFieldGet);
4955
4956 private:
4957 const FieldInfo field_info_;
4958
4959 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4960};
4961
4962class HStaticFieldSet : public HTemplateInstruction<2> {
4963 public:
4964 HStaticFieldSet(HInstruction* cls,
4965 HInstruction* value,
4966 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004967 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004968 bool is_volatile,
4969 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004970 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004971 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004972 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004973 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004974 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4975 dex_pc),
4976 field_info_(field_offset,
4977 field_type,
4978 is_volatile,
4979 field_idx,
4980 declaring_class_def_index,
4981 dex_file,
4982 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004983 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004984 SetRawInputAt(0, cls);
4985 SetRawInputAt(1, value);
4986 }
4987
Calin Juravle52c48962014-12-16 17:02:57 +00004988 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004989 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4990 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004991 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004992
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004993 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004994 bool GetValueCanBeNull() const { return value_can_be_null_; }
4995 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004996
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004997 DECLARE_INSTRUCTION(StaticFieldSet);
4998
4999 private:
5000 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005001 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005002
5003 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5004};
5005
Calin Juravlee460d1d2015-09-29 04:52:17 +01005006class HUnresolvedInstanceFieldGet : public HExpression<1> {
5007 public:
5008 HUnresolvedInstanceFieldGet(HInstruction* obj,
5009 Primitive::Type field_type,
5010 uint32_t field_index,
5011 uint32_t dex_pc)
5012 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5013 field_index_(field_index) {
5014 SetRawInputAt(0, obj);
5015 }
5016
5017 bool NeedsEnvironment() const OVERRIDE { return true; }
5018 bool CanThrow() const OVERRIDE { return true; }
5019
5020 Primitive::Type GetFieldType() const { return GetType(); }
5021 uint32_t GetFieldIndex() const { return field_index_; }
5022
5023 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5024
5025 private:
5026 const uint32_t field_index_;
5027
5028 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5029};
5030
5031class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5032 public:
5033 HUnresolvedInstanceFieldSet(HInstruction* obj,
5034 HInstruction* value,
5035 Primitive::Type field_type,
5036 uint32_t field_index,
5037 uint32_t dex_pc)
5038 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5039 field_type_(field_type),
5040 field_index_(field_index) {
5041 DCHECK_EQ(field_type, value->GetType());
5042 SetRawInputAt(0, obj);
5043 SetRawInputAt(1, value);
5044 }
5045
5046 bool NeedsEnvironment() const OVERRIDE { return true; }
5047 bool CanThrow() const OVERRIDE { return true; }
5048
5049 Primitive::Type GetFieldType() const { return field_type_; }
5050 uint32_t GetFieldIndex() const { return field_index_; }
5051
5052 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5053
5054 private:
5055 const Primitive::Type field_type_;
5056 const uint32_t field_index_;
5057
5058 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5059};
5060
5061class HUnresolvedStaticFieldGet : public HExpression<0> {
5062 public:
5063 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5064 uint32_t field_index,
5065 uint32_t dex_pc)
5066 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5067 field_index_(field_index) {
5068 }
5069
5070 bool NeedsEnvironment() const OVERRIDE { return true; }
5071 bool CanThrow() const OVERRIDE { return true; }
5072
5073 Primitive::Type GetFieldType() const { return GetType(); }
5074 uint32_t GetFieldIndex() const { return field_index_; }
5075
5076 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5077
5078 private:
5079 const uint32_t field_index_;
5080
5081 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5082};
5083
5084class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5085 public:
5086 HUnresolvedStaticFieldSet(HInstruction* value,
5087 Primitive::Type field_type,
5088 uint32_t field_index,
5089 uint32_t dex_pc)
5090 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5091 field_type_(field_type),
5092 field_index_(field_index) {
5093 DCHECK_EQ(field_type, value->GetType());
5094 SetRawInputAt(0, value);
5095 }
5096
5097 bool NeedsEnvironment() const OVERRIDE { return true; }
5098 bool CanThrow() const OVERRIDE { return true; }
5099
5100 Primitive::Type GetFieldType() const { return field_type_; }
5101 uint32_t GetFieldIndex() const { return field_index_; }
5102
5103 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5104
5105 private:
5106 const Primitive::Type field_type_;
5107 const uint32_t field_index_;
5108
5109 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5110};
5111
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005112// Implement the move-exception DEX instruction.
5113class HLoadException : public HExpression<0> {
5114 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005115 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5116 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005117
David Brazdilbbd733e2015-08-18 17:48:17 +01005118 bool CanBeNull() const OVERRIDE { return false; }
5119
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005120 DECLARE_INSTRUCTION(LoadException);
5121
5122 private:
5123 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5124};
5125
David Brazdilcb1c0552015-08-04 16:22:25 +01005126// Implicit part of move-exception which clears thread-local exception storage.
5127// Must not be removed because the runtime expects the TLS to get cleared.
5128class HClearException : public HTemplateInstruction<0> {
5129 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005130 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5131 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005132
5133 DECLARE_INSTRUCTION(ClearException);
5134
5135 private:
5136 DISALLOW_COPY_AND_ASSIGN(HClearException);
5137};
5138
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005139class HThrow : public HTemplateInstruction<1> {
5140 public:
5141 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005142 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005143 SetRawInputAt(0, exception);
5144 }
5145
5146 bool IsControlFlow() const OVERRIDE { return true; }
5147
5148 bool NeedsEnvironment() const OVERRIDE { return true; }
5149
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005150 bool CanThrow() const OVERRIDE { return true; }
5151
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005152
5153 DECLARE_INSTRUCTION(Throw);
5154
5155 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005156 DISALLOW_COPY_AND_ASSIGN(HThrow);
5157};
5158
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005159/**
5160 * Implementation strategies for the code generator of a HInstanceOf
5161 * or `HCheckCast`.
5162 */
5163enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005164 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005165 kExactCheck, // Can do a single class compare.
5166 kClassHierarchyCheck, // Can just walk the super class chain.
5167 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5168 kInterfaceCheck, // No optimization yet when checking against an interface.
5169 kArrayObjectCheck, // Can just check if the array is not primitive.
5170 kArrayCheck // No optimization yet when checking against a generic array.
5171};
5172
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005173class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005174 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005175 HInstanceOf(HInstruction* object,
5176 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005177 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005178 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005179 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005180 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005181 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005182 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005183 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005184 SetRawInputAt(0, object);
5185 SetRawInputAt(1, constant);
5186 }
5187
5188 bool CanBeMoved() const OVERRIDE { return true; }
5189
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005190 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005191 return true;
5192 }
5193
5194 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005195 return false;
5196 }
5197
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005198 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5199
5200 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005201
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005202 // Used only in code generation.
5203 bool MustDoNullCheck() const { return must_do_null_check_; }
5204 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5205
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005206 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5207 return (check_kind == TypeCheckKind::kExactCheck)
5208 ? SideEffects::None()
5209 // Mips currently does runtime calls for any other checks.
5210 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005211 }
5212
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005213 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005214
5215 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005216 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005217 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005218
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005219 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5220};
5221
Calin Juravleb1498f62015-02-16 13:13:29 +00005222class HBoundType : public HExpression<1> {
5223 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005224 // Constructs an HBoundType with the given upper_bound.
5225 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005226 HBoundType(HInstruction* input,
5227 ReferenceTypeInfo upper_bound,
5228 bool upper_can_be_null,
5229 uint32_t dex_pc = kNoDexPc)
5230 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005231 upper_bound_(upper_bound),
5232 upper_can_be_null_(upper_can_be_null),
5233 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005234 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005235 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005236 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005237 }
5238
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005239 // GetUpper* should only be used in reference type propagation.
5240 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5241 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005242
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005243 void SetCanBeNull(bool can_be_null) {
5244 DCHECK(upper_can_be_null_ || !can_be_null);
5245 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005246 }
5247
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005248 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5249
Calin Juravleb1498f62015-02-16 13:13:29 +00005250 DECLARE_INSTRUCTION(BoundType);
5251
5252 private:
5253 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005254 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5255 // It is used to bound the type in cases like:
5256 // if (x instanceof ClassX) {
5257 // // uper_bound_ will be ClassX
5258 // }
5259 const ReferenceTypeInfo upper_bound_;
5260 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5261 // is false then can_be_null_ cannot be true).
5262 const bool upper_can_be_null_;
5263 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005264
5265 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5266};
5267
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005268class HCheckCast : public HTemplateInstruction<2> {
5269 public:
5270 HCheckCast(HInstruction* object,
5271 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005272 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005273 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005274 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005275 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005276 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005277 SetRawInputAt(0, object);
5278 SetRawInputAt(1, constant);
5279 }
5280
5281 bool CanBeMoved() const OVERRIDE { return true; }
5282
5283 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5284 return true;
5285 }
5286
5287 bool NeedsEnvironment() const OVERRIDE {
5288 // Instruction may throw a CheckCastError.
5289 return true;
5290 }
5291
5292 bool CanThrow() const OVERRIDE { return true; }
5293
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005294 bool MustDoNullCheck() const { return must_do_null_check_; }
5295 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005296 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005297
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005298 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005299
5300 DECLARE_INSTRUCTION(CheckCast);
5301
5302 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005303 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005304 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005305
5306 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005307};
5308
Calin Juravle27df7582015-04-17 19:12:31 +01005309class HMemoryBarrier : public HTemplateInstruction<0> {
5310 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005311 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005312 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005313 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005314 barrier_kind_(barrier_kind) {}
5315
5316 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5317
5318 DECLARE_INSTRUCTION(MemoryBarrier);
5319
5320 private:
5321 const MemBarrierKind barrier_kind_;
5322
5323 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5324};
5325
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005326class HMonitorOperation : public HTemplateInstruction<1> {
5327 public:
5328 enum OperationKind {
5329 kEnter,
5330 kExit,
5331 };
5332
5333 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005334 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005335 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5336 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005337 SetRawInputAt(0, object);
5338 }
5339
5340 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005341 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5342
5343 bool CanThrow() const OVERRIDE {
5344 // Verifier guarantees that monitor-exit cannot throw.
5345 // This is important because it allows the HGraphBuilder to remove
5346 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5347 return IsEnter();
5348 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005349
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005350
5351 bool IsEnter() const { return kind_ == kEnter; }
5352
5353 DECLARE_INSTRUCTION(MonitorOperation);
5354
Calin Juravle52c48962014-12-16 17:02:57 +00005355 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005356 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005357
5358 private:
5359 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5360};
5361
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005362/**
5363 * A HInstruction used as a marker for the replacement of new + <init>
5364 * of a String to a call to a StringFactory. Only baseline will see
5365 * the node at code generation, where it will be be treated as null.
5366 * When compiling non-baseline, `HFakeString` instructions are being removed
5367 * in the instruction simplifier.
5368 */
5369class HFakeString : public HTemplateInstruction<0> {
5370 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005371 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5372 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005373
5374 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5375
5376 DECLARE_INSTRUCTION(FakeString);
5377
5378 private:
5379 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5380};
5381
Vladimir Markof9f64412015-09-02 14:05:49 +01005382class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005383 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005384 MoveOperands(Location source,
5385 Location destination,
5386 Primitive::Type type,
5387 HInstruction* instruction)
5388 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005389
5390 Location GetSource() const { return source_; }
5391 Location GetDestination() const { return destination_; }
5392
5393 void SetSource(Location value) { source_ = value; }
5394 void SetDestination(Location value) { destination_ = value; }
5395
5396 // The parallel move resolver marks moves as "in-progress" by clearing the
5397 // destination (but not the source).
5398 Location MarkPending() {
5399 DCHECK(!IsPending());
5400 Location dest = destination_;
5401 destination_ = Location::NoLocation();
5402 return dest;
5403 }
5404
5405 void ClearPending(Location dest) {
5406 DCHECK(IsPending());
5407 destination_ = dest;
5408 }
5409
5410 bool IsPending() const {
5411 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5412 return destination_.IsInvalid() && !source_.IsInvalid();
5413 }
5414
5415 // True if this blocks a move from the given location.
5416 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005417 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005418 }
5419
5420 // A move is redundant if it's been eliminated, if its source and
5421 // destination are the same, or if its destination is unneeded.
5422 bool IsRedundant() const {
5423 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5424 }
5425
5426 // We clear both operands to indicate move that's been eliminated.
5427 void Eliminate() {
5428 source_ = destination_ = Location::NoLocation();
5429 }
5430
5431 bool IsEliminated() const {
5432 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5433 return source_.IsInvalid();
5434 }
5435
Alexey Frunze4dda3372015-06-01 18:31:49 -07005436 Primitive::Type GetType() const { return type_; }
5437
Nicolas Geoffray90218252015-04-15 11:56:51 +01005438 bool Is64BitMove() const {
5439 return Primitive::Is64BitType(type_);
5440 }
5441
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005442 HInstruction* GetInstruction() const { return instruction_; }
5443
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005444 private:
5445 Location source_;
5446 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005447 // The type this move is for.
5448 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005449 // The instruction this move is assocatied with. Null when this move is
5450 // for moving an input in the expected locations of user (including a phi user).
5451 // This is only used in debug mode, to ensure we do not connect interval siblings
5452 // in the same parallel move.
5453 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005454};
5455
5456static constexpr size_t kDefaultNumberOfMoves = 4;
5457
5458class HParallelMove : public HTemplateInstruction<0> {
5459 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005460 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005461 : HTemplateInstruction(SideEffects::None(), dex_pc),
5462 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5463 moves_.reserve(kDefaultNumberOfMoves);
5464 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005465
Nicolas Geoffray90218252015-04-15 11:56:51 +01005466 void AddMove(Location source,
5467 Location destination,
5468 Primitive::Type type,
5469 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005470 DCHECK(source.IsValid());
5471 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005472 if (kIsDebugBuild) {
5473 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005474 for (const MoveOperands& move : moves_) {
5475 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005476 // Special case the situation where the move is for the spill slot
5477 // of the instruction.
5478 if ((GetPrevious() == instruction)
5479 || ((GetPrevious() == nullptr)
5480 && instruction->IsPhi()
5481 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005482 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005483 << "Doing parallel moves for the same instruction.";
5484 } else {
5485 DCHECK(false) << "Doing parallel moves for the same instruction.";
5486 }
5487 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005488 }
5489 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005490 for (const MoveOperands& move : moves_) {
5491 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005492 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005493 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005494 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005495 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005496 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005497 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005498 }
5499
Vladimir Marko225b6462015-09-28 12:17:40 +01005500 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005501 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005502 }
5503
Vladimir Marko225b6462015-09-28 12:17:40 +01005504 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005505
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005506 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005507
5508 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005509 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005510
5511 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5512};
5513
Mark Mendell0616ae02015-04-17 12:49:27 -04005514} // namespace art
5515
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005516#ifdef ART_ENABLE_CODEGEN_arm64
5517#include "nodes_arm64.h"
5518#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005519#ifdef ART_ENABLE_CODEGEN_x86
5520#include "nodes_x86.h"
5521#endif
5522
5523namespace art {
5524
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005525class HGraphVisitor : public ValueObject {
5526 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005527 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5528 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005529
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005530 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005531 virtual void VisitBasicBlock(HBasicBlock* block);
5532
Roland Levillain633021e2014-10-01 14:12:25 +01005533 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005534 void VisitInsertionOrder();
5535
Roland Levillain633021e2014-10-01 14:12:25 +01005536 // Visit the graph following dominator tree reverse post-order.
5537 void VisitReversePostOrder();
5538
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005539 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005540
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005541 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005542#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005543 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5544
5545 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5546
5547#undef DECLARE_VISIT_INSTRUCTION
5548
5549 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005550 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005551
5552 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5553};
5554
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005555class HGraphDelegateVisitor : public HGraphVisitor {
5556 public:
5557 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5558 virtual ~HGraphDelegateVisitor() {}
5559
5560 // Visit functions that delegate to to super class.
5561#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005562 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005563
5564 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5565
5566#undef DECLARE_VISIT_INSTRUCTION
5567
5568 private:
5569 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5570};
5571
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005572class HInsertionOrderIterator : public ValueObject {
5573 public:
5574 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5575
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005576 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005577 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005578 void Advance() { ++index_; }
5579
5580 private:
5581 const HGraph& graph_;
5582 size_t index_;
5583
5584 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5585};
5586
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005587class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005588 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005589 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5590 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005591 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005592 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005593
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005594 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5595 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005596 void Advance() { ++index_; }
5597
5598 private:
5599 const HGraph& graph_;
5600 size_t index_;
5601
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005602 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005603};
5604
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005605class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005606 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005607 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005608 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005609 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005610 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005611 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005612
5613 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005614 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005615 void Advance() { --index_; }
5616
5617 private:
5618 const HGraph& graph_;
5619 size_t index_;
5620
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005621 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005622};
5623
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005624class HLinearPostOrderIterator : public ValueObject {
5625 public:
5626 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005627 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005628
5629 bool Done() const { return index_ == 0; }
5630
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005631 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005632
5633 void Advance() {
5634 --index_;
5635 DCHECK_GE(index_, 0U);
5636 }
5637
5638 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005639 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005640 size_t index_;
5641
5642 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5643};
5644
5645class HLinearOrderIterator : public ValueObject {
5646 public:
5647 explicit HLinearOrderIterator(const HGraph& graph)
5648 : order_(graph.GetLinearOrder()), index_(0) {}
5649
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005650 bool Done() const { return index_ == order_.size(); }
5651 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005652 void Advance() { ++index_; }
5653
5654 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005655 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005656 size_t index_;
5657
5658 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5659};
5660
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005661// Iterator over the blocks that art part of the loop. Includes blocks part
5662// of an inner loop. The order in which the blocks are iterated is on their
5663// block id.
5664class HBlocksInLoopIterator : public ValueObject {
5665 public:
5666 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5667 : blocks_in_loop_(info.GetBlocks()),
5668 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5669 index_(0) {
5670 if (!blocks_in_loop_.IsBitSet(index_)) {
5671 Advance();
5672 }
5673 }
5674
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005675 bool Done() const { return index_ == blocks_.size(); }
5676 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005677 void Advance() {
5678 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005679 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005680 if (blocks_in_loop_.IsBitSet(index_)) {
5681 break;
5682 }
5683 }
5684 }
5685
5686 private:
5687 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005688 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005689 size_t index_;
5690
5691 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5692};
5693
Mingyao Yang3584bce2015-05-19 16:01:59 -07005694// Iterator over the blocks that art part of the loop. Includes blocks part
5695// of an inner loop. The order in which the blocks are iterated is reverse
5696// post order.
5697class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5698 public:
5699 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5700 : blocks_in_loop_(info.GetBlocks()),
5701 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5702 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005703 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005704 Advance();
5705 }
5706 }
5707
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005708 bool Done() const { return index_ == blocks_.size(); }
5709 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005710 void Advance() {
5711 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005712 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5713 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005714 break;
5715 }
5716 }
5717 }
5718
5719 private:
5720 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005721 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005722 size_t index_;
5723
5724 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5725};
5726
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005727inline int64_t Int64FromConstant(HConstant* constant) {
5728 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5729 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5730 : constant->AsLongConstant()->GetValue();
5731}
5732
Vladimir Marko58155012015-08-19 12:49:41 +00005733inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5734 // For the purposes of the compiler, the dex files must actually be the same object
5735 // if we want to safely treat them as the same. This is especially important for JIT
5736 // as custom class loaders can open the same underlying file (or memory) multiple
5737 // times and provide different class resolution but no two class loaders should ever
5738 // use the same DexFile object - doing so is an unsupported hack that can lead to
5739 // all sorts of weird failures.
5740 return &lhs == &rhs;
5741}
5742
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005743} // namespace art
5744
5745#endif // ART_COMPILER_OPTIMIZING_NODES_H_