blob: 6028d4b6fa4c9e4c0b633823c22d81a1c2663bd4 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39namespace art {
40
David Brazdil1abb4192015-02-17 18:33:36 +000041class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010045class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010046class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070085 // All types.
86 kCondEQ, // ==
87 kCondNE, // !=
88 // Signed integers and floating-point numbers.
89 kCondLT, // <
90 kCondLE, // <=
91 kCondGT, // >
92 kCondGE, // >=
93 // Unsigned integers.
94 kCondB, // <
95 kCondBE, // <=
96 kCondA, // >
97 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070098};
99
Vladimir Markof9f64412015-09-02 14:05:49 +0100100class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100101 public:
102 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
103
104 void AddInstruction(HInstruction* instruction);
105 void RemoveInstruction(HInstruction* instruction);
106
David Brazdilc3d743f2015-04-22 13:40:50 +0100107 // Insert `instruction` before/after an existing instruction `cursor`.
108 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
109 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
110
Roland Levillain6b469232014-09-25 10:10:38 +0100111 // Return true if this list contains `instruction`.
112 bool Contains(HInstruction* instruction) const;
113
Roland Levillainccc07a92014-09-16 14:48:16 +0100114 // Return true if `instruction1` is found before `instruction2` in
115 // this instruction list and false otherwise. Abort if none
116 // of these instructions is found.
117 bool FoundBefore(const HInstruction* instruction1,
118 const HInstruction* instruction2) const;
119
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000120 bool IsEmpty() const { return first_instruction_ == nullptr; }
121 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
122
123 // Update the block of all instructions to be `block`.
124 void SetBlockOfInstructions(HBasicBlock* block) const;
125
126 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
127 void Add(const HInstructionList& instruction_list);
128
David Brazdil2d7352b2015-04-20 14:52:42 +0100129 // Return the number of instructions in the list. This is an expensive operation.
130 size_t CountSize() const;
131
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100132 private:
133 HInstruction* first_instruction_;
134 HInstruction* last_instruction_;
135
136 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000137 friend class HGraph;
138 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100140 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100141
142 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
143};
144
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000145// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100146class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100148 HGraph(ArenaAllocator* arena,
149 const DexFile& dex_file,
150 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100151 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700152 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100153 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100154 bool debuggable = false,
155 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000156 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100157 blocks_(arena->Adapter(kArenaAllocBlockList)),
158 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
159 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700160 entry_block_(nullptr),
161 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100162 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100163 number_of_vregs_(0),
164 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000165 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400166 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000167 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000168 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000169 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100170 dex_file_(dex_file),
171 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100172 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100173 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100174 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700175 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000176 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100177 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
178 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
179 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
180 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
181 cached_current_method_(nullptr) {
182 blocks_.reserve(kDefaultNumberOfBlocks);
183 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000184
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000185 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100186 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
187
David Brazdil69ba7b72015-06-23 18:27:30 +0100188 bool IsInSsaForm() const { return in_ssa_form_; }
189
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000190 HBasicBlock* GetEntryBlock() const { return entry_block_; }
191 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100192 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000193
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000194 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
195 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000196
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000197 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 // Try building the SSA form of this graph, with dominance computation and loop
200 // recognition. Returns whether it was successful in doing all these steps.
201 bool TryBuildingSsa() {
202 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000203 // The SSA builder requires loops to all be natural. Specifically, the dead phi
204 // elimination phase checks the consistency of the graph when doing a post-order
205 // visit for eliminating dead phis: a dead phi can only have loop header phi
206 // users remaining when being visited.
207 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100208 // Precompute per-block try membership before entering the SSA builder,
209 // which needs the information to build catch block phis from values of
210 // locals at throwing instructions inside try blocks.
211 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100213 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000214 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 }
216
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100217 void ComputeDominanceInformation();
218 void ClearDominanceInformation();
219
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000220 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100222 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100223 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000224
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000225 // Analyze all natural loops in this graph. Returns false if one
226 // loop is not natural, that is the header does not dominate the
227 // back edge.
228 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100229
David Brazdilffee3d32015-07-06 11:48:53 +0100230 // Iterate over blocks to compute try block membership. Needs reverse post
231 // order and loop information.
232 void ComputeTryBlockInformation();
233
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000234 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000235 // Returns the instruction used to replace the invoke expression or null if the
236 // invoke is for a void method.
237 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238
Mingyao Yang3584bce2015-05-19 16:01:59 -0700239 // Need to add a couple of blocks to test if the loop body is entered and
240 // put deoptimization instructions, etc.
241 void TransformLoopHeaderForBCE(HBasicBlock* header);
242
David Brazdil2d7352b2015-04-20 14:52:42 +0100243 // Removes `block` from the graph.
244 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000245
David Brazdilfc6a86a2015-06-26 10:33:45 +0000246 // Splits the edge between `block` and `successor` while preserving the
247 // indices in the predecessor/successor lists. If there are multiple edges
248 // between the blocks, the lowest indices are used.
249 // Returns the new block which is empty and has the same dex pc as `successor`.
250 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
251
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100252 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
253 void SimplifyLoop(HBasicBlock* header);
254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000255 int32_t GetNextInstructionId() {
256 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000257 return current_instruction_id_++;
258 }
259
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000260 int32_t GetCurrentInstructionId() const {
261 return current_instruction_id_;
262 }
263
264 void SetCurrentInstructionId(int32_t id) {
265 current_instruction_id_ = id;
266 }
267
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100268 uint16_t GetMaximumNumberOfOutVRegs() const {
269 return maximum_number_of_out_vregs_;
270 }
271
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000272 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
273 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100274 }
275
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100276 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
277 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
278 }
279
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000280 void UpdateTemporariesVRegSlots(size_t slots) {
281 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100282 }
283
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000284 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100285 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000286 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100287 }
288
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100289 void SetNumberOfVRegs(uint16_t number_of_vregs) {
290 number_of_vregs_ = number_of_vregs;
291 }
292
293 uint16_t GetNumberOfVRegs() const {
294 return number_of_vregs_;
295 }
296
297 void SetNumberOfInVRegs(uint16_t value) {
298 number_of_in_vregs_ = value;
299 }
300
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100301 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100302 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100303 return number_of_vregs_ - number_of_in_vregs_;
304 }
305
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100306 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100307 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100308 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100309
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100311 return linear_order_;
312 }
313
Mark Mendell1152c922015-04-24 17:06:35 -0400314 bool HasBoundsChecks() const {
315 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800316 }
317
Mark Mendell1152c922015-04-24 17:06:35 -0400318 void SetHasBoundsChecks(bool value) {
319 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800320 }
321
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100322 bool ShouldGenerateConstructorBarrier() const {
323 return should_generate_constructor_barrier_;
324 }
325
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000326 bool IsDebuggable() const { return debuggable_; }
327
David Brazdil8d5b8b22015-03-24 10:51:52 +0000328 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000329 // already, it is created and inserted into the graph. This method is only for
330 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600331 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000332
333 // TODO: This is problematic for the consistency of reference type propagation
334 // because it can be created anytime after the pass and thus it will be left
335 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600336 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000337
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600338 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
339 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000340 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600341 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
342 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000343 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600344 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
345 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000346 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600347 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
348 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000349 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000350
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100351 HCurrentMethod* GetCurrentMethod();
352
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000353 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100354
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100355 const DexFile& GetDexFile() const {
356 return dex_file_;
357 }
358
359 uint32_t GetMethodIdx() const {
360 return method_idx_;
361 }
362
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100363 InvokeType GetInvokeType() const {
364 return invoke_type_;
365 }
366
Mark Mendellc4701932015-04-10 13:18:51 -0400367 InstructionSet GetInstructionSet() const {
368 return instruction_set_;
369 }
370
David Brazdil77a48ae2015-09-15 12:34:04 +0000371 bool HasTryCatch() const { return has_try_catch_; }
372 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100373
David Brazdil2d7352b2015-04-20 14:52:42 +0100374 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100375 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000376 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100377 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000378
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000379 template <class InstructionType, typename ValueType>
380 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600381 ArenaSafeMap<ValueType, InstructionType*>* cache,
382 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000383 // Try to find an existing constant of the given value.
384 InstructionType* constant = nullptr;
385 auto cached_constant = cache->find(value);
386 if (cached_constant != cache->end()) {
387 constant = cached_constant->second;
388 }
389
390 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100391 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000392 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600393 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000394 cache->Overwrite(value, constant);
395 InsertConstant(constant);
396 }
397 return constant;
398 }
399
David Brazdil8d5b8b22015-03-24 10:51:52 +0000400 void InsertConstant(HConstant* instruction);
401
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000402 // Cache a float constant into the graph. This method should only be
403 // called by the SsaBuilder when creating "equivalent" instructions.
404 void CacheFloatConstant(HFloatConstant* constant);
405
406 // See CacheFloatConstant comment.
407 void CacheDoubleConstant(HDoubleConstant* constant);
408
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000409 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000410
411 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100412 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100414 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100415 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000416
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100417 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100418 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100419
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000420 HBasicBlock* entry_block_;
421 HBasicBlock* exit_block_;
422
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100423 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100424 uint16_t maximum_number_of_out_vregs_;
425
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100426 // The number of virtual registers in this method. Contains the parameters.
427 uint16_t number_of_vregs_;
428
429 // The number of virtual registers used by parameters of this method.
430 uint16_t number_of_in_vregs_;
431
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000432 // Number of vreg size slots that the temporaries use (used in baseline compiler).
433 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100434
Mark Mendell1152c922015-04-24 17:06:35 -0400435 // Has bounds checks. We can totally skip BCE if it's false.
436 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800437
David Brazdil77a48ae2015-09-15 12:34:04 +0000438 // Flag whether there are any try/catch blocks in the graph. We will skip
439 // try/catch-related passes if false.
440 bool has_try_catch_;
441
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000442 // Indicates whether the graph should be compiled in a way that
443 // ensures full debuggability. If false, we can apply more
444 // aggressive optimizations that may limit the level of debugging.
445 const bool debuggable_;
446
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000447 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000448 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000449
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100450 // The dex file from which the method is from.
451 const DexFile& dex_file_;
452
453 // The method index in the dex file.
454 const uint32_t method_idx_;
455
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100456 // If inlined, this encodes how the callee is being invoked.
457 const InvokeType invoke_type_;
458
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100459 // Whether the graph has been transformed to SSA form. Only used
460 // in debug mode to ensure we are not using properties only valid
461 // for non-SSA form (like the number of temporaries).
462 bool in_ssa_form_;
463
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100464 const bool should_generate_constructor_barrier_;
465
Mathieu Chartiere401d142015-04-22 13:56:20 -0700466 const InstructionSet instruction_set_;
467
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000468 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000469 HNullConstant* cached_null_constant_;
470 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000471 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000472 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000473 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000474
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100475 HCurrentMethod* cached_current_method_;
476
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000477 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100478 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000479 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000480 DISALLOW_COPY_AND_ASSIGN(HGraph);
481};
482
Vladimir Markof9f64412015-09-02 14:05:49 +0100483class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 public:
485 HLoopInformation(HBasicBlock* header, HGraph* graph)
486 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100487 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100488 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100489 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100490 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
491 back_edges_.reserve(kDefaultNumberOfBackEdges);
492 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493
494 HBasicBlock* GetHeader() const {
495 return header_;
496 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000497
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000498 void SetHeader(HBasicBlock* block) {
499 header_ = block;
500 }
501
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100502 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
503 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
504 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
505
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000506 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100507 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000508 }
509
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100510 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100511 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100512 }
513
David Brazdil46e2a392015-03-16 17:31:52 +0000514 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100515 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100516 }
517
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000518 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100519 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000520 }
521
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100523
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100524 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100525 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100526 }
527
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100528 // Returns the lifetime position of the back edge that has the
529 // greatest lifetime position.
530 size_t GetLifetimeEnd() const;
531
532 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100533 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100534 }
535
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100536 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100537 // that is the header dominates the back edge.
538 bool Populate();
539
David Brazdila4b8c212015-05-07 09:59:30 +0100540 // Reanalyzes the loop by removing loop info from its blocks and re-running
541 // Populate(). If there are no back edges left, the loop info is completely
542 // removed as well as its SuspendCheck instruction. It must be run on nested
543 // inner loops first.
544 void Update();
545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100546 // Returns whether this loop information contains `block`.
547 // Note that this loop information *must* be populated before entering this function.
548 bool Contains(const HBasicBlock& block) const;
549
550 // Returns whether this loop information is an inner loop of `other`.
551 // Note that `other` *must* be populated before entering this function.
552 bool IsIn(const HLoopInformation& other) const;
553
554 const ArenaBitVector& GetBlocks() const { return blocks_; }
555
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000556 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000557 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000558
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000559 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 // Internal recursive implementation of `Populate`.
561 void PopulateRecursive(HBasicBlock* block);
562
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100564 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100565 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100566 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567
568 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
569};
570
David Brazdilec16f792015-08-19 15:04:01 +0100571// Stores try/catch information for basic blocks.
572// Note that HGraph is constructed so that catch blocks cannot simultaneously
573// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100574class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100575 public:
576 // Try block information constructor.
577 explicit TryCatchInformation(const HTryBoundary& try_entry)
578 : try_entry_(&try_entry),
579 catch_dex_file_(nullptr),
580 catch_type_index_(DexFile::kDexNoIndex16) {
581 DCHECK(try_entry_ != nullptr);
582 }
583
584 // Catch block information constructor.
585 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
586 : try_entry_(nullptr),
587 catch_dex_file_(&dex_file),
588 catch_type_index_(catch_type_index) {}
589
590 bool IsTryBlock() const { return try_entry_ != nullptr; }
591
592 const HTryBoundary& GetTryEntry() const {
593 DCHECK(IsTryBlock());
594 return *try_entry_;
595 }
596
597 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
598
599 bool IsCatchAllTypeIndex() const {
600 DCHECK(IsCatchBlock());
601 return catch_type_index_ == DexFile::kDexNoIndex16;
602 }
603
604 uint16_t GetCatchTypeIndex() const {
605 DCHECK(IsCatchBlock());
606 return catch_type_index_;
607 }
608
609 const DexFile& GetCatchDexFile() const {
610 DCHECK(IsCatchBlock());
611 return *catch_dex_file_;
612 }
613
614 private:
615 // One of possibly several TryBoundary instructions entering the block's try.
616 // Only set for try blocks.
617 const HTryBoundary* try_entry_;
618
619 // Exception type information. Only set for catch blocks.
620 const DexFile* catch_dex_file_;
621 const uint16_t catch_type_index_;
622};
623
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100624static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100625static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100626
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000627// A block in a method. Contains the list of instructions represented
628// as a double linked list. Each block knows its predecessors and
629// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100630
Vladimir Markof9f64412015-09-02 14:05:49 +0100631class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600633 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000634 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000635 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
636 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000637 loop_information_(nullptr),
638 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000639 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100640 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100641 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100642 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000643 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000644 try_catch_information_(nullptr) {
645 predecessors_.reserve(kDefaultNumberOfPredecessors);
646 successors_.reserve(kDefaultNumberOfSuccessors);
647 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
648 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000649
Vladimir Marko60584552015-09-03 13:35:12 +0000650 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100651 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652 }
653
Vladimir Marko60584552015-09-03 13:35:12 +0000654 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100655 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656 }
657
Vladimir Marko60584552015-09-03 13:35:12 +0000658 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
659 return ContainsElement(successors_, block, start_from);
660 }
661
662 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100663 return dominated_blocks_;
664 }
665
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100666 bool IsEntryBlock() const {
667 return graph_->GetEntryBlock() == this;
668 }
669
670 bool IsExitBlock() const {
671 return graph_->GetExitBlock() == this;
672 }
673
David Brazdil46e2a392015-03-16 17:31:52 +0000674 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000675 bool IsSingleTryBoundary() const;
676
677 // Returns true if this block emits nothing but a jump.
678 bool IsSingleJump() const {
679 HLoopInformation* loop_info = GetLoopInformation();
680 return (IsSingleGoto() || IsSingleTryBoundary())
681 // Back edges generate a suspend check.
682 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
683 }
David Brazdil46e2a392015-03-16 17:31:52 +0000684
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000685 void AddBackEdge(HBasicBlock* back_edge) {
686 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000687 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000688 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100689 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000690 loop_information_->AddBackEdge(back_edge);
691 }
692
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000693 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000694 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000695
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100696 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000697 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600698 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000699
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000700 HBasicBlock* GetDominator() const { return dominator_; }
701 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000702 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
703
704 void RemoveDominatedBlock(HBasicBlock* block) {
705 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100706 }
Vladimir Marko60584552015-09-03 13:35:12 +0000707
708 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
709 ReplaceElement(dominated_blocks_, existing, new_block);
710 }
711
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100712 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000713
714 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100715 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000716 }
717
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100718 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
719 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100720 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100721 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100722 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
723 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000724
725 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000726 successors_.push_back(block);
727 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000728 }
729
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100730 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
731 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100732 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000733 new_block->predecessors_.push_back(this);
734 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000735 }
736
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000737 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
738 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000739 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000740 new_block->successors_.push_back(this);
741 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000742 }
743
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100744 // Insert `this` between `predecessor` and `successor. This method
745 // preserves the indicies, and will update the first edge found between
746 // `predecessor` and `successor`.
747 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
748 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100749 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000750 successor->predecessors_[predecessor_index] = this;
751 predecessor->successors_[successor_index] = this;
752 successors_.push_back(successor);
753 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100754 }
755
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100756 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000757 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100758 }
759
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000760 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000761 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000762 }
763
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000765 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100766 }
767
768 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000769 predecessors_.push_back(block);
770 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100771 }
772
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100773 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000774 DCHECK_EQ(predecessors_.size(), 2u);
775 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100776 }
777
David Brazdil769c9e52015-04-27 13:54:09 +0100778 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000779 DCHECK_EQ(successors_.size(), 2u);
780 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100781 }
782
David Brazdilfc6a86a2015-06-26 10:33:45 +0000783 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000784 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100785 }
786
David Brazdilfc6a86a2015-06-26 10:33:45 +0000787 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100789 }
790
David Brazdilfc6a86a2015-06-26 10:33:45 +0000791 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000792 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100793 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000794 }
795
796 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000797 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100798 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000799 }
800
801 // Returns whether the first occurrence of `predecessor` in the list of
802 // predecessors is at index `idx`.
803 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100804 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805 return GetPredecessorIndexOf(predecessor) == idx;
806 }
807
David Brazdilffee3d32015-07-06 11:48:53 +0100808 // Returns the number of non-exceptional successors. SsaChecker ensures that
809 // these are stored at the beginning of the successor list.
810 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000811 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100812 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813
David Brazdild7558da2015-09-22 13:04:14 +0100814 // Create a new block between this block and its predecessors. The new block
815 // is added to the graph, all predecessor edges are relinked to it and an edge
816 // is created to `this`. Returns the new empty block. Reverse post order or
817 // loop and try/catch information are not updated.
818 HBasicBlock* CreateImmediateDominator();
819
David Brazdilfc6a86a2015-06-26 10:33:45 +0000820 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100821 // created, latter block. Note that this method will add the block to the
822 // graph, create a Goto at the end of the former block and will create an edge
823 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100824 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000825 HBasicBlock* SplitBefore(HInstruction* cursor);
826
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000827 // Split the block into two blocks just after `cursor`. Returns the newly
828 // created block. Note that this method just updates raw block information,
829 // like predecessors, successors, dominators, and instruction list. It does not
830 // update the graph, reverse post order, loop information, nor make sure the
831 // blocks are consistent (for example ending with a control flow instruction).
832 HBasicBlock* SplitAfter(HInstruction* cursor);
833
834 // Merge `other` at the end of `this`. Successors and dominated blocks of
835 // `other` are changed to be successors and dominated blocks of `this`. Note
836 // that this method does not update the graph, reverse post order, loop
837 // information, nor make sure the blocks are consistent (for example ending
838 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100839 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000840
841 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
842 // of `this` are moved to `other`.
843 // Note that this method does not update the graph, reverse post order, loop
844 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000845 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846 void ReplaceWith(HBasicBlock* other);
847
David Brazdil2d7352b2015-04-20 14:52:42 +0100848 // Merge `other` at the end of `this`. This method updates loops, reverse post
849 // order, links to predecessors, successors, dominators and deletes the block
850 // from the graph. The two blocks must be successive, i.e. `this` the only
851 // predecessor of `other` and vice versa.
852 void MergeWith(HBasicBlock* other);
853
854 // Disconnects `this` from all its predecessors, successors and dominator,
855 // removes it from all loops it is included in and eventually from the graph.
856 // The block must not dominate any other block. Predecessors and successors
857 // are safely updated.
858 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000859
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000860 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100861 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100862 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100863 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100864 // Replace instruction `initial` with `replacement` within this block.
865 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
866 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100867 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100868 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000869 // RemoveInstruction and RemovePhi delete a given instruction from the respective
870 // instruction list. With 'ensure_safety' set to true, it verifies that the
871 // instruction is not in use and removes it from the use lists of its inputs.
872 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
873 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100874 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100875
876 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100877 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100878 }
879
Roland Levillain6b879dd2014-09-22 17:13:44 +0100880 bool IsLoopPreHeaderFirstPredecessor() const {
881 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100882 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100883 }
884
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100885 HLoopInformation* GetLoopInformation() const {
886 return loop_information_;
887 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000888
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000889 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100890 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000891 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100892 void SetInLoop(HLoopInformation* info) {
893 if (IsLoopHeader()) {
894 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100895 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100896 loop_information_ = info;
897 } else if (loop_information_->Contains(*info->GetHeader())) {
898 // Block is currently part of an outer loop. Make it part of this inner loop.
899 // Note that a non loop header having a loop information means this loop information
900 // has already been populated
901 loop_information_ = info;
902 } else {
903 // Block is part of an inner loop. Do not update the loop information.
904 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
905 // at this point, because this method is being called while populating `info`.
906 }
907 }
908
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000909 // Raw update of the loop information.
910 void SetLoopInformation(HLoopInformation* info) {
911 loop_information_ = info;
912 }
913
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100914 bool IsInLoop() const { return loop_information_ != nullptr; }
915
David Brazdilec16f792015-08-19 15:04:01 +0100916 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
917
918 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
919 try_catch_information_ = try_catch_information;
920 }
921
922 bool IsTryBlock() const {
923 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
924 }
925
926 bool IsCatchBlock() const {
927 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
928 }
David Brazdilffee3d32015-07-06 11:48:53 +0100929
930 // Returns the try entry that this block's successors should have. They will
931 // be in the same try, unless the block ends in a try boundary. In that case,
932 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100933 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100934
David Brazdild7558da2015-09-22 13:04:14 +0100935 bool HasThrowingInstructions() const;
936
David Brazdila4b8c212015-05-07 09:59:30 +0100937 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100938 bool Dominates(HBasicBlock* block) const;
939
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100940 size_t GetLifetimeStart() const { return lifetime_start_; }
941 size_t GetLifetimeEnd() const { return lifetime_end_; }
942
943 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
944 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
945
David Brazdil8d5b8b22015-03-24 10:51:52 +0000946 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000947 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100948 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000949 bool HasSinglePhi() const;
950
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000951 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000952 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000953 ArenaVector<HBasicBlock*> predecessors_;
954 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100955 HInstructionList instructions_;
956 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000957 HLoopInformation* loop_information_;
958 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000959 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100960 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100961 // The dex program counter of the first instruction of this block.
962 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100963 size_t lifetime_start_;
964 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100965 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100966
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000967 friend class HGraph;
968 friend class HInstruction;
969
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
971};
972
David Brazdilb2bd1c52015-03-25 11:17:37 +0000973// Iterates over the LoopInformation of all loops which contain 'block'
974// from the innermost to the outermost.
975class HLoopInformationOutwardIterator : public ValueObject {
976 public:
977 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
978 : current_(block.GetLoopInformation()) {}
979
980 bool Done() const { return current_ == nullptr; }
981
982 void Advance() {
983 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100984 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000985 }
986
987 HLoopInformation* Current() const {
988 DCHECK(!Done());
989 return current_;
990 }
991
992 private:
993 HLoopInformation* current_;
994
995 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
996};
997
Alexandre Ramesef20f712015-06-09 10:29:30 +0100998#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -0700999 M(Above, Condition) \
1000 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001001 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001002 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001003 M(ArrayGet, Instruction) \
1004 M(ArrayLength, Instruction) \
1005 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001006 M(Below, Condition) \
1007 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001008 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001009 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001010 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001011 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001012 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001013 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001014 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001015 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001016 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001017 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001018 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001019 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001020 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001021 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001022 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001023 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001024 M(FloatConstant, Constant) \
1025 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001026 M(GreaterThan, Condition) \
1027 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001028 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001029 M(InstanceFieldGet, Instruction) \
1030 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001031 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001033 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001034 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001035 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001036 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001037 M(LessThan, Condition) \
1038 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001039 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001040 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001041 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001042 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001043 M(Local, Instruction) \
1044 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001045 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001046 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001047 M(Mul, BinaryOperation) \
1048 M(Neg, UnaryOperation) \
1049 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001050 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001051 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001052 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001053 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001054 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001055 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001056 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001057 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001058 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001059 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001060 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001061 M(Return, Instruction) \
1062 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001063 M(Shl, BinaryOperation) \
1064 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001065 M(StaticFieldGet, Instruction) \
1066 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001067 M(UnresolvedInstanceFieldGet, Instruction) \
1068 M(UnresolvedInstanceFieldSet, Instruction) \
1069 M(UnresolvedStaticFieldGet, Instruction) \
1070 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001071 M(StoreLocal, Instruction) \
1072 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001073 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001074 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001075 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001076 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001077 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001078 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001079 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001080
Alexandre Ramesef20f712015-06-09 10:29:30 +01001081#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1082
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001083#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001084#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001085#else
1086#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1087 M(Arm64IntermediateAddress, Instruction)
1088#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001089
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001090#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1091
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001092#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1093
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001094#ifndef ART_ENABLE_CODEGEN_x86
1095#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1096#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001097#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1098 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001099 M(X86LoadFromConstantTable, Instruction) \
1100 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001101#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001102
1103#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1104
1105#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1106 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1107 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1108 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001109 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001110 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001111 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1112 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1113
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001114#define FOR_EACH_INSTRUCTION(M) \
1115 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1116 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001117 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001118 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001119 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001120
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001121#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001122FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1123#undef FORWARD_DECLARATION
1124
Roland Levillainccc07a92014-09-16 14:48:16 +01001125#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001126 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1127 const char* DebugName() const OVERRIDE { return #type; } \
1128 const H##type* As##type() const OVERRIDE { return this; } \
1129 H##type* As##type() OVERRIDE { return this; } \
1130 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001131 return other->Is##type(); \
1132 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001133 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001134
David Brazdiled596192015-01-23 10:39:45 +00001135template <typename T> class HUseList;
1136
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001137template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001138class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001139 public:
David Brazdiled596192015-01-23 10:39:45 +00001140 HUseListNode* GetPrevious() const { return prev_; }
1141 HUseListNode* GetNext() const { return next_; }
1142 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001143 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001144 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001145
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001146 private:
David Brazdiled596192015-01-23 10:39:45 +00001147 HUseListNode(T user, size_t index)
1148 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1149
1150 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001151 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001152 HUseListNode<T>* prev_;
1153 HUseListNode<T>* next_;
1154
1155 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001156
1157 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1158};
1159
David Brazdiled596192015-01-23 10:39:45 +00001160template <typename T>
1161class HUseList : public ValueObject {
1162 public:
1163 HUseList() : first_(nullptr) {}
1164
1165 void Clear() {
1166 first_ = nullptr;
1167 }
1168
1169 // Adds a new entry at the beginning of the use list and returns
1170 // the newly created node.
1171 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001172 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001173 if (IsEmpty()) {
1174 first_ = new_node;
1175 } else {
1176 first_->prev_ = new_node;
1177 new_node->next_ = first_;
1178 first_ = new_node;
1179 }
1180 return new_node;
1181 }
1182
1183 HUseListNode<T>* GetFirst() const {
1184 return first_;
1185 }
1186
1187 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001188 DCHECK(node != nullptr);
1189 DCHECK(Contains(node));
1190
David Brazdiled596192015-01-23 10:39:45 +00001191 if (node->prev_ != nullptr) {
1192 node->prev_->next_ = node->next_;
1193 }
1194 if (node->next_ != nullptr) {
1195 node->next_->prev_ = node->prev_;
1196 }
1197 if (node == first_) {
1198 first_ = node->next_;
1199 }
1200 }
1201
David Brazdil1abb4192015-02-17 18:33:36 +00001202 bool Contains(const HUseListNode<T>* node) const {
1203 if (node == nullptr) {
1204 return false;
1205 }
1206 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1207 if (current == node) {
1208 return true;
1209 }
1210 }
1211 return false;
1212 }
1213
David Brazdiled596192015-01-23 10:39:45 +00001214 bool IsEmpty() const {
1215 return first_ == nullptr;
1216 }
1217
1218 bool HasOnlyOneUse() const {
1219 return first_ != nullptr && first_->next_ == nullptr;
1220 }
1221
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001222 size_t SizeSlow() const {
1223 size_t count = 0;
1224 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1225 ++count;
1226 }
1227 return count;
1228 }
1229
David Brazdiled596192015-01-23 10:39:45 +00001230 private:
1231 HUseListNode<T>* first_;
1232};
1233
1234template<typename T>
1235class HUseIterator : public ValueObject {
1236 public:
1237 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1238
1239 bool Done() const { return current_ == nullptr; }
1240
1241 void Advance() {
1242 DCHECK(!Done());
1243 current_ = current_->GetNext();
1244 }
1245
1246 HUseListNode<T>* Current() const {
1247 DCHECK(!Done());
1248 return current_;
1249 }
1250
1251 private:
1252 HUseListNode<T>* current_;
1253
1254 friend class HValue;
1255};
1256
David Brazdil1abb4192015-02-17 18:33:36 +00001257// This class is used by HEnvironment and HInstruction classes to record the
1258// instructions they use and pointers to the corresponding HUseListNodes kept
1259// by the used instructions.
1260template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001261class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001262 public:
1263 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1264 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1265
1266 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1267 : instruction_(old_record.instruction_), use_node_(use_node) {
1268 DCHECK(instruction_ != nullptr);
1269 DCHECK(use_node_ != nullptr);
1270 DCHECK(old_record.use_node_ == nullptr);
1271 }
1272
1273 HInstruction* GetInstruction() const { return instruction_; }
1274 HUseListNode<T>* GetUseNode() const { return use_node_; }
1275
1276 private:
1277 // Instruction used by the user.
1278 HInstruction* instruction_;
1279
1280 // Corresponding entry in the use list kept by 'instruction_'.
1281 HUseListNode<T>* use_node_;
1282};
1283
Aart Bik854a02b2015-07-14 16:07:00 -07001284/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001285 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001286 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001287 * For write/read dependences on fields/arrays, the dependence analysis uses
1288 * type disambiguation (e.g. a float field write cannot modify the value of an
1289 * integer field read) and the access type (e.g. a reference array write cannot
1290 * modify the value of a reference field read [although it may modify the
1291 * reference fetch prior to reading the field, which is represented by its own
1292 * write/read dependence]). The analysis makes conservative points-to
1293 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1294 * the same, and any reference read depends on any reference read without
1295 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001296 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001297 * The internal representation uses 38-bit and is described in the table below.
1298 * The first line indicates the side effect, and for field/array accesses the
1299 * second line indicates the type of the access (in the order of the
1300 * Primitive::Type enum).
1301 * The two numbered lines below indicate the bit position in the bitfield (read
1302 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001303 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001304 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1305 * +-------------+---------+---------+--------------+---------+---------+
1306 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1307 * | 3 |333333322|222222221| 1 |111111110|000000000|
1308 * | 7 |654321098|765432109| 8 |765432109|876543210|
1309 *
1310 * Note that, to ease the implementation, 'changes' bits are least significant
1311 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001312 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001313class SideEffects : public ValueObject {
1314 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001315 SideEffects() : flags_(0) {}
1316
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001317 static SideEffects None() {
1318 return SideEffects(0);
1319 }
1320
1321 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001322 return SideEffects(kAllChangeBits | kAllDependOnBits);
1323 }
1324
1325 static SideEffects AllChanges() {
1326 return SideEffects(kAllChangeBits);
1327 }
1328
1329 static SideEffects AllDependencies() {
1330 return SideEffects(kAllDependOnBits);
1331 }
1332
1333 static SideEffects AllExceptGCDependency() {
1334 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1335 }
1336
1337 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001338 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001339 }
1340
Aart Bik34c3ba92015-07-20 14:08:59 -07001341 static SideEffects AllWrites() {
1342 return SideEffects(kAllWrites);
1343 }
1344
1345 static SideEffects AllReads() {
1346 return SideEffects(kAllReads);
1347 }
1348
1349 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1350 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001351 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001352 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001353 }
1354
Aart Bik854a02b2015-07-14 16:07:00 -07001355 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1356 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001357 }
1358
Aart Bik34c3ba92015-07-20 14:08:59 -07001359 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1360 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001361 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001362 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001363 }
1364
1365 static SideEffects ArrayReadOfType(Primitive::Type type) {
1366 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1367 }
1368
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001369 static SideEffects CanTriggerGC() {
1370 return SideEffects(1ULL << kCanTriggerGCBit);
1371 }
1372
1373 static SideEffects DependsOnGC() {
1374 return SideEffects(1ULL << kDependsOnGCBit);
1375 }
1376
Aart Bik854a02b2015-07-14 16:07:00 -07001377 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001378 SideEffects Union(SideEffects other) const {
1379 return SideEffects(flags_ | other.flags_);
1380 }
1381
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001382 SideEffects Exclusion(SideEffects other) const {
1383 return SideEffects(flags_ & ~other.flags_);
1384 }
1385
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001386 void Add(SideEffects other) {
1387 flags_ |= other.flags_;
1388 }
1389
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001390 bool Includes(SideEffects other) const {
1391 return (other.flags_ & flags_) == other.flags_;
1392 }
1393
1394 bool HasSideEffects() const {
1395 return (flags_ & kAllChangeBits);
1396 }
1397
1398 bool HasDependencies() const {
1399 return (flags_ & kAllDependOnBits);
1400 }
1401
1402 // Returns true if there are no side effects or dependencies.
1403 bool DoesNothing() const {
1404 return flags_ == 0;
1405 }
1406
Aart Bik854a02b2015-07-14 16:07:00 -07001407 // Returns true if something is written.
1408 bool DoesAnyWrite() const {
1409 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001410 }
1411
Aart Bik854a02b2015-07-14 16:07:00 -07001412 // Returns true if something is read.
1413 bool DoesAnyRead() const {
1414 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001415 }
1416
Aart Bik854a02b2015-07-14 16:07:00 -07001417 // Returns true if potentially everything is written and read
1418 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001419 bool DoesAllReadWrite() const {
1420 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1421 }
1422
Aart Bik854a02b2015-07-14 16:07:00 -07001423 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001424 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001425 }
1426
1427 // Returns true if this may read something written by other.
1428 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001429 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1430 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001431 }
1432
1433 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001434 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001435 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001436 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001437 for (int s = kLastBit; s >= 0; s--) {
1438 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1439 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1440 // This is a bit for the GC side effect.
1441 if (current_bit_is_set) {
1442 flags += "GC";
1443 }
Aart Bik854a02b2015-07-14 16:07:00 -07001444 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001445 } else {
1446 // This is a bit for the array/field analysis.
1447 // The underscore character stands for the 'can trigger GC' bit.
1448 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1449 if (current_bit_is_set) {
1450 flags += kDebug[s];
1451 }
1452 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1453 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1454 flags += "|";
1455 }
1456 }
Aart Bik854a02b2015-07-14 16:07:00 -07001457 }
1458 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001459 }
1460
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001461 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001462
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001463 private:
1464 static constexpr int kFieldArrayAnalysisBits = 9;
1465
1466 static constexpr int kFieldWriteOffset = 0;
1467 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1468 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1469 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1470
1471 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1472
1473 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1474 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1475 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1476 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1477
1478 static constexpr int kLastBit = kDependsOnGCBit;
1479 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1480
1481 // Aliases.
1482
1483 static_assert(kChangeBits == kDependOnBits,
1484 "the 'change' bits should match the 'depend on' bits.");
1485
1486 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1487 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1488 static constexpr uint64_t kAllWrites =
1489 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1490 static constexpr uint64_t kAllReads =
1491 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001492
Aart Bik854a02b2015-07-14 16:07:00 -07001493 // Work around the fact that HIR aliases I/F and J/D.
1494 // TODO: remove this interceptor once HIR types are clean
1495 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1496 switch (type) {
1497 case Primitive::kPrimInt:
1498 case Primitive::kPrimFloat:
1499 return TypeFlag(Primitive::kPrimInt, offset) |
1500 TypeFlag(Primitive::kPrimFloat, offset);
1501 case Primitive::kPrimLong:
1502 case Primitive::kPrimDouble:
1503 return TypeFlag(Primitive::kPrimLong, offset) |
1504 TypeFlag(Primitive::kPrimDouble, offset);
1505 default:
1506 return TypeFlag(type, offset);
1507 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001508 }
1509
Aart Bik854a02b2015-07-14 16:07:00 -07001510 // Translates type to bit flag.
1511 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1512 CHECK_NE(type, Primitive::kPrimVoid);
1513 const uint64_t one = 1;
1514 const int shift = type; // 0-based consecutive enum
1515 DCHECK_LE(kFieldWriteOffset, shift);
1516 DCHECK_LT(shift, kArrayWriteOffset);
1517 return one << (type + offset);
1518 }
1519
1520 // Private constructor on direct flags value.
1521 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1522
1523 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001524};
1525
David Brazdiled596192015-01-23 10:39:45 +00001526// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001527class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001528 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001529 HEnvironment(ArenaAllocator* arena,
1530 size_t number_of_vregs,
1531 const DexFile& dex_file,
1532 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001533 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001534 InvokeType invoke_type,
1535 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001536 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1537 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001538 parent_(nullptr),
1539 dex_file_(dex_file),
1540 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001541 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001542 invoke_type_(invoke_type),
1543 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001544 }
1545
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001546 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001547 : HEnvironment(arena,
1548 to_copy.Size(),
1549 to_copy.GetDexFile(),
1550 to_copy.GetMethodIdx(),
1551 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001552 to_copy.GetInvokeType(),
1553 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001554
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001555 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001556 if (parent_ != nullptr) {
1557 parent_->SetAndCopyParentChain(allocator, parent);
1558 } else {
1559 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1560 parent_->CopyFrom(parent);
1561 if (parent->GetParent() != nullptr) {
1562 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1563 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001564 }
David Brazdiled596192015-01-23 10:39:45 +00001565 }
1566
Vladimir Marko71bf8092015-09-15 15:33:14 +01001567 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001568 void CopyFrom(HEnvironment* environment);
1569
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001570 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1571 // input to the loop phi instead. This is for inserting instructions that
1572 // require an environment (like HDeoptimization) in the loop pre-header.
1573 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001574
1575 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001576 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001577 }
1578
1579 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001580 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001581 }
1582
David Brazdil1abb4192015-02-17 18:33:36 +00001583 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001584
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001585 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001586
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001587 HEnvironment* GetParent() const { return parent_; }
1588
1589 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001590 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001591 }
1592
1593 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001594 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001595 }
1596
1597 uint32_t GetDexPc() const {
1598 return dex_pc_;
1599 }
1600
1601 uint32_t GetMethodIdx() const {
1602 return method_idx_;
1603 }
1604
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001605 InvokeType GetInvokeType() const {
1606 return invoke_type_;
1607 }
1608
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001609 const DexFile& GetDexFile() const {
1610 return dex_file_;
1611 }
1612
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001613 HInstruction* GetHolder() const {
1614 return holder_;
1615 }
1616
David Brazdiled596192015-01-23 10:39:45 +00001617 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001618 // Record instructions' use entries of this environment for constant-time removal.
1619 // It should only be called by HInstruction when a new environment use is added.
1620 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1621 DCHECK(env_use->GetUser() == this);
1622 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001623 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001624 }
David Brazdiled596192015-01-23 10:39:45 +00001625
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001626 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1627 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001628 HEnvironment* parent_;
1629 const DexFile& dex_file_;
1630 const uint32_t method_idx_;
1631 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001632 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001633
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001634 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001635 HInstruction* const holder_;
1636
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001637 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001638
1639 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1640};
1641
Calin Juravleacf735c2015-02-12 15:25:22 +00001642class ReferenceTypeInfo : ValueObject {
1643 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001644 typedef Handle<mirror::Class> TypeHandle;
1645
Calin Juravle2e768302015-07-28 14:41:11 +00001646 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1647 // The constructor will check that the type_handle is valid.
1648 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001649 }
1650
Calin Juravle2e768302015-07-28 14:41:11 +00001651 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1652
1653 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1654 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001655 }
1656
Calin Juravle2e768302015-07-28 14:41:11 +00001657 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1658 return IsValidHandle(type_handle_);
1659 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001660
Calin Juravleacf735c2015-02-12 15:25:22 +00001661 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001662
1663 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1664 DCHECK(IsValid());
1665 return GetTypeHandle()->IsObjectClass();
1666 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001667
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001668 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001669 DCHECK(IsValid());
1670 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001671 }
1672
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001673 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1674 DCHECK(IsValid());
1675 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1676 }
1677
Mathieu Chartier90443472015-07-16 20:32:27 -07001678 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001679 DCHECK(IsValid());
1680 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001681 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001682
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001683 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001684 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001685 return GetTypeHandle()->IsArrayClass();
1686 }
1687
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001688 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1689 DCHECK(IsValid());
1690 return GetTypeHandle()->IsPrimitiveArray();
1691 }
1692
1693 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1694 DCHECK(IsValid());
1695 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1696 }
1697
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001698 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001699 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001700 if (!IsExact()) return false;
1701 if (!IsArrayClass()) return false;
1702 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1703 }
1704
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001705 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1706 DCHECK(IsValid());
1707 if (!IsExact()) return false;
1708 if (!IsArrayClass()) return false;
1709 if (!rti.IsArrayClass()) return false;
1710 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1711 rti.GetTypeHandle()->GetComponentType());
1712 }
1713
Calin Juravleacf735c2015-02-12 15:25:22 +00001714 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1715
Mathieu Chartier90443472015-07-16 20:32:27 -07001716 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001717 DCHECK(IsValid());
1718 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001719 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1720 }
1721
1722 // Returns true if the type information provide the same amount of details.
1723 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001724 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001725 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001726 if (!IsValid() && !rti.IsValid()) {
1727 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001728 return true;
1729 }
Calin Juravle2e768302015-07-28 14:41:11 +00001730 if (!IsValid() || !rti.IsValid()) {
1731 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001732 return false;
1733 }
Calin Juravle2e768302015-07-28 14:41:11 +00001734 return IsExact() == rti.IsExact()
1735 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001736 }
1737
1738 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001739 ReferenceTypeInfo();
1740 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001741
Calin Juravleacf735c2015-02-12 15:25:22 +00001742 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001743 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001744 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001745 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001746 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001747};
1748
1749std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1750
Vladimir Markof9f64412015-09-02 14:05:49 +01001751class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001752 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001753 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001754 : previous_(nullptr),
1755 next_(nullptr),
1756 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001757 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001758 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001759 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001760 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001761 locations_(nullptr),
1762 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001763 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001764 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001765 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001766
Dave Allison20dfc792014-06-16 20:44:29 -07001767 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001768
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001769#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001770 enum InstructionKind {
1771 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1772 };
1773#undef DECLARE_KIND
1774
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001775 HInstruction* GetNext() const { return next_; }
1776 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001777
Calin Juravle77520bc2015-01-12 18:45:46 +00001778 HInstruction* GetNextDisregardingMoves() const;
1779 HInstruction* GetPreviousDisregardingMoves() const;
1780
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001781 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001782 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001783 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001784 bool IsInBlock() const { return block_ != nullptr; }
1785 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001786 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001787
Roland Levillain6b879dd2014-09-22 17:13:44 +01001788 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001789 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001790
1791 virtual void Accept(HGraphVisitor* visitor) = 0;
1792 virtual const char* DebugName() const = 0;
1793
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001794 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001795 void SetRawInputAt(size_t index, HInstruction* input) {
1796 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1797 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001798
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001799 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001800
1801 uint32_t GetDexPc() const { return dex_pc_; }
1802
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001803 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001804
Roland Levillaine161a2a2014-10-03 12:45:18 +01001805 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001806 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001807
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001808 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001809 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001810
Calin Juravle10e244f2015-01-26 18:54:32 +00001811 // Does not apply for all instructions, but having this at top level greatly
1812 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001813 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001814 virtual bool CanBeNull() const {
1815 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1816 return true;
1817 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001818
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001819 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001820 return false;
1821 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001822
Calin Juravle2e768302015-07-28 14:41:11 +00001823 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001824
Calin Juravle61d544b2015-02-23 16:46:57 +00001825 ReferenceTypeInfo GetReferenceTypeInfo() const {
1826 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1827 return reference_type_info_;
1828 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001829
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001830 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001831 DCHECK(user != nullptr);
1832 HUseListNode<HInstruction*>* use =
1833 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1834 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001835 }
1836
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001837 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001838 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001839 HUseListNode<HEnvironment*>* env_use =
1840 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1841 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001842 }
1843
David Brazdil1abb4192015-02-17 18:33:36 +00001844 void RemoveAsUserOfInput(size_t input) {
1845 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1846 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1847 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001848
David Brazdil1abb4192015-02-17 18:33:36 +00001849 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1850 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001851
David Brazdiled596192015-01-23 10:39:45 +00001852 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1853 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001854 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001855 bool HasOnlyOneNonEnvironmentUse() const {
1856 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1857 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858
Roland Levillain6c82d402014-10-13 16:10:27 +01001859 // Does this instruction strictly dominate `other_instruction`?
1860 // Returns false if this instruction and `other_instruction` are the same.
1861 // Aborts if this instruction and `other_instruction` are both phis.
1862 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001863
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001864 int GetId() const { return id_; }
1865 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001866
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001867 int GetSsaIndex() const { return ssa_index_; }
1868 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1869 bool HasSsaIndex() const { return ssa_index_ != -1; }
1870
1871 bool HasEnvironment() const { return environment_ != nullptr; }
1872 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001873 // Set the `environment_` field. Raw because this method does not
1874 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001875 void SetRawEnvironment(HEnvironment* environment) {
1876 DCHECK(environment_ == nullptr);
1877 DCHECK_EQ(environment->GetHolder(), this);
1878 environment_ = environment;
1879 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001880
1881 // Set the environment of this instruction, copying it from `environment`. While
1882 // copying, the uses lists are being updated.
1883 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001884 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001885 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001886 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001887 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001888 if (environment->GetParent() != nullptr) {
1889 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1890 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001891 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001892
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001893 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1894 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001895 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001896 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001897 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001898 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001899 if (environment->GetParent() != nullptr) {
1900 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1901 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001902 }
1903
Nicolas Geoffray39468442014-09-02 15:17:15 +01001904 // Returns the number of entries in the environment. Typically, that is the
1905 // number of dex registers in a method. It could be more in case of inlining.
1906 size_t EnvironmentSize() const;
1907
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001908 LocationSummary* GetLocations() const { return locations_; }
1909 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001910
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001912 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913
Alexandre Rames188d4312015-04-09 18:30:21 +01001914 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1915 // uses of this instruction by `other` are *not* updated.
1916 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1917 ReplaceWith(other);
1918 other->ReplaceInput(this, use_index);
1919 }
1920
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001921 // Move `this` instruction before `cursor`.
1922 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001923
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001924#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001925 bool Is##type() const { return (As##type() != nullptr); } \
1926 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001927 virtual H##type* As##type() { return nullptr; }
1928
1929 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1930#undef INSTRUCTION_TYPE_CHECK
1931
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001932 // Returns whether the instruction can be moved within the graph.
1933 virtual bool CanBeMoved() const { return false; }
1934
1935 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001936 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001937 return false;
1938 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001939
1940 // Returns whether any data encoded in the two instructions is equal.
1941 // This method does not look at the inputs. Both instructions must be
1942 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001943 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001944 return false;
1945 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001946
1947 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001948 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001949 // 2) Their inputs are identical.
1950 bool Equals(HInstruction* other) const;
1951
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001952 virtual InstructionKind GetKind() const = 0;
1953
1954 virtual size_t ComputeHashCode() const {
1955 size_t result = GetKind();
1956 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1957 result = (result * 31) + InputAt(i)->GetId();
1958 }
1959 return result;
1960 }
1961
1962 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001963 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001964
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001965 size_t GetLifetimePosition() const { return lifetime_position_; }
1966 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1967 LiveInterval* GetLiveInterval() const { return live_interval_; }
1968 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1969 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1970
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001971 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1972
1973 // Returns whether the code generation of the instruction will require to have access
1974 // to the current method. Such instructions are:
1975 // (1): Instructions that require an environment, as calling the runtime requires
1976 // to walk the stack and have the current method stored at a specific stack address.
1977 // (2): Object literals like classes and strings, that are loaded from the dex cache
1978 // fields of the current method.
1979 bool NeedsCurrentMethod() const {
1980 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1981 }
1982
Vladimir Markodc151b22015-10-15 18:02:30 +01001983 // Returns whether the code generation of the instruction will require to have access
1984 // to the dex cache of the current method's declaring class via the current method.
1985 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001986
Mark Mendellc4701932015-04-10 13:18:51 -04001987 // Does this instruction have any use in an environment before
1988 // control flow hits 'other'?
1989 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1990
1991 // Remove all references to environment uses of this instruction.
1992 // The caller must ensure that this is safe to do.
1993 void RemoveEnvironmentUsers();
1994
David Brazdil1abb4192015-02-17 18:33:36 +00001995 protected:
1996 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1997 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1998
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001999 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002000 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2001
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002002 HInstruction* previous_;
2003 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002004 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002005 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002006
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002007 // An instruction gets an id when it is added to the graph.
2008 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002009 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002010 int id_;
2011
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002012 // When doing liveness analysis, instructions that have uses get an SSA index.
2013 int ssa_index_;
2014
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002015 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002016 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002017
2018 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002019 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002020
Nicolas Geoffray39468442014-09-02 15:17:15 +01002021 // The environment associated with this instruction. Not null if the instruction
2022 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002023 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002025 // Set by the code generator.
2026 LocationSummary* locations_;
2027
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002028 // Set by the liveness analysis.
2029 LiveInterval* live_interval_;
2030
2031 // Set by the liveness analysis, this is the position in a linear
2032 // order of blocks where this instruction's live interval start.
2033 size_t lifetime_position_;
2034
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002035 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002036
Calin Juravleacf735c2015-02-12 15:25:22 +00002037 // TODO: for primitive types this should be marked as invalid.
2038 ReferenceTypeInfo reference_type_info_;
2039
David Brazdil1abb4192015-02-17 18:33:36 +00002040 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002041 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002042 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002043 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002044 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002045
2046 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2047};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002048std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002049
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002050class HInputIterator : public ValueObject {
2051 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002052 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002053
2054 bool Done() const { return index_ == instruction_->InputCount(); }
2055 HInstruction* Current() const { return instruction_->InputAt(index_); }
2056 void Advance() { index_++; }
2057
2058 private:
2059 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002060 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002061
2062 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2063};
2064
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002065class HInstructionIterator : public ValueObject {
2066 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002067 explicit HInstructionIterator(const HInstructionList& instructions)
2068 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002069 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002070 }
2071
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002072 bool Done() const { return instruction_ == nullptr; }
2073 HInstruction* Current() const { return instruction_; }
2074 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002075 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002076 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002077 }
2078
2079 private:
2080 HInstruction* instruction_;
2081 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002082
2083 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002084};
2085
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002086class HBackwardInstructionIterator : public ValueObject {
2087 public:
2088 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2089 : instruction_(instructions.last_instruction_) {
2090 next_ = Done() ? nullptr : instruction_->GetPrevious();
2091 }
2092
2093 bool Done() const { return instruction_ == nullptr; }
2094 HInstruction* Current() const { return instruction_; }
2095 void Advance() {
2096 instruction_ = next_;
2097 next_ = Done() ? nullptr : instruction_->GetPrevious();
2098 }
2099
2100 private:
2101 HInstruction* instruction_;
2102 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002103
2104 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002105};
2106
Vladimir Markof9f64412015-09-02 14:05:49 +01002107template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002108class HTemplateInstruction: public HInstruction {
2109 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002110 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002111 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002112 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002114 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002115
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002116 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002117 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2118 DCHECK_LT(i, N);
2119 return inputs_[i];
2120 }
David Brazdil1abb4192015-02-17 18:33:36 +00002121
2122 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002123 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002124 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002125 }
2126
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002128 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002129
2130 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002131};
2132
Vladimir Markof9f64412015-09-02 14:05:49 +01002133// HTemplateInstruction specialization for N=0.
2134template<>
2135class HTemplateInstruction<0>: public HInstruction {
2136 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002137 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002138 : HInstruction(side_effects, dex_pc) {}
2139
Vladimir Markof9f64412015-09-02 14:05:49 +01002140 virtual ~HTemplateInstruction() {}
2141
2142 size_t InputCount() const OVERRIDE { return 0; }
2143
2144 protected:
2145 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2146 LOG(FATAL) << "Unreachable";
2147 UNREACHABLE();
2148 }
2149
2150 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2151 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2152 LOG(FATAL) << "Unreachable";
2153 UNREACHABLE();
2154 }
2155
2156 private:
2157 friend class SsaBuilder;
2158};
2159
Dave Allison20dfc792014-06-16 20:44:29 -07002160template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002161class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002162 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002163 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002164 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002165 virtual ~HExpression() {}
2166
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002167 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002168
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002169 protected:
2170 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002171};
2172
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002173// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2174// instruction that branches to the exit block.
2175class HReturnVoid : public HTemplateInstruction<0> {
2176 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002177 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2178 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002179
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002180 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002182 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002183
2184 private:
2185 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2186};
2187
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002188// Represents dex's RETURN opcodes. A HReturn is a control flow
2189// instruction that branches to the exit block.
2190class HReturn : public HTemplateInstruction<1> {
2191 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002192 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2193 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002194 SetRawInputAt(0, value);
2195 }
2196
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002197 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002198
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002199 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002200
2201 private:
2202 DISALLOW_COPY_AND_ASSIGN(HReturn);
2203};
2204
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002205// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002206// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002207// exit block.
2208class HExit : public HTemplateInstruction<0> {
2209 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002210 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002211
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002212 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002214 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002215
2216 private:
2217 DISALLOW_COPY_AND_ASSIGN(HExit);
2218};
2219
2220// Jumps from one block to another.
2221class HGoto : public HTemplateInstruction<0> {
2222 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002223 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002224
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002225 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002227 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002228 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002229 }
2230
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002231 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232
2233 private:
2234 DISALLOW_COPY_AND_ASSIGN(HGoto);
2235};
2236
Roland Levillain9867bc72015-08-05 10:21:34 +01002237class HConstant : public HExpression<0> {
2238 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002239 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2240 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002241
2242 bool CanBeMoved() const OVERRIDE { return true; }
2243
2244 virtual bool IsMinusOne() const { return false; }
2245 virtual bool IsZero() const { return false; }
2246 virtual bool IsOne() const { return false; }
2247
David Brazdil77a48ae2015-09-15 12:34:04 +00002248 virtual uint64_t GetValueAsUint64() const = 0;
2249
Roland Levillain9867bc72015-08-05 10:21:34 +01002250 DECLARE_INSTRUCTION(Constant);
2251
2252 private:
2253 DISALLOW_COPY_AND_ASSIGN(HConstant);
2254};
2255
2256class HNullConstant : public HConstant {
2257 public:
2258 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2259 return true;
2260 }
2261
David Brazdil77a48ae2015-09-15 12:34:04 +00002262 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2263
Roland Levillain9867bc72015-08-05 10:21:34 +01002264 size_t ComputeHashCode() const OVERRIDE { return 0; }
2265
2266 DECLARE_INSTRUCTION(NullConstant);
2267
2268 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002269 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002270
2271 friend class HGraph;
2272 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2273};
2274
2275// Constants of the type int. Those can be from Dex instructions, or
2276// synthesized (for example with the if-eqz instruction).
2277class HIntConstant : public HConstant {
2278 public:
2279 int32_t GetValue() const { return value_; }
2280
David Brazdil9f389d42015-10-01 14:32:56 +01002281 uint64_t GetValueAsUint64() const OVERRIDE {
2282 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2283 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002284
Roland Levillain9867bc72015-08-05 10:21:34 +01002285 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2286 DCHECK(other->IsIntConstant());
2287 return other->AsIntConstant()->value_ == value_;
2288 }
2289
2290 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2291
2292 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2293 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2294 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2295
2296 DECLARE_INSTRUCTION(IntConstant);
2297
2298 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002299 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2300 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2301 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2302 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002303
2304 const int32_t value_;
2305
2306 friend class HGraph;
2307 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2308 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2309 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2310};
2311
2312class HLongConstant : public HConstant {
2313 public:
2314 int64_t GetValue() const { return value_; }
2315
David Brazdil77a48ae2015-09-15 12:34:04 +00002316 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2317
Roland Levillain9867bc72015-08-05 10:21:34 +01002318 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2319 DCHECK(other->IsLongConstant());
2320 return other->AsLongConstant()->value_ == value_;
2321 }
2322
2323 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2324
2325 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2326 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2327 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2328
2329 DECLARE_INSTRUCTION(LongConstant);
2330
2331 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002332 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2333 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002334
2335 const int64_t value_;
2336
2337 friend class HGraph;
2338 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2339};
Dave Allison20dfc792014-06-16 20:44:29 -07002340
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002341// Conditional branch. A block ending with an HIf instruction must have
2342// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002343class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002344 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002345 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2346 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002347 SetRawInputAt(0, input);
2348 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002349
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002350 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002351
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002352 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002353 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002354 }
2355
2356 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002357 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002358 }
2359
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002360 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002361
2362 private:
2363 DISALLOW_COPY_AND_ASSIGN(HIf);
2364};
2365
David Brazdilfc6a86a2015-06-26 10:33:45 +00002366
2367// Abstract instruction which marks the beginning and/or end of a try block and
2368// links it to the respective exception handlers. Behaves the same as a Goto in
2369// non-exceptional control flow.
2370// Normal-flow successor is stored at index zero, exception handlers under
2371// higher indices in no particular order.
2372class HTryBoundary : public HTemplateInstruction<0> {
2373 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002374 enum BoundaryKind {
2375 kEntry,
2376 kExit,
2377 };
2378
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002379 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2380 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002381
2382 bool IsControlFlow() const OVERRIDE { return true; }
2383
2384 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002385 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002386
2387 // Returns whether `handler` is among its exception handlers (non-zero index
2388 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002389 bool HasExceptionHandler(const HBasicBlock& handler) const {
2390 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002391 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002392 }
2393
2394 // If not present already, adds `handler` to its block's list of exception
2395 // handlers.
2396 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002397 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002398 GetBlock()->AddSuccessor(handler);
2399 }
2400 }
2401
David Brazdil56e1acc2015-06-30 15:41:36 +01002402 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002403
David Brazdilffee3d32015-07-06 11:48:53 +01002404 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2405
David Brazdilfc6a86a2015-06-26 10:33:45 +00002406 DECLARE_INSTRUCTION(TryBoundary);
2407
2408 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002409 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002410
2411 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2412};
2413
David Brazdilffee3d32015-07-06 11:48:53 +01002414// Iterator over exception handlers of a given HTryBoundary, i.e. over
2415// exceptional successors of its basic block.
2416class HExceptionHandlerIterator : public ValueObject {
2417 public:
2418 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2419 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2420
Vladimir Marko60584552015-09-03 13:35:12 +00002421 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002422 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002423 size_t CurrentSuccessorIndex() const { return index_; }
2424 void Advance() { ++index_; }
2425
2426 private:
2427 const HBasicBlock& block_;
2428 size_t index_;
2429
2430 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2431};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002432
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002433// Deoptimize to interpreter, upon checking a condition.
2434class HDeoptimize : public HTemplateInstruction<1> {
2435 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002436 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2437 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002438 SetRawInputAt(0, cond);
2439 }
2440
2441 bool NeedsEnvironment() const OVERRIDE { return true; }
2442 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002443
2444 DECLARE_INSTRUCTION(Deoptimize);
2445
2446 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002447 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2448};
2449
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002450// Represents the ArtMethod that was passed as a first argument to
2451// the method. It is used by instructions that depend on it, like
2452// instructions that work with the dex cache.
2453class HCurrentMethod : public HExpression<0> {
2454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002455 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2456 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002457
2458 DECLARE_INSTRUCTION(CurrentMethod);
2459
2460 private:
2461 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2462};
2463
Mark Mendellfe57faa2015-09-18 09:26:15 -04002464// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2465// have one successor for each entry in the switch table, and the final successor
2466// will be the block containing the next Dex opcode.
2467class HPackedSwitch : public HTemplateInstruction<1> {
2468 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002469 HPackedSwitch(int32_t start_value,
2470 uint32_t num_entries,
2471 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002472 uint32_t dex_pc = kNoDexPc)
2473 : HTemplateInstruction(SideEffects::None(), dex_pc),
2474 start_value_(start_value),
2475 num_entries_(num_entries) {
2476 SetRawInputAt(0, input);
2477 }
2478
2479 bool IsControlFlow() const OVERRIDE { return true; }
2480
2481 int32_t GetStartValue() const { return start_value_; }
2482
Vladimir Marko211c2112015-09-24 16:52:33 +01002483 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002484
2485 HBasicBlock* GetDefaultBlock() const {
2486 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002487 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002488 }
2489 DECLARE_INSTRUCTION(PackedSwitch);
2490
2491 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002492 const int32_t start_value_;
2493 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002494
2495 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2496};
2497
Roland Levillain88cb1752014-10-20 16:36:47 +01002498class HUnaryOperation : public HExpression<1> {
2499 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002500 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2501 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002502 SetRawInputAt(0, input);
2503 }
2504
2505 HInstruction* GetInput() const { return InputAt(0); }
2506 Primitive::Type GetResultType() const { return GetType(); }
2507
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002508 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002509 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002510 return true;
2511 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002512
Roland Levillain9240d6a2014-10-20 16:47:04 +01002513 // Try to statically evaluate `operation` and return a HConstant
2514 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002515 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002516 HConstant* TryStaticEvaluation() const;
2517
2518 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002519 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2520 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002521
Roland Levillain88cb1752014-10-20 16:36:47 +01002522 DECLARE_INSTRUCTION(UnaryOperation);
2523
2524 private:
2525 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2526};
2527
Dave Allison20dfc792014-06-16 20:44:29 -07002528class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002529 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002530 HBinaryOperation(Primitive::Type result_type,
2531 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002532 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002533 SideEffects side_effects = SideEffects::None(),
2534 uint32_t dex_pc = kNoDexPc)
2535 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002536 SetRawInputAt(0, left);
2537 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002538 }
2539
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002540 HInstruction* GetLeft() const { return InputAt(0); }
2541 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002542 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002543
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002544 virtual bool IsCommutative() const { return false; }
2545
2546 // Put constant on the right.
2547 // Returns whether order is changed.
2548 bool OrderInputsWithConstantOnTheRight() {
2549 HInstruction* left = InputAt(0);
2550 HInstruction* right = InputAt(1);
2551 if (left->IsConstant() && !right->IsConstant()) {
2552 ReplaceInput(right, 0);
2553 ReplaceInput(left, 1);
2554 return true;
2555 }
2556 return false;
2557 }
2558
2559 // Order inputs by instruction id, but favor constant on the right side.
2560 // This helps GVN for commutative ops.
2561 void OrderInputs() {
2562 DCHECK(IsCommutative());
2563 HInstruction* left = InputAt(0);
2564 HInstruction* right = InputAt(1);
2565 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2566 return;
2567 }
2568 if (OrderInputsWithConstantOnTheRight()) {
2569 return;
2570 }
2571 // Order according to instruction id.
2572 if (left->GetId() > right->GetId()) {
2573 ReplaceInput(right, 0);
2574 ReplaceInput(left, 1);
2575 }
2576 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002577
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002578 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002579 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002580 return true;
2581 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002582
Roland Levillain9240d6a2014-10-20 16:47:04 +01002583 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002584 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002585 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002586 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002587
2588 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002589 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2590 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2591 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2592 HLongConstant* y ATTRIBUTE_UNUSED) const {
2593 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2594 return nullptr;
2595 }
2596 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2597 HIntConstant* y ATTRIBUTE_UNUSED) const {
2598 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2599 return nullptr;
2600 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002601
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002602 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002603 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002604 HConstant* GetConstantRight() const;
2605
2606 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002607 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002608 HInstruction* GetLeastConstantLeft() const;
2609
Roland Levillainccc07a92014-09-16 14:48:16 +01002610 DECLARE_INSTRUCTION(BinaryOperation);
2611
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002612 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002613 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2614};
2615
Mark Mendellc4701932015-04-10 13:18:51 -04002616// The comparison bias applies for floating point operations and indicates how NaN
2617// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002618enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002619 kNoBias, // bias is not applicable (i.e. for long operation)
2620 kGtBias, // return 1 for NaN comparisons
2621 kLtBias, // return -1 for NaN comparisons
2622};
2623
Dave Allison20dfc792014-06-16 20:44:29 -07002624class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002625 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002626 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2627 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002628 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002629 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002630
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002631 bool NeedsMaterialization() const { return needs_materialization_; }
2632 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002633
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002634 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002635 // `instruction`, and disregard moves in between.
2636 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002637
Dave Allison20dfc792014-06-16 20:44:29 -07002638 DECLARE_INSTRUCTION(Condition);
2639
2640 virtual IfCondition GetCondition() const = 0;
2641
Mark Mendellc4701932015-04-10 13:18:51 -04002642 virtual IfCondition GetOppositeCondition() const = 0;
2643
Roland Levillain4fa13f62015-07-06 18:11:54 +01002644 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002645
2646 void SetBias(ComparisonBias bias) { bias_ = bias; }
2647
2648 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2649 return bias_ == other->AsCondition()->bias_;
2650 }
2651
Roland Levillain4fa13f62015-07-06 18:11:54 +01002652 bool IsFPConditionTrueIfNaN() const {
2653 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2654 IfCondition if_cond = GetCondition();
2655 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2656 }
2657
2658 bool IsFPConditionFalseIfNaN() const {
2659 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2660 IfCondition if_cond = GetCondition();
2661 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2662 }
2663
Dave Allison20dfc792014-06-16 20:44:29 -07002664 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002665 // For register allocation purposes, returns whether this instruction needs to be
2666 // materialized (that is, not just be in the processor flags).
2667 bool needs_materialization_;
2668
Mark Mendellc4701932015-04-10 13:18:51 -04002669 // Needed if we merge a HCompare into a HCondition.
2670 ComparisonBias bias_;
2671
Dave Allison20dfc792014-06-16 20:44:29 -07002672 DISALLOW_COPY_AND_ASSIGN(HCondition);
2673};
2674
2675// Instruction to check if two inputs are equal to each other.
2676class HEqual : public HCondition {
2677 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002678 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2679 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002680
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002681 bool IsCommutative() const OVERRIDE { return true; }
2682
Roland Levillain9867bc72015-08-05 10:21:34 +01002683 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002684 return GetBlock()->GetGraph()->GetIntConstant(
2685 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002686 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002687 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002688 return GetBlock()->GetGraph()->GetIntConstant(
2689 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002690 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002691
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002692 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002693
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002694 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002695 return kCondEQ;
2696 }
2697
Mark Mendellc4701932015-04-10 13:18:51 -04002698 IfCondition GetOppositeCondition() const OVERRIDE {
2699 return kCondNE;
2700 }
2701
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002702 private:
Aart Bike9f37602015-10-09 11:15:55 -07002703 template <typename T> bool Compute(T x, T y) const { return x == y; }
2704
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002705 DISALLOW_COPY_AND_ASSIGN(HEqual);
2706};
2707
Dave Allison20dfc792014-06-16 20:44:29 -07002708class HNotEqual : public HCondition {
2709 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002710 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2711 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002712
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002713 bool IsCommutative() const OVERRIDE { return true; }
2714
Roland Levillain9867bc72015-08-05 10:21:34 +01002715 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002716 return GetBlock()->GetGraph()->GetIntConstant(
2717 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002718 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002720 return GetBlock()->GetGraph()->GetIntConstant(
2721 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002722 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002723
Dave Allison20dfc792014-06-16 20:44:29 -07002724 DECLARE_INSTRUCTION(NotEqual);
2725
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002726 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002727 return kCondNE;
2728 }
2729
Mark Mendellc4701932015-04-10 13:18:51 -04002730 IfCondition GetOppositeCondition() const OVERRIDE {
2731 return kCondEQ;
2732 }
2733
Dave Allison20dfc792014-06-16 20:44:29 -07002734 private:
Aart Bike9f37602015-10-09 11:15:55 -07002735 template <typename T> bool Compute(T x, T y) const { return x != y; }
2736
Dave Allison20dfc792014-06-16 20:44:29 -07002737 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2738};
2739
2740class HLessThan : public HCondition {
2741 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002742 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2743 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002744
Roland Levillain9867bc72015-08-05 10:21:34 +01002745 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002746 return GetBlock()->GetGraph()->GetIntConstant(
2747 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002748 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002749 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002750 return GetBlock()->GetGraph()->GetIntConstant(
2751 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002752 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002753
Dave Allison20dfc792014-06-16 20:44:29 -07002754 DECLARE_INSTRUCTION(LessThan);
2755
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002756 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002757 return kCondLT;
2758 }
2759
Mark Mendellc4701932015-04-10 13:18:51 -04002760 IfCondition GetOppositeCondition() const OVERRIDE {
2761 return kCondGE;
2762 }
2763
Dave Allison20dfc792014-06-16 20:44:29 -07002764 private:
Aart Bike9f37602015-10-09 11:15:55 -07002765 template <typename T> bool Compute(T x, T y) const { return x < y; }
2766
Dave Allison20dfc792014-06-16 20:44:29 -07002767 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2768};
2769
2770class HLessThanOrEqual : public HCondition {
2771 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002772 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2773 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002774
Roland Levillain9867bc72015-08-05 10:21:34 +01002775 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002776 return GetBlock()->GetGraph()->GetIntConstant(
2777 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002778 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002779 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002780 return GetBlock()->GetGraph()->GetIntConstant(
2781 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002782 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002783
Dave Allison20dfc792014-06-16 20:44:29 -07002784 DECLARE_INSTRUCTION(LessThanOrEqual);
2785
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002786 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002787 return kCondLE;
2788 }
2789
Mark Mendellc4701932015-04-10 13:18:51 -04002790 IfCondition GetOppositeCondition() const OVERRIDE {
2791 return kCondGT;
2792 }
2793
Dave Allison20dfc792014-06-16 20:44:29 -07002794 private:
Aart Bike9f37602015-10-09 11:15:55 -07002795 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2796
Dave Allison20dfc792014-06-16 20:44:29 -07002797 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2798};
2799
2800class HGreaterThan : public HCondition {
2801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002802 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2803 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002804
Roland Levillain9867bc72015-08-05 10:21:34 +01002805 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002806 return GetBlock()->GetGraph()->GetIntConstant(
2807 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002808 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002809 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002810 return GetBlock()->GetGraph()->GetIntConstant(
2811 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002812 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002813
Dave Allison20dfc792014-06-16 20:44:29 -07002814 DECLARE_INSTRUCTION(GreaterThan);
2815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002816 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002817 return kCondGT;
2818 }
2819
Mark Mendellc4701932015-04-10 13:18:51 -04002820 IfCondition GetOppositeCondition() const OVERRIDE {
2821 return kCondLE;
2822 }
2823
Dave Allison20dfc792014-06-16 20:44:29 -07002824 private:
Aart Bike9f37602015-10-09 11:15:55 -07002825 template <typename T> bool Compute(T x, T y) const { return x > y; }
2826
Dave Allison20dfc792014-06-16 20:44:29 -07002827 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2828};
2829
2830class HGreaterThanOrEqual : public HCondition {
2831 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002832 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2833 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002834
Roland Levillain9867bc72015-08-05 10:21:34 +01002835 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002836 return GetBlock()->GetGraph()->GetIntConstant(
2837 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002838 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002839 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002840 return GetBlock()->GetGraph()->GetIntConstant(
2841 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002842 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002843
Dave Allison20dfc792014-06-16 20:44:29 -07002844 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2845
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002846 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002847 return kCondGE;
2848 }
2849
Mark Mendellc4701932015-04-10 13:18:51 -04002850 IfCondition GetOppositeCondition() const OVERRIDE {
2851 return kCondLT;
2852 }
2853
Dave Allison20dfc792014-06-16 20:44:29 -07002854 private:
Aart Bike9f37602015-10-09 11:15:55 -07002855 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2856
Dave Allison20dfc792014-06-16 20:44:29 -07002857 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2858};
2859
Aart Bike9f37602015-10-09 11:15:55 -07002860class HBelow : public HCondition {
2861 public:
2862 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2863 : HCondition(first, second, dex_pc) {}
2864
2865 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2866 return GetBlock()->GetGraph()->GetIntConstant(
2867 Compute(static_cast<uint32_t>(x->GetValue()),
2868 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2869 }
2870 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2871 return GetBlock()->GetGraph()->GetIntConstant(
2872 Compute(static_cast<uint64_t>(x->GetValue()),
2873 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2874 }
2875
2876 DECLARE_INSTRUCTION(Below);
2877
2878 IfCondition GetCondition() const OVERRIDE {
2879 return kCondB;
2880 }
2881
2882 IfCondition GetOppositeCondition() const OVERRIDE {
2883 return kCondAE;
2884 }
2885
2886 private:
2887 template <typename T> bool Compute(T x, T y) const { return x < y; }
2888
2889 DISALLOW_COPY_AND_ASSIGN(HBelow);
2890};
2891
2892class HBelowOrEqual : public HCondition {
2893 public:
2894 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2895 : HCondition(first, second, dex_pc) {}
2896
2897 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2898 return GetBlock()->GetGraph()->GetIntConstant(
2899 Compute(static_cast<uint32_t>(x->GetValue()),
2900 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2901 }
2902 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2903 return GetBlock()->GetGraph()->GetIntConstant(
2904 Compute(static_cast<uint64_t>(x->GetValue()),
2905 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2906 }
2907
2908 DECLARE_INSTRUCTION(BelowOrEqual);
2909
2910 IfCondition GetCondition() const OVERRIDE {
2911 return kCondBE;
2912 }
2913
2914 IfCondition GetOppositeCondition() const OVERRIDE {
2915 return kCondA;
2916 }
2917
2918 private:
2919 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2920
2921 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2922};
2923
2924class HAbove : public HCondition {
2925 public:
2926 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2927 : HCondition(first, second, dex_pc) {}
2928
2929 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2930 return GetBlock()->GetGraph()->GetIntConstant(
2931 Compute(static_cast<uint32_t>(x->GetValue()),
2932 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2933 }
2934 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2935 return GetBlock()->GetGraph()->GetIntConstant(
2936 Compute(static_cast<uint64_t>(x->GetValue()),
2937 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2938 }
2939
2940 DECLARE_INSTRUCTION(Above);
2941
2942 IfCondition GetCondition() const OVERRIDE {
2943 return kCondA;
2944 }
2945
2946 IfCondition GetOppositeCondition() const OVERRIDE {
2947 return kCondBE;
2948 }
2949
2950 private:
2951 template <typename T> bool Compute(T x, T y) const { return x > y; }
2952
2953 DISALLOW_COPY_AND_ASSIGN(HAbove);
2954};
2955
2956class HAboveOrEqual : public HCondition {
2957 public:
2958 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2959 : HCondition(first, second, dex_pc) {}
2960
2961 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2962 return GetBlock()->GetGraph()->GetIntConstant(
2963 Compute(static_cast<uint32_t>(x->GetValue()),
2964 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2965 }
2966 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2967 return GetBlock()->GetGraph()->GetIntConstant(
2968 Compute(static_cast<uint64_t>(x->GetValue()),
2969 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2970 }
2971
2972 DECLARE_INSTRUCTION(AboveOrEqual);
2973
2974 IfCondition GetCondition() const OVERRIDE {
2975 return kCondAE;
2976 }
2977
2978 IfCondition GetOppositeCondition() const OVERRIDE {
2979 return kCondB;
2980 }
2981
2982 private:
2983 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2984
2985 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
2986};
Dave Allison20dfc792014-06-16 20:44:29 -07002987
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002988// Instruction to check how two inputs compare to each other.
2989// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2990class HCompare : public HBinaryOperation {
2991 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002992 HCompare(Primitive::Type type,
2993 HInstruction* first,
2994 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002995 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002996 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002997 : HBinaryOperation(Primitive::kPrimInt,
2998 first,
2999 second,
3000 SideEffectsForArchRuntimeCalls(type),
3001 dex_pc),
3002 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003003 DCHECK_EQ(type, first->GetType());
3004 DCHECK_EQ(type, second->GetType());
3005 }
3006
Roland Levillain9867bc72015-08-05 10:21:34 +01003007 template <typename T>
3008 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003009
Roland Levillain9867bc72015-08-05 10:21:34 +01003010 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003011 return GetBlock()->GetGraph()->GetIntConstant(
3012 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003013 }
3014 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003015 return GetBlock()->GetGraph()->GetIntConstant(
3016 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003017 }
3018
Calin Juravleddb7df22014-11-25 20:56:51 +00003019 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3020 return bias_ == other->AsCompare()->bias_;
3021 }
3022
Mark Mendellc4701932015-04-10 13:18:51 -04003023 ComparisonBias GetBias() const { return bias_; }
3024
Roland Levillain4fa13f62015-07-06 18:11:54 +01003025 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003026
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003027
3028 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3029 // MIPS64 uses a runtime call for FP comparisons.
3030 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3031 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003032
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003033 DECLARE_INSTRUCTION(Compare);
3034
3035 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003036 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003037
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003038 DISALLOW_COPY_AND_ASSIGN(HCompare);
3039};
3040
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003041// A local in the graph. Corresponds to a Dex register.
3042class HLocal : public HTemplateInstruction<0> {
3043 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003044 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003045 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003046
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003047 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003048
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003049 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003050
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003051 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003052 // The Dex register number.
3053 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003054
3055 DISALLOW_COPY_AND_ASSIGN(HLocal);
3056};
3057
3058// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003059class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003060 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003061 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3062 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003063 SetRawInputAt(0, local);
3064 }
3065
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003066 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3067
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003068 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003069
3070 private:
3071 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3072};
3073
3074// Store a value in a given local. This instruction has two inputs: the value
3075// and the local.
3076class HStoreLocal : public HTemplateInstruction<2> {
3077 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003078 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3079 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003080 SetRawInputAt(0, local);
3081 SetRawInputAt(1, value);
3082 }
3083
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003084 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3085
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003086 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003087
3088 private:
3089 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3090};
3091
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003092class HFloatConstant : public HConstant {
3093 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003094 float GetValue() const { return value_; }
3095
David Brazdil77a48ae2015-09-15 12:34:04 +00003096 uint64_t GetValueAsUint64() const OVERRIDE {
3097 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3098 }
3099
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003100 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003101 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003102 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003103 }
3104
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003105 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003106
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003107 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003108 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003109 }
3110 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003111 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003112 }
3113 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003114 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3115 }
3116 bool IsNaN() const {
3117 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003118 }
3119
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003120 DECLARE_INSTRUCTION(FloatConstant);
3121
3122 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003123 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3124 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3125 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3126 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003127
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003128 const float value_;
3129
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003130 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003131 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003132 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003133 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3134};
3135
3136class HDoubleConstant : public HConstant {
3137 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003138 double GetValue() const { return value_; }
3139
David Brazdil77a48ae2015-09-15 12:34:04 +00003140 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3141
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003142 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003143 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003144 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003145 }
3146
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003147 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003148
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003149 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003150 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003151 }
3152 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003153 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003154 }
3155 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003156 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3157 }
3158 bool IsNaN() const {
3159 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003160 }
3161
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003162 DECLARE_INSTRUCTION(DoubleConstant);
3163
3164 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003165 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3166 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3167 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3168 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003169
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003170 const double value_;
3171
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003172 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003173 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003174 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003175 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3176};
3177
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003178enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003179#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003180#include "intrinsics_list.h"
3181 kNone,
3182 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3183#undef INTRINSICS_LIST
3184#undef OPTIMIZING_INTRINSICS
3185};
3186std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3187
Agi Csaki05f20562015-08-19 14:58:14 -07003188enum IntrinsicNeedsEnvironmentOrCache {
3189 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3190 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003191};
3192
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003193class HInvoke : public HInstruction {
3194 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003195 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003196
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003197 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003198
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003199 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003200 SetRawInputAt(index, argument);
3201 }
3202
Roland Levillain3e3d7332015-04-28 11:00:54 +01003203 // Return the number of arguments. This number can be lower than
3204 // the number of inputs returned by InputCount(), as some invoke
3205 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3206 // inputs at the end of their list of inputs.
3207 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3208
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003209 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003210
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003211
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003212 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003213 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003214
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003215 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3216
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003217 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003218 return intrinsic_;
3219 }
3220
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003221 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003222
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003223 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003224 return GetEnvironment()->GetParent() != nullptr;
3225 }
3226
3227 bool CanThrow() const OVERRIDE { return true; }
3228
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003229 uint32_t* GetIntrinsicOptimizations() {
3230 return &intrinsic_optimizations_;
3231 }
3232
3233 const uint32_t* GetIntrinsicOptimizations() const {
3234 return &intrinsic_optimizations_;
3235 }
3236
3237 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3238
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003239 DECLARE_INSTRUCTION(Invoke);
3240
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003241 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003242 HInvoke(ArenaAllocator* arena,
3243 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003244 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003245 Primitive::Type return_type,
3246 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003247 uint32_t dex_method_index,
3248 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003249 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003250 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003251 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003252 inputs_(number_of_arguments + number_of_other_inputs,
3253 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003254 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003255 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003256 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003257 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003258 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003259 }
3260
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003261 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003262 return inputs_[index];
3263 }
3264
David Brazdil1abb4192015-02-17 18:33:36 +00003265 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003266 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003267 }
3268
Roland Levillain3e3d7332015-04-28 11:00:54 +01003269 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003270 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003271 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003272 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003273 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003274 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003275
3276 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3277 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003278
3279 private:
3280 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3281};
3282
Calin Juravle175dc732015-08-25 15:42:32 +01003283class HInvokeUnresolved : public HInvoke {
3284 public:
3285 HInvokeUnresolved(ArenaAllocator* arena,
3286 uint32_t number_of_arguments,
3287 Primitive::Type return_type,
3288 uint32_t dex_pc,
3289 uint32_t dex_method_index,
3290 InvokeType invoke_type)
3291 : HInvoke(arena,
3292 number_of_arguments,
3293 0u /* number_of_other_inputs */,
3294 return_type,
3295 dex_pc,
3296 dex_method_index,
3297 invoke_type) {
3298 }
3299
3300 DECLARE_INSTRUCTION(InvokeUnresolved);
3301
3302 private:
3303 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3304};
3305
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003306class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003307 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003308 // Requirements of this method call regarding the class
3309 // initialization (clinit) check of its declaring class.
3310 enum class ClinitCheckRequirement {
3311 kNone, // Class already initialized.
3312 kExplicit, // Static call having explicit clinit check as last input.
3313 kImplicit, // Static call implicitly requiring a clinit check.
3314 };
3315
Vladimir Marko58155012015-08-19 12:49:41 +00003316 // Determines how to load the target ArtMethod*.
3317 enum class MethodLoadKind {
3318 // Use a String init ArtMethod* loaded from Thread entrypoints.
3319 kStringInit,
3320
3321 // Use the method's own ArtMethod* loaded by the register allocator.
3322 kRecursive,
3323
3324 // Use ArtMethod* at a known address, embed the direct address in the code.
3325 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3326 kDirectAddress,
3327
3328 // Use ArtMethod* at an address that will be known at link time, embed the direct
3329 // address in the code. If the image is relocatable, emit .patch_oat entry.
3330 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3331 // the image relocatable or not.
3332 kDirectAddressWithFixup,
3333
3334 // Load from resoved methods array in the dex cache using a PC-relative load.
3335 // Used when we need to use the dex cache, for example for invoke-static that
3336 // may cause class initialization (the entry may point to a resolution method),
3337 // and we know that we can access the dex cache arrays using a PC-relative load.
3338 kDexCachePcRelative,
3339
3340 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3341 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3342 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3343 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3344 kDexCacheViaMethod,
3345 };
3346
3347 // Determines the location of the code pointer.
3348 enum class CodePtrLocation {
3349 // Recursive call, use local PC-relative call instruction.
3350 kCallSelf,
3351
3352 // Use PC-relative call instruction patched at link time.
3353 // Used for calls within an oat file, boot->boot or app->app.
3354 kCallPCRelative,
3355
3356 // Call to a known target address, embed the direct address in code.
3357 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3358 kCallDirect,
3359
3360 // Call to a target address that will be known at link time, embed the direct
3361 // address in code. If the image is relocatable, emit .patch_oat entry.
3362 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3363 // the image relocatable or not.
3364 kCallDirectWithFixup,
3365
3366 // Use code pointer from the ArtMethod*.
3367 // Used when we don't know the target code. This is also the last-resort-kind used when
3368 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3369 kCallArtMethod,
3370 };
3371
3372 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003373 MethodLoadKind method_load_kind;
3374 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003375 // The method load data holds
3376 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3377 // Note that there are multiple string init methods, each having its own offset.
3378 // - the method address for kDirectAddress
3379 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003380 uint64_t method_load_data;
3381 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003382 };
3383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003384 HInvokeStaticOrDirect(ArenaAllocator* arena,
3385 uint32_t number_of_arguments,
3386 Primitive::Type return_type,
3387 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003388 uint32_t method_index,
3389 MethodReference target_method,
3390 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003391 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003392 InvokeType invoke_type,
3393 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003394 : HInvoke(arena,
3395 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003396 // There is one extra argument for the HCurrentMethod node, and
3397 // potentially one other if the clinit check is explicit, and one other
3398 // if the method is a string factory.
3399 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003400 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003401 return_type,
3402 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003403 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003404 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003405 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003406 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003407 target_method_(target_method),
3408 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003409
Vladimir Markodc151b22015-10-15 18:02:30 +01003410 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
3411 dispatch_info_ = dispatch_info;
3412 }
3413
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003414 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003415 // We access the method via the dex cache so we can't do an implicit null check.
3416 // TODO: for intrinsics we can generate implicit null checks.
3417 return false;
3418 }
3419
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003420 bool CanBeNull() const OVERRIDE {
3421 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3422 }
3423
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003424 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003425 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3426 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3427 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003428 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003429 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003430 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003431 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003432 bool HasPcRelDexCache() const {
3433 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3434 }
Vladimir Marko58155012015-08-19 12:49:41 +00003435 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3436 MethodReference GetTargetMethod() const { return target_method_; }
3437
3438 int32_t GetStringInitOffset() const {
3439 DCHECK(IsStringInit());
3440 return dispatch_info_.method_load_data;
3441 }
3442
3443 uint64_t GetMethodAddress() const {
3444 DCHECK(HasMethodAddress());
3445 return dispatch_info_.method_load_data;
3446 }
3447
3448 uint32_t GetDexCacheArrayOffset() const {
3449 DCHECK(HasPcRelDexCache());
3450 return dispatch_info_.method_load_data;
3451 }
3452
3453 uint64_t GetDirectCodePtr() const {
3454 DCHECK(HasDirectCodePtr());
3455 return dispatch_info_.direct_code_ptr;
3456 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003457
Calin Juravle68ad6492015-08-18 17:08:12 +01003458 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3459
Roland Levillain4c0eb422015-04-24 16:43:49 +01003460 // Is this instruction a call to a static method?
3461 bool IsStatic() const {
3462 return GetInvokeType() == kStatic;
3463 }
3464
Roland Levillain3e3d7332015-04-28 11:00:54 +01003465 // Remove the art::HLoadClass instruction set as last input by
3466 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3467 // the initial art::HClinitCheck instruction (only relevant for
3468 // static calls with explicit clinit check).
3469 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003470 DCHECK(IsStaticWithExplicitClinitCheck());
3471 size_t last_input_index = InputCount() - 1;
3472 HInstruction* last_input = InputAt(last_input_index);
3473 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003474 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003475 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003476 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003477 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3478 DCHECK(IsStaticWithImplicitClinitCheck());
3479 }
3480
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003481 bool IsStringFactoryFor(HFakeString* str) const {
3482 if (!IsStringInit()) return false;
3483 // +1 for the current method.
3484 if (InputCount() == (number_of_arguments_ + 1)) return false;
3485 return InputAt(InputCount() - 1)->AsFakeString() == str;
3486 }
3487
3488 void RemoveFakeStringArgumentAsLastInput() {
3489 DCHECK(IsStringInit());
3490 size_t last_input_index = InputCount() - 1;
3491 HInstruction* last_input = InputAt(last_input_index);
3492 DCHECK(last_input != nullptr);
3493 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3494 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003495 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003496 }
3497
Roland Levillain4c0eb422015-04-24 16:43:49 +01003498 // Is this a call to a static method whose declaring class has an
3499 // explicit intialization check in the graph?
3500 bool IsStaticWithExplicitClinitCheck() const {
3501 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3502 }
3503
3504 // Is this a call to a static method whose declaring class has an
3505 // implicit intialization check requirement?
3506 bool IsStaticWithImplicitClinitCheck() const {
3507 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3508 }
3509
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003510 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003511
Roland Levillain4c0eb422015-04-24 16:43:49 +01003512 protected:
3513 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3514 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3515 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3516 HInstruction* input = input_record.GetInstruction();
3517 // `input` is the last input of a static invoke marked as having
3518 // an explicit clinit check. It must either be:
3519 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3520 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3521 DCHECK(input != nullptr);
3522 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3523 }
3524 return input_record;
3525 }
3526
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003527 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003528 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003529 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003530 // The target method may refer to different dex file or method index than the original
3531 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3532 // in derived class to increase visibility.
3533 MethodReference target_method_;
3534 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003535
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003536 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003537};
3538
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003539class HInvokeVirtual : public HInvoke {
3540 public:
3541 HInvokeVirtual(ArenaAllocator* arena,
3542 uint32_t number_of_arguments,
3543 Primitive::Type return_type,
3544 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003545 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003546 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003547 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003548 vtable_index_(vtable_index) {}
3549
Calin Juravle641547a2015-04-21 22:08:51 +01003550 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003551 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003552 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003553 }
3554
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003555 uint32_t GetVTableIndex() const { return vtable_index_; }
3556
3557 DECLARE_INSTRUCTION(InvokeVirtual);
3558
3559 private:
3560 const uint32_t vtable_index_;
3561
3562 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3563};
3564
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003565class HInvokeInterface : public HInvoke {
3566 public:
3567 HInvokeInterface(ArenaAllocator* arena,
3568 uint32_t number_of_arguments,
3569 Primitive::Type return_type,
3570 uint32_t dex_pc,
3571 uint32_t dex_method_index,
3572 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003573 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003574 imt_index_(imt_index) {}
3575
Calin Juravle641547a2015-04-21 22:08:51 +01003576 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003577 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003578 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003579 }
3580
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003581 uint32_t GetImtIndex() const { return imt_index_; }
3582 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3583
3584 DECLARE_INSTRUCTION(InvokeInterface);
3585
3586 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003587 const uint32_t imt_index_;
3588
3589 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3590};
3591
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003592class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003593 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003594 HNewInstance(HCurrentMethod* current_method,
3595 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003596 uint16_t type_index,
3597 const DexFile& dex_file,
3598 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003599 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003600 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003601 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003602 entrypoint_(entrypoint) {
3603 SetRawInputAt(0, current_method);
3604 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003605
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003606 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003607 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003608
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003609 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003610 bool NeedsEnvironment() const OVERRIDE { return true; }
Andreas Gampe55d02cf2015-10-29 02:59:50 +00003611 // It may throw when called on:
3612 // - interfaces
3613 // - abstract/innaccessible/unknown classes
3614 // TODO: optimize when possible.
3615 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003616
Calin Juravle10e244f2015-01-26 18:54:32 +00003617 bool CanBeNull() const OVERRIDE { return false; }
3618
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003619 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3620
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003621 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003622
3623 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003624 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003625 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003626 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003627
3628 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3629};
3630
Roland Levillain88cb1752014-10-20 16:36:47 +01003631class HNeg : public HUnaryOperation {
3632 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003633 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3634 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003635
Roland Levillain9867bc72015-08-05 10:21:34 +01003636 template <typename T> T Compute(T x) const { return -x; }
3637
3638 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003639 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003640 }
3641 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003642 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003643 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003644
Roland Levillain88cb1752014-10-20 16:36:47 +01003645 DECLARE_INSTRUCTION(Neg);
3646
3647 private:
3648 DISALLOW_COPY_AND_ASSIGN(HNeg);
3649};
3650
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003651class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003652 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003653 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003654 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003655 uint32_t dex_pc,
3656 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003657 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003658 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003659 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003660 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003661 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003662 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003663 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003664 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003665 }
3666
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003667 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003668 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003669
3670 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003671 bool NeedsEnvironment() const OVERRIDE { return true; }
3672
Mingyao Yang0c365e62015-03-31 15:09:29 -07003673 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3674 bool CanThrow() const OVERRIDE { return true; }
3675
Calin Juravle10e244f2015-01-26 18:54:32 +00003676 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003677
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003678 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3679
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003680 DECLARE_INSTRUCTION(NewArray);
3681
3682 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003683 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003684 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003685 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003686
3687 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3688};
3689
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003690class HAdd : public HBinaryOperation {
3691 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003692 HAdd(Primitive::Type result_type,
3693 HInstruction* left,
3694 HInstruction* right,
3695 uint32_t dex_pc = kNoDexPc)
3696 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003697
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003698 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003699
Roland Levillain9867bc72015-08-05 10:21:34 +01003700 template <typename T> T Compute(T x, T y) const { return x + y; }
3701
3702 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003703 return GetBlock()->GetGraph()->GetIntConstant(
3704 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003705 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003706 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003707 return GetBlock()->GetGraph()->GetLongConstant(
3708 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003709 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003710
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003711 DECLARE_INSTRUCTION(Add);
3712
3713 private:
3714 DISALLOW_COPY_AND_ASSIGN(HAdd);
3715};
3716
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003717class HSub : public HBinaryOperation {
3718 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003719 HSub(Primitive::Type result_type,
3720 HInstruction* left,
3721 HInstruction* right,
3722 uint32_t dex_pc = kNoDexPc)
3723 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003724
Roland Levillain9867bc72015-08-05 10:21:34 +01003725 template <typename T> T Compute(T x, T y) const { return x - y; }
3726
3727 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003728 return GetBlock()->GetGraph()->GetIntConstant(
3729 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003730 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003731 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003732 return GetBlock()->GetGraph()->GetLongConstant(
3733 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003734 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003735
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003736 DECLARE_INSTRUCTION(Sub);
3737
3738 private:
3739 DISALLOW_COPY_AND_ASSIGN(HSub);
3740};
3741
Calin Juravle34bacdf2014-10-07 20:23:36 +01003742class HMul : public HBinaryOperation {
3743 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003744 HMul(Primitive::Type result_type,
3745 HInstruction* left,
3746 HInstruction* right,
3747 uint32_t dex_pc = kNoDexPc)
3748 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003749
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003750 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003751
Roland Levillain9867bc72015-08-05 10:21:34 +01003752 template <typename T> T Compute(T x, T y) const { return x * y; }
3753
3754 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003755 return GetBlock()->GetGraph()->GetIntConstant(
3756 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003757 }
3758 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003759 return GetBlock()->GetGraph()->GetLongConstant(
3760 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003761 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003762
3763 DECLARE_INSTRUCTION(Mul);
3764
3765 private:
3766 DISALLOW_COPY_AND_ASSIGN(HMul);
3767};
3768
Calin Juravle7c4954d2014-10-28 16:57:40 +00003769class HDiv : public HBinaryOperation {
3770 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003771 HDiv(Primitive::Type result_type,
3772 HInstruction* left,
3773 HInstruction* right,
3774 uint32_t dex_pc)
3775 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003776
Roland Levillain9867bc72015-08-05 10:21:34 +01003777 template <typename T>
3778 T Compute(T x, T y) const {
3779 // Our graph structure ensures we never have 0 for `y` during
3780 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003781 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003782 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003783 return (y == -1) ? -x : x / y;
3784 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003785
Roland Levillain9867bc72015-08-05 10:21:34 +01003786 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003787 return GetBlock()->GetGraph()->GetIntConstant(
3788 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003789 }
3790 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003791 return GetBlock()->GetGraph()->GetLongConstant(
3792 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003793 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003794
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003795 static SideEffects SideEffectsForArchRuntimeCalls() {
3796 // The generated code can use a runtime call.
3797 return SideEffects::CanTriggerGC();
3798 }
3799
Calin Juravle7c4954d2014-10-28 16:57:40 +00003800 DECLARE_INSTRUCTION(Div);
3801
3802 private:
3803 DISALLOW_COPY_AND_ASSIGN(HDiv);
3804};
3805
Calin Juravlebacfec32014-11-14 15:54:36 +00003806class HRem : public HBinaryOperation {
3807 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003808 HRem(Primitive::Type result_type,
3809 HInstruction* left,
3810 HInstruction* right,
3811 uint32_t dex_pc)
3812 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003813
Roland Levillain9867bc72015-08-05 10:21:34 +01003814 template <typename T>
3815 T Compute(T x, T y) const {
3816 // Our graph structure ensures we never have 0 for `y` during
3817 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003818 DCHECK_NE(y, 0);
3819 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3820 return (y == -1) ? 0 : x % y;
3821 }
3822
Roland Levillain9867bc72015-08-05 10:21:34 +01003823 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003824 return GetBlock()->GetGraph()->GetIntConstant(
3825 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003826 }
3827 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003828 return GetBlock()->GetGraph()->GetLongConstant(
3829 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003830 }
3831
Calin Juravlebacfec32014-11-14 15:54:36 +00003832
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003833 static SideEffects SideEffectsForArchRuntimeCalls() {
3834 return SideEffects::CanTriggerGC();
3835 }
3836
Calin Juravlebacfec32014-11-14 15:54:36 +00003837 DECLARE_INSTRUCTION(Rem);
3838
3839 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003840 DISALLOW_COPY_AND_ASSIGN(HRem);
3841};
3842
Calin Juravled0d48522014-11-04 16:40:20 +00003843class HDivZeroCheck : public HExpression<1> {
3844 public:
3845 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003846 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003847 SetRawInputAt(0, value);
3848 }
3849
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003850 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3851
Calin Juravled0d48522014-11-04 16:40:20 +00003852 bool CanBeMoved() const OVERRIDE { return true; }
3853
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003854 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003855 return true;
3856 }
3857
3858 bool NeedsEnvironment() const OVERRIDE { return true; }
3859 bool CanThrow() const OVERRIDE { return true; }
3860
Calin Juravled0d48522014-11-04 16:40:20 +00003861 DECLARE_INSTRUCTION(DivZeroCheck);
3862
3863 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003864 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3865};
3866
Calin Juravle9aec02f2014-11-18 23:06:35 +00003867class HShl : public HBinaryOperation {
3868 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003869 HShl(Primitive::Type result_type,
3870 HInstruction* left,
3871 HInstruction* right,
3872 uint32_t dex_pc = kNoDexPc)
3873 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003874
Roland Levillain9867bc72015-08-05 10:21:34 +01003875 template <typename T, typename U, typename V>
3876 T Compute(T x, U y, V max_shift_value) const {
3877 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3878 "V is not the unsigned integer type corresponding to T");
3879 return x << (y & max_shift_value);
3880 }
3881
3882 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3883 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003884 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003885 }
3886 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3887 // case is handled as `x << static_cast<int>(y)`.
3888 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3889 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003890 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003891 }
3892 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3893 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003894 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003895 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003896
3897 DECLARE_INSTRUCTION(Shl);
3898
3899 private:
3900 DISALLOW_COPY_AND_ASSIGN(HShl);
3901};
3902
3903class HShr : public HBinaryOperation {
3904 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003905 HShr(Primitive::Type result_type,
3906 HInstruction* left,
3907 HInstruction* right,
3908 uint32_t dex_pc = kNoDexPc)
3909 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003910
Roland Levillain9867bc72015-08-05 10:21:34 +01003911 template <typename T, typename U, typename V>
3912 T Compute(T x, U y, V max_shift_value) const {
3913 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3914 "V is not the unsigned integer type corresponding to T");
3915 return x >> (y & max_shift_value);
3916 }
3917
3918 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3919 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003920 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003921 }
3922 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3923 // case is handled as `x >> static_cast<int>(y)`.
3924 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3925 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003926 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003927 }
3928 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3929 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003930 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003931 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003932
3933 DECLARE_INSTRUCTION(Shr);
3934
3935 private:
3936 DISALLOW_COPY_AND_ASSIGN(HShr);
3937};
3938
3939class HUShr : public HBinaryOperation {
3940 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003941 HUShr(Primitive::Type result_type,
3942 HInstruction* left,
3943 HInstruction* right,
3944 uint32_t dex_pc = kNoDexPc)
3945 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003946
Roland Levillain9867bc72015-08-05 10:21:34 +01003947 template <typename T, typename U, typename V>
3948 T Compute(T x, U y, V max_shift_value) const {
3949 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3950 "V is not the unsigned integer type corresponding to T");
3951 V ux = static_cast<V>(x);
3952 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003953 }
3954
Roland Levillain9867bc72015-08-05 10:21:34 +01003955 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3956 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003957 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003958 }
3959 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3960 // case is handled as `x >>> static_cast<int>(y)`.
3961 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3962 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003963 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003964 }
3965 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3966 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003967 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003968 }
3969
3970 DECLARE_INSTRUCTION(UShr);
3971
3972 private:
3973 DISALLOW_COPY_AND_ASSIGN(HUShr);
3974};
3975
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003976class HAnd : public HBinaryOperation {
3977 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003978 HAnd(Primitive::Type result_type,
3979 HInstruction* left,
3980 HInstruction* right,
3981 uint32_t dex_pc = kNoDexPc)
3982 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003983
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003984 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003985
Roland Levillain9867bc72015-08-05 10:21:34 +01003986 template <typename T, typename U>
3987 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3988
3989 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003990 return GetBlock()->GetGraph()->GetIntConstant(
3991 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 }
3993 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003994 return GetBlock()->GetGraph()->GetLongConstant(
3995 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003996 }
3997 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003998 return GetBlock()->GetGraph()->GetLongConstant(
3999 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004000 }
4001 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004002 return GetBlock()->GetGraph()->GetLongConstant(
4003 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004004 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004005
4006 DECLARE_INSTRUCTION(And);
4007
4008 private:
4009 DISALLOW_COPY_AND_ASSIGN(HAnd);
4010};
4011
4012class HOr : public HBinaryOperation {
4013 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004014 HOr(Primitive::Type result_type,
4015 HInstruction* left,
4016 HInstruction* right,
4017 uint32_t dex_pc = kNoDexPc)
4018 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004019
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004020 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004021
Roland Levillain9867bc72015-08-05 10:21:34 +01004022 template <typename T, typename U>
4023 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4024
4025 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004026 return GetBlock()->GetGraph()->GetIntConstant(
4027 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004028 }
4029 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004030 return GetBlock()->GetGraph()->GetLongConstant(
4031 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004032 }
4033 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004034 return GetBlock()->GetGraph()->GetLongConstant(
4035 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004036 }
4037 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004038 return GetBlock()->GetGraph()->GetLongConstant(
4039 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004040 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004041
4042 DECLARE_INSTRUCTION(Or);
4043
4044 private:
4045 DISALLOW_COPY_AND_ASSIGN(HOr);
4046};
4047
4048class HXor : public HBinaryOperation {
4049 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004050 HXor(Primitive::Type result_type,
4051 HInstruction* left,
4052 HInstruction* right,
4053 uint32_t dex_pc = kNoDexPc)
4054 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004055
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004056 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004057
Roland Levillain9867bc72015-08-05 10:21:34 +01004058 template <typename T, typename U>
4059 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4060
4061 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004062 return GetBlock()->GetGraph()->GetIntConstant(
4063 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004064 }
4065 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004066 return GetBlock()->GetGraph()->GetLongConstant(
4067 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004068 }
4069 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 return GetBlock()->GetGraph()->GetLongConstant(
4071 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004072 }
4073 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004074 return GetBlock()->GetGraph()->GetLongConstant(
4075 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004076 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004077
4078 DECLARE_INSTRUCTION(Xor);
4079
4080 private:
4081 DISALLOW_COPY_AND_ASSIGN(HXor);
4082};
4083
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004084// The value of a parameter in this method. Its location depends on
4085// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004086class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004087 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004088 HParameterValue(const DexFile& dex_file,
4089 uint16_t type_index,
4090 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004091 Primitive::Type parameter_type,
4092 bool is_this = false)
4093 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004094 dex_file_(dex_file),
4095 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004096 index_(index),
4097 is_this_(is_this),
4098 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004099
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004100 const DexFile& GetDexFile() const { return dex_file_; }
4101 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004102 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004103 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004104
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004105 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4106 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004107
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004108 DECLARE_INSTRUCTION(ParameterValue);
4109
4110 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004111 const DexFile& dex_file_;
4112 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004113 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004114 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004115 const uint8_t index_;
4116
Calin Juravle10e244f2015-01-26 18:54:32 +00004117 // Whether or not the parameter value corresponds to 'this' argument.
4118 const bool is_this_;
4119
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004120 bool can_be_null_;
4121
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004122 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4123};
4124
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004125class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004126 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004127 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4128 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004129
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004130 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004131 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004132 return true;
4133 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004134
Roland Levillain9867bc72015-08-05 10:21:34 +01004135 template <typename T> T Compute(T x) const { return ~x; }
4136
4137 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004138 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004139 }
4140 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004141 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004142 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004143
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004144 DECLARE_INSTRUCTION(Not);
4145
4146 private:
4147 DISALLOW_COPY_AND_ASSIGN(HNot);
4148};
4149
David Brazdil66d126e2015-04-03 16:02:44 +01004150class HBooleanNot : public HUnaryOperation {
4151 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004152 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4153 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004154
4155 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004156 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004157 return true;
4158 }
4159
Roland Levillain9867bc72015-08-05 10:21:34 +01004160 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004161 DCHECK(IsUint<1>(x));
4162 return !x;
4163 }
4164
Roland Levillain9867bc72015-08-05 10:21:34 +01004165 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004166 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004167 }
4168 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4169 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004170 UNREACHABLE();
4171 }
4172
4173 DECLARE_INSTRUCTION(BooleanNot);
4174
4175 private:
4176 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4177};
4178
Roland Levillaindff1f282014-11-05 14:15:05 +00004179class HTypeConversion : public HExpression<1> {
4180 public:
4181 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004182 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004183 : HExpression(result_type,
4184 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4185 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004186 SetRawInputAt(0, input);
4187 DCHECK_NE(input->GetType(), result_type);
4188 }
4189
4190 HInstruction* GetInput() const { return InputAt(0); }
4191 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4192 Primitive::Type GetResultType() const { return GetType(); }
4193
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004194 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004195 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004196
Roland Levillaindff1f282014-11-05 14:15:05 +00004197 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004198 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004199
Mark Mendelle82549b2015-05-06 10:55:34 -04004200 // Try to statically evaluate the conversion and return a HConstant
4201 // containing the result. If the input cannot be converted, return nullptr.
4202 HConstant* TryStaticEvaluation() const;
4203
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004204 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4205 Primitive::Type result_type) {
4206 // Some architectures may not require the 'GC' side effects, but at this point
4207 // in the compilation process we do not know what architecture we will
4208 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004209 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4210 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004211 return SideEffects::CanTriggerGC();
4212 }
4213 return SideEffects::None();
4214 }
4215
Roland Levillaindff1f282014-11-05 14:15:05 +00004216 DECLARE_INSTRUCTION(TypeConversion);
4217
4218 private:
4219 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4220};
4221
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004222static constexpr uint32_t kNoRegNumber = -1;
4223
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004224class HPhi : public HInstruction {
4225 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004226 HPhi(ArenaAllocator* arena,
4227 uint32_t reg_number,
4228 size_t number_of_inputs,
4229 Primitive::Type type,
4230 uint32_t dex_pc = kNoDexPc)
4231 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004232 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004233 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004234 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004235 is_live_(false),
4236 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004237 }
4238
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004239 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4240 static Primitive::Type ToPhiType(Primitive::Type type) {
4241 switch (type) {
4242 case Primitive::kPrimBoolean:
4243 case Primitive::kPrimByte:
4244 case Primitive::kPrimShort:
4245 case Primitive::kPrimChar:
4246 return Primitive::kPrimInt;
4247 default:
4248 return type;
4249 }
4250 }
4251
David Brazdilffee3d32015-07-06 11:48:53 +01004252 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4253
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004254 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004255
4256 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004257 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004258
Calin Juravle10e244f2015-01-26 18:54:32 +00004259 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004260 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004261
Calin Juravle10e244f2015-01-26 18:54:32 +00004262 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4263 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4264
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004265 uint32_t GetRegNumber() const { return reg_number_; }
4266
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004267 void SetDead() { is_live_ = false; }
4268 void SetLive() { is_live_ = true; }
4269 bool IsDead() const { return !is_live_; }
4270 bool IsLive() const { return is_live_; }
4271
David Brazdil77a48ae2015-09-15 12:34:04 +00004272 bool IsVRegEquivalentOf(HInstruction* other) const {
4273 return other != nullptr
4274 && other->IsPhi()
4275 && other->AsPhi()->GetBlock() == GetBlock()
4276 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4277 }
4278
Calin Juravlea4f88312015-04-16 12:57:19 +01004279 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4280 // An equivalent phi is a phi having the same dex register and type.
4281 // It assumes that phis with the same dex register are adjacent.
4282 HPhi* GetNextEquivalentPhiWithSameType() {
4283 HInstruction* next = GetNext();
4284 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4285 if (next->GetType() == GetType()) {
4286 return next->AsPhi();
4287 }
4288 next = next->GetNext();
4289 }
4290 return nullptr;
4291 }
4292
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004293 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004294
David Brazdil1abb4192015-02-17 18:33:36 +00004295 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004296 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004297 return inputs_[index];
4298 }
David Brazdil1abb4192015-02-17 18:33:36 +00004299
4300 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004301 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004302 }
4303
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004304 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004305 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004306 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004307 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004308 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004309 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004310
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004311 DISALLOW_COPY_AND_ASSIGN(HPhi);
4312};
4313
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004314class HNullCheck : public HExpression<1> {
4315 public:
4316 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004317 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004318 SetRawInputAt(0, value);
4319 }
4320
Calin Juravle10e244f2015-01-26 18:54:32 +00004321 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004322 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004323 return true;
4324 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004325
Calin Juravle10e244f2015-01-26 18:54:32 +00004326 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004327
Calin Juravle10e244f2015-01-26 18:54:32 +00004328 bool CanThrow() const OVERRIDE { return true; }
4329
4330 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004331
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004332
4333 DECLARE_INSTRUCTION(NullCheck);
4334
4335 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004336 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4337};
4338
4339class FieldInfo : public ValueObject {
4340 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004341 FieldInfo(MemberOffset field_offset,
4342 Primitive::Type field_type,
4343 bool is_volatile,
4344 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004345 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004346 const DexFile& dex_file,
4347 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004348 : field_offset_(field_offset),
4349 field_type_(field_type),
4350 is_volatile_(is_volatile),
4351 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004352 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004353 dex_file_(dex_file),
4354 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004355
4356 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004357 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004358 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004359 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004360 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004361 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004362 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004363
4364 private:
4365 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004366 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004367 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004368 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004369 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004370 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004371 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004372};
4373
4374class HInstanceFieldGet : public HExpression<1> {
4375 public:
4376 HInstanceFieldGet(HInstruction* value,
4377 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004378 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004379 bool is_volatile,
4380 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004381 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004382 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004383 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004384 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004385 : HExpression(field_type,
4386 SideEffects::FieldReadOfType(field_type, is_volatile),
4387 dex_pc),
4388 field_info_(field_offset,
4389 field_type,
4390 is_volatile,
4391 field_idx,
4392 declaring_class_def_index,
4393 dex_file,
4394 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004395 SetRawInputAt(0, value);
4396 }
4397
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004398 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004399
4400 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4401 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4402 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004403 }
4404
Calin Juravle641547a2015-04-21 22:08:51 +01004405 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4406 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004407 }
4408
4409 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004410 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4411 }
4412
Calin Juravle52c48962014-12-16 17:02:57 +00004413 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004414 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004415 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004416 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004417
4418 DECLARE_INSTRUCTION(InstanceFieldGet);
4419
4420 private:
4421 const FieldInfo field_info_;
4422
4423 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4424};
4425
4426class HInstanceFieldSet : public HTemplateInstruction<2> {
4427 public:
4428 HInstanceFieldSet(HInstruction* object,
4429 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004430 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004431 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004432 bool is_volatile,
4433 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004434 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004435 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004436 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004437 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004438 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4439 dex_pc),
4440 field_info_(field_offset,
4441 field_type,
4442 is_volatile,
4443 field_idx,
4444 declaring_class_def_index,
4445 dex_file,
4446 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004447 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004448 SetRawInputAt(0, object);
4449 SetRawInputAt(1, value);
4450 }
4451
Calin Juravle641547a2015-04-21 22:08:51 +01004452 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4453 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004454 }
4455
Calin Juravle52c48962014-12-16 17:02:57 +00004456 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004457 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004458 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004459 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004460 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004461 bool GetValueCanBeNull() const { return value_can_be_null_; }
4462 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004463
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004464 DECLARE_INSTRUCTION(InstanceFieldSet);
4465
4466 private:
4467 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004468 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004469
4470 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4471};
4472
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004473class HArrayGet : public HExpression<2> {
4474 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004475 HArrayGet(HInstruction* array,
4476 HInstruction* index,
4477 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004478 uint32_t dex_pc,
4479 SideEffects additional_side_effects = SideEffects::None())
4480 : HExpression(type,
4481 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
4482 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004483 SetRawInputAt(0, array);
4484 SetRawInputAt(1, index);
4485 }
4486
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004487 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004488 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004489 return true;
4490 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004491 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004492 // TODO: We can be smarter here.
4493 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4494 // which generates the implicit null check. There are cases when these can be removed
4495 // to produce better code. If we ever add optimizations to do so we should allow an
4496 // implicit check here (as long as the address falls in the first page).
4497 return false;
4498 }
4499
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004500 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004501
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004502 HInstruction* GetArray() const { return InputAt(0); }
4503 HInstruction* GetIndex() const { return InputAt(1); }
4504
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004505 DECLARE_INSTRUCTION(ArrayGet);
4506
4507 private:
4508 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4509};
4510
4511class HArraySet : public HTemplateInstruction<3> {
4512 public:
4513 HArraySet(HInstruction* array,
4514 HInstruction* index,
4515 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004516 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004517 uint32_t dex_pc,
4518 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004519 : HTemplateInstruction(
4520 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004521 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4522 additional_side_effects),
4523 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004524 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004525 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004526 value_can_be_null_(true),
4527 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004528 SetRawInputAt(0, array);
4529 SetRawInputAt(1, index);
4530 SetRawInputAt(2, value);
4531 }
4532
Calin Juravle77520bc2015-01-12 18:45:46 +00004533 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004534 // We currently always call a runtime method to catch array store
4535 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004536 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004537 }
4538
Mingyao Yang81014cb2015-06-02 03:16:27 -07004539 // Can throw ArrayStoreException.
4540 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4541
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004542 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004543 // TODO: Same as for ArrayGet.
4544 return false;
4545 }
4546
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004547 void ClearNeedsTypeCheck() {
4548 needs_type_check_ = false;
4549 }
4550
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004551 void ClearValueCanBeNull() {
4552 value_can_be_null_ = false;
4553 }
4554
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004555 void SetStaticTypeOfArrayIsObjectArray() {
4556 static_type_of_array_is_object_array_ = true;
4557 }
4558
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004559 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004560 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004561 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004562
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004563 HInstruction* GetArray() const { return InputAt(0); }
4564 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004565 HInstruction* GetValue() const { return InputAt(2); }
4566
4567 Primitive::Type GetComponentType() const {
4568 // The Dex format does not type floating point index operations. Since the
4569 // `expected_component_type_` is set during building and can therefore not
4570 // be correct, we also check what is the value type. If it is a floating
4571 // point type, we must use that type.
4572 Primitive::Type value_type = GetValue()->GetType();
4573 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4574 ? value_type
4575 : expected_component_type_;
4576 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004577
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004578 Primitive::Type GetRawExpectedComponentType() const {
4579 return expected_component_type_;
4580 }
4581
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004582 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4583 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4584 }
4585
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004586 DECLARE_INSTRUCTION(ArraySet);
4587
4588 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004589 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004590 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004591 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004592 // Cached information for the reference_type_info_ so that codegen
4593 // does not need to inspect the static type.
4594 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004595
4596 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4597};
4598
4599class HArrayLength : public HExpression<1> {
4600 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004601 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004602 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004603 // Note that arrays do not change length, so the instruction does not
4604 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605 SetRawInputAt(0, array);
4606 }
4607
Calin Juravle77520bc2015-01-12 18:45:46 +00004608 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004609 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004610 return true;
4611 }
Calin Juravle641547a2015-04-21 22:08:51 +01004612 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4613 return obj == InputAt(0);
4614 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004615
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004616 DECLARE_INSTRUCTION(ArrayLength);
4617
4618 private:
4619 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4620};
4621
4622class HBoundsCheck : public HExpression<2> {
4623 public:
4624 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004625 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004626 DCHECK(index->GetType() == Primitive::kPrimInt);
4627 SetRawInputAt(0, index);
4628 SetRawInputAt(1, length);
4629 }
4630
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004631 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004632 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004633 return true;
4634 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004635
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004636 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004637
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004638 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004639
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004640 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004641
4642 DECLARE_INSTRUCTION(BoundsCheck);
4643
4644 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004645 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4646};
4647
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004648/**
4649 * Some DEX instructions are folded into multiple HInstructions that need
4650 * to stay live until the last HInstruction. This class
4651 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004652 * HInstruction stays live. `index` represents the stack location index of the
4653 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004654 */
4655class HTemporary : public HTemplateInstruction<0> {
4656 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004657 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4658 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004659
4660 size_t GetIndex() const { return index_; }
4661
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004662 Primitive::Type GetType() const OVERRIDE {
4663 // The previous instruction is the one that will be stored in the temporary location.
4664 DCHECK(GetPrevious() != nullptr);
4665 return GetPrevious()->GetType();
4666 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004667
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004668 DECLARE_INSTRUCTION(Temporary);
4669
4670 private:
4671 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004672 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4673};
4674
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004675class HSuspendCheck : public HTemplateInstruction<0> {
4676 public:
4677 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004678 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004679
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004680 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004681 return true;
4682 }
4683
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004684 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4685 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004686
4687 DECLARE_INSTRUCTION(SuspendCheck);
4688
4689 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004690 // Only used for code generation, in order to share the same slow path between back edges
4691 // of a same loop.
4692 SlowPathCode* slow_path_;
4693
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004694 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4695};
4696
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004697/**
4698 * Instruction to load a Class object.
4699 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004700class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004701 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004702 HLoadClass(HCurrentMethod* current_method,
4703 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004704 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004705 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004706 uint32_t dex_pc,
4707 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004708 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004709 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004710 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004711 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004712 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004713 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004714 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004715 // Referrers class should not need access check. We never inline unverified
4716 // methods so we can't possibly end up in this situation.
4717 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004718 SetRawInputAt(0, current_method);
4719 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004720
4721 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004722
4723 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004724 // Note that we don't need to test for generate_clinit_check_.
4725 // Whether or not we need to generate the clinit check is processed in
4726 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004727 return other->AsLoadClass()->type_index_ == type_index_ &&
4728 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004729 }
4730
4731 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4732
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004733 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004734 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004735 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004736
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004737 bool NeedsEnvironment() const OVERRIDE {
4738 // Will call runtime and load the class if the class is not loaded yet.
4739 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004740 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004741 }
4742
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004743 bool MustGenerateClinitCheck() const {
4744 return generate_clinit_check_;
4745 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004746 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4747 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004748 }
4749
4750 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004751 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4752 }
4753
4754 bool NeedsAccessCheck() const {
4755 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004756 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004757
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004758 bool CanThrow() const OVERRIDE {
4759 // May call runtime and and therefore can throw.
4760 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004761 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004762 }
4763
Calin Juravleacf735c2015-02-12 15:25:22 +00004764 ReferenceTypeInfo GetLoadedClassRTI() {
4765 return loaded_class_rti_;
4766 }
4767
4768 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4769 // Make sure we only set exact types (the loaded class should never be merged).
4770 DCHECK(rti.IsExact());
4771 loaded_class_rti_ = rti;
4772 }
4773
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004774 const DexFile& GetDexFile() { return dex_file_; }
4775
Vladimir Markodc151b22015-10-15 18:02:30 +01004776 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004777
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004778 static SideEffects SideEffectsForArchRuntimeCalls() {
4779 return SideEffects::CanTriggerGC();
4780 }
4781
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004782 DECLARE_INSTRUCTION(LoadClass);
4783
4784 private:
4785 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004786 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004787 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004788 // Whether this instruction must generate the initialization check.
4789 // Used for code generation.
4790 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004791 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004792
Calin Juravleacf735c2015-02-12 15:25:22 +00004793 ReferenceTypeInfo loaded_class_rti_;
4794
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004795 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4796};
4797
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004798class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004799 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004800 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004801 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4802 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004803 SetRawInputAt(0, current_method);
4804 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004805
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004806 bool CanBeMoved() const OVERRIDE { return true; }
4807
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004808 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4809 return other->AsLoadString()->string_index_ == string_index_;
4810 }
4811
4812 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4813
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004814 uint32_t GetStringIndex() const { return string_index_; }
4815
4816 // TODO: Can we deopt or debug when we resolve a string?
4817 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004818 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004819 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004820
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004821 static SideEffects SideEffectsForArchRuntimeCalls() {
4822 return SideEffects::CanTriggerGC();
4823 }
4824
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004825 DECLARE_INSTRUCTION(LoadString);
4826
4827 private:
4828 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004829
4830 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4831};
4832
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004833/**
4834 * Performs an initialization check on its Class object input.
4835 */
4836class HClinitCheck : public HExpression<1> {
4837 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004838 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004839 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004840 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004841 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4842 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004843 SetRawInputAt(0, constant);
4844 }
4845
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004846 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004847 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004848 return true;
4849 }
4850
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004851 bool NeedsEnvironment() const OVERRIDE {
4852 // May call runtime to initialize the class.
4853 return true;
4854 }
4855
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004856
4857 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4858
4859 DECLARE_INSTRUCTION(ClinitCheck);
4860
4861 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004862 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4863};
4864
4865class HStaticFieldGet : public HExpression<1> {
4866 public:
4867 HStaticFieldGet(HInstruction* cls,
4868 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004869 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004870 bool is_volatile,
4871 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004872 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004873 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004874 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004875 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004876 : HExpression(field_type,
4877 SideEffects::FieldReadOfType(field_type, is_volatile),
4878 dex_pc),
4879 field_info_(field_offset,
4880 field_type,
4881 is_volatile,
4882 field_idx,
4883 declaring_class_def_index,
4884 dex_file,
4885 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004886 SetRawInputAt(0, cls);
4887 }
4888
Calin Juravle52c48962014-12-16 17:02:57 +00004889
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004890 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004891
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004892 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004893 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4894 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004895 }
4896
4897 size_t ComputeHashCode() const OVERRIDE {
4898 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4899 }
4900
Calin Juravle52c48962014-12-16 17:02:57 +00004901 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004902 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4903 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004904 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004905
4906 DECLARE_INSTRUCTION(StaticFieldGet);
4907
4908 private:
4909 const FieldInfo field_info_;
4910
4911 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4912};
4913
4914class HStaticFieldSet : public HTemplateInstruction<2> {
4915 public:
4916 HStaticFieldSet(HInstruction* cls,
4917 HInstruction* value,
4918 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004919 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004920 bool is_volatile,
4921 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004922 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004923 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004924 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004925 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004926 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4927 dex_pc),
4928 field_info_(field_offset,
4929 field_type,
4930 is_volatile,
4931 field_idx,
4932 declaring_class_def_index,
4933 dex_file,
4934 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004935 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004936 SetRawInputAt(0, cls);
4937 SetRawInputAt(1, value);
4938 }
4939
Calin Juravle52c48962014-12-16 17:02:57 +00004940 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004941 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4942 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004943 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004944
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004945 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004946 bool GetValueCanBeNull() const { return value_can_be_null_; }
4947 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004948
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004949 DECLARE_INSTRUCTION(StaticFieldSet);
4950
4951 private:
4952 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004953 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004954
4955 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4956};
4957
Calin Juravlee460d1d2015-09-29 04:52:17 +01004958class HUnresolvedInstanceFieldGet : public HExpression<1> {
4959 public:
4960 HUnresolvedInstanceFieldGet(HInstruction* obj,
4961 Primitive::Type field_type,
4962 uint32_t field_index,
4963 uint32_t dex_pc)
4964 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4965 field_index_(field_index) {
4966 SetRawInputAt(0, obj);
4967 }
4968
4969 bool NeedsEnvironment() const OVERRIDE { return true; }
4970 bool CanThrow() const OVERRIDE { return true; }
4971
4972 Primitive::Type GetFieldType() const { return GetType(); }
4973 uint32_t GetFieldIndex() const { return field_index_; }
4974
4975 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4976
4977 private:
4978 const uint32_t field_index_;
4979
4980 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4981};
4982
4983class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4984 public:
4985 HUnresolvedInstanceFieldSet(HInstruction* obj,
4986 HInstruction* value,
4987 Primitive::Type field_type,
4988 uint32_t field_index,
4989 uint32_t dex_pc)
4990 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4991 field_type_(field_type),
4992 field_index_(field_index) {
4993 DCHECK_EQ(field_type, value->GetType());
4994 SetRawInputAt(0, obj);
4995 SetRawInputAt(1, value);
4996 }
4997
4998 bool NeedsEnvironment() const OVERRIDE { return true; }
4999 bool CanThrow() const OVERRIDE { return true; }
5000
5001 Primitive::Type GetFieldType() const { return field_type_; }
5002 uint32_t GetFieldIndex() const { return field_index_; }
5003
5004 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5005
5006 private:
5007 const Primitive::Type field_type_;
5008 const uint32_t field_index_;
5009
5010 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5011};
5012
5013class HUnresolvedStaticFieldGet : public HExpression<0> {
5014 public:
5015 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5016 uint32_t field_index,
5017 uint32_t dex_pc)
5018 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5019 field_index_(field_index) {
5020 }
5021
5022 bool NeedsEnvironment() const OVERRIDE { return true; }
5023 bool CanThrow() const OVERRIDE { return true; }
5024
5025 Primitive::Type GetFieldType() const { return GetType(); }
5026 uint32_t GetFieldIndex() const { return field_index_; }
5027
5028 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5029
5030 private:
5031 const uint32_t field_index_;
5032
5033 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5034};
5035
5036class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5037 public:
5038 HUnresolvedStaticFieldSet(HInstruction* value,
5039 Primitive::Type field_type,
5040 uint32_t field_index,
5041 uint32_t dex_pc)
5042 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5043 field_type_(field_type),
5044 field_index_(field_index) {
5045 DCHECK_EQ(field_type, value->GetType());
5046 SetRawInputAt(0, value);
5047 }
5048
5049 bool NeedsEnvironment() const OVERRIDE { return true; }
5050 bool CanThrow() const OVERRIDE { return true; }
5051
5052 Primitive::Type GetFieldType() const { return field_type_; }
5053 uint32_t GetFieldIndex() const { return field_index_; }
5054
5055 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5056
5057 private:
5058 const Primitive::Type field_type_;
5059 const uint32_t field_index_;
5060
5061 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5062};
5063
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005064// Implement the move-exception DEX instruction.
5065class HLoadException : public HExpression<0> {
5066 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005067 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5068 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005069
David Brazdilbbd733e2015-08-18 17:48:17 +01005070 bool CanBeNull() const OVERRIDE { return false; }
5071
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005072 DECLARE_INSTRUCTION(LoadException);
5073
5074 private:
5075 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5076};
5077
David Brazdilcb1c0552015-08-04 16:22:25 +01005078// Implicit part of move-exception which clears thread-local exception storage.
5079// Must not be removed because the runtime expects the TLS to get cleared.
5080class HClearException : public HTemplateInstruction<0> {
5081 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005082 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5083 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005084
5085 DECLARE_INSTRUCTION(ClearException);
5086
5087 private:
5088 DISALLOW_COPY_AND_ASSIGN(HClearException);
5089};
5090
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005091class HThrow : public HTemplateInstruction<1> {
5092 public:
5093 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005094 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005095 SetRawInputAt(0, exception);
5096 }
5097
5098 bool IsControlFlow() const OVERRIDE { return true; }
5099
5100 bool NeedsEnvironment() const OVERRIDE { return true; }
5101
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005102 bool CanThrow() const OVERRIDE { return true; }
5103
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005104
5105 DECLARE_INSTRUCTION(Throw);
5106
5107 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005108 DISALLOW_COPY_AND_ASSIGN(HThrow);
5109};
5110
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005111/**
5112 * Implementation strategies for the code generator of a HInstanceOf
5113 * or `HCheckCast`.
5114 */
5115enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005116 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005117 kExactCheck, // Can do a single class compare.
5118 kClassHierarchyCheck, // Can just walk the super class chain.
5119 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5120 kInterfaceCheck, // No optimization yet when checking against an interface.
5121 kArrayObjectCheck, // Can just check if the array is not primitive.
5122 kArrayCheck // No optimization yet when checking against a generic array.
5123};
5124
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005125class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005126 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005127 HInstanceOf(HInstruction* object,
5128 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005129 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005130 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005131 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005132 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005133 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005134 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005135 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005136 SetRawInputAt(0, object);
5137 SetRawInputAt(1, constant);
5138 }
5139
5140 bool CanBeMoved() const OVERRIDE { return true; }
5141
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005142 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005143 return true;
5144 }
5145
5146 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005147 return false;
5148 }
5149
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005150 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5151
5152 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005153
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005154 // Used only in code generation.
5155 bool MustDoNullCheck() const { return must_do_null_check_; }
5156 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5157
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005158 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5159 return (check_kind == TypeCheckKind::kExactCheck)
5160 ? SideEffects::None()
5161 // Mips currently does runtime calls for any other checks.
5162 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005163 }
5164
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005165 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005166
5167 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005168 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005169 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005170
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005171 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5172};
5173
Calin Juravleb1498f62015-02-16 13:13:29 +00005174class HBoundType : public HExpression<1> {
5175 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005176 // Constructs an HBoundType with the given upper_bound.
5177 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005178 HBoundType(HInstruction* input,
5179 ReferenceTypeInfo upper_bound,
5180 bool upper_can_be_null,
5181 uint32_t dex_pc = kNoDexPc)
5182 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005183 upper_bound_(upper_bound),
5184 upper_can_be_null_(upper_can_be_null),
5185 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005186 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005187 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005188 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005189 }
5190
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005191 // GetUpper* should only be used in reference type propagation.
5192 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5193 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005194
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005195 void SetCanBeNull(bool can_be_null) {
5196 DCHECK(upper_can_be_null_ || !can_be_null);
5197 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005198 }
5199
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005200 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5201
Calin Juravleb1498f62015-02-16 13:13:29 +00005202 DECLARE_INSTRUCTION(BoundType);
5203
5204 private:
5205 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005206 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5207 // It is used to bound the type in cases like:
5208 // if (x instanceof ClassX) {
5209 // // uper_bound_ will be ClassX
5210 // }
5211 const ReferenceTypeInfo upper_bound_;
5212 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5213 // is false then can_be_null_ cannot be true).
5214 const bool upper_can_be_null_;
5215 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005216
5217 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5218};
5219
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005220class HCheckCast : public HTemplateInstruction<2> {
5221 public:
5222 HCheckCast(HInstruction* object,
5223 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005224 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005225 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005226 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005227 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005228 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005229 SetRawInputAt(0, object);
5230 SetRawInputAt(1, constant);
5231 }
5232
5233 bool CanBeMoved() const OVERRIDE { return true; }
5234
5235 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5236 return true;
5237 }
5238
5239 bool NeedsEnvironment() const OVERRIDE {
5240 // Instruction may throw a CheckCastError.
5241 return true;
5242 }
5243
5244 bool CanThrow() const OVERRIDE { return true; }
5245
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005246 bool MustDoNullCheck() const { return must_do_null_check_; }
5247 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005248 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005249
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005250 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005251
5252 DECLARE_INSTRUCTION(CheckCast);
5253
5254 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005255 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005256 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005257
5258 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005259};
5260
Calin Juravle27df7582015-04-17 19:12:31 +01005261class HMemoryBarrier : public HTemplateInstruction<0> {
5262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005263 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005264 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005265 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005266 barrier_kind_(barrier_kind) {}
5267
5268 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5269
5270 DECLARE_INSTRUCTION(MemoryBarrier);
5271
5272 private:
5273 const MemBarrierKind barrier_kind_;
5274
5275 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5276};
5277
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005278class HMonitorOperation : public HTemplateInstruction<1> {
5279 public:
5280 enum OperationKind {
5281 kEnter,
5282 kExit,
5283 };
5284
5285 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005286 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005287 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5288 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005289 SetRawInputAt(0, object);
5290 }
5291
5292 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005293 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5294
5295 bool CanThrow() const OVERRIDE {
5296 // Verifier guarantees that monitor-exit cannot throw.
5297 // This is important because it allows the HGraphBuilder to remove
5298 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5299 return IsEnter();
5300 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005301
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005302
5303 bool IsEnter() const { return kind_ == kEnter; }
5304
5305 DECLARE_INSTRUCTION(MonitorOperation);
5306
Calin Juravle52c48962014-12-16 17:02:57 +00005307 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005308 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005309
5310 private:
5311 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5312};
5313
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005314/**
5315 * A HInstruction used as a marker for the replacement of new + <init>
5316 * of a String to a call to a StringFactory. Only baseline will see
5317 * the node at code generation, where it will be be treated as null.
5318 * When compiling non-baseline, `HFakeString` instructions are being removed
5319 * in the instruction simplifier.
5320 */
5321class HFakeString : public HTemplateInstruction<0> {
5322 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005323 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5324 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005325
5326 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5327
5328 DECLARE_INSTRUCTION(FakeString);
5329
5330 private:
5331 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5332};
5333
Vladimir Markof9f64412015-09-02 14:05:49 +01005334class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005335 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005336 MoveOperands(Location source,
5337 Location destination,
5338 Primitive::Type type,
5339 HInstruction* instruction)
5340 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005341
5342 Location GetSource() const { return source_; }
5343 Location GetDestination() const { return destination_; }
5344
5345 void SetSource(Location value) { source_ = value; }
5346 void SetDestination(Location value) { destination_ = value; }
5347
5348 // The parallel move resolver marks moves as "in-progress" by clearing the
5349 // destination (but not the source).
5350 Location MarkPending() {
5351 DCHECK(!IsPending());
5352 Location dest = destination_;
5353 destination_ = Location::NoLocation();
5354 return dest;
5355 }
5356
5357 void ClearPending(Location dest) {
5358 DCHECK(IsPending());
5359 destination_ = dest;
5360 }
5361
5362 bool IsPending() const {
5363 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5364 return destination_.IsInvalid() && !source_.IsInvalid();
5365 }
5366
5367 // True if this blocks a move from the given location.
5368 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005369 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005370 }
5371
5372 // A move is redundant if it's been eliminated, if its source and
5373 // destination are the same, or if its destination is unneeded.
5374 bool IsRedundant() const {
5375 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5376 }
5377
5378 // We clear both operands to indicate move that's been eliminated.
5379 void Eliminate() {
5380 source_ = destination_ = Location::NoLocation();
5381 }
5382
5383 bool IsEliminated() const {
5384 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5385 return source_.IsInvalid();
5386 }
5387
Alexey Frunze4dda3372015-06-01 18:31:49 -07005388 Primitive::Type GetType() const { return type_; }
5389
Nicolas Geoffray90218252015-04-15 11:56:51 +01005390 bool Is64BitMove() const {
5391 return Primitive::Is64BitType(type_);
5392 }
5393
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005394 HInstruction* GetInstruction() const { return instruction_; }
5395
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005396 private:
5397 Location source_;
5398 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005399 // The type this move is for.
5400 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005401 // The instruction this move is assocatied with. Null when this move is
5402 // for moving an input in the expected locations of user (including a phi user).
5403 // This is only used in debug mode, to ensure we do not connect interval siblings
5404 // in the same parallel move.
5405 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005406};
5407
5408static constexpr size_t kDefaultNumberOfMoves = 4;
5409
5410class HParallelMove : public HTemplateInstruction<0> {
5411 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005412 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005413 : HTemplateInstruction(SideEffects::None(), dex_pc),
5414 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5415 moves_.reserve(kDefaultNumberOfMoves);
5416 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005417
Nicolas Geoffray90218252015-04-15 11:56:51 +01005418 void AddMove(Location source,
5419 Location destination,
5420 Primitive::Type type,
5421 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005422 DCHECK(source.IsValid());
5423 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005424 if (kIsDebugBuild) {
5425 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005426 for (const MoveOperands& move : moves_) {
5427 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005428 // Special case the situation where the move is for the spill slot
5429 // of the instruction.
5430 if ((GetPrevious() == instruction)
5431 || ((GetPrevious() == nullptr)
5432 && instruction->IsPhi()
5433 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005434 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005435 << "Doing parallel moves for the same instruction.";
5436 } else {
5437 DCHECK(false) << "Doing parallel moves for the same instruction.";
5438 }
5439 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005440 }
5441 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005442 for (const MoveOperands& move : moves_) {
5443 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005444 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005445 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005446 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005447 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005448 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005449 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005450 }
5451
Vladimir Marko225b6462015-09-28 12:17:40 +01005452 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005453 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005454 }
5455
Vladimir Marko225b6462015-09-28 12:17:40 +01005456 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005457
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005458 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005459
5460 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005461 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005462
5463 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5464};
5465
Mark Mendell0616ae02015-04-17 12:49:27 -04005466} // namespace art
5467
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005468#ifdef ART_ENABLE_CODEGEN_arm64
5469#include "nodes_arm64.h"
5470#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005471#ifdef ART_ENABLE_CODEGEN_x86
5472#include "nodes_x86.h"
5473#endif
5474
5475namespace art {
5476
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005477class HGraphVisitor : public ValueObject {
5478 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005479 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5480 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005481
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005482 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005483 virtual void VisitBasicBlock(HBasicBlock* block);
5484
Roland Levillain633021e2014-10-01 14:12:25 +01005485 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005486 void VisitInsertionOrder();
5487
Roland Levillain633021e2014-10-01 14:12:25 +01005488 // Visit the graph following dominator tree reverse post-order.
5489 void VisitReversePostOrder();
5490
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005491 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005492
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005493 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005494#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005495 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5496
5497 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5498
5499#undef DECLARE_VISIT_INSTRUCTION
5500
5501 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005502 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005503
5504 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5505};
5506
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005507class HGraphDelegateVisitor : public HGraphVisitor {
5508 public:
5509 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5510 virtual ~HGraphDelegateVisitor() {}
5511
5512 // Visit functions that delegate to to super class.
5513#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005514 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005515
5516 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5517
5518#undef DECLARE_VISIT_INSTRUCTION
5519
5520 private:
5521 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5522};
5523
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005524class HInsertionOrderIterator : public ValueObject {
5525 public:
5526 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5527
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005528 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005529 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005530 void Advance() { ++index_; }
5531
5532 private:
5533 const HGraph& graph_;
5534 size_t index_;
5535
5536 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5537};
5538
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005539class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005540 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005541 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5542 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005543 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005544 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005545
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005546 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5547 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005548 void Advance() { ++index_; }
5549
5550 private:
5551 const HGraph& graph_;
5552 size_t index_;
5553
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005554 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005555};
5556
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005557class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005558 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005559 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005560 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005561 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005562 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005563 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005564
5565 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005566 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005567 void Advance() { --index_; }
5568
5569 private:
5570 const HGraph& graph_;
5571 size_t index_;
5572
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005573 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005574};
5575
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005576class HLinearPostOrderIterator : public ValueObject {
5577 public:
5578 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005579 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005580
5581 bool Done() const { return index_ == 0; }
5582
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005583 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005584
5585 void Advance() {
5586 --index_;
5587 DCHECK_GE(index_, 0U);
5588 }
5589
5590 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005591 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005592 size_t index_;
5593
5594 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5595};
5596
5597class HLinearOrderIterator : public ValueObject {
5598 public:
5599 explicit HLinearOrderIterator(const HGraph& graph)
5600 : order_(graph.GetLinearOrder()), index_(0) {}
5601
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005602 bool Done() const { return index_ == order_.size(); }
5603 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005604 void Advance() { ++index_; }
5605
5606 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005607 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005608 size_t index_;
5609
5610 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5611};
5612
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005613// Iterator over the blocks that art part of the loop. Includes blocks part
5614// of an inner loop. The order in which the blocks are iterated is on their
5615// block id.
5616class HBlocksInLoopIterator : public ValueObject {
5617 public:
5618 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5619 : blocks_in_loop_(info.GetBlocks()),
5620 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5621 index_(0) {
5622 if (!blocks_in_loop_.IsBitSet(index_)) {
5623 Advance();
5624 }
5625 }
5626
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005627 bool Done() const { return index_ == blocks_.size(); }
5628 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005629 void Advance() {
5630 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005631 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005632 if (blocks_in_loop_.IsBitSet(index_)) {
5633 break;
5634 }
5635 }
5636 }
5637
5638 private:
5639 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005640 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005641 size_t index_;
5642
5643 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5644};
5645
Mingyao Yang3584bce2015-05-19 16:01:59 -07005646// Iterator over the blocks that art part of the loop. Includes blocks part
5647// of an inner loop. The order in which the blocks are iterated is reverse
5648// post order.
5649class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5650 public:
5651 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5652 : blocks_in_loop_(info.GetBlocks()),
5653 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5654 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005655 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005656 Advance();
5657 }
5658 }
5659
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005660 bool Done() const { return index_ == blocks_.size(); }
5661 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005662 void Advance() {
5663 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005664 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5665 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005666 break;
5667 }
5668 }
5669 }
5670
5671 private:
5672 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005673 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005674 size_t index_;
5675
5676 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5677};
5678
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005679inline int64_t Int64FromConstant(HConstant* constant) {
5680 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5681 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5682 : constant->AsLongConstant()->GetValue();
5683}
5684
Vladimir Marko58155012015-08-19 12:49:41 +00005685inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5686 // For the purposes of the compiler, the dex files must actually be the same object
5687 // if we want to safely treat them as the same. This is especially important for JIT
5688 // as custom class loaders can open the same underlying file (or memory) multiple
5689 // times and provide different class resolution but no two class loaders should ever
5690 // use the same DexFile object - doing so is an unsupported hack that can lead to
5691 // all sorts of weird failures.
5692 return &lhs == &rhs;
5693}
5694
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005695} // namespace art
5696
5697#endif // ART_COMPILER_OPTIMIZING_NODES_H_