blob: 4c4d0f268d28273bc277257e46b638c41ff6d1d4 [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 Geoffray0a23d742015-05-07 11:57:35 +0100353 const DexFile& GetDexFile() const {
354 return dex_file_;
355 }
356
357 uint32_t GetMethodIdx() const {
358 return method_idx_;
359 }
360
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100361 InvokeType GetInvokeType() const {
362 return invoke_type_;
363 }
364
Mark Mendellc4701932015-04-10 13:18:51 -0400365 InstructionSet GetInstructionSet() const {
366 return instruction_set_;
367 }
368
David Brazdil77a48ae2015-09-15 12:34:04 +0000369 bool HasTryCatch() const { return has_try_catch_; }
370 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100371
David Brazdil2d7352b2015-04-20 14:52:42 +0100372 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100373 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000374 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100375 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000376
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000377 template <class InstructionType, typename ValueType>
378 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600379 ArenaSafeMap<ValueType, InstructionType*>* cache,
380 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000381 // Try to find an existing constant of the given value.
382 InstructionType* constant = nullptr;
383 auto cached_constant = cache->find(value);
384 if (cached_constant != cache->end()) {
385 constant = cached_constant->second;
386 }
387
388 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100389 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000390 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600391 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000392 cache->Overwrite(value, constant);
393 InsertConstant(constant);
394 }
395 return constant;
396 }
397
David Brazdil8d5b8b22015-03-24 10:51:52 +0000398 void InsertConstant(HConstant* instruction);
399
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000400 // Cache a float constant into the graph. This method should only be
401 // called by the SsaBuilder when creating "equivalent" instructions.
402 void CacheFloatConstant(HFloatConstant* constant);
403
404 // See CacheFloatConstant comment.
405 void CacheDoubleConstant(HDoubleConstant* constant);
406
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000407 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000408
409 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100410 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000411
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100412 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100413 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000414
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100415 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100416 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100417
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000418 HBasicBlock* entry_block_;
419 HBasicBlock* exit_block_;
420
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100421 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100422 uint16_t maximum_number_of_out_vregs_;
423
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100424 // The number of virtual registers in this method. Contains the parameters.
425 uint16_t number_of_vregs_;
426
427 // The number of virtual registers used by parameters of this method.
428 uint16_t number_of_in_vregs_;
429
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000430 // Number of vreg size slots that the temporaries use (used in baseline compiler).
431 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100432
Mark Mendell1152c922015-04-24 17:06:35 -0400433 // Has bounds checks. We can totally skip BCE if it's false.
434 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800435
David Brazdil77a48ae2015-09-15 12:34:04 +0000436 // Flag whether there are any try/catch blocks in the graph. We will skip
437 // try/catch-related passes if false.
438 bool has_try_catch_;
439
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000440 // Indicates whether the graph should be compiled in a way that
441 // ensures full debuggability. If false, we can apply more
442 // aggressive optimizations that may limit the level of debugging.
443 const bool debuggable_;
444
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000445 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000447
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100448 // The dex file from which the method is from.
449 const DexFile& dex_file_;
450
451 // The method index in the dex file.
452 const uint32_t method_idx_;
453
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100454 // If inlined, this encodes how the callee is being invoked.
455 const InvokeType invoke_type_;
456
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100457 // Whether the graph has been transformed to SSA form. Only used
458 // in debug mode to ensure we are not using properties only valid
459 // for non-SSA form (like the number of temporaries).
460 bool in_ssa_form_;
461
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100462 const bool should_generate_constructor_barrier_;
463
Mathieu Chartiere401d142015-04-22 13:56:20 -0700464 const InstructionSet instruction_set_;
465
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000466 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000467 HNullConstant* cached_null_constant_;
468 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000469 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000470 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000471 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000472
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100473 HCurrentMethod* cached_current_method_;
474
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000475 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100476 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000477 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000478 DISALLOW_COPY_AND_ASSIGN(HGraph);
479};
480
Vladimir Markof9f64412015-09-02 14:05:49 +0100481class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000482 public:
483 HLoopInformation(HBasicBlock* header, HGraph* graph)
484 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100485 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100486 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100487 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100488 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
489 back_edges_.reserve(kDefaultNumberOfBackEdges);
490 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100491
492 HBasicBlock* GetHeader() const {
493 return header_;
494 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000495
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000496 void SetHeader(HBasicBlock* block) {
497 header_ = block;
498 }
499
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100500 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
501 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
502 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
503
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000504 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100505 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000506 }
507
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100508 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100509 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100510 }
511
David Brazdil46e2a392015-03-16 17:31:52 +0000512 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100513 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100514 }
515
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000516 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100517 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000518 }
519
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100520 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100521
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100522 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100523 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100524 }
525
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100526 // Returns the lifetime position of the back edge that has the
527 // greatest lifetime position.
528 size_t GetLifetimeEnd() const;
529
530 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100531 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100532 }
533
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100534 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100535 // that is the header dominates the back edge.
536 bool Populate();
537
David Brazdila4b8c212015-05-07 09:59:30 +0100538 // Reanalyzes the loop by removing loop info from its blocks and re-running
539 // Populate(). If there are no back edges left, the loop info is completely
540 // removed as well as its SuspendCheck instruction. It must be run on nested
541 // inner loops first.
542 void Update();
543
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100544 // Returns whether this loop information contains `block`.
545 // Note that this loop information *must* be populated before entering this function.
546 bool Contains(const HBasicBlock& block) const;
547
548 // Returns whether this loop information is an inner loop of `other`.
549 // Note that `other` *must* be populated before entering this function.
550 bool IsIn(const HLoopInformation& other) const;
551
Aart Bik73f1f3b2015-10-28 15:28:08 -0700552 // Returns true if instruction is not defined within this loop or any loop nested inside
553 // this loop. If must_dominate is set, only definitions that actually dominate the loop
554 // header can be invariant. Otherwise, any definition outside the loop, including
555 // definitions that appear after the loop, is invariant.
556 bool IsLoopInvariant(HInstruction* instruction, bool must_dominate) const;
557
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100558 const ArenaBitVector& GetBlocks() const { return blocks_; }
559
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000560 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000561 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000562
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100564 // Internal recursive implementation of `Populate`.
565 void PopulateRecursive(HBasicBlock* block);
566
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100568 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100569 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100570 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000571
572 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
573};
574
David Brazdilec16f792015-08-19 15:04:01 +0100575// Stores try/catch information for basic blocks.
576// Note that HGraph is constructed so that catch blocks cannot simultaneously
577// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100578class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100579 public:
580 // Try block information constructor.
581 explicit TryCatchInformation(const HTryBoundary& try_entry)
582 : try_entry_(&try_entry),
583 catch_dex_file_(nullptr),
584 catch_type_index_(DexFile::kDexNoIndex16) {
585 DCHECK(try_entry_ != nullptr);
586 }
587
588 // Catch block information constructor.
589 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
590 : try_entry_(nullptr),
591 catch_dex_file_(&dex_file),
592 catch_type_index_(catch_type_index) {}
593
594 bool IsTryBlock() const { return try_entry_ != nullptr; }
595
596 const HTryBoundary& GetTryEntry() const {
597 DCHECK(IsTryBlock());
598 return *try_entry_;
599 }
600
601 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
602
603 bool IsCatchAllTypeIndex() const {
604 DCHECK(IsCatchBlock());
605 return catch_type_index_ == DexFile::kDexNoIndex16;
606 }
607
608 uint16_t GetCatchTypeIndex() const {
609 DCHECK(IsCatchBlock());
610 return catch_type_index_;
611 }
612
613 const DexFile& GetCatchDexFile() const {
614 DCHECK(IsCatchBlock());
615 return *catch_dex_file_;
616 }
617
618 private:
619 // One of possibly several TryBoundary instructions entering the block's try.
620 // Only set for try blocks.
621 const HTryBoundary* try_entry_;
622
623 // Exception type information. Only set for catch blocks.
624 const DexFile* catch_dex_file_;
625 const uint16_t catch_type_index_;
626};
627
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100628static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100629static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100630
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000631// A block in a method. Contains the list of instructions represented
632// as a double linked list. Each block knows its predecessors and
633// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100634
Vladimir Markof9f64412015-09-02 14:05:49 +0100635class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000636 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600637 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000639 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
640 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000641 loop_information_(nullptr),
642 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000643 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100644 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100645 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100646 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000647 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000648 try_catch_information_(nullptr) {
649 predecessors_.reserve(kDefaultNumberOfPredecessors);
650 successors_.reserve(kDefaultNumberOfSuccessors);
651 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
652 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000653
Vladimir Marko60584552015-09-03 13:35:12 +0000654 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100655 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656 }
657
Vladimir Marko60584552015-09-03 13:35:12 +0000658 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100659 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660 }
661
Vladimir Marko60584552015-09-03 13:35:12 +0000662 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
663 return ContainsElement(successors_, block, start_from);
664 }
665
666 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100667 return dominated_blocks_;
668 }
669
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100670 bool IsEntryBlock() const {
671 return graph_->GetEntryBlock() == this;
672 }
673
674 bool IsExitBlock() const {
675 return graph_->GetExitBlock() == this;
676 }
677
David Brazdil46e2a392015-03-16 17:31:52 +0000678 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000679 bool IsSingleTryBoundary() const;
680
681 // Returns true if this block emits nothing but a jump.
682 bool IsSingleJump() const {
683 HLoopInformation* loop_info = GetLoopInformation();
684 return (IsSingleGoto() || IsSingleTryBoundary())
685 // Back edges generate a suspend check.
686 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
687 }
David Brazdil46e2a392015-03-16 17:31:52 +0000688
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000689 void AddBackEdge(HBasicBlock* back_edge) {
690 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000691 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000692 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100693 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000694 loop_information_->AddBackEdge(back_edge);
695 }
696
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000697 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000698 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000699
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100700 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000701 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600702 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000703
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000704 HBasicBlock* GetDominator() const { return dominator_; }
705 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000706 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
707
708 void RemoveDominatedBlock(HBasicBlock* block) {
709 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100710 }
Vladimir Marko60584552015-09-03 13:35:12 +0000711
712 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
713 ReplaceElement(dominated_blocks_, existing, new_block);
714 }
715
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100716 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000717
718 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100719 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000720 }
721
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100722 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
723 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100724 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100725 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100726 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
727 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000728
729 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000730 successors_.push_back(block);
731 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000732 }
733
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100734 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
735 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100736 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000737 new_block->predecessors_.push_back(this);
738 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000739 }
740
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000741 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
742 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000743 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000744 new_block->successors_.push_back(this);
745 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000746 }
747
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100748 // Insert `this` between `predecessor` and `successor. This method
749 // preserves the indicies, and will update the first edge found between
750 // `predecessor` and `successor`.
751 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
752 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100753 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000754 successor->predecessors_[predecessor_index] = this;
755 predecessor->successors_[successor_index] = this;
756 successors_.push_back(successor);
757 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100758 }
759
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100760 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000761 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100762 }
763
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000764 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000765 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000766 }
767
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100768 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000769 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100770 }
771
772 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000773 predecessors_.push_back(block);
774 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100775 }
776
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100777 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000778 DCHECK_EQ(predecessors_.size(), 2u);
779 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100780 }
781
David Brazdil769c9e52015-04-27 13:54:09 +0100782 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000783 DCHECK_EQ(successors_.size(), 2u);
784 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100785 }
786
David Brazdilfc6a86a2015-06-26 10:33:45 +0000787 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100789 }
790
David Brazdilfc6a86a2015-06-26 10:33:45 +0000791 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000792 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100793 }
794
David Brazdilfc6a86a2015-06-26 10:33:45 +0000795 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000796 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100797 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000798 }
799
800 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000801 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100802 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000803 }
804
805 // Returns whether the first occurrence of `predecessor` in the list of
806 // predecessors is at index `idx`.
807 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100808 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000809 return GetPredecessorIndexOf(predecessor) == idx;
810 }
811
David Brazdilffee3d32015-07-06 11:48:53 +0100812 // Returns the number of non-exceptional successors. SsaChecker ensures that
813 // these are stored at the beginning of the successor list.
814 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000815 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100816 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000817
David Brazdild7558da2015-09-22 13:04:14 +0100818 // Create a new block between this block and its predecessors. The new block
819 // is added to the graph, all predecessor edges are relinked to it and an edge
820 // is created to `this`. Returns the new empty block. Reverse post order or
821 // loop and try/catch information are not updated.
822 HBasicBlock* CreateImmediateDominator();
823
David Brazdilfc6a86a2015-06-26 10:33:45 +0000824 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100825 // created, latter block. Note that this method will add the block to the
826 // graph, create a Goto at the end of the former block and will create an edge
827 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100828 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000829 HBasicBlock* SplitBefore(HInstruction* cursor);
830
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000831 // Split the block into two blocks just after `cursor`. Returns the newly
832 // created block. Note that this method just updates raw block information,
833 // like predecessors, successors, dominators, and instruction list. It does not
834 // update the graph, reverse post order, loop information, nor make sure the
835 // blocks are consistent (for example ending with a control flow instruction).
836 HBasicBlock* SplitAfter(HInstruction* cursor);
837
David Brazdil9bc43612015-11-05 21:25:24 +0000838 // Split catch block into two blocks after the original move-exception bytecode
839 // instruction, or at the beginning if not present. Returns the newly created,
840 // latter block, or nullptr if such block could not be created (must be dead
841 // in that case). Note that this method just updates raw block information,
842 // like predecessors, successors, dominators, and instruction list. It does not
843 // update the graph, reverse post order, loop information, nor make sure the
844 // blocks are consistent (for example ending with a control flow instruction).
845 HBasicBlock* SplitCatchBlockAfterMoveException();
846
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000847 // Merge `other` at the end of `this`. Successors and dominated blocks of
848 // `other` are changed to be successors and dominated blocks of `this`. Note
849 // that this method does not update the graph, reverse post order, loop
850 // information, nor make sure the blocks are consistent (for example ending
851 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100852 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000853
854 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
855 // of `this` are moved to `other`.
856 // Note that this method does not update the graph, reverse post order, loop
857 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000858 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000859 void ReplaceWith(HBasicBlock* other);
860
David Brazdil2d7352b2015-04-20 14:52:42 +0100861 // Merge `other` at the end of `this`. This method updates loops, reverse post
862 // order, links to predecessors, successors, dominators and deletes the block
863 // from the graph. The two blocks must be successive, i.e. `this` the only
864 // predecessor of `other` and vice versa.
865 void MergeWith(HBasicBlock* other);
866
867 // Disconnects `this` from all its predecessors, successors and dominator,
868 // removes it from all loops it is included in and eventually from the graph.
869 // The block must not dominate any other block. Predecessors and successors
870 // are safely updated.
871 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000872
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000873 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100874 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100875 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100876 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100877 // Replace instruction `initial` with `replacement` within this block.
878 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
879 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100880 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100881 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000882 // RemoveInstruction and RemovePhi delete a given instruction from the respective
883 // instruction list. With 'ensure_safety' set to true, it verifies that the
884 // instruction is not in use and removes it from the use lists of its inputs.
885 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
886 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100887 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100888
889 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100890 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100891 }
892
Roland Levillain6b879dd2014-09-22 17:13:44 +0100893 bool IsLoopPreHeaderFirstPredecessor() const {
894 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100895 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100896 }
897
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100898 HLoopInformation* GetLoopInformation() const {
899 return loop_information_;
900 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000901
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000902 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100903 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000904 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100905 void SetInLoop(HLoopInformation* info) {
906 if (IsLoopHeader()) {
907 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100908 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100909 loop_information_ = info;
910 } else if (loop_information_->Contains(*info->GetHeader())) {
911 // Block is currently part of an outer loop. Make it part of this inner loop.
912 // Note that a non loop header having a loop information means this loop information
913 // has already been populated
914 loop_information_ = info;
915 } else {
916 // Block is part of an inner loop. Do not update the loop information.
917 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
918 // at this point, because this method is being called while populating `info`.
919 }
920 }
921
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000922 // Raw update of the loop information.
923 void SetLoopInformation(HLoopInformation* info) {
924 loop_information_ = info;
925 }
926
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100927 bool IsInLoop() const { return loop_information_ != nullptr; }
928
David Brazdilec16f792015-08-19 15:04:01 +0100929 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
930
931 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
932 try_catch_information_ = try_catch_information;
933 }
934
935 bool IsTryBlock() const {
936 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
937 }
938
939 bool IsCatchBlock() const {
940 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
941 }
David Brazdilffee3d32015-07-06 11:48:53 +0100942
943 // Returns the try entry that this block's successors should have. They will
944 // be in the same try, unless the block ends in a try boundary. In that case,
945 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100946 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100947
David Brazdild7558da2015-09-22 13:04:14 +0100948 bool HasThrowingInstructions() const;
949
David Brazdila4b8c212015-05-07 09:59:30 +0100950 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100951 bool Dominates(HBasicBlock* block) const;
952
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100953 size_t GetLifetimeStart() const { return lifetime_start_; }
954 size_t GetLifetimeEnd() const { return lifetime_end_; }
955
956 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
957 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
958
David Brazdil8d5b8b22015-03-24 10:51:52 +0000959 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000960 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100961 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000962 bool HasSinglePhi() const;
963
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000965 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000966 ArenaVector<HBasicBlock*> predecessors_;
967 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100968 HInstructionList instructions_;
969 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000970 HLoopInformation* loop_information_;
971 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000972 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100973 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100974 // The dex program counter of the first instruction of this block.
975 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100976 size_t lifetime_start_;
977 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100978 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100979
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000980 friend class HGraph;
981 friend class HInstruction;
982
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
984};
985
David Brazdilb2bd1c52015-03-25 11:17:37 +0000986// Iterates over the LoopInformation of all loops which contain 'block'
987// from the innermost to the outermost.
988class HLoopInformationOutwardIterator : public ValueObject {
989 public:
990 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
991 : current_(block.GetLoopInformation()) {}
992
993 bool Done() const { return current_ == nullptr; }
994
995 void Advance() {
996 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100997 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000998 }
999
1000 HLoopInformation* Current() const {
1001 DCHECK(!Done());
1002 return current_;
1003 }
1004
1005 private:
1006 HLoopInformation* current_;
1007
1008 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1009};
1010
Alexandre Ramesef20f712015-06-09 10:29:30 +01001011#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001012 M(Above, Condition) \
1013 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001014 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001015 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001016 M(ArrayGet, Instruction) \
1017 M(ArrayLength, Instruction) \
1018 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001019 M(Below, Condition) \
1020 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001021 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001022 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001023 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001024 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001025 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001026 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001027 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001028 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001029 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001030 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001031 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001032 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001033 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001034 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001035 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001036 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001037 M(FloatConstant, Constant) \
1038 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001039 M(GreaterThan, Condition) \
1040 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001041 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001042 M(InstanceFieldGet, Instruction) \
1043 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001044 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001045 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001046 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001047 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001048 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001050 M(LessThan, Condition) \
1051 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001052 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001053 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001054 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001055 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001056 M(Local, Instruction) \
1057 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001058 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001059 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001060 M(Mul, BinaryOperation) \
1061 M(Neg, UnaryOperation) \
1062 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001063 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001064 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001065 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001066 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001067 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001068 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001069 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001070 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001071 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001072 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001073 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001074 M(Return, Instruction) \
1075 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001076 M(Shl, BinaryOperation) \
1077 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001078 M(StaticFieldGet, Instruction) \
1079 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001080 M(UnresolvedInstanceFieldGet, Instruction) \
1081 M(UnresolvedInstanceFieldSet, Instruction) \
1082 M(UnresolvedStaticFieldGet, Instruction) \
1083 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001084 M(StoreLocal, Instruction) \
1085 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001086 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001087 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001088 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001089 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001090 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001091 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001092 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001093
Alexandre Ramesef20f712015-06-09 10:29:30 +01001094#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1095
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001096#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001097#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001098#else
1099#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1100 M(Arm64IntermediateAddress, Instruction)
1101#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001102
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001103#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1104
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001105#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1106
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001107#ifndef ART_ENABLE_CODEGEN_x86
1108#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1109#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001110#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1111 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001112 M(X86LoadFromConstantTable, Instruction) \
1113 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001114#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001115
1116#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1117
1118#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1119 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1120 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1121 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001122 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001123 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001124 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1125 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1126
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001127#define FOR_EACH_INSTRUCTION(M) \
1128 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1129 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001130 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001131 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001132 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001133
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001134#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001135FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1136#undef FORWARD_DECLARATION
1137
Roland Levillainccc07a92014-09-16 14:48:16 +01001138#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001139 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1140 const char* DebugName() const OVERRIDE { return #type; } \
1141 const H##type* As##type() const OVERRIDE { return this; } \
1142 H##type* As##type() OVERRIDE { return this; } \
1143 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001144 return other->Is##type(); \
1145 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001146 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001147
David Brazdiled596192015-01-23 10:39:45 +00001148template <typename T> class HUseList;
1149
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001150template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001151class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001152 public:
David Brazdiled596192015-01-23 10:39:45 +00001153 HUseListNode* GetPrevious() const { return prev_; }
1154 HUseListNode* GetNext() const { return next_; }
1155 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001156 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001157 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001158
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001159 private:
David Brazdiled596192015-01-23 10:39:45 +00001160 HUseListNode(T user, size_t index)
1161 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1162
1163 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001164 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001165 HUseListNode<T>* prev_;
1166 HUseListNode<T>* next_;
1167
1168 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001169
1170 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1171};
1172
David Brazdiled596192015-01-23 10:39:45 +00001173template <typename T>
1174class HUseList : public ValueObject {
1175 public:
1176 HUseList() : first_(nullptr) {}
1177
1178 void Clear() {
1179 first_ = nullptr;
1180 }
1181
1182 // Adds a new entry at the beginning of the use list and returns
1183 // the newly created node.
1184 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001185 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001186 if (IsEmpty()) {
1187 first_ = new_node;
1188 } else {
1189 first_->prev_ = new_node;
1190 new_node->next_ = first_;
1191 first_ = new_node;
1192 }
1193 return new_node;
1194 }
1195
1196 HUseListNode<T>* GetFirst() const {
1197 return first_;
1198 }
1199
1200 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001201 DCHECK(node != nullptr);
1202 DCHECK(Contains(node));
1203
David Brazdiled596192015-01-23 10:39:45 +00001204 if (node->prev_ != nullptr) {
1205 node->prev_->next_ = node->next_;
1206 }
1207 if (node->next_ != nullptr) {
1208 node->next_->prev_ = node->prev_;
1209 }
1210 if (node == first_) {
1211 first_ = node->next_;
1212 }
1213 }
1214
David Brazdil1abb4192015-02-17 18:33:36 +00001215 bool Contains(const HUseListNode<T>* node) const {
1216 if (node == nullptr) {
1217 return false;
1218 }
1219 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1220 if (current == node) {
1221 return true;
1222 }
1223 }
1224 return false;
1225 }
1226
David Brazdiled596192015-01-23 10:39:45 +00001227 bool IsEmpty() const {
1228 return first_ == nullptr;
1229 }
1230
1231 bool HasOnlyOneUse() const {
1232 return first_ != nullptr && first_->next_ == nullptr;
1233 }
1234
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001235 size_t SizeSlow() const {
1236 size_t count = 0;
1237 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1238 ++count;
1239 }
1240 return count;
1241 }
1242
David Brazdiled596192015-01-23 10:39:45 +00001243 private:
1244 HUseListNode<T>* first_;
1245};
1246
1247template<typename T>
1248class HUseIterator : public ValueObject {
1249 public:
1250 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1251
1252 bool Done() const { return current_ == nullptr; }
1253
1254 void Advance() {
1255 DCHECK(!Done());
1256 current_ = current_->GetNext();
1257 }
1258
1259 HUseListNode<T>* Current() const {
1260 DCHECK(!Done());
1261 return current_;
1262 }
1263
1264 private:
1265 HUseListNode<T>* current_;
1266
1267 friend class HValue;
1268};
1269
David Brazdil1abb4192015-02-17 18:33:36 +00001270// This class is used by HEnvironment and HInstruction classes to record the
1271// instructions they use and pointers to the corresponding HUseListNodes kept
1272// by the used instructions.
1273template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001274class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001275 public:
1276 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1277 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1278
1279 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1280 : instruction_(old_record.instruction_), use_node_(use_node) {
1281 DCHECK(instruction_ != nullptr);
1282 DCHECK(use_node_ != nullptr);
1283 DCHECK(old_record.use_node_ == nullptr);
1284 }
1285
1286 HInstruction* GetInstruction() const { return instruction_; }
1287 HUseListNode<T>* GetUseNode() const { return use_node_; }
1288
1289 private:
1290 // Instruction used by the user.
1291 HInstruction* instruction_;
1292
1293 // Corresponding entry in the use list kept by 'instruction_'.
1294 HUseListNode<T>* use_node_;
1295};
1296
Aart Bik854a02b2015-07-14 16:07:00 -07001297/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001298 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001299 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001300 * For write/read dependences on fields/arrays, the dependence analysis uses
1301 * type disambiguation (e.g. a float field write cannot modify the value of an
1302 * integer field read) and the access type (e.g. a reference array write cannot
1303 * modify the value of a reference field read [although it may modify the
1304 * reference fetch prior to reading the field, which is represented by its own
1305 * write/read dependence]). The analysis makes conservative points-to
1306 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1307 * the same, and any reference read depends on any reference read without
1308 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001309 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001310 * The internal representation uses 38-bit and is described in the table below.
1311 * The first line indicates the side effect, and for field/array accesses the
1312 * second line indicates the type of the access (in the order of the
1313 * Primitive::Type enum).
1314 * The two numbered lines below indicate the bit position in the bitfield (read
1315 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001316 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001317 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1318 * +-------------+---------+---------+--------------+---------+---------+
1319 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1320 * | 3 |333333322|222222221| 1 |111111110|000000000|
1321 * | 7 |654321098|765432109| 8 |765432109|876543210|
1322 *
1323 * Note that, to ease the implementation, 'changes' bits are least significant
1324 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001325 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001326class SideEffects : public ValueObject {
1327 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001328 SideEffects() : flags_(0) {}
1329
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001330 static SideEffects None() {
1331 return SideEffects(0);
1332 }
1333
1334 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001335 return SideEffects(kAllChangeBits | kAllDependOnBits);
1336 }
1337
1338 static SideEffects AllChanges() {
1339 return SideEffects(kAllChangeBits);
1340 }
1341
1342 static SideEffects AllDependencies() {
1343 return SideEffects(kAllDependOnBits);
1344 }
1345
1346 static SideEffects AllExceptGCDependency() {
1347 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1348 }
1349
1350 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001351 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001352 }
1353
Aart Bik34c3ba92015-07-20 14:08:59 -07001354 static SideEffects AllWrites() {
1355 return SideEffects(kAllWrites);
1356 }
1357
1358 static SideEffects AllReads() {
1359 return SideEffects(kAllReads);
1360 }
1361
1362 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1363 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001364 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001365 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001366 }
1367
Aart Bik854a02b2015-07-14 16:07:00 -07001368 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1369 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001370 }
1371
Aart Bik34c3ba92015-07-20 14:08:59 -07001372 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1373 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001374 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001375 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001376 }
1377
1378 static SideEffects ArrayReadOfType(Primitive::Type type) {
1379 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1380 }
1381
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001382 static SideEffects CanTriggerGC() {
1383 return SideEffects(1ULL << kCanTriggerGCBit);
1384 }
1385
1386 static SideEffects DependsOnGC() {
1387 return SideEffects(1ULL << kDependsOnGCBit);
1388 }
1389
Aart Bik854a02b2015-07-14 16:07:00 -07001390 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001391 SideEffects Union(SideEffects other) const {
1392 return SideEffects(flags_ | other.flags_);
1393 }
1394
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001395 SideEffects Exclusion(SideEffects other) const {
1396 return SideEffects(flags_ & ~other.flags_);
1397 }
1398
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001399 void Add(SideEffects other) {
1400 flags_ |= other.flags_;
1401 }
1402
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001403 bool Includes(SideEffects other) const {
1404 return (other.flags_ & flags_) == other.flags_;
1405 }
1406
1407 bool HasSideEffects() const {
1408 return (flags_ & kAllChangeBits);
1409 }
1410
1411 bool HasDependencies() const {
1412 return (flags_ & kAllDependOnBits);
1413 }
1414
1415 // Returns true if there are no side effects or dependencies.
1416 bool DoesNothing() const {
1417 return flags_ == 0;
1418 }
1419
Aart Bik854a02b2015-07-14 16:07:00 -07001420 // Returns true if something is written.
1421 bool DoesAnyWrite() const {
1422 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001423 }
1424
Aart Bik854a02b2015-07-14 16:07:00 -07001425 // Returns true if something is read.
1426 bool DoesAnyRead() const {
1427 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001428 }
1429
Aart Bik854a02b2015-07-14 16:07:00 -07001430 // Returns true if potentially everything is written and read
1431 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001432 bool DoesAllReadWrite() const {
1433 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1434 }
1435
Aart Bik854a02b2015-07-14 16:07:00 -07001436 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001437 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001438 }
1439
1440 // Returns true if this may read something written by other.
1441 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001442 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1443 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001444 }
1445
1446 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001448 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001449 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001450 for (int s = kLastBit; s >= 0; s--) {
1451 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1452 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1453 // This is a bit for the GC side effect.
1454 if (current_bit_is_set) {
1455 flags += "GC";
1456 }
Aart Bik854a02b2015-07-14 16:07:00 -07001457 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001458 } else {
1459 // This is a bit for the array/field analysis.
1460 // The underscore character stands for the 'can trigger GC' bit.
1461 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1462 if (current_bit_is_set) {
1463 flags += kDebug[s];
1464 }
1465 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1466 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1467 flags += "|";
1468 }
1469 }
Aart Bik854a02b2015-07-14 16:07:00 -07001470 }
1471 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001472 }
1473
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001474 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001476 private:
1477 static constexpr int kFieldArrayAnalysisBits = 9;
1478
1479 static constexpr int kFieldWriteOffset = 0;
1480 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1481 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1482 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1483
1484 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1485
1486 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1487 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1488 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1489 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1490
1491 static constexpr int kLastBit = kDependsOnGCBit;
1492 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1493
1494 // Aliases.
1495
1496 static_assert(kChangeBits == kDependOnBits,
1497 "the 'change' bits should match the 'depend on' bits.");
1498
1499 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1500 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1501 static constexpr uint64_t kAllWrites =
1502 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1503 static constexpr uint64_t kAllReads =
1504 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001505
Aart Bik854a02b2015-07-14 16:07:00 -07001506 // Work around the fact that HIR aliases I/F and J/D.
1507 // TODO: remove this interceptor once HIR types are clean
1508 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1509 switch (type) {
1510 case Primitive::kPrimInt:
1511 case Primitive::kPrimFloat:
1512 return TypeFlag(Primitive::kPrimInt, offset) |
1513 TypeFlag(Primitive::kPrimFloat, offset);
1514 case Primitive::kPrimLong:
1515 case Primitive::kPrimDouble:
1516 return TypeFlag(Primitive::kPrimLong, offset) |
1517 TypeFlag(Primitive::kPrimDouble, offset);
1518 default:
1519 return TypeFlag(type, offset);
1520 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001521 }
1522
Aart Bik854a02b2015-07-14 16:07:00 -07001523 // Translates type to bit flag.
1524 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1525 CHECK_NE(type, Primitive::kPrimVoid);
1526 const uint64_t one = 1;
1527 const int shift = type; // 0-based consecutive enum
1528 DCHECK_LE(kFieldWriteOffset, shift);
1529 DCHECK_LT(shift, kArrayWriteOffset);
1530 return one << (type + offset);
1531 }
1532
1533 // Private constructor on direct flags value.
1534 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1535
1536 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001537};
1538
David Brazdiled596192015-01-23 10:39:45 +00001539// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001540class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001541 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001542 HEnvironment(ArenaAllocator* arena,
1543 size_t number_of_vregs,
1544 const DexFile& dex_file,
1545 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001546 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001547 InvokeType invoke_type,
1548 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001549 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1550 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001551 parent_(nullptr),
1552 dex_file_(dex_file),
1553 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001554 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001555 invoke_type_(invoke_type),
1556 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001557 }
1558
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001559 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001560 : HEnvironment(arena,
1561 to_copy.Size(),
1562 to_copy.GetDexFile(),
1563 to_copy.GetMethodIdx(),
1564 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001565 to_copy.GetInvokeType(),
1566 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001567
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001568 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001569 if (parent_ != nullptr) {
1570 parent_->SetAndCopyParentChain(allocator, parent);
1571 } else {
1572 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1573 parent_->CopyFrom(parent);
1574 if (parent->GetParent() != nullptr) {
1575 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1576 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001577 }
David Brazdiled596192015-01-23 10:39:45 +00001578 }
1579
Vladimir Marko71bf8092015-09-15 15:33:14 +01001580 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001581 void CopyFrom(HEnvironment* environment);
1582
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001583 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1584 // input to the loop phi instead. This is for inserting instructions that
1585 // require an environment (like HDeoptimization) in the loop pre-header.
1586 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001587
1588 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001589 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001590 }
1591
1592 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001593 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001594 }
1595
David Brazdil1abb4192015-02-17 18:33:36 +00001596 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001597
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001598 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001599
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001600 HEnvironment* GetParent() const { return parent_; }
1601
1602 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001603 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001604 }
1605
1606 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001607 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001608 }
1609
1610 uint32_t GetDexPc() const {
1611 return dex_pc_;
1612 }
1613
1614 uint32_t GetMethodIdx() const {
1615 return method_idx_;
1616 }
1617
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001618 InvokeType GetInvokeType() const {
1619 return invoke_type_;
1620 }
1621
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001622 const DexFile& GetDexFile() const {
1623 return dex_file_;
1624 }
1625
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001626 HInstruction* GetHolder() const {
1627 return holder_;
1628 }
1629
David Brazdiled596192015-01-23 10:39:45 +00001630 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001631 // Record instructions' use entries of this environment for constant-time removal.
1632 // It should only be called by HInstruction when a new environment use is added.
1633 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1634 DCHECK(env_use->GetUser() == this);
1635 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001636 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001637 }
David Brazdiled596192015-01-23 10:39:45 +00001638
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001639 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1640 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001641 HEnvironment* parent_;
1642 const DexFile& dex_file_;
1643 const uint32_t method_idx_;
1644 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001645 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001646
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001647 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001648 HInstruction* const holder_;
1649
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001650 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001651
1652 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1653};
1654
Calin Juravleacf735c2015-02-12 15:25:22 +00001655class ReferenceTypeInfo : ValueObject {
1656 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001657 typedef Handle<mirror::Class> TypeHandle;
1658
Calin Juravle2e768302015-07-28 14:41:11 +00001659 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1660 // The constructor will check that the type_handle is valid.
1661 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001662 }
1663
Calin Juravle2e768302015-07-28 14:41:11 +00001664 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1665
1666 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1667 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001668 }
1669
Calin Juravle2e768302015-07-28 14:41:11 +00001670 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1671 return IsValidHandle(type_handle_);
1672 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001673
Calin Juravleacf735c2015-02-12 15:25:22 +00001674 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001675
1676 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1677 DCHECK(IsValid());
1678 return GetTypeHandle()->IsObjectClass();
1679 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001680
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001681 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001682 DCHECK(IsValid());
1683 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001684 }
1685
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001686 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1687 DCHECK(IsValid());
1688 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1689 }
1690
Mathieu Chartier90443472015-07-16 20:32:27 -07001691 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001692 DCHECK(IsValid());
1693 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001694 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001695
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001696 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001697 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001698 return GetTypeHandle()->IsArrayClass();
1699 }
1700
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001701 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1702 DCHECK(IsValid());
1703 return GetTypeHandle()->IsPrimitiveArray();
1704 }
1705
1706 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1707 DCHECK(IsValid());
1708 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1709 }
1710
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001711 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001712 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001713 if (!IsExact()) return false;
1714 if (!IsArrayClass()) return false;
1715 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1716 }
1717
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001718 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1719 DCHECK(IsValid());
1720 if (!IsExact()) return false;
1721 if (!IsArrayClass()) return false;
1722 if (!rti.IsArrayClass()) return false;
1723 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1724 rti.GetTypeHandle()->GetComponentType());
1725 }
1726
Calin Juravleacf735c2015-02-12 15:25:22 +00001727 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1728
Mathieu Chartier90443472015-07-16 20:32:27 -07001729 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001730 DCHECK(IsValid());
1731 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001732 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1733 }
1734
1735 // Returns true if the type information provide the same amount of details.
1736 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001737 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001738 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001739 if (!IsValid() && !rti.IsValid()) {
1740 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001741 return true;
1742 }
Calin Juravle2e768302015-07-28 14:41:11 +00001743 if (!IsValid() || !rti.IsValid()) {
1744 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001745 return false;
1746 }
Calin Juravle2e768302015-07-28 14:41:11 +00001747 return IsExact() == rti.IsExact()
1748 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001749 }
1750
1751 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001752 ReferenceTypeInfo();
1753 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001754
Calin Juravleacf735c2015-02-12 15:25:22 +00001755 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001756 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001757 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001758 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001759 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001760};
1761
1762std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1763
Vladimir Markof9f64412015-09-02 14:05:49 +01001764class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001765 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001766 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001767 : previous_(nullptr),
1768 next_(nullptr),
1769 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001770 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001771 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001772 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001773 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001774 locations_(nullptr),
1775 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001776 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001777 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001778 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001779
Dave Allison20dfc792014-06-16 20:44:29 -07001780 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001781
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001782#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001783 enum InstructionKind {
1784 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1785 };
1786#undef DECLARE_KIND
1787
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001788 HInstruction* GetNext() const { return next_; }
1789 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001790
Calin Juravle77520bc2015-01-12 18:45:46 +00001791 HInstruction* GetNextDisregardingMoves() const;
1792 HInstruction* GetPreviousDisregardingMoves() const;
1793
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001794 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001795 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001796 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001797 bool IsInBlock() const { return block_ != nullptr; }
1798 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001799 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001800
Roland Levillain6b879dd2014-09-22 17:13:44 +01001801 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001802 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001803
1804 virtual void Accept(HGraphVisitor* visitor) = 0;
1805 virtual const char* DebugName() const = 0;
1806
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001807 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001808 void SetRawInputAt(size_t index, HInstruction* input) {
1809 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1810 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001811
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001812 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001813
1814 uint32_t GetDexPc() const { return dex_pc_; }
1815
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001816 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001817
Roland Levillaine161a2a2014-10-03 12:45:18 +01001818 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001819 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001820
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001821 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001822 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001823
Calin Juravle10e244f2015-01-26 18:54:32 +00001824 // Does not apply for all instructions, but having this at top level greatly
1825 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001826 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001827 virtual bool CanBeNull() const {
1828 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1829 return true;
1830 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001831
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001832 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001833 return false;
1834 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001835
Calin Juravle2e768302015-07-28 14:41:11 +00001836 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001837
Calin Juravle61d544b2015-02-23 16:46:57 +00001838 ReferenceTypeInfo GetReferenceTypeInfo() const {
1839 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1840 return reference_type_info_;
1841 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001842
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001843 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001844 DCHECK(user != nullptr);
1845 HUseListNode<HInstruction*>* use =
1846 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1847 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001848 }
1849
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001850 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001851 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001852 HUseListNode<HEnvironment*>* env_use =
1853 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1854 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001855 }
1856
David Brazdil1abb4192015-02-17 18:33:36 +00001857 void RemoveAsUserOfInput(size_t input) {
1858 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1859 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1860 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001861
David Brazdil1abb4192015-02-17 18:33:36 +00001862 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1863 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001864
David Brazdiled596192015-01-23 10:39:45 +00001865 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1866 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001867 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001868 bool HasOnlyOneNonEnvironmentUse() const {
1869 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1870 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001871
Roland Levillain6c82d402014-10-13 16:10:27 +01001872 // Does this instruction strictly dominate `other_instruction`?
1873 // Returns false if this instruction and `other_instruction` are the same.
1874 // Aborts if this instruction and `other_instruction` are both phis.
1875 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001876
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001877 int GetId() const { return id_; }
1878 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001879
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001880 int GetSsaIndex() const { return ssa_index_; }
1881 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1882 bool HasSsaIndex() const { return ssa_index_ != -1; }
1883
1884 bool HasEnvironment() const { return environment_ != nullptr; }
1885 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001886 // Set the `environment_` field. Raw because this method does not
1887 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001888 void SetRawEnvironment(HEnvironment* environment) {
1889 DCHECK(environment_ == nullptr);
1890 DCHECK_EQ(environment->GetHolder(), this);
1891 environment_ = environment;
1892 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001893
1894 // Set the environment of this instruction, copying it from `environment`. While
1895 // copying, the uses lists are being updated.
1896 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001897 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001898 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001899 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001900 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001901 if (environment->GetParent() != nullptr) {
1902 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1903 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001904 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001905
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001906 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1907 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001908 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001909 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001910 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001911 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001912 if (environment->GetParent() != nullptr) {
1913 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1914 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001915 }
1916
Nicolas Geoffray39468442014-09-02 15:17:15 +01001917 // Returns the number of entries in the environment. Typically, that is the
1918 // number of dex registers in a method. It could be more in case of inlining.
1919 size_t EnvironmentSize() const;
1920
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001921 LocationSummary* GetLocations() const { return locations_; }
1922 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001923
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001924 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001925 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001926
Alexandre Rames188d4312015-04-09 18:30:21 +01001927 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1928 // uses of this instruction by `other` are *not* updated.
1929 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1930 ReplaceWith(other);
1931 other->ReplaceInput(this, use_index);
1932 }
1933
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001934 // Move `this` instruction before `cursor`.
1935 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001936
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001937#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001938 bool Is##type() const { return (As##type() != nullptr); } \
1939 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001940 virtual H##type* As##type() { return nullptr; }
1941
1942 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1943#undef INSTRUCTION_TYPE_CHECK
1944
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001945 // Returns whether the instruction can be moved within the graph.
1946 virtual bool CanBeMoved() const { return false; }
1947
1948 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001949 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001950 return false;
1951 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001952
1953 // Returns whether any data encoded in the two instructions is equal.
1954 // This method does not look at the inputs. Both instructions must be
1955 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001956 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001957 return false;
1958 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001959
1960 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001961 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001962 // 2) Their inputs are identical.
1963 bool Equals(HInstruction* other) const;
1964
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001965 virtual InstructionKind GetKind() const = 0;
1966
1967 virtual size_t ComputeHashCode() const {
1968 size_t result = GetKind();
1969 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1970 result = (result * 31) + InputAt(i)->GetId();
1971 }
1972 return result;
1973 }
1974
1975 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001976 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001977
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001978 size_t GetLifetimePosition() const { return lifetime_position_; }
1979 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1980 LiveInterval* GetLiveInterval() const { return live_interval_; }
1981 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1982 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1983
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001984 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1985
1986 // Returns whether the code generation of the instruction will require to have access
1987 // to the current method. Such instructions are:
1988 // (1): Instructions that require an environment, as calling the runtime requires
1989 // to walk the stack and have the current method stored at a specific stack address.
1990 // (2): Object literals like classes and strings, that are loaded from the dex cache
1991 // fields of the current method.
1992 bool NeedsCurrentMethod() const {
1993 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1994 }
1995
Vladimir Markodc151b22015-10-15 18:02:30 +01001996 // Returns whether the code generation of the instruction will require to have access
1997 // to the dex cache of the current method's declaring class via the current method.
1998 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001999
Mark Mendellc4701932015-04-10 13:18:51 -04002000 // Does this instruction have any use in an environment before
2001 // control flow hits 'other'?
2002 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2003
2004 // Remove all references to environment uses of this instruction.
2005 // The caller must ensure that this is safe to do.
2006 void RemoveEnvironmentUsers();
2007
David Brazdil1abb4192015-02-17 18:33:36 +00002008 protected:
2009 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2010 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2011
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002012 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002013 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2014
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002015 HInstruction* previous_;
2016 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002017 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002018 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002019
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002020 // An instruction gets an id when it is added to the graph.
2021 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002022 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002023 int id_;
2024
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002025 // When doing liveness analysis, instructions that have uses get an SSA index.
2026 int ssa_index_;
2027
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002028 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002029 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002030
2031 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002032 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002033
Nicolas Geoffray39468442014-09-02 15:17:15 +01002034 // The environment associated with this instruction. Not null if the instruction
2035 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002036 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002037
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002038 // Set by the code generator.
2039 LocationSummary* locations_;
2040
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002041 // Set by the liveness analysis.
2042 LiveInterval* live_interval_;
2043
2044 // Set by the liveness analysis, this is the position in a linear
2045 // order of blocks where this instruction's live interval start.
2046 size_t lifetime_position_;
2047
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002048 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002049
Calin Juravleacf735c2015-02-12 15:25:22 +00002050 // TODO: for primitive types this should be marked as invalid.
2051 ReferenceTypeInfo reference_type_info_;
2052
David Brazdil1abb4192015-02-17 18:33:36 +00002053 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002054 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002055 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002056 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002057 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002058
2059 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2060};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002061std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002062
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002063class HInputIterator : public ValueObject {
2064 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002065 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002066
2067 bool Done() const { return index_ == instruction_->InputCount(); }
2068 HInstruction* Current() const { return instruction_->InputAt(index_); }
2069 void Advance() { index_++; }
2070
2071 private:
2072 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002073 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002074
2075 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2076};
2077
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002078class HInstructionIterator : public ValueObject {
2079 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002080 explicit HInstructionIterator(const HInstructionList& instructions)
2081 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002082 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002083 }
2084
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002085 bool Done() const { return instruction_ == nullptr; }
2086 HInstruction* Current() const { return instruction_; }
2087 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002088 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002089 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002090 }
2091
2092 private:
2093 HInstruction* instruction_;
2094 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002095
2096 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002097};
2098
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002099class HBackwardInstructionIterator : public ValueObject {
2100 public:
2101 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2102 : instruction_(instructions.last_instruction_) {
2103 next_ = Done() ? nullptr : instruction_->GetPrevious();
2104 }
2105
2106 bool Done() const { return instruction_ == nullptr; }
2107 HInstruction* Current() const { return instruction_; }
2108 void Advance() {
2109 instruction_ = next_;
2110 next_ = Done() ? nullptr : instruction_->GetPrevious();
2111 }
2112
2113 private:
2114 HInstruction* instruction_;
2115 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002116
2117 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002118};
2119
Vladimir Markof9f64412015-09-02 14:05:49 +01002120template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002121class HTemplateInstruction: public HInstruction {
2122 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002123 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002124 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002125 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002126
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002127 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002128
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002129 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002130 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2131 DCHECK_LT(i, N);
2132 return inputs_[i];
2133 }
David Brazdil1abb4192015-02-17 18:33:36 +00002134
2135 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002136 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002137 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002138 }
2139
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002140 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002141 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002142
2143 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002144};
2145
Vladimir Markof9f64412015-09-02 14:05:49 +01002146// HTemplateInstruction specialization for N=0.
2147template<>
2148class HTemplateInstruction<0>: public HInstruction {
2149 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002150 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002151 : HInstruction(side_effects, dex_pc) {}
2152
Vladimir Markof9f64412015-09-02 14:05:49 +01002153 virtual ~HTemplateInstruction() {}
2154
2155 size_t InputCount() const OVERRIDE { return 0; }
2156
2157 protected:
2158 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2159 LOG(FATAL) << "Unreachable";
2160 UNREACHABLE();
2161 }
2162
2163 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2164 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2165 LOG(FATAL) << "Unreachable";
2166 UNREACHABLE();
2167 }
2168
2169 private:
2170 friend class SsaBuilder;
2171};
2172
Dave Allison20dfc792014-06-16 20:44:29 -07002173template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002174class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002175 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002176 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002177 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002178 virtual ~HExpression() {}
2179
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002180 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002181
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002182 protected:
2183 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002184};
2185
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002186// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2187// instruction that branches to the exit block.
2188class HReturnVoid : public HTemplateInstruction<0> {
2189 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002190 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2191 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002192
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002193 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002194
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002195 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002196
2197 private:
2198 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2199};
2200
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002201// Represents dex's RETURN opcodes. A HReturn is a control flow
2202// instruction that branches to the exit block.
2203class HReturn : public HTemplateInstruction<1> {
2204 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002205 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2206 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002207 SetRawInputAt(0, value);
2208 }
2209
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002210 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002211
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002212 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002213
2214 private:
2215 DISALLOW_COPY_AND_ASSIGN(HReturn);
2216};
2217
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002218// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002219// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002220// exit block.
2221class HExit : public HTemplateInstruction<0> {
2222 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002223 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002224
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002225 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002227 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002228
2229 private:
2230 DISALLOW_COPY_AND_ASSIGN(HExit);
2231};
2232
2233// Jumps from one block to another.
2234class HGoto : public HTemplateInstruction<0> {
2235 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002236 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002237
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002238 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002239
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002240 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002241 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002242 }
2243
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002244 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002245
2246 private:
2247 DISALLOW_COPY_AND_ASSIGN(HGoto);
2248};
2249
Roland Levillain9867bc72015-08-05 10:21:34 +01002250class HConstant : public HExpression<0> {
2251 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002252 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2253 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002254
2255 bool CanBeMoved() const OVERRIDE { return true; }
2256
2257 virtual bool IsMinusOne() const { return false; }
2258 virtual bool IsZero() const { return false; }
2259 virtual bool IsOne() const { return false; }
2260
David Brazdil77a48ae2015-09-15 12:34:04 +00002261 virtual uint64_t GetValueAsUint64() const = 0;
2262
Roland Levillain9867bc72015-08-05 10:21:34 +01002263 DECLARE_INSTRUCTION(Constant);
2264
2265 private:
2266 DISALLOW_COPY_AND_ASSIGN(HConstant);
2267};
2268
2269class HNullConstant : public HConstant {
2270 public:
2271 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2272 return true;
2273 }
2274
David Brazdil77a48ae2015-09-15 12:34:04 +00002275 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2276
Roland Levillain9867bc72015-08-05 10:21:34 +01002277 size_t ComputeHashCode() const OVERRIDE { return 0; }
2278
2279 DECLARE_INSTRUCTION(NullConstant);
2280
2281 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002282 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002283
2284 friend class HGraph;
2285 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2286};
2287
2288// Constants of the type int. Those can be from Dex instructions, or
2289// synthesized (for example with the if-eqz instruction).
2290class HIntConstant : public HConstant {
2291 public:
2292 int32_t GetValue() const { return value_; }
2293
David Brazdil9f389d42015-10-01 14:32:56 +01002294 uint64_t GetValueAsUint64() const OVERRIDE {
2295 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2296 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002297
Roland Levillain9867bc72015-08-05 10:21:34 +01002298 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2299 DCHECK(other->IsIntConstant());
2300 return other->AsIntConstant()->value_ == value_;
2301 }
2302
2303 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2304
2305 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2306 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2307 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2308
2309 DECLARE_INSTRUCTION(IntConstant);
2310
2311 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002312 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2313 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2314 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2315 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002316
2317 const int32_t value_;
2318
2319 friend class HGraph;
2320 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2321 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2322 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2323};
2324
2325class HLongConstant : public HConstant {
2326 public:
2327 int64_t GetValue() const { return value_; }
2328
David Brazdil77a48ae2015-09-15 12:34:04 +00002329 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2330
Roland Levillain9867bc72015-08-05 10:21:34 +01002331 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2332 DCHECK(other->IsLongConstant());
2333 return other->AsLongConstant()->value_ == value_;
2334 }
2335
2336 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2337
2338 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2339 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2340 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2341
2342 DECLARE_INSTRUCTION(LongConstant);
2343
2344 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002345 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2346 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002347
2348 const int64_t value_;
2349
2350 friend class HGraph;
2351 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2352};
Dave Allison20dfc792014-06-16 20:44:29 -07002353
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002354// Conditional branch. A block ending with an HIf instruction must have
2355// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002356class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002357 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002358 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2359 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002360 SetRawInputAt(0, input);
2361 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002362
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002363 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002364
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002365 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002366 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002367 }
2368
2369 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002370 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002371 }
2372
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002373 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002374
2375 private:
2376 DISALLOW_COPY_AND_ASSIGN(HIf);
2377};
2378
David Brazdilfc6a86a2015-06-26 10:33:45 +00002379
2380// Abstract instruction which marks the beginning and/or end of a try block and
2381// links it to the respective exception handlers. Behaves the same as a Goto in
2382// non-exceptional control flow.
2383// Normal-flow successor is stored at index zero, exception handlers under
2384// higher indices in no particular order.
2385class HTryBoundary : public HTemplateInstruction<0> {
2386 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002387 enum BoundaryKind {
2388 kEntry,
2389 kExit,
2390 };
2391
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002392 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2393 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002394
2395 bool IsControlFlow() const OVERRIDE { return true; }
2396
2397 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002398 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002399
2400 // Returns whether `handler` is among its exception handlers (non-zero index
2401 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002402 bool HasExceptionHandler(const HBasicBlock& handler) const {
2403 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002404 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002405 }
2406
2407 // If not present already, adds `handler` to its block's list of exception
2408 // handlers.
2409 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002410 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002411 GetBlock()->AddSuccessor(handler);
2412 }
2413 }
2414
David Brazdil56e1acc2015-06-30 15:41:36 +01002415 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002416
David Brazdilffee3d32015-07-06 11:48:53 +01002417 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2418
David Brazdilfc6a86a2015-06-26 10:33:45 +00002419 DECLARE_INSTRUCTION(TryBoundary);
2420
2421 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002422 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002423
2424 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2425};
2426
David Brazdilffee3d32015-07-06 11:48:53 +01002427// Iterator over exception handlers of a given HTryBoundary, i.e. over
2428// exceptional successors of its basic block.
2429class HExceptionHandlerIterator : public ValueObject {
2430 public:
2431 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2432 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2433
Vladimir Marko60584552015-09-03 13:35:12 +00002434 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002435 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002436 size_t CurrentSuccessorIndex() const { return index_; }
2437 void Advance() { ++index_; }
2438
2439 private:
2440 const HBasicBlock& block_;
2441 size_t index_;
2442
2443 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2444};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002445
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002446// Deoptimize to interpreter, upon checking a condition.
2447class HDeoptimize : public HTemplateInstruction<1> {
2448 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002449 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2450 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002451 SetRawInputAt(0, cond);
2452 }
2453
2454 bool NeedsEnvironment() const OVERRIDE { return true; }
2455 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002456
2457 DECLARE_INSTRUCTION(Deoptimize);
2458
2459 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002460 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2461};
2462
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002463// Represents the ArtMethod that was passed as a first argument to
2464// the method. It is used by instructions that depend on it, like
2465// instructions that work with the dex cache.
2466class HCurrentMethod : public HExpression<0> {
2467 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002468 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2469 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002470
2471 DECLARE_INSTRUCTION(CurrentMethod);
2472
2473 private:
2474 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2475};
2476
Mark Mendellfe57faa2015-09-18 09:26:15 -04002477// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2478// have one successor for each entry in the switch table, and the final successor
2479// will be the block containing the next Dex opcode.
2480class HPackedSwitch : public HTemplateInstruction<1> {
2481 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002482 HPackedSwitch(int32_t start_value,
2483 uint32_t num_entries,
2484 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002485 uint32_t dex_pc = kNoDexPc)
2486 : HTemplateInstruction(SideEffects::None(), dex_pc),
2487 start_value_(start_value),
2488 num_entries_(num_entries) {
2489 SetRawInputAt(0, input);
2490 }
2491
2492 bool IsControlFlow() const OVERRIDE { return true; }
2493
2494 int32_t GetStartValue() const { return start_value_; }
2495
Vladimir Marko211c2112015-09-24 16:52:33 +01002496 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002497
2498 HBasicBlock* GetDefaultBlock() const {
2499 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002500 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002501 }
2502 DECLARE_INSTRUCTION(PackedSwitch);
2503
2504 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002505 const int32_t start_value_;
2506 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002507
2508 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2509};
2510
Roland Levillain88cb1752014-10-20 16:36:47 +01002511class HUnaryOperation : public HExpression<1> {
2512 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002513 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2514 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002515 SetRawInputAt(0, input);
2516 }
2517
2518 HInstruction* GetInput() const { return InputAt(0); }
2519 Primitive::Type GetResultType() const { return GetType(); }
2520
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002521 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002522 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002523 return true;
2524 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002525
Roland Levillain9240d6a2014-10-20 16:47:04 +01002526 // Try to statically evaluate `operation` and return a HConstant
2527 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002528 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002529 HConstant* TryStaticEvaluation() const;
2530
2531 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002532 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2533 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002534
Roland Levillain88cb1752014-10-20 16:36:47 +01002535 DECLARE_INSTRUCTION(UnaryOperation);
2536
2537 private:
2538 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2539};
2540
Dave Allison20dfc792014-06-16 20:44:29 -07002541class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002542 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002543 HBinaryOperation(Primitive::Type result_type,
2544 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002545 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002546 SideEffects side_effects = SideEffects::None(),
2547 uint32_t dex_pc = kNoDexPc)
2548 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002549 SetRawInputAt(0, left);
2550 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002551 }
2552
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002553 HInstruction* GetLeft() const { return InputAt(0); }
2554 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002555 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002556
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002557 virtual bool IsCommutative() const { return false; }
2558
2559 // Put constant on the right.
2560 // Returns whether order is changed.
2561 bool OrderInputsWithConstantOnTheRight() {
2562 HInstruction* left = InputAt(0);
2563 HInstruction* right = InputAt(1);
2564 if (left->IsConstant() && !right->IsConstant()) {
2565 ReplaceInput(right, 0);
2566 ReplaceInput(left, 1);
2567 return true;
2568 }
2569 return false;
2570 }
2571
2572 // Order inputs by instruction id, but favor constant on the right side.
2573 // This helps GVN for commutative ops.
2574 void OrderInputs() {
2575 DCHECK(IsCommutative());
2576 HInstruction* left = InputAt(0);
2577 HInstruction* right = InputAt(1);
2578 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2579 return;
2580 }
2581 if (OrderInputsWithConstantOnTheRight()) {
2582 return;
2583 }
2584 // Order according to instruction id.
2585 if (left->GetId() > right->GetId()) {
2586 ReplaceInput(right, 0);
2587 ReplaceInput(left, 1);
2588 }
2589 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002590
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002591 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002592 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002593 return true;
2594 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002595
Roland Levillain9240d6a2014-10-20 16:47:04 +01002596 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002597 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002598 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002599 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002600
2601 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002602 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2603 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2604 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2605 HLongConstant* y ATTRIBUTE_UNUSED) const {
2606 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2607 return nullptr;
2608 }
2609 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2610 HIntConstant* y ATTRIBUTE_UNUSED) const {
2611 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2612 return nullptr;
2613 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002614
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002615 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002616 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002617 HConstant* GetConstantRight() const;
2618
2619 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002620 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002621 HInstruction* GetLeastConstantLeft() const;
2622
Roland Levillainccc07a92014-09-16 14:48:16 +01002623 DECLARE_INSTRUCTION(BinaryOperation);
2624
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002625 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002626 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2627};
2628
Mark Mendellc4701932015-04-10 13:18:51 -04002629// The comparison bias applies for floating point operations and indicates how NaN
2630// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002631enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002632 kNoBias, // bias is not applicable (i.e. for long operation)
2633 kGtBias, // return 1 for NaN comparisons
2634 kLtBias, // return -1 for NaN comparisons
2635};
2636
Dave Allison20dfc792014-06-16 20:44:29 -07002637class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002639 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2640 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002641 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002642 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002643
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002644 bool NeedsMaterialization() const { return needs_materialization_; }
2645 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002646
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002647 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002648 // `instruction`, and disregard moves in between.
2649 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002650
Dave Allison20dfc792014-06-16 20:44:29 -07002651 DECLARE_INSTRUCTION(Condition);
2652
2653 virtual IfCondition GetCondition() const = 0;
2654
Mark Mendellc4701932015-04-10 13:18:51 -04002655 virtual IfCondition GetOppositeCondition() const = 0;
2656
Roland Levillain4fa13f62015-07-06 18:11:54 +01002657 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002658
2659 void SetBias(ComparisonBias bias) { bias_ = bias; }
2660
2661 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2662 return bias_ == other->AsCondition()->bias_;
2663 }
2664
Roland Levillain4fa13f62015-07-06 18:11:54 +01002665 bool IsFPConditionTrueIfNaN() const {
2666 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2667 IfCondition if_cond = GetCondition();
2668 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2669 }
2670
2671 bool IsFPConditionFalseIfNaN() const {
2672 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2673 IfCondition if_cond = GetCondition();
2674 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2675 }
2676
Dave Allison20dfc792014-06-16 20:44:29 -07002677 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002678 // For register allocation purposes, returns whether this instruction needs to be
2679 // materialized (that is, not just be in the processor flags).
2680 bool needs_materialization_;
2681
Mark Mendellc4701932015-04-10 13:18:51 -04002682 // Needed if we merge a HCompare into a HCondition.
2683 ComparisonBias bias_;
2684
Dave Allison20dfc792014-06-16 20:44:29 -07002685 DISALLOW_COPY_AND_ASSIGN(HCondition);
2686};
2687
2688// Instruction to check if two inputs are equal to each other.
2689class HEqual : public HCondition {
2690 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002691 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2692 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002693
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002694 bool IsCommutative() const OVERRIDE { return true; }
2695
Roland Levillain9867bc72015-08-05 10:21:34 +01002696 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002697 return GetBlock()->GetGraph()->GetIntConstant(
2698 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002699 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002700 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002701 return GetBlock()->GetGraph()->GetIntConstant(
2702 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002703 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002704
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002705 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002706
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002707 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002708 return kCondEQ;
2709 }
2710
Mark Mendellc4701932015-04-10 13:18:51 -04002711 IfCondition GetOppositeCondition() const OVERRIDE {
2712 return kCondNE;
2713 }
2714
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002715 private:
Aart Bike9f37602015-10-09 11:15:55 -07002716 template <typename T> bool Compute(T x, T y) const { return x == y; }
2717
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002718 DISALLOW_COPY_AND_ASSIGN(HEqual);
2719};
2720
Dave Allison20dfc792014-06-16 20:44:29 -07002721class HNotEqual : public HCondition {
2722 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002723 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2724 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002725
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002726 bool IsCommutative() const OVERRIDE { return true; }
2727
Roland Levillain9867bc72015-08-05 10:21:34 +01002728 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002729 return GetBlock()->GetGraph()->GetIntConstant(
2730 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002731 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002732 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002733 return GetBlock()->GetGraph()->GetIntConstant(
2734 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002735 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002736
Dave Allison20dfc792014-06-16 20:44:29 -07002737 DECLARE_INSTRUCTION(NotEqual);
2738
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002739 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002740 return kCondNE;
2741 }
2742
Mark Mendellc4701932015-04-10 13:18:51 -04002743 IfCondition GetOppositeCondition() const OVERRIDE {
2744 return kCondEQ;
2745 }
2746
Dave Allison20dfc792014-06-16 20:44:29 -07002747 private:
Aart Bike9f37602015-10-09 11:15:55 -07002748 template <typename T> bool Compute(T x, T y) const { return x != y; }
2749
Dave Allison20dfc792014-06-16 20:44:29 -07002750 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2751};
2752
2753class HLessThan : public HCondition {
2754 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002755 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2756 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002757
Roland Levillain9867bc72015-08-05 10:21:34 +01002758 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002759 return GetBlock()->GetGraph()->GetIntConstant(
2760 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002761 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002762 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002763 return GetBlock()->GetGraph()->GetIntConstant(
2764 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002765 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002766
Dave Allison20dfc792014-06-16 20:44:29 -07002767 DECLARE_INSTRUCTION(LessThan);
2768
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002769 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002770 return kCondLT;
2771 }
2772
Mark Mendellc4701932015-04-10 13:18:51 -04002773 IfCondition GetOppositeCondition() const OVERRIDE {
2774 return kCondGE;
2775 }
2776
Dave Allison20dfc792014-06-16 20:44:29 -07002777 private:
Aart Bike9f37602015-10-09 11:15:55 -07002778 template <typename T> bool Compute(T x, T y) const { return x < y; }
2779
Dave Allison20dfc792014-06-16 20:44:29 -07002780 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2781};
2782
2783class HLessThanOrEqual : public HCondition {
2784 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002785 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2786 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002787
Roland Levillain9867bc72015-08-05 10:21:34 +01002788 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002789 return GetBlock()->GetGraph()->GetIntConstant(
2790 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002791 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002792 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002793 return GetBlock()->GetGraph()->GetIntConstant(
2794 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002795 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002796
Dave Allison20dfc792014-06-16 20:44:29 -07002797 DECLARE_INSTRUCTION(LessThanOrEqual);
2798
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002799 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002800 return kCondLE;
2801 }
2802
Mark Mendellc4701932015-04-10 13:18:51 -04002803 IfCondition GetOppositeCondition() const OVERRIDE {
2804 return kCondGT;
2805 }
2806
Dave Allison20dfc792014-06-16 20:44:29 -07002807 private:
Aart Bike9f37602015-10-09 11:15:55 -07002808 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2809
Dave Allison20dfc792014-06-16 20:44:29 -07002810 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2811};
2812
2813class HGreaterThan : public HCondition {
2814 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002815 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2816 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002817
Roland Levillain9867bc72015-08-05 10:21:34 +01002818 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002819 return GetBlock()->GetGraph()->GetIntConstant(
2820 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002821 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002822 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002823 return GetBlock()->GetGraph()->GetIntConstant(
2824 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002825 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002826
Dave Allison20dfc792014-06-16 20:44:29 -07002827 DECLARE_INSTRUCTION(GreaterThan);
2828
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002829 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002830 return kCondGT;
2831 }
2832
Mark Mendellc4701932015-04-10 13:18:51 -04002833 IfCondition GetOppositeCondition() const OVERRIDE {
2834 return kCondLE;
2835 }
2836
Dave Allison20dfc792014-06-16 20:44:29 -07002837 private:
Aart Bike9f37602015-10-09 11:15:55 -07002838 template <typename T> bool Compute(T x, T y) const { return x > y; }
2839
Dave Allison20dfc792014-06-16 20:44:29 -07002840 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2841};
2842
2843class HGreaterThanOrEqual : public HCondition {
2844 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002845 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2846 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002847
Roland Levillain9867bc72015-08-05 10:21:34 +01002848 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002849 return GetBlock()->GetGraph()->GetIntConstant(
2850 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002851 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002852 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002853 return GetBlock()->GetGraph()->GetIntConstant(
2854 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002855 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002856
Dave Allison20dfc792014-06-16 20:44:29 -07002857 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2858
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002859 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002860 return kCondGE;
2861 }
2862
Mark Mendellc4701932015-04-10 13:18:51 -04002863 IfCondition GetOppositeCondition() const OVERRIDE {
2864 return kCondLT;
2865 }
2866
Dave Allison20dfc792014-06-16 20:44:29 -07002867 private:
Aart Bike9f37602015-10-09 11:15:55 -07002868 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2869
Dave Allison20dfc792014-06-16 20:44:29 -07002870 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2871};
2872
Aart Bike9f37602015-10-09 11:15:55 -07002873class HBelow : public HCondition {
2874 public:
2875 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2876 : HCondition(first, second, dex_pc) {}
2877
2878 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2879 return GetBlock()->GetGraph()->GetIntConstant(
2880 Compute(static_cast<uint32_t>(x->GetValue()),
2881 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2882 }
2883 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2884 return GetBlock()->GetGraph()->GetIntConstant(
2885 Compute(static_cast<uint64_t>(x->GetValue()),
2886 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2887 }
2888
2889 DECLARE_INSTRUCTION(Below);
2890
2891 IfCondition GetCondition() const OVERRIDE {
2892 return kCondB;
2893 }
2894
2895 IfCondition GetOppositeCondition() const OVERRIDE {
2896 return kCondAE;
2897 }
2898
2899 private:
2900 template <typename T> bool Compute(T x, T y) const { return x < y; }
2901
2902 DISALLOW_COPY_AND_ASSIGN(HBelow);
2903};
2904
2905class HBelowOrEqual : public HCondition {
2906 public:
2907 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2908 : HCondition(first, second, dex_pc) {}
2909
2910 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2911 return GetBlock()->GetGraph()->GetIntConstant(
2912 Compute(static_cast<uint32_t>(x->GetValue()),
2913 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2914 }
2915 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2916 return GetBlock()->GetGraph()->GetIntConstant(
2917 Compute(static_cast<uint64_t>(x->GetValue()),
2918 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2919 }
2920
2921 DECLARE_INSTRUCTION(BelowOrEqual);
2922
2923 IfCondition GetCondition() const OVERRIDE {
2924 return kCondBE;
2925 }
2926
2927 IfCondition GetOppositeCondition() const OVERRIDE {
2928 return kCondA;
2929 }
2930
2931 private:
2932 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2933
2934 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2935};
2936
2937class HAbove : public HCondition {
2938 public:
2939 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2940 : HCondition(first, second, dex_pc) {}
2941
2942 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2943 return GetBlock()->GetGraph()->GetIntConstant(
2944 Compute(static_cast<uint32_t>(x->GetValue()),
2945 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2946 }
2947 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2948 return GetBlock()->GetGraph()->GetIntConstant(
2949 Compute(static_cast<uint64_t>(x->GetValue()),
2950 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2951 }
2952
2953 DECLARE_INSTRUCTION(Above);
2954
2955 IfCondition GetCondition() const OVERRIDE {
2956 return kCondA;
2957 }
2958
2959 IfCondition GetOppositeCondition() const OVERRIDE {
2960 return kCondBE;
2961 }
2962
2963 private:
2964 template <typename T> bool Compute(T x, T y) const { return x > y; }
2965
2966 DISALLOW_COPY_AND_ASSIGN(HAbove);
2967};
2968
2969class HAboveOrEqual : public HCondition {
2970 public:
2971 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2972 : HCondition(first, second, dex_pc) {}
2973
2974 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2975 return GetBlock()->GetGraph()->GetIntConstant(
2976 Compute(static_cast<uint32_t>(x->GetValue()),
2977 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2978 }
2979 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2980 return GetBlock()->GetGraph()->GetIntConstant(
2981 Compute(static_cast<uint64_t>(x->GetValue()),
2982 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2983 }
2984
2985 DECLARE_INSTRUCTION(AboveOrEqual);
2986
2987 IfCondition GetCondition() const OVERRIDE {
2988 return kCondAE;
2989 }
2990
2991 IfCondition GetOppositeCondition() const OVERRIDE {
2992 return kCondB;
2993 }
2994
2995 private:
2996 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2997
2998 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
2999};
Dave Allison20dfc792014-06-16 20:44:29 -07003000
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003001// Instruction to check how two inputs compare to each other.
3002// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3003class HCompare : public HBinaryOperation {
3004 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003005 HCompare(Primitive::Type type,
3006 HInstruction* first,
3007 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003008 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003009 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003010 : HBinaryOperation(Primitive::kPrimInt,
3011 first,
3012 second,
3013 SideEffectsForArchRuntimeCalls(type),
3014 dex_pc),
3015 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003016 DCHECK_EQ(type, first->GetType());
3017 DCHECK_EQ(type, second->GetType());
3018 }
3019
Roland Levillain9867bc72015-08-05 10:21:34 +01003020 template <typename T>
3021 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003022
Roland Levillain9867bc72015-08-05 10:21:34 +01003023 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003024 return GetBlock()->GetGraph()->GetIntConstant(
3025 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003026 }
3027 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003028 return GetBlock()->GetGraph()->GetIntConstant(
3029 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003030 }
3031
Calin Juravleddb7df22014-11-25 20:56:51 +00003032 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3033 return bias_ == other->AsCompare()->bias_;
3034 }
3035
Mark Mendellc4701932015-04-10 13:18:51 -04003036 ComparisonBias GetBias() const { return bias_; }
3037
Roland Levillain4fa13f62015-07-06 18:11:54 +01003038 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003039
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003040
3041 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3042 // MIPS64 uses a runtime call for FP comparisons.
3043 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3044 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003045
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003046 DECLARE_INSTRUCTION(Compare);
3047
3048 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003049 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003050
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003051 DISALLOW_COPY_AND_ASSIGN(HCompare);
3052};
3053
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003054// A local in the graph. Corresponds to a Dex register.
3055class HLocal : public HTemplateInstruction<0> {
3056 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003057 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003058 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003059
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003060 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003061
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003062 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003063
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003064 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003065 // The Dex register number.
3066 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003067
3068 DISALLOW_COPY_AND_ASSIGN(HLocal);
3069};
3070
3071// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003072class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003073 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003074 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3075 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003076 SetRawInputAt(0, local);
3077 }
3078
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003079 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3080
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003081 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003082
3083 private:
3084 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3085};
3086
3087// Store a value in a given local. This instruction has two inputs: the value
3088// and the local.
3089class HStoreLocal : public HTemplateInstruction<2> {
3090 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003091 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3092 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003093 SetRawInputAt(0, local);
3094 SetRawInputAt(1, value);
3095 }
3096
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003097 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3098
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003099 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003100
3101 private:
3102 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3103};
3104
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003105class HFloatConstant : public HConstant {
3106 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003107 float GetValue() const { return value_; }
3108
David Brazdil77a48ae2015-09-15 12:34:04 +00003109 uint64_t GetValueAsUint64() const OVERRIDE {
3110 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3111 }
3112
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003113 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003114 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003115 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003116 }
3117
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003118 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003119
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003120 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003121 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003122 }
3123 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003124 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003125 }
3126 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003127 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3128 }
3129 bool IsNaN() const {
3130 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003131 }
3132
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003133 DECLARE_INSTRUCTION(FloatConstant);
3134
3135 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003136 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3137 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3138 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3139 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003140
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003141 const float value_;
3142
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003143 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003144 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003145 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003146 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3147};
3148
3149class HDoubleConstant : public HConstant {
3150 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003151 double GetValue() const { return value_; }
3152
David Brazdil77a48ae2015-09-15 12:34:04 +00003153 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3154
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003155 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003156 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003157 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003158 }
3159
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003160 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003161
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003162 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003163 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003164 }
3165 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003166 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003167 }
3168 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003169 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3170 }
3171 bool IsNaN() const {
3172 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003173 }
3174
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003175 DECLARE_INSTRUCTION(DoubleConstant);
3176
3177 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003178 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3179 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3180 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3181 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003182
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003183 const double value_;
3184
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003185 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003186 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003187 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003188 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3189};
3190
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003191enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003192#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003193#include "intrinsics_list.h"
3194 kNone,
3195 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3196#undef INTRINSICS_LIST
3197#undef OPTIMIZING_INTRINSICS
3198};
3199std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3200
Agi Csaki05f20562015-08-19 14:58:14 -07003201enum IntrinsicNeedsEnvironmentOrCache {
3202 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3203 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003204};
3205
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003206class HInvoke : public HInstruction {
3207 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003208 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003209
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003210 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003211
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003212 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003213 SetRawInputAt(index, argument);
3214 }
3215
Roland Levillain3e3d7332015-04-28 11:00:54 +01003216 // Return the number of arguments. This number can be lower than
3217 // the number of inputs returned by InputCount(), as some invoke
3218 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3219 // inputs at the end of their list of inputs.
3220 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3221
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003222 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003223
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003224
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003225 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003226 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003227
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003228 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3229
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003230 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003231 return intrinsic_;
3232 }
3233
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003234 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003235
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003236 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003237 return GetEnvironment()->GetParent() != nullptr;
3238 }
3239
3240 bool CanThrow() const OVERRIDE { return true; }
3241
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003242 uint32_t* GetIntrinsicOptimizations() {
3243 return &intrinsic_optimizations_;
3244 }
3245
3246 const uint32_t* GetIntrinsicOptimizations() const {
3247 return &intrinsic_optimizations_;
3248 }
3249
3250 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3251
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003252 DECLARE_INSTRUCTION(Invoke);
3253
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003254 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003255 HInvoke(ArenaAllocator* arena,
3256 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003257 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003258 Primitive::Type return_type,
3259 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003260 uint32_t dex_method_index,
3261 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003262 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003263 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003264 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003265 inputs_(number_of_arguments + number_of_other_inputs,
3266 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003267 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003268 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003269 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003270 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003271 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003272 }
3273
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003274 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003275 return inputs_[index];
3276 }
3277
David Brazdil1abb4192015-02-17 18:33:36 +00003278 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003279 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003280 }
3281
Roland Levillain3e3d7332015-04-28 11:00:54 +01003282 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003283 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003284 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003285 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003286 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003287 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003288
3289 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3290 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003291
3292 private:
3293 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3294};
3295
Calin Juravle175dc732015-08-25 15:42:32 +01003296class HInvokeUnresolved : public HInvoke {
3297 public:
3298 HInvokeUnresolved(ArenaAllocator* arena,
3299 uint32_t number_of_arguments,
3300 Primitive::Type return_type,
3301 uint32_t dex_pc,
3302 uint32_t dex_method_index,
3303 InvokeType invoke_type)
3304 : HInvoke(arena,
3305 number_of_arguments,
3306 0u /* number_of_other_inputs */,
3307 return_type,
3308 dex_pc,
3309 dex_method_index,
3310 invoke_type) {
3311 }
3312
3313 DECLARE_INSTRUCTION(InvokeUnresolved);
3314
3315 private:
3316 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3317};
3318
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003319class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003320 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003321 // Requirements of this method call regarding the class
3322 // initialization (clinit) check of its declaring class.
3323 enum class ClinitCheckRequirement {
3324 kNone, // Class already initialized.
3325 kExplicit, // Static call having explicit clinit check as last input.
3326 kImplicit, // Static call implicitly requiring a clinit check.
3327 };
3328
Vladimir Marko58155012015-08-19 12:49:41 +00003329 // Determines how to load the target ArtMethod*.
3330 enum class MethodLoadKind {
3331 // Use a String init ArtMethod* loaded from Thread entrypoints.
3332 kStringInit,
3333
3334 // Use the method's own ArtMethod* loaded by the register allocator.
3335 kRecursive,
3336
3337 // Use ArtMethod* at a known address, embed the direct address in the code.
3338 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3339 kDirectAddress,
3340
3341 // Use ArtMethod* at an address that will be known at link time, embed the direct
3342 // address in the code. If the image is relocatable, emit .patch_oat entry.
3343 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3344 // the image relocatable or not.
3345 kDirectAddressWithFixup,
3346
3347 // Load from resoved methods array in the dex cache using a PC-relative load.
3348 // Used when we need to use the dex cache, for example for invoke-static that
3349 // may cause class initialization (the entry may point to a resolution method),
3350 // and we know that we can access the dex cache arrays using a PC-relative load.
3351 kDexCachePcRelative,
3352
3353 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3354 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3355 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3356 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3357 kDexCacheViaMethod,
3358 };
3359
3360 // Determines the location of the code pointer.
3361 enum class CodePtrLocation {
3362 // Recursive call, use local PC-relative call instruction.
3363 kCallSelf,
3364
3365 // Use PC-relative call instruction patched at link time.
3366 // Used for calls within an oat file, boot->boot or app->app.
3367 kCallPCRelative,
3368
3369 // Call to a known target address, embed the direct address in code.
3370 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3371 kCallDirect,
3372
3373 // Call to a target address that will be known at link time, embed the direct
3374 // address in code. If the image is relocatable, emit .patch_oat entry.
3375 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3376 // the image relocatable or not.
3377 kCallDirectWithFixup,
3378
3379 // Use code pointer from the ArtMethod*.
3380 // Used when we don't know the target code. This is also the last-resort-kind used when
3381 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3382 kCallArtMethod,
3383 };
3384
3385 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003386 MethodLoadKind method_load_kind;
3387 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003388 // The method load data holds
3389 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3390 // Note that there are multiple string init methods, each having its own offset.
3391 // - the method address for kDirectAddress
3392 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003393 uint64_t method_load_data;
3394 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003395 };
3396
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003397 HInvokeStaticOrDirect(ArenaAllocator* arena,
3398 uint32_t number_of_arguments,
3399 Primitive::Type return_type,
3400 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003401 uint32_t method_index,
3402 MethodReference target_method,
3403 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003404 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003405 InvokeType invoke_type,
3406 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003407 : HInvoke(arena,
3408 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003409 // There is potentially one extra argument for the HCurrentMethod node, and
3410 // potentially one other if the clinit check is explicit, and potentially
3411 // one other if the method is a string factory.
3412 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3413 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3414 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003415 return_type,
3416 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003417 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003418 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003419 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003420 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003421 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003422 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003423
Vladimir Markodc151b22015-10-15 18:02:30 +01003424 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003425 bool had_current_method_input = HasCurrentMethodInput();
3426 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3427
3428 // Using the current method is the default and once we find a better
3429 // method load kind, we should not go back to using the current method.
3430 DCHECK(had_current_method_input || !needs_current_method_input);
3431
3432 if (had_current_method_input && !needs_current_method_input) {
3433 DCHECK_EQ(InputAt(GetCurrentMethodInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3434 RemoveInputAt(GetCurrentMethodInputIndex());
3435 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003436 dispatch_info_ = dispatch_info;
3437 }
3438
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003439 void InsertInputAt(size_t index, HInstruction* input);
Vladimir Markob554b5a2015-11-06 12:57:55 +00003440 void RemoveInputAt(size_t index);
3441
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003442 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003443 // We access the method via the dex cache so we can't do an implicit null check.
3444 // TODO: for intrinsics we can generate implicit null checks.
3445 return false;
3446 }
3447
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003448 bool CanBeNull() const OVERRIDE {
3449 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3450 }
3451
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003452 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003453 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3454 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3455 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003456 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003457 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003458 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003459 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003460 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003461 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3462 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003463 bool HasCurrentMethodInput() const {
3464 // This function can be called only after the invoke has been fully initialized by the builder.
3465 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
3466 DCHECK(InputAt(GetCurrentMethodInputIndex())->IsCurrentMethod());
3467 return true;
3468 } else {
3469 DCHECK(InputCount() == GetCurrentMethodInputIndex() ||
3470 !InputAt(GetCurrentMethodInputIndex())->IsCurrentMethod());
3471 return false;
3472 }
3473 }
Vladimir Marko58155012015-08-19 12:49:41 +00003474 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3475 MethodReference GetTargetMethod() const { return target_method_; }
3476
3477 int32_t GetStringInitOffset() const {
3478 DCHECK(IsStringInit());
3479 return dispatch_info_.method_load_data;
3480 }
3481
3482 uint64_t GetMethodAddress() const {
3483 DCHECK(HasMethodAddress());
3484 return dispatch_info_.method_load_data;
3485 }
3486
3487 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003488 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003489 return dispatch_info_.method_load_data;
3490 }
3491
3492 uint64_t GetDirectCodePtr() const {
3493 DCHECK(HasDirectCodePtr());
3494 return dispatch_info_.direct_code_ptr;
3495 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003496
Calin Juravle68ad6492015-08-18 17:08:12 +01003497 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3498
Roland Levillain4c0eb422015-04-24 16:43:49 +01003499 // Is this instruction a call to a static method?
3500 bool IsStatic() const {
3501 return GetInvokeType() == kStatic;
3502 }
3503
Roland Levillain3e3d7332015-04-28 11:00:54 +01003504 // Remove the art::HLoadClass instruction set as last input by
3505 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3506 // the initial art::HClinitCheck instruction (only relevant for
3507 // static calls with explicit clinit check).
3508 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003509 DCHECK(IsStaticWithExplicitClinitCheck());
3510 size_t last_input_index = InputCount() - 1;
3511 HInstruction* last_input = InputAt(last_input_index);
3512 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003513 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003514 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003515 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003516 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3517 DCHECK(IsStaticWithImplicitClinitCheck());
3518 }
3519
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003520 bool IsStringFactoryFor(HFakeString* str) const {
3521 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003522 DCHECK(!HasCurrentMethodInput());
3523 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003524 return InputAt(InputCount() - 1)->AsFakeString() == str;
3525 }
3526
3527 void RemoveFakeStringArgumentAsLastInput() {
3528 DCHECK(IsStringInit());
3529 size_t last_input_index = InputCount() - 1;
3530 HInstruction* last_input = InputAt(last_input_index);
3531 DCHECK(last_input != nullptr);
3532 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3533 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003534 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003535 }
3536
Roland Levillain4c0eb422015-04-24 16:43:49 +01003537 // Is this a call to a static method whose declaring class has an
3538 // explicit intialization check in the graph?
3539 bool IsStaticWithExplicitClinitCheck() const {
3540 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3541 }
3542
3543 // Is this a call to a static method whose declaring class has an
3544 // implicit intialization check requirement?
3545 bool IsStaticWithImplicitClinitCheck() const {
3546 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3547 }
3548
Vladimir Markob554b5a2015-11-06 12:57:55 +00003549 // Does this method load kind need the current method as an input?
3550 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3551 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3552 }
3553
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003554 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003555
Roland Levillain4c0eb422015-04-24 16:43:49 +01003556 protected:
3557 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3558 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3559 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3560 HInstruction* input = input_record.GetInstruction();
3561 // `input` is the last input of a static invoke marked as having
3562 // an explicit clinit check. It must either be:
3563 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3564 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3565 DCHECK(input != nullptr);
3566 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3567 }
3568 return input_record;
3569 }
3570
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003571 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003572 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003573 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003574 // The target method may refer to different dex file or method index than the original
3575 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3576 // in derived class to increase visibility.
3577 MethodReference target_method_;
3578 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003579
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003580 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003581};
3582
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003583class HInvokeVirtual : public HInvoke {
3584 public:
3585 HInvokeVirtual(ArenaAllocator* arena,
3586 uint32_t number_of_arguments,
3587 Primitive::Type return_type,
3588 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003589 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003590 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003591 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003592 vtable_index_(vtable_index) {}
3593
Calin Juravle641547a2015-04-21 22:08:51 +01003594 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003595 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003596 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003597 }
3598
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003599 uint32_t GetVTableIndex() const { return vtable_index_; }
3600
3601 DECLARE_INSTRUCTION(InvokeVirtual);
3602
3603 private:
3604 const uint32_t vtable_index_;
3605
3606 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3607};
3608
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003609class HInvokeInterface : public HInvoke {
3610 public:
3611 HInvokeInterface(ArenaAllocator* arena,
3612 uint32_t number_of_arguments,
3613 Primitive::Type return_type,
3614 uint32_t dex_pc,
3615 uint32_t dex_method_index,
3616 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003617 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003618 imt_index_(imt_index) {}
3619
Calin Juravle641547a2015-04-21 22:08:51 +01003620 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003621 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003622 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003623 }
3624
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003625 uint32_t GetImtIndex() const { return imt_index_; }
3626 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3627
3628 DECLARE_INSTRUCTION(InvokeInterface);
3629
3630 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003631 const uint32_t imt_index_;
3632
3633 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3634};
3635
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003636class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003637 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003638 HNewInstance(HCurrentMethod* current_method,
3639 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003640 uint16_t type_index,
3641 const DexFile& dex_file,
3642 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003643 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003644 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003645 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003646 entrypoint_(entrypoint) {
3647 SetRawInputAt(0, current_method);
3648 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003649
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003650 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003651 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003652
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003653 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003654 bool NeedsEnvironment() const OVERRIDE { return true; }
Andreas Gampe55d02cf2015-10-29 02:59:50 +00003655 // It may throw when called on:
3656 // - interfaces
3657 // - abstract/innaccessible/unknown classes
3658 // TODO: optimize when possible.
3659 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003660
Calin Juravle10e244f2015-01-26 18:54:32 +00003661 bool CanBeNull() const OVERRIDE { return false; }
3662
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003663 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3664
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003665 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003666
3667 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003668 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003669 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003670 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003671
3672 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3673};
3674
Roland Levillain88cb1752014-10-20 16:36:47 +01003675class HNeg : public HUnaryOperation {
3676 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003677 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3678 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003679
Roland Levillain9867bc72015-08-05 10:21:34 +01003680 template <typename T> T Compute(T x) const { return -x; }
3681
3682 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003683 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003684 }
3685 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003686 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003687 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003688
Roland Levillain88cb1752014-10-20 16:36:47 +01003689 DECLARE_INSTRUCTION(Neg);
3690
3691 private:
3692 DISALLOW_COPY_AND_ASSIGN(HNeg);
3693};
3694
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003695class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003696 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003697 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003698 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003699 uint32_t dex_pc,
3700 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003701 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003702 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003703 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003704 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003705 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003706 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003707 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003708 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003709 }
3710
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003711 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003712 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003713
3714 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003715 bool NeedsEnvironment() const OVERRIDE { return true; }
3716
Mingyao Yang0c365e62015-03-31 15:09:29 -07003717 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3718 bool CanThrow() const OVERRIDE { return true; }
3719
Calin Juravle10e244f2015-01-26 18:54:32 +00003720 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003721
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003722 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3723
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003724 DECLARE_INSTRUCTION(NewArray);
3725
3726 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003727 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003728 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003729 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003730
3731 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3732};
3733
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003734class HAdd : public HBinaryOperation {
3735 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003736 HAdd(Primitive::Type result_type,
3737 HInstruction* left,
3738 HInstruction* right,
3739 uint32_t dex_pc = kNoDexPc)
3740 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003741
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003742 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003743
Roland Levillain9867bc72015-08-05 10:21:34 +01003744 template <typename T> T Compute(T x, T y) const { return x + y; }
3745
3746 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003747 return GetBlock()->GetGraph()->GetIntConstant(
3748 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003749 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003750 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003751 return GetBlock()->GetGraph()->GetLongConstant(
3752 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003753 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003754
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003755 DECLARE_INSTRUCTION(Add);
3756
3757 private:
3758 DISALLOW_COPY_AND_ASSIGN(HAdd);
3759};
3760
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003761class HSub : public HBinaryOperation {
3762 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003763 HSub(Primitive::Type result_type,
3764 HInstruction* left,
3765 HInstruction* right,
3766 uint32_t dex_pc = kNoDexPc)
3767 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003768
Roland Levillain9867bc72015-08-05 10:21:34 +01003769 template <typename T> T Compute(T x, T y) const { return x - y; }
3770
3771 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003772 return GetBlock()->GetGraph()->GetIntConstant(
3773 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003774 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003775 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003776 return GetBlock()->GetGraph()->GetLongConstant(
3777 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003778 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003779
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003780 DECLARE_INSTRUCTION(Sub);
3781
3782 private:
3783 DISALLOW_COPY_AND_ASSIGN(HSub);
3784};
3785
Calin Juravle34bacdf2014-10-07 20:23:36 +01003786class HMul : public HBinaryOperation {
3787 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003788 HMul(Primitive::Type result_type,
3789 HInstruction* left,
3790 HInstruction* right,
3791 uint32_t dex_pc = kNoDexPc)
3792 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003793
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003794 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003795
Roland Levillain9867bc72015-08-05 10:21:34 +01003796 template <typename T> T Compute(T x, T y) const { return x * y; }
3797
3798 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003799 return GetBlock()->GetGraph()->GetIntConstant(
3800 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003801 }
3802 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003803 return GetBlock()->GetGraph()->GetLongConstant(
3804 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003805 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003806
3807 DECLARE_INSTRUCTION(Mul);
3808
3809 private:
3810 DISALLOW_COPY_AND_ASSIGN(HMul);
3811};
3812
Calin Juravle7c4954d2014-10-28 16:57:40 +00003813class HDiv : public HBinaryOperation {
3814 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003815 HDiv(Primitive::Type result_type,
3816 HInstruction* left,
3817 HInstruction* right,
3818 uint32_t dex_pc)
3819 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003820
Roland Levillain9867bc72015-08-05 10:21:34 +01003821 template <typename T>
3822 T Compute(T x, T y) const {
3823 // Our graph structure ensures we never have 0 for `y` during
3824 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003825 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003826 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003827 return (y == -1) ? -x : x / y;
3828 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003829
Roland Levillain9867bc72015-08-05 10:21:34 +01003830 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003831 return GetBlock()->GetGraph()->GetIntConstant(
3832 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003833 }
3834 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003835 return GetBlock()->GetGraph()->GetLongConstant(
3836 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003837 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003838
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003839 static SideEffects SideEffectsForArchRuntimeCalls() {
3840 // The generated code can use a runtime call.
3841 return SideEffects::CanTriggerGC();
3842 }
3843
Calin Juravle7c4954d2014-10-28 16:57:40 +00003844 DECLARE_INSTRUCTION(Div);
3845
3846 private:
3847 DISALLOW_COPY_AND_ASSIGN(HDiv);
3848};
3849
Calin Juravlebacfec32014-11-14 15:54:36 +00003850class HRem : public HBinaryOperation {
3851 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003852 HRem(Primitive::Type result_type,
3853 HInstruction* left,
3854 HInstruction* right,
3855 uint32_t dex_pc)
3856 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003857
Roland Levillain9867bc72015-08-05 10:21:34 +01003858 template <typename T>
3859 T Compute(T x, T y) const {
3860 // Our graph structure ensures we never have 0 for `y` during
3861 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003862 DCHECK_NE(y, 0);
3863 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3864 return (y == -1) ? 0 : x % y;
3865 }
3866
Roland Levillain9867bc72015-08-05 10:21:34 +01003867 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003868 return GetBlock()->GetGraph()->GetIntConstant(
3869 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003870 }
3871 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003872 return GetBlock()->GetGraph()->GetLongConstant(
3873 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003874 }
3875
Calin Juravlebacfec32014-11-14 15:54:36 +00003876
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003877 static SideEffects SideEffectsForArchRuntimeCalls() {
3878 return SideEffects::CanTriggerGC();
3879 }
3880
Calin Juravlebacfec32014-11-14 15:54:36 +00003881 DECLARE_INSTRUCTION(Rem);
3882
3883 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003884 DISALLOW_COPY_AND_ASSIGN(HRem);
3885};
3886
Calin Juravled0d48522014-11-04 16:40:20 +00003887class HDivZeroCheck : public HExpression<1> {
3888 public:
3889 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003890 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003891 SetRawInputAt(0, value);
3892 }
3893
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003894 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3895
Calin Juravled0d48522014-11-04 16:40:20 +00003896 bool CanBeMoved() const OVERRIDE { return true; }
3897
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003898 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003899 return true;
3900 }
3901
3902 bool NeedsEnvironment() const OVERRIDE { return true; }
3903 bool CanThrow() const OVERRIDE { return true; }
3904
Calin Juravled0d48522014-11-04 16:40:20 +00003905 DECLARE_INSTRUCTION(DivZeroCheck);
3906
3907 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003908 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3909};
3910
Calin Juravle9aec02f2014-11-18 23:06:35 +00003911class HShl : public HBinaryOperation {
3912 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003913 HShl(Primitive::Type result_type,
3914 HInstruction* left,
3915 HInstruction* right,
3916 uint32_t dex_pc = kNoDexPc)
3917 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003918
Roland Levillain9867bc72015-08-05 10:21:34 +01003919 template <typename T, typename U, typename V>
3920 T Compute(T x, U y, V max_shift_value) const {
3921 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3922 "V is not the unsigned integer type corresponding to T");
3923 return x << (y & max_shift_value);
3924 }
3925
3926 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3927 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003928 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003929 }
3930 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3931 // case is handled as `x << static_cast<int>(y)`.
3932 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3933 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003934 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003935 }
3936 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3937 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003938 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003939 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003940
3941 DECLARE_INSTRUCTION(Shl);
3942
3943 private:
3944 DISALLOW_COPY_AND_ASSIGN(HShl);
3945};
3946
3947class HShr : public HBinaryOperation {
3948 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003949 HShr(Primitive::Type result_type,
3950 HInstruction* left,
3951 HInstruction* right,
3952 uint32_t dex_pc = kNoDexPc)
3953 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003954
Roland Levillain9867bc72015-08-05 10:21:34 +01003955 template <typename T, typename U, typename V>
3956 T Compute(T x, U y, V max_shift_value) const {
3957 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3958 "V is not the unsigned integer type corresponding to T");
3959 return x >> (y & max_shift_value);
3960 }
3961
3962 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3963 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003964 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003965 }
3966 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3967 // case is handled as `x >> static_cast<int>(y)`.
3968 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3969 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003970 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003971 }
3972 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3973 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003974 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003975 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003976
3977 DECLARE_INSTRUCTION(Shr);
3978
3979 private:
3980 DISALLOW_COPY_AND_ASSIGN(HShr);
3981};
3982
3983class HUShr : public HBinaryOperation {
3984 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003985 HUShr(Primitive::Type result_type,
3986 HInstruction* left,
3987 HInstruction* right,
3988 uint32_t dex_pc = kNoDexPc)
3989 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003990
Roland Levillain9867bc72015-08-05 10:21:34 +01003991 template <typename T, typename U, typename V>
3992 T Compute(T x, U y, V max_shift_value) const {
3993 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3994 "V is not the unsigned integer type corresponding to T");
3995 V ux = static_cast<V>(x);
3996 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003997 }
3998
Roland Levillain9867bc72015-08-05 10:21:34 +01003999 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4000 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004001 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004002 }
4003 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4004 // case is handled as `x >>> static_cast<int>(y)`.
4005 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4006 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004007 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004008 }
4009 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4010 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004011 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004012 }
4013
4014 DECLARE_INSTRUCTION(UShr);
4015
4016 private:
4017 DISALLOW_COPY_AND_ASSIGN(HUShr);
4018};
4019
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004020class HAnd : public HBinaryOperation {
4021 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004022 HAnd(Primitive::Type result_type,
4023 HInstruction* left,
4024 HInstruction* right,
4025 uint32_t dex_pc = kNoDexPc)
4026 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004027
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004028 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004029
Roland Levillain9867bc72015-08-05 10:21:34 +01004030 template <typename T, typename U>
4031 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4032
4033 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004034 return GetBlock()->GetGraph()->GetIntConstant(
4035 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004036 }
4037 HConstant* Evaluate(HIntConstant* 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 }
4041 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004042 return GetBlock()->GetGraph()->GetLongConstant(
4043 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004044 }
4045 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004046 return GetBlock()->GetGraph()->GetLongConstant(
4047 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004048 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004049
4050 DECLARE_INSTRUCTION(And);
4051
4052 private:
4053 DISALLOW_COPY_AND_ASSIGN(HAnd);
4054};
4055
4056class HOr : public HBinaryOperation {
4057 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004058 HOr(Primitive::Type result_type,
4059 HInstruction* left,
4060 HInstruction* right,
4061 uint32_t dex_pc = kNoDexPc)
4062 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004063
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004064 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004065
Roland Levillain9867bc72015-08-05 10:21:34 +01004066 template <typename T, typename U>
4067 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4068
4069 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 return GetBlock()->GetGraph()->GetIntConstant(
4071 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004072 }
4073 HConstant* Evaluate(HIntConstant* 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 }
4077 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004078 return GetBlock()->GetGraph()->GetLongConstant(
4079 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004080 }
4081 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004082 return GetBlock()->GetGraph()->GetLongConstant(
4083 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004084 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004085
4086 DECLARE_INSTRUCTION(Or);
4087
4088 private:
4089 DISALLOW_COPY_AND_ASSIGN(HOr);
4090};
4091
4092class HXor : public HBinaryOperation {
4093 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004094 HXor(Primitive::Type result_type,
4095 HInstruction* left,
4096 HInstruction* right,
4097 uint32_t dex_pc = kNoDexPc)
4098 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004099
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004100 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004101
Roland Levillain9867bc72015-08-05 10:21:34 +01004102 template <typename T, typename U>
4103 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4104
4105 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004106 return GetBlock()->GetGraph()->GetIntConstant(
4107 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004108 }
4109 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004110 return GetBlock()->GetGraph()->GetLongConstant(
4111 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004112 }
4113 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004114 return GetBlock()->GetGraph()->GetLongConstant(
4115 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004116 }
4117 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004118 return GetBlock()->GetGraph()->GetLongConstant(
4119 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004120 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004121
4122 DECLARE_INSTRUCTION(Xor);
4123
4124 private:
4125 DISALLOW_COPY_AND_ASSIGN(HXor);
4126};
4127
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004128// The value of a parameter in this method. Its location depends on
4129// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004130class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004131 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004132 HParameterValue(const DexFile& dex_file,
4133 uint16_t type_index,
4134 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004135 Primitive::Type parameter_type,
4136 bool is_this = false)
4137 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004138 dex_file_(dex_file),
4139 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004140 index_(index),
4141 is_this_(is_this),
4142 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004143
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004144 const DexFile& GetDexFile() const { return dex_file_; }
4145 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004146 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004147 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004148
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004149 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4150 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004151
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004152 DECLARE_INSTRUCTION(ParameterValue);
4153
4154 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004155 const DexFile& dex_file_;
4156 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004157 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004158 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004159 const uint8_t index_;
4160
Calin Juravle10e244f2015-01-26 18:54:32 +00004161 // Whether or not the parameter value corresponds to 'this' argument.
4162 const bool is_this_;
4163
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004164 bool can_be_null_;
4165
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004166 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4167};
4168
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004169class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004170 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004171 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4172 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004173
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004174 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004175 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004176 return true;
4177 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004178
Roland Levillain9867bc72015-08-05 10:21:34 +01004179 template <typename T> T Compute(T x) const { return ~x; }
4180
4181 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004182 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004183 }
4184 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004185 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004186 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004187
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004188 DECLARE_INSTRUCTION(Not);
4189
4190 private:
4191 DISALLOW_COPY_AND_ASSIGN(HNot);
4192};
4193
David Brazdil66d126e2015-04-03 16:02:44 +01004194class HBooleanNot : public HUnaryOperation {
4195 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004196 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4197 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004198
4199 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004200 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004201 return true;
4202 }
4203
Roland Levillain9867bc72015-08-05 10:21:34 +01004204 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004205 DCHECK(IsUint<1>(x));
4206 return !x;
4207 }
4208
Roland Levillain9867bc72015-08-05 10:21:34 +01004209 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004211 }
4212 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4213 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004214 UNREACHABLE();
4215 }
4216
4217 DECLARE_INSTRUCTION(BooleanNot);
4218
4219 private:
4220 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4221};
4222
Roland Levillaindff1f282014-11-05 14:15:05 +00004223class HTypeConversion : public HExpression<1> {
4224 public:
4225 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004226 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004227 : HExpression(result_type,
4228 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4229 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004230 SetRawInputAt(0, input);
4231 DCHECK_NE(input->GetType(), result_type);
4232 }
4233
4234 HInstruction* GetInput() const { return InputAt(0); }
4235 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4236 Primitive::Type GetResultType() const { return GetType(); }
4237
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004238 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004239 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004240
Roland Levillaindff1f282014-11-05 14:15:05 +00004241 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004242 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004243
Mark Mendelle82549b2015-05-06 10:55:34 -04004244 // Try to statically evaluate the conversion and return a HConstant
4245 // containing the result. If the input cannot be converted, return nullptr.
4246 HConstant* TryStaticEvaluation() const;
4247
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004248 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4249 Primitive::Type result_type) {
4250 // Some architectures may not require the 'GC' side effects, but at this point
4251 // in the compilation process we do not know what architecture we will
4252 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004253 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4254 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004255 return SideEffects::CanTriggerGC();
4256 }
4257 return SideEffects::None();
4258 }
4259
Roland Levillaindff1f282014-11-05 14:15:05 +00004260 DECLARE_INSTRUCTION(TypeConversion);
4261
4262 private:
4263 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4264};
4265
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004266static constexpr uint32_t kNoRegNumber = -1;
4267
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004268class HPhi : public HInstruction {
4269 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004270 HPhi(ArenaAllocator* arena,
4271 uint32_t reg_number,
4272 size_t number_of_inputs,
4273 Primitive::Type type,
4274 uint32_t dex_pc = kNoDexPc)
4275 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004276 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004277 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004278 type_(type),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004279 is_live_(false),
Calin Juravle10e244f2015-01-26 18:54:32 +00004280 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004281 }
4282
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004283 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4284 static Primitive::Type ToPhiType(Primitive::Type type) {
4285 switch (type) {
4286 case Primitive::kPrimBoolean:
4287 case Primitive::kPrimByte:
4288 case Primitive::kPrimShort:
4289 case Primitive::kPrimChar:
4290 return Primitive::kPrimInt;
4291 default:
4292 return type;
4293 }
4294 }
4295
David Brazdilffee3d32015-07-06 11:48:53 +01004296 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4297
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004298 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004299
4300 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004301 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004302
Calin Juravle10e244f2015-01-26 18:54:32 +00004303 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004304 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004305
Calin Juravle10e244f2015-01-26 18:54:32 +00004306 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4307 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4308
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004309 uint32_t GetRegNumber() const { return reg_number_; }
4310
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004311 void SetDead() { is_live_ = false; }
4312 void SetLive() { is_live_ = true; }
4313 bool IsDead() const { return !is_live_; }
4314 bool IsLive() const { return is_live_; }
4315
David Brazdil77a48ae2015-09-15 12:34:04 +00004316 bool IsVRegEquivalentOf(HInstruction* other) const {
4317 return other != nullptr
4318 && other->IsPhi()
4319 && other->AsPhi()->GetBlock() == GetBlock()
4320 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4321 }
4322
Calin Juravlea4f88312015-04-16 12:57:19 +01004323 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4324 // An equivalent phi is a phi having the same dex register and type.
4325 // It assumes that phis with the same dex register are adjacent.
4326 HPhi* GetNextEquivalentPhiWithSameType() {
4327 HInstruction* next = GetNext();
4328 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4329 if (next->GetType() == GetType()) {
4330 return next->AsPhi();
4331 }
4332 next = next->GetNext();
4333 }
4334 return nullptr;
4335 }
4336
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004337 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004338
David Brazdil1abb4192015-02-17 18:33:36 +00004339 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004340 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004341 return inputs_[index];
4342 }
David Brazdil1abb4192015-02-17 18:33:36 +00004343
4344 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004345 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004346 }
4347
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004348 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004349 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004350 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004351 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004352 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004353 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004354
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004355 DISALLOW_COPY_AND_ASSIGN(HPhi);
4356};
4357
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004358class HNullCheck : public HExpression<1> {
4359 public:
4360 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004361 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004362 SetRawInputAt(0, value);
4363 }
4364
Calin Juravle10e244f2015-01-26 18:54:32 +00004365 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004366 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004367 return true;
4368 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004369
Calin Juravle10e244f2015-01-26 18:54:32 +00004370 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371
Calin Juravle10e244f2015-01-26 18:54:32 +00004372 bool CanThrow() const OVERRIDE { return true; }
4373
4374 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004375
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004376
4377 DECLARE_INSTRUCTION(NullCheck);
4378
4379 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004380 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4381};
4382
4383class FieldInfo : public ValueObject {
4384 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004385 FieldInfo(MemberOffset field_offset,
4386 Primitive::Type field_type,
4387 bool is_volatile,
4388 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004389 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004390 const DexFile& dex_file,
4391 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004392 : field_offset_(field_offset),
4393 field_type_(field_type),
4394 is_volatile_(is_volatile),
4395 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004396 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004397 dex_file_(dex_file),
4398 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004399
4400 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004401 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004402 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004403 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004404 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004405 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004406 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004407
4408 private:
4409 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004410 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004411 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004412 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004413 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004414 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004415 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004416};
4417
4418class HInstanceFieldGet : public HExpression<1> {
4419 public:
4420 HInstanceFieldGet(HInstruction* value,
4421 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004422 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004423 bool is_volatile,
4424 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004425 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004426 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004427 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004428 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004429 : HExpression(field_type,
4430 SideEffects::FieldReadOfType(field_type, is_volatile),
4431 dex_pc),
4432 field_info_(field_offset,
4433 field_type,
4434 is_volatile,
4435 field_idx,
4436 declaring_class_def_index,
4437 dex_file,
4438 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004439 SetRawInputAt(0, value);
4440 }
4441
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004442 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004443
4444 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4445 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4446 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004447 }
4448
Calin Juravle641547a2015-04-21 22:08:51 +01004449 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4450 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004451 }
4452
4453 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004454 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4455 }
4456
Calin Juravle52c48962014-12-16 17:02:57 +00004457 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004458 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004459 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004460 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004461
4462 DECLARE_INSTRUCTION(InstanceFieldGet);
4463
4464 private:
4465 const FieldInfo field_info_;
4466
4467 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4468};
4469
4470class HInstanceFieldSet : public HTemplateInstruction<2> {
4471 public:
4472 HInstanceFieldSet(HInstruction* object,
4473 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004474 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004475 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004476 bool is_volatile,
4477 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004478 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004479 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004480 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004481 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004482 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4483 dex_pc),
4484 field_info_(field_offset,
4485 field_type,
4486 is_volatile,
4487 field_idx,
4488 declaring_class_def_index,
4489 dex_file,
4490 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004491 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004492 SetRawInputAt(0, object);
4493 SetRawInputAt(1, value);
4494 }
4495
Calin Juravle641547a2015-04-21 22:08:51 +01004496 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4497 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004498 }
4499
Calin Juravle52c48962014-12-16 17:02:57 +00004500 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004501 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004502 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004503 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004504 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004505 bool GetValueCanBeNull() const { return value_can_be_null_; }
4506 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004507
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004508 DECLARE_INSTRUCTION(InstanceFieldSet);
4509
4510 private:
4511 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004512 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004513
4514 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4515};
4516
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004517class HArrayGet : public HExpression<2> {
4518 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004519 HArrayGet(HInstruction* array,
4520 HInstruction* index,
4521 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004522 uint32_t dex_pc,
4523 SideEffects additional_side_effects = SideEffects::None())
4524 : HExpression(type,
4525 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004526 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004527 SetRawInputAt(0, array);
4528 SetRawInputAt(1, index);
4529 }
4530
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004531 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004532 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004533 return true;
4534 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004535 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004536 // TODO: We can be smarter here.
4537 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4538 // which generates the implicit null check. There are cases when these can be removed
4539 // to produce better code. If we ever add optimizations to do so we should allow an
4540 // implicit check here (as long as the address falls in the first page).
4541 return false;
4542 }
4543
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004544 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004545
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004546 HInstruction* GetArray() const { return InputAt(0); }
4547 HInstruction* GetIndex() const { return InputAt(1); }
4548
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004549 DECLARE_INSTRUCTION(ArrayGet);
4550
4551 private:
4552 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4553};
4554
4555class HArraySet : public HTemplateInstruction<3> {
4556 public:
4557 HArraySet(HInstruction* array,
4558 HInstruction* index,
4559 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004560 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004561 uint32_t dex_pc,
4562 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004563 : HTemplateInstruction(
4564 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004565 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4566 additional_side_effects),
4567 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004568 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004569 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004570 value_can_be_null_(true),
4571 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004572 SetRawInputAt(0, array);
4573 SetRawInputAt(1, index);
4574 SetRawInputAt(2, value);
4575 }
4576
Calin Juravle77520bc2015-01-12 18:45:46 +00004577 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004578 // We currently always call a runtime method to catch array store
4579 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004580 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004581 }
4582
Mingyao Yang81014cb2015-06-02 03:16:27 -07004583 // Can throw ArrayStoreException.
4584 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4585
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004586 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004587 // TODO: Same as for ArrayGet.
4588 return false;
4589 }
4590
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004591 void ClearNeedsTypeCheck() {
4592 needs_type_check_ = false;
4593 }
4594
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004595 void ClearValueCanBeNull() {
4596 value_can_be_null_ = false;
4597 }
4598
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004599 void SetStaticTypeOfArrayIsObjectArray() {
4600 static_type_of_array_is_object_array_ = true;
4601 }
4602
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004603 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004604 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004605 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004606
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004607 HInstruction* GetArray() const { return InputAt(0); }
4608 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004609 HInstruction* GetValue() const { return InputAt(2); }
4610
4611 Primitive::Type GetComponentType() const {
4612 // The Dex format does not type floating point index operations. Since the
4613 // `expected_component_type_` is set during building and can therefore not
4614 // be correct, we also check what is the value type. If it is a floating
4615 // point type, we must use that type.
4616 Primitive::Type value_type = GetValue()->GetType();
4617 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4618 ? value_type
4619 : expected_component_type_;
4620 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004621
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004622 Primitive::Type GetRawExpectedComponentType() const {
4623 return expected_component_type_;
4624 }
4625
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004626 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4627 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4628 }
4629
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004630 DECLARE_INSTRUCTION(ArraySet);
4631
4632 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004633 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004634 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004635 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004636 // Cached information for the reference_type_info_ so that codegen
4637 // does not need to inspect the static type.
4638 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004639
4640 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4641};
4642
4643class HArrayLength : public HExpression<1> {
4644 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004645 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004646 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004647 // Note that arrays do not change length, so the instruction does not
4648 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004649 SetRawInputAt(0, array);
4650 }
4651
Calin Juravle77520bc2015-01-12 18:45:46 +00004652 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004653 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004654 return true;
4655 }
Calin Juravle641547a2015-04-21 22:08:51 +01004656 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4657 return obj == InputAt(0);
4658 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004659
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004660 DECLARE_INSTRUCTION(ArrayLength);
4661
4662 private:
4663 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4664};
4665
4666class HBoundsCheck : public HExpression<2> {
4667 public:
4668 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004669 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004670 DCHECK(index->GetType() == Primitive::kPrimInt);
4671 SetRawInputAt(0, index);
4672 SetRawInputAt(1, length);
4673 }
4674
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004675 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004676 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004677 return true;
4678 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004679
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004680 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004681
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004682 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004683
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004684 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004685
4686 DECLARE_INSTRUCTION(BoundsCheck);
4687
4688 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004689 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4690};
4691
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004692/**
4693 * Some DEX instructions are folded into multiple HInstructions that need
4694 * to stay live until the last HInstruction. This class
4695 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004696 * HInstruction stays live. `index` represents the stack location index of the
4697 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004698 */
4699class HTemporary : public HTemplateInstruction<0> {
4700 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004701 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4702 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004703
4704 size_t GetIndex() const { return index_; }
4705
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004706 Primitive::Type GetType() const OVERRIDE {
4707 // The previous instruction is the one that will be stored in the temporary location.
4708 DCHECK(GetPrevious() != nullptr);
4709 return GetPrevious()->GetType();
4710 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004711
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004712 DECLARE_INSTRUCTION(Temporary);
4713
4714 private:
4715 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004716 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4717};
4718
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004719class HSuspendCheck : public HTemplateInstruction<0> {
4720 public:
4721 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004722 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004723
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004724 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004725 return true;
4726 }
4727
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004728 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4729 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004730
4731 DECLARE_INSTRUCTION(SuspendCheck);
4732
4733 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004734 // Only used for code generation, in order to share the same slow path between back edges
4735 // of a same loop.
4736 SlowPathCode* slow_path_;
4737
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004738 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4739};
4740
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004741/**
4742 * Instruction to load a Class object.
4743 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004744class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004745 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004746 HLoadClass(HCurrentMethod* current_method,
4747 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004748 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004749 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004750 uint32_t dex_pc,
4751 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004752 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004753 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004754 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004755 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004756 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004757 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004758 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004759 // Referrers class should not need access check. We never inline unverified
4760 // methods so we can't possibly end up in this situation.
4761 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004762 SetRawInputAt(0, current_method);
4763 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004764
4765 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004766
4767 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004768 // Note that we don't need to test for generate_clinit_check_.
4769 // Whether or not we need to generate the clinit check is processed in
4770 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004771 return other->AsLoadClass()->type_index_ == type_index_ &&
4772 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004773 }
4774
4775 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4776
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004777 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004778 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004779 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004780
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004781 bool NeedsEnvironment() const OVERRIDE {
4782 // Will call runtime and load the class if the class is not loaded yet.
4783 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004784 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004785 }
4786
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004787 bool MustGenerateClinitCheck() const {
4788 return generate_clinit_check_;
4789 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004790 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004791 // The entrypoint the code generator is going to call does not do
4792 // clinit of the class.
4793 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004794 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004795 }
4796
4797 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004798 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4799 }
4800
4801 bool NeedsAccessCheck() const {
4802 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004803 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004804
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004805 bool CanThrow() const OVERRIDE {
4806 // May call runtime and and therefore can throw.
4807 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004808 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004809 }
4810
Calin Juravleacf735c2015-02-12 15:25:22 +00004811 ReferenceTypeInfo GetLoadedClassRTI() {
4812 return loaded_class_rti_;
4813 }
4814
4815 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4816 // Make sure we only set exact types (the loaded class should never be merged).
4817 DCHECK(rti.IsExact());
4818 loaded_class_rti_ = rti;
4819 }
4820
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004821 const DexFile& GetDexFile() { return dex_file_; }
4822
Vladimir Markodc151b22015-10-15 18:02:30 +01004823 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004824
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004825 static SideEffects SideEffectsForArchRuntimeCalls() {
4826 return SideEffects::CanTriggerGC();
4827 }
4828
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004829 DECLARE_INSTRUCTION(LoadClass);
4830
4831 private:
4832 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004833 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004834 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004835 // Whether this instruction must generate the initialization check.
4836 // Used for code generation.
4837 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004838 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004839
Calin Juravleacf735c2015-02-12 15:25:22 +00004840 ReferenceTypeInfo loaded_class_rti_;
4841
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004842 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4843};
4844
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004845class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004846 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004847 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4849 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004850 SetRawInputAt(0, current_method);
4851 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004852
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004853 bool CanBeMoved() const OVERRIDE { return true; }
4854
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004855 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4856 return other->AsLoadString()->string_index_ == string_index_;
4857 }
4858
4859 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4860
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004861 uint32_t GetStringIndex() const { return string_index_; }
4862
4863 // TODO: Can we deopt or debug when we resolve a string?
4864 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004865 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004866 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004867
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004868 static SideEffects SideEffectsForArchRuntimeCalls() {
4869 return SideEffects::CanTriggerGC();
4870 }
4871
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004872 DECLARE_INSTRUCTION(LoadString);
4873
4874 private:
4875 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004876
4877 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4878};
4879
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004880/**
4881 * Performs an initialization check on its Class object input.
4882 */
4883class HClinitCheck : public HExpression<1> {
4884 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004885 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004886 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004887 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004888 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4889 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004890 SetRawInputAt(0, constant);
4891 }
4892
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004893 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004894 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004895 return true;
4896 }
4897
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004898 bool NeedsEnvironment() const OVERRIDE {
4899 // May call runtime to initialize the class.
4900 return true;
4901 }
4902
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004903
4904 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4905
4906 DECLARE_INSTRUCTION(ClinitCheck);
4907
4908 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004909 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4910};
4911
4912class HStaticFieldGet : public HExpression<1> {
4913 public:
4914 HStaticFieldGet(HInstruction* cls,
4915 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004916 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004917 bool is_volatile,
4918 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004919 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004920 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004921 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004922 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004923 : HExpression(field_type,
4924 SideEffects::FieldReadOfType(field_type, is_volatile),
4925 dex_pc),
4926 field_info_(field_offset,
4927 field_type,
4928 is_volatile,
4929 field_idx,
4930 declaring_class_def_index,
4931 dex_file,
4932 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004933 SetRawInputAt(0, cls);
4934 }
4935
Calin Juravle52c48962014-12-16 17:02:57 +00004936
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004937 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004938
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004939 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004940 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4941 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004942 }
4943
4944 size_t ComputeHashCode() const OVERRIDE {
4945 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4946 }
4947
Calin Juravle52c48962014-12-16 17:02:57 +00004948 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004949 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4950 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004951 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004952
4953 DECLARE_INSTRUCTION(StaticFieldGet);
4954
4955 private:
4956 const FieldInfo field_info_;
4957
4958 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4959};
4960
4961class HStaticFieldSet : public HTemplateInstruction<2> {
4962 public:
4963 HStaticFieldSet(HInstruction* cls,
4964 HInstruction* value,
4965 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004966 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004967 bool is_volatile,
4968 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004969 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004970 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004971 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004972 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004973 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4974 dex_pc),
4975 field_info_(field_offset,
4976 field_type,
4977 is_volatile,
4978 field_idx,
4979 declaring_class_def_index,
4980 dex_file,
4981 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004982 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004983 SetRawInputAt(0, cls);
4984 SetRawInputAt(1, value);
4985 }
4986
Calin Juravle52c48962014-12-16 17:02:57 +00004987 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004988 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4989 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004990 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004991
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004992 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004993 bool GetValueCanBeNull() const { return value_can_be_null_; }
4994 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004995
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004996 DECLARE_INSTRUCTION(StaticFieldSet);
4997
4998 private:
4999 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005000 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005001
5002 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5003};
5004
Calin Juravlee460d1d2015-09-29 04:52:17 +01005005class HUnresolvedInstanceFieldGet : public HExpression<1> {
5006 public:
5007 HUnresolvedInstanceFieldGet(HInstruction* obj,
5008 Primitive::Type field_type,
5009 uint32_t field_index,
5010 uint32_t dex_pc)
5011 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5012 field_index_(field_index) {
5013 SetRawInputAt(0, obj);
5014 }
5015
5016 bool NeedsEnvironment() const OVERRIDE { return true; }
5017 bool CanThrow() const OVERRIDE { return true; }
5018
5019 Primitive::Type GetFieldType() const { return GetType(); }
5020 uint32_t GetFieldIndex() const { return field_index_; }
5021
5022 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5023
5024 private:
5025 const uint32_t field_index_;
5026
5027 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5028};
5029
5030class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5031 public:
5032 HUnresolvedInstanceFieldSet(HInstruction* obj,
5033 HInstruction* value,
5034 Primitive::Type field_type,
5035 uint32_t field_index,
5036 uint32_t dex_pc)
5037 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5038 field_type_(field_type),
5039 field_index_(field_index) {
5040 DCHECK_EQ(field_type, value->GetType());
5041 SetRawInputAt(0, obj);
5042 SetRawInputAt(1, value);
5043 }
5044
5045 bool NeedsEnvironment() const OVERRIDE { return true; }
5046 bool CanThrow() const OVERRIDE { return true; }
5047
5048 Primitive::Type GetFieldType() const { return field_type_; }
5049 uint32_t GetFieldIndex() const { return field_index_; }
5050
5051 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5052
5053 private:
5054 const Primitive::Type field_type_;
5055 const uint32_t field_index_;
5056
5057 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5058};
5059
5060class HUnresolvedStaticFieldGet : public HExpression<0> {
5061 public:
5062 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5063 uint32_t field_index,
5064 uint32_t dex_pc)
5065 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5066 field_index_(field_index) {
5067 }
5068
5069 bool NeedsEnvironment() const OVERRIDE { return true; }
5070 bool CanThrow() const OVERRIDE { return true; }
5071
5072 Primitive::Type GetFieldType() const { return GetType(); }
5073 uint32_t GetFieldIndex() const { return field_index_; }
5074
5075 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5076
5077 private:
5078 const uint32_t field_index_;
5079
5080 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5081};
5082
5083class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5084 public:
5085 HUnresolvedStaticFieldSet(HInstruction* value,
5086 Primitive::Type field_type,
5087 uint32_t field_index,
5088 uint32_t dex_pc)
5089 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5090 field_type_(field_type),
5091 field_index_(field_index) {
5092 DCHECK_EQ(field_type, value->GetType());
5093 SetRawInputAt(0, value);
5094 }
5095
5096 bool NeedsEnvironment() const OVERRIDE { return true; }
5097 bool CanThrow() const OVERRIDE { return true; }
5098
5099 Primitive::Type GetFieldType() const { return field_type_; }
5100 uint32_t GetFieldIndex() const { return field_index_; }
5101
5102 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5103
5104 private:
5105 const Primitive::Type field_type_;
5106 const uint32_t field_index_;
5107
5108 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5109};
5110
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005111// Implement the move-exception DEX instruction.
5112class HLoadException : public HExpression<0> {
5113 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005114 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5115 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005116
David Brazdilbbd733e2015-08-18 17:48:17 +01005117 bool CanBeNull() const OVERRIDE { return false; }
5118
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005119 DECLARE_INSTRUCTION(LoadException);
5120
5121 private:
5122 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5123};
5124
David Brazdilcb1c0552015-08-04 16:22:25 +01005125// Implicit part of move-exception which clears thread-local exception storage.
5126// Must not be removed because the runtime expects the TLS to get cleared.
5127class HClearException : public HTemplateInstruction<0> {
5128 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005129 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5130 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005131
5132 DECLARE_INSTRUCTION(ClearException);
5133
5134 private:
5135 DISALLOW_COPY_AND_ASSIGN(HClearException);
5136};
5137
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005138class HThrow : public HTemplateInstruction<1> {
5139 public:
5140 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005141 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005142 SetRawInputAt(0, exception);
5143 }
5144
5145 bool IsControlFlow() const OVERRIDE { return true; }
5146
5147 bool NeedsEnvironment() const OVERRIDE { return true; }
5148
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005149 bool CanThrow() const OVERRIDE { return true; }
5150
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005151
5152 DECLARE_INSTRUCTION(Throw);
5153
5154 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005155 DISALLOW_COPY_AND_ASSIGN(HThrow);
5156};
5157
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005158/**
5159 * Implementation strategies for the code generator of a HInstanceOf
5160 * or `HCheckCast`.
5161 */
5162enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005163 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005164 kExactCheck, // Can do a single class compare.
5165 kClassHierarchyCheck, // Can just walk the super class chain.
5166 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5167 kInterfaceCheck, // No optimization yet when checking against an interface.
5168 kArrayObjectCheck, // Can just check if the array is not primitive.
5169 kArrayCheck // No optimization yet when checking against a generic array.
5170};
5171
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005172class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005173 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005174 HInstanceOf(HInstruction* object,
5175 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005176 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005177 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005178 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005179 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005180 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005181 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005182 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005183 SetRawInputAt(0, object);
5184 SetRawInputAt(1, constant);
5185 }
5186
5187 bool CanBeMoved() const OVERRIDE { return true; }
5188
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005189 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005190 return true;
5191 }
5192
5193 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005194 return false;
5195 }
5196
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005197 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5198
5199 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005200
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005201 // Used only in code generation.
5202 bool MustDoNullCheck() const { return must_do_null_check_; }
5203 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5204
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005205 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5206 return (check_kind == TypeCheckKind::kExactCheck)
5207 ? SideEffects::None()
5208 // Mips currently does runtime calls for any other checks.
5209 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005210 }
5211
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005212 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005213
5214 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005215 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005216 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005217
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005218 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5219};
5220
Calin Juravleb1498f62015-02-16 13:13:29 +00005221class HBoundType : public HExpression<1> {
5222 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005223 // Constructs an HBoundType with the given upper_bound.
5224 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005225 HBoundType(HInstruction* input,
5226 ReferenceTypeInfo upper_bound,
5227 bool upper_can_be_null,
5228 uint32_t dex_pc = kNoDexPc)
5229 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005230 upper_bound_(upper_bound),
5231 upper_can_be_null_(upper_can_be_null),
5232 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005233 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005234 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005235 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005236 }
5237
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005238 // GetUpper* should only be used in reference type propagation.
5239 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5240 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005241
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005242 void SetCanBeNull(bool can_be_null) {
5243 DCHECK(upper_can_be_null_ || !can_be_null);
5244 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005245 }
5246
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005247 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5248
Calin Juravleb1498f62015-02-16 13:13:29 +00005249 DECLARE_INSTRUCTION(BoundType);
5250
5251 private:
5252 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005253 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5254 // It is used to bound the type in cases like:
5255 // if (x instanceof ClassX) {
5256 // // uper_bound_ will be ClassX
5257 // }
5258 const ReferenceTypeInfo upper_bound_;
5259 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5260 // is false then can_be_null_ cannot be true).
5261 const bool upper_can_be_null_;
5262 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005263
5264 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5265};
5266
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005267class HCheckCast : public HTemplateInstruction<2> {
5268 public:
5269 HCheckCast(HInstruction* object,
5270 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005271 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005272 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005273 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005274 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005275 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005276 SetRawInputAt(0, object);
5277 SetRawInputAt(1, constant);
5278 }
5279
5280 bool CanBeMoved() const OVERRIDE { return true; }
5281
5282 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5283 return true;
5284 }
5285
5286 bool NeedsEnvironment() const OVERRIDE {
5287 // Instruction may throw a CheckCastError.
5288 return true;
5289 }
5290
5291 bool CanThrow() const OVERRIDE { return true; }
5292
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005293 bool MustDoNullCheck() const { return must_do_null_check_; }
5294 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005295 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005296
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005297 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005298
5299 DECLARE_INSTRUCTION(CheckCast);
5300
5301 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005302 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005303 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005304
5305 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005306};
5307
Calin Juravle27df7582015-04-17 19:12:31 +01005308class HMemoryBarrier : public HTemplateInstruction<0> {
5309 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005310 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005311 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005312 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005313 barrier_kind_(barrier_kind) {}
5314
5315 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5316
5317 DECLARE_INSTRUCTION(MemoryBarrier);
5318
5319 private:
5320 const MemBarrierKind barrier_kind_;
5321
5322 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5323};
5324
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005325class HMonitorOperation : public HTemplateInstruction<1> {
5326 public:
5327 enum OperationKind {
5328 kEnter,
5329 kExit,
5330 };
5331
5332 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005333 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005334 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5335 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005336 SetRawInputAt(0, object);
5337 }
5338
5339 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005340 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5341
5342 bool CanThrow() const OVERRIDE {
5343 // Verifier guarantees that monitor-exit cannot throw.
5344 // This is important because it allows the HGraphBuilder to remove
5345 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5346 return IsEnter();
5347 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005348
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005349
5350 bool IsEnter() const { return kind_ == kEnter; }
5351
5352 DECLARE_INSTRUCTION(MonitorOperation);
5353
Calin Juravle52c48962014-12-16 17:02:57 +00005354 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005355 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005356
5357 private:
5358 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5359};
5360
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005361/**
5362 * A HInstruction used as a marker for the replacement of new + <init>
5363 * of a String to a call to a StringFactory. Only baseline will see
5364 * the node at code generation, where it will be be treated as null.
5365 * When compiling non-baseline, `HFakeString` instructions are being removed
5366 * in the instruction simplifier.
5367 */
5368class HFakeString : public HTemplateInstruction<0> {
5369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005370 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5371 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005372
5373 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5374
5375 DECLARE_INSTRUCTION(FakeString);
5376
5377 private:
5378 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5379};
5380
Vladimir Markof9f64412015-09-02 14:05:49 +01005381class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005382 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005383 MoveOperands(Location source,
5384 Location destination,
5385 Primitive::Type type,
5386 HInstruction* instruction)
5387 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005388
5389 Location GetSource() const { return source_; }
5390 Location GetDestination() const { return destination_; }
5391
5392 void SetSource(Location value) { source_ = value; }
5393 void SetDestination(Location value) { destination_ = value; }
5394
5395 // The parallel move resolver marks moves as "in-progress" by clearing the
5396 // destination (but not the source).
5397 Location MarkPending() {
5398 DCHECK(!IsPending());
5399 Location dest = destination_;
5400 destination_ = Location::NoLocation();
5401 return dest;
5402 }
5403
5404 void ClearPending(Location dest) {
5405 DCHECK(IsPending());
5406 destination_ = dest;
5407 }
5408
5409 bool IsPending() const {
5410 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5411 return destination_.IsInvalid() && !source_.IsInvalid();
5412 }
5413
5414 // True if this blocks a move from the given location.
5415 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005416 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005417 }
5418
5419 // A move is redundant if it's been eliminated, if its source and
5420 // destination are the same, or if its destination is unneeded.
5421 bool IsRedundant() const {
5422 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5423 }
5424
5425 // We clear both operands to indicate move that's been eliminated.
5426 void Eliminate() {
5427 source_ = destination_ = Location::NoLocation();
5428 }
5429
5430 bool IsEliminated() const {
5431 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5432 return source_.IsInvalid();
5433 }
5434
Alexey Frunze4dda3372015-06-01 18:31:49 -07005435 Primitive::Type GetType() const { return type_; }
5436
Nicolas Geoffray90218252015-04-15 11:56:51 +01005437 bool Is64BitMove() const {
5438 return Primitive::Is64BitType(type_);
5439 }
5440
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005441 HInstruction* GetInstruction() const { return instruction_; }
5442
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005443 private:
5444 Location source_;
5445 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005446 // The type this move is for.
5447 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005448 // The instruction this move is assocatied with. Null when this move is
5449 // for moving an input in the expected locations of user (including a phi user).
5450 // This is only used in debug mode, to ensure we do not connect interval siblings
5451 // in the same parallel move.
5452 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005453};
5454
5455static constexpr size_t kDefaultNumberOfMoves = 4;
5456
5457class HParallelMove : public HTemplateInstruction<0> {
5458 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005459 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005460 : HTemplateInstruction(SideEffects::None(), dex_pc),
5461 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5462 moves_.reserve(kDefaultNumberOfMoves);
5463 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005464
Nicolas Geoffray90218252015-04-15 11:56:51 +01005465 void AddMove(Location source,
5466 Location destination,
5467 Primitive::Type type,
5468 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005469 DCHECK(source.IsValid());
5470 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005471 if (kIsDebugBuild) {
5472 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005473 for (const MoveOperands& move : moves_) {
5474 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005475 // Special case the situation where the move is for the spill slot
5476 // of the instruction.
5477 if ((GetPrevious() == instruction)
5478 || ((GetPrevious() == nullptr)
5479 && instruction->IsPhi()
5480 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005481 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005482 << "Doing parallel moves for the same instruction.";
5483 } else {
5484 DCHECK(false) << "Doing parallel moves for the same instruction.";
5485 }
5486 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005487 }
5488 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005489 for (const MoveOperands& move : moves_) {
5490 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005491 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005492 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005493 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005494 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005495 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005496 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005497 }
5498
Vladimir Marko225b6462015-09-28 12:17:40 +01005499 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005500 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005501 }
5502
Vladimir Marko225b6462015-09-28 12:17:40 +01005503 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005504
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005505 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005506
5507 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005508 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005509
5510 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5511};
5512
Mark Mendell0616ae02015-04-17 12:49:27 -04005513} // namespace art
5514
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005515#ifdef ART_ENABLE_CODEGEN_arm64
5516#include "nodes_arm64.h"
5517#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005518#ifdef ART_ENABLE_CODEGEN_x86
5519#include "nodes_x86.h"
5520#endif
5521
5522namespace art {
5523
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005524class HGraphVisitor : public ValueObject {
5525 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005526 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5527 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005528
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005529 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005530 virtual void VisitBasicBlock(HBasicBlock* block);
5531
Roland Levillain633021e2014-10-01 14:12:25 +01005532 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005533 void VisitInsertionOrder();
5534
Roland Levillain633021e2014-10-01 14:12:25 +01005535 // Visit the graph following dominator tree reverse post-order.
5536 void VisitReversePostOrder();
5537
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005538 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005539
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005540 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005541#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005542 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5543
5544 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5545
5546#undef DECLARE_VISIT_INSTRUCTION
5547
5548 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005549 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005550
5551 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5552};
5553
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005554class HGraphDelegateVisitor : public HGraphVisitor {
5555 public:
5556 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5557 virtual ~HGraphDelegateVisitor() {}
5558
5559 // Visit functions that delegate to to super class.
5560#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005561 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005562
5563 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5564
5565#undef DECLARE_VISIT_INSTRUCTION
5566
5567 private:
5568 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5569};
5570
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005571class HInsertionOrderIterator : public ValueObject {
5572 public:
5573 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5574
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005575 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005576 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005577 void Advance() { ++index_; }
5578
5579 private:
5580 const HGraph& graph_;
5581 size_t index_;
5582
5583 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5584};
5585
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005586class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005587 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005588 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5589 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005590 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005591 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005592
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005593 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5594 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005595 void Advance() { ++index_; }
5596
5597 private:
5598 const HGraph& graph_;
5599 size_t index_;
5600
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005601 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005602};
5603
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005604class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005605 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005606 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005607 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005608 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005609 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005610 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005611
5612 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005613 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005614 void Advance() { --index_; }
5615
5616 private:
5617 const HGraph& graph_;
5618 size_t index_;
5619
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005620 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005621};
5622
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005623class HLinearPostOrderIterator : public ValueObject {
5624 public:
5625 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005626 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005627
5628 bool Done() const { return index_ == 0; }
5629
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005630 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005631
5632 void Advance() {
5633 --index_;
5634 DCHECK_GE(index_, 0U);
5635 }
5636
5637 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005638 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005639 size_t index_;
5640
5641 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5642};
5643
5644class HLinearOrderIterator : public ValueObject {
5645 public:
5646 explicit HLinearOrderIterator(const HGraph& graph)
5647 : order_(graph.GetLinearOrder()), index_(0) {}
5648
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005649 bool Done() const { return index_ == order_.size(); }
5650 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005651 void Advance() { ++index_; }
5652
5653 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005654 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005655 size_t index_;
5656
5657 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5658};
5659
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005660// Iterator over the blocks that art part of the loop. Includes blocks part
5661// of an inner loop. The order in which the blocks are iterated is on their
5662// block id.
5663class HBlocksInLoopIterator : public ValueObject {
5664 public:
5665 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5666 : blocks_in_loop_(info.GetBlocks()),
5667 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5668 index_(0) {
5669 if (!blocks_in_loop_.IsBitSet(index_)) {
5670 Advance();
5671 }
5672 }
5673
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005674 bool Done() const { return index_ == blocks_.size(); }
5675 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005676 void Advance() {
5677 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005678 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005679 if (blocks_in_loop_.IsBitSet(index_)) {
5680 break;
5681 }
5682 }
5683 }
5684
5685 private:
5686 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005687 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005688 size_t index_;
5689
5690 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5691};
5692
Mingyao Yang3584bce2015-05-19 16:01:59 -07005693// Iterator over the blocks that art part of the loop. Includes blocks part
5694// of an inner loop. The order in which the blocks are iterated is reverse
5695// post order.
5696class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5697 public:
5698 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5699 : blocks_in_loop_(info.GetBlocks()),
5700 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5701 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005702 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005703 Advance();
5704 }
5705 }
5706
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005707 bool Done() const { return index_ == blocks_.size(); }
5708 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005709 void Advance() {
5710 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005711 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5712 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005713 break;
5714 }
5715 }
5716 }
5717
5718 private:
5719 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005720 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005721 size_t index_;
5722
5723 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5724};
5725
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005726inline int64_t Int64FromConstant(HConstant* constant) {
5727 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5728 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5729 : constant->AsLongConstant()->GetValue();
5730}
5731
Vladimir Marko58155012015-08-19 12:49:41 +00005732inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5733 // For the purposes of the compiler, the dex files must actually be the same object
5734 // if we want to safely treat them as the same. This is especially important for JIT
5735 // as custom class loaders can open the same underlying file (or memory) multiple
5736 // times and provide different class resolution but no two class loaders should ever
5737 // use the same DexFile object - doing so is an unsupported hack that can lead to
5738 // all sorts of weird failures.
5739 return &lhs == &rhs;
5740}
5741
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005742} // namespace art
5743
5744#endif // ART_COMPILER_OPTIMIZING_NODES_H_