blob: 0565c9a1cb1f3ae001606aa566405c2127a107cb [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39namespace art {
40
David Brazdil1abb4192015-02-17 18:33:36 +000041class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010045class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010046class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070085 // All types.
86 kCondEQ, // ==
87 kCondNE, // !=
88 // Signed integers and floating-point numbers.
89 kCondLT, // <
90 kCondLE, // <=
91 kCondGT, // >
92 kCondGE, // >=
93 // Unsigned integers.
94 kCondB, // <
95 kCondBE, // <=
96 kCondA, // >
97 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070098};
99
Vladimir Markof9f64412015-09-02 14:05:49 +0100100class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100101 public:
102 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
103
104 void AddInstruction(HInstruction* instruction);
105 void RemoveInstruction(HInstruction* instruction);
106
David Brazdilc3d743f2015-04-22 13:40:50 +0100107 // Insert `instruction` before/after an existing instruction `cursor`.
108 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
109 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
110
Roland Levillain6b469232014-09-25 10:10:38 +0100111 // Return true if this list contains `instruction`.
112 bool Contains(HInstruction* instruction) const;
113
Roland Levillainccc07a92014-09-16 14:48:16 +0100114 // Return true if `instruction1` is found before `instruction2` in
115 // this instruction list and false otherwise. Abort if none
116 // of these instructions is found.
117 bool FoundBefore(const HInstruction* instruction1,
118 const HInstruction* instruction2) const;
119
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000120 bool IsEmpty() const { return first_instruction_ == nullptr; }
121 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
122
123 // Update the block of all instructions to be `block`.
124 void SetBlockOfInstructions(HBasicBlock* block) const;
125
126 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
127 void Add(const HInstructionList& instruction_list);
128
David Brazdil2d7352b2015-04-20 14:52:42 +0100129 // Return the number of instructions in the list. This is an expensive operation.
130 size_t CountSize() const;
131
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100132 private:
133 HInstruction* first_instruction_;
134 HInstruction* last_instruction_;
135
136 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000137 friend class HGraph;
138 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100140 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100141
142 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
143};
144
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000145// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100146class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100148 HGraph(ArenaAllocator* arena,
149 const DexFile& dex_file,
150 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100151 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700152 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100153 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100154 bool debuggable = false,
155 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000156 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100157 blocks_(arena->Adapter(kArenaAllocBlockList)),
158 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
159 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700160 entry_block_(nullptr),
161 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100162 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100163 number_of_vregs_(0),
164 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000165 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400166 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000167 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000168 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000169 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100170 dex_file_(dex_file),
171 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100172 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100173 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100174 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700175 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000176 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100177 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
178 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
179 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
180 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
181 cached_current_method_(nullptr) {
182 blocks_.reserve(kDefaultNumberOfBlocks);
183 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000184
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000185 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100186 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
187
David Brazdil69ba7b72015-06-23 18:27:30 +0100188 bool IsInSsaForm() const { return in_ssa_form_; }
189
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000190 HBasicBlock* GetEntryBlock() const { return entry_block_; }
191 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100192 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000193
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000194 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
195 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000196
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000197 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 // Try building the SSA form of this graph, with dominance computation and loop
200 // recognition. Returns whether it was successful in doing all these steps.
201 bool TryBuildingSsa() {
202 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000203 // The SSA builder requires loops to all be natural. Specifically, the dead phi
204 // elimination phase checks the consistency of the graph when doing a post-order
205 // visit for eliminating dead phis: a dead phi can only have loop header phi
206 // users remaining when being visited.
207 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100208 // Precompute per-block try membership before entering the SSA builder,
209 // which needs the information to build catch block phis from values of
210 // locals at throwing instructions inside try blocks.
211 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100213 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000214 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 }
216
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100217 void ComputeDominanceInformation();
218 void ClearDominanceInformation();
219
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000220 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100222 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100223 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000224
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000225 // Analyze all natural loops in this graph. Returns false if one
226 // loop is not natural, that is the header does not dominate the
227 // back edge.
228 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100229
David Brazdilffee3d32015-07-06 11:48:53 +0100230 // Iterate over blocks to compute try block membership. Needs reverse post
231 // order and loop information.
232 void ComputeTryBlockInformation();
233
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000234 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000235 // Returns the instruction used to replace the invoke expression or null if the
236 // invoke is for a void method.
237 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238
Mingyao Yang3584bce2015-05-19 16:01:59 -0700239 // Need to add a couple of blocks to test if the loop body is entered and
240 // put deoptimization instructions, etc.
241 void TransformLoopHeaderForBCE(HBasicBlock* header);
242
David Brazdil2d7352b2015-04-20 14:52:42 +0100243 // Removes `block` from the graph.
244 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000245
David Brazdilfc6a86a2015-06-26 10:33:45 +0000246 // Splits the edge between `block` and `successor` while preserving the
247 // indices in the predecessor/successor lists. If there are multiple edges
248 // between the blocks, the lowest indices are used.
249 // Returns the new block which is empty and has the same dex pc as `successor`.
250 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
251
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100252 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
253 void SimplifyLoop(HBasicBlock* header);
254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000255 int32_t GetNextInstructionId() {
256 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000257 return current_instruction_id_++;
258 }
259
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000260 int32_t GetCurrentInstructionId() const {
261 return current_instruction_id_;
262 }
263
264 void SetCurrentInstructionId(int32_t id) {
265 current_instruction_id_ = id;
266 }
267
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100268 uint16_t GetMaximumNumberOfOutVRegs() const {
269 return maximum_number_of_out_vregs_;
270 }
271
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000272 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
273 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100274 }
275
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100276 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
277 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
278 }
279
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000280 void UpdateTemporariesVRegSlots(size_t slots) {
281 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100282 }
283
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000284 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100285 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000286 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100287 }
288
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100289 void SetNumberOfVRegs(uint16_t number_of_vregs) {
290 number_of_vregs_ = number_of_vregs;
291 }
292
293 uint16_t GetNumberOfVRegs() const {
294 return number_of_vregs_;
295 }
296
297 void SetNumberOfInVRegs(uint16_t value) {
298 number_of_in_vregs_ = value;
299 }
300
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100301 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100302 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100303 return number_of_vregs_ - number_of_in_vregs_;
304 }
305
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100306 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100307 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100308 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100309
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100311 return linear_order_;
312 }
313
Mark Mendell1152c922015-04-24 17:06:35 -0400314 bool HasBoundsChecks() const {
315 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800316 }
317
Mark Mendell1152c922015-04-24 17:06:35 -0400318 void SetHasBoundsChecks(bool value) {
319 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800320 }
321
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100322 bool ShouldGenerateConstructorBarrier() const {
323 return should_generate_constructor_barrier_;
324 }
325
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000326 bool IsDebuggable() const { return debuggable_; }
327
David Brazdil8d5b8b22015-03-24 10:51:52 +0000328 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000329 // already, it is created and inserted into the graph. This method is only for
330 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600331 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000332
333 // TODO: This is problematic for the consistency of reference type propagation
334 // because it can be created anytime after the pass and thus it will be left
335 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600336 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000337
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600338 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
339 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000340 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600341 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
342 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000343 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600344 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
345 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000346 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600347 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
348 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000349 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000350
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100351 HCurrentMethod* GetCurrentMethod();
352
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000353 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100354
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100355 const DexFile& GetDexFile() const {
356 return dex_file_;
357 }
358
359 uint32_t GetMethodIdx() const {
360 return method_idx_;
361 }
362
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100363 InvokeType GetInvokeType() const {
364 return invoke_type_;
365 }
366
Mark Mendellc4701932015-04-10 13:18:51 -0400367 InstructionSet GetInstructionSet() const {
368 return instruction_set_;
369 }
370
David Brazdil77a48ae2015-09-15 12:34:04 +0000371 bool HasTryCatch() const { return has_try_catch_; }
372 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100373
David Brazdil2d7352b2015-04-20 14:52:42 +0100374 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100375 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000376 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100377 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000378
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000379 template <class InstructionType, typename ValueType>
380 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600381 ArenaSafeMap<ValueType, InstructionType*>* cache,
382 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000383 // Try to find an existing constant of the given value.
384 InstructionType* constant = nullptr;
385 auto cached_constant = cache->find(value);
386 if (cached_constant != cache->end()) {
387 constant = cached_constant->second;
388 }
389
390 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100391 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000392 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600393 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000394 cache->Overwrite(value, constant);
395 InsertConstant(constant);
396 }
397 return constant;
398 }
399
David Brazdil8d5b8b22015-03-24 10:51:52 +0000400 void InsertConstant(HConstant* instruction);
401
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000402 // Cache a float constant into the graph. This method should only be
403 // called by the SsaBuilder when creating "equivalent" instructions.
404 void CacheFloatConstant(HFloatConstant* constant);
405
406 // See CacheFloatConstant comment.
407 void CacheDoubleConstant(HDoubleConstant* constant);
408
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000409 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000410
411 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100412 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100414 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100415 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000416
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100417 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100418 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100419
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000420 HBasicBlock* entry_block_;
421 HBasicBlock* exit_block_;
422
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100423 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100424 uint16_t maximum_number_of_out_vregs_;
425
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100426 // The number of virtual registers in this method. Contains the parameters.
427 uint16_t number_of_vregs_;
428
429 // The number of virtual registers used by parameters of this method.
430 uint16_t number_of_in_vregs_;
431
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000432 // Number of vreg size slots that the temporaries use (used in baseline compiler).
433 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100434
Mark Mendell1152c922015-04-24 17:06:35 -0400435 // Has bounds checks. We can totally skip BCE if it's false.
436 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800437
David Brazdil77a48ae2015-09-15 12:34:04 +0000438 // Flag whether there are any try/catch blocks in the graph. We will skip
439 // try/catch-related passes if false.
440 bool has_try_catch_;
441
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000442 // Indicates whether the graph should be compiled in a way that
443 // ensures full debuggability. If false, we can apply more
444 // aggressive optimizations that may limit the level of debugging.
445 const bool debuggable_;
446
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000447 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000448 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000449
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100450 // The dex file from which the method is from.
451 const DexFile& dex_file_;
452
453 // The method index in the dex file.
454 const uint32_t method_idx_;
455
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100456 // If inlined, this encodes how the callee is being invoked.
457 const InvokeType invoke_type_;
458
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100459 // Whether the graph has been transformed to SSA form. Only used
460 // in debug mode to ensure we are not using properties only valid
461 // for non-SSA form (like the number of temporaries).
462 bool in_ssa_form_;
463
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100464 const bool should_generate_constructor_barrier_;
465
Mathieu Chartiere401d142015-04-22 13:56:20 -0700466 const InstructionSet instruction_set_;
467
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000468 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000469 HNullConstant* cached_null_constant_;
470 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000471 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000472 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000473 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000474
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100475 HCurrentMethod* cached_current_method_;
476
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000477 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100478 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000479 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000480 DISALLOW_COPY_AND_ASSIGN(HGraph);
481};
482
Vladimir Markof9f64412015-09-02 14:05:49 +0100483class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 public:
485 HLoopInformation(HBasicBlock* header, HGraph* graph)
486 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100487 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100488 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100489 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100490 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
491 back_edges_.reserve(kDefaultNumberOfBackEdges);
492 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493
494 HBasicBlock* GetHeader() const {
495 return header_;
496 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000497
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000498 void SetHeader(HBasicBlock* block) {
499 header_ = block;
500 }
501
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100502 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
503 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
504 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
505
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000506 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100507 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000508 }
509
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100510 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100511 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100512 }
513
David Brazdil46e2a392015-03-16 17:31:52 +0000514 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100515 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100516 }
517
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000518 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100519 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000520 }
521
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100523
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100524 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100525 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100526 }
527
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100528 // Returns the lifetime position of the back edge that has the
529 // greatest lifetime position.
530 size_t GetLifetimeEnd() const;
531
532 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100533 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100534 }
535
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100536 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100537 // that is the header dominates the back edge.
538 bool Populate();
539
David Brazdila4b8c212015-05-07 09:59:30 +0100540 // Reanalyzes the loop by removing loop info from its blocks and re-running
541 // Populate(). If there are no back edges left, the loop info is completely
542 // removed as well as its SuspendCheck instruction. It must be run on nested
543 // inner loops first.
544 void Update();
545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100546 // Returns whether this loop information contains `block`.
547 // Note that this loop information *must* be populated before entering this function.
548 bool Contains(const HBasicBlock& block) const;
549
550 // Returns whether this loop information is an inner loop of `other`.
551 // Note that `other` *must* be populated before entering this function.
552 bool IsIn(const HLoopInformation& other) const;
553
Aart Bik73f1f3b2015-10-28 15:28:08 -0700554 // Returns true if instruction is not defined within this loop or any loop nested inside
555 // this loop. If must_dominate is set, only definitions that actually dominate the loop
556 // header can be invariant. Otherwise, any definition outside the loop, including
557 // definitions that appear after the loop, is invariant.
558 bool IsLoopInvariant(HInstruction* instruction, bool must_dominate) const;
559
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 const ArenaBitVector& GetBlocks() const { return blocks_; }
561
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000562 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000563 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000564
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000565 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100566 // Internal recursive implementation of `Populate`.
567 void PopulateRecursive(HBasicBlock* block);
568
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000569 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100570 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100571 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100572 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000573
574 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
575};
576
David Brazdilec16f792015-08-19 15:04:01 +0100577// Stores try/catch information for basic blocks.
578// Note that HGraph is constructed so that catch blocks cannot simultaneously
579// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100580class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100581 public:
582 // Try block information constructor.
583 explicit TryCatchInformation(const HTryBoundary& try_entry)
584 : try_entry_(&try_entry),
585 catch_dex_file_(nullptr),
586 catch_type_index_(DexFile::kDexNoIndex16) {
587 DCHECK(try_entry_ != nullptr);
588 }
589
590 // Catch block information constructor.
591 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
592 : try_entry_(nullptr),
593 catch_dex_file_(&dex_file),
594 catch_type_index_(catch_type_index) {}
595
596 bool IsTryBlock() const { return try_entry_ != nullptr; }
597
598 const HTryBoundary& GetTryEntry() const {
599 DCHECK(IsTryBlock());
600 return *try_entry_;
601 }
602
603 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
604
605 bool IsCatchAllTypeIndex() const {
606 DCHECK(IsCatchBlock());
607 return catch_type_index_ == DexFile::kDexNoIndex16;
608 }
609
610 uint16_t GetCatchTypeIndex() const {
611 DCHECK(IsCatchBlock());
612 return catch_type_index_;
613 }
614
615 const DexFile& GetCatchDexFile() const {
616 DCHECK(IsCatchBlock());
617 return *catch_dex_file_;
618 }
619
620 private:
621 // One of possibly several TryBoundary instructions entering the block's try.
622 // Only set for try blocks.
623 const HTryBoundary* try_entry_;
624
625 // Exception type information. Only set for catch blocks.
626 const DexFile* catch_dex_file_;
627 const uint16_t catch_type_index_;
628};
629
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100630static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100631static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100632
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000633// A block in a method. Contains the list of instructions represented
634// as a double linked list. Each block knows its predecessors and
635// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100636
Vladimir Markof9f64412015-09-02 14:05:49 +0100637class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600639 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000640 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000641 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
642 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 loop_information_(nullptr),
644 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000645 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100646 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100647 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100648 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000649 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000650 try_catch_information_(nullptr) {
651 predecessors_.reserve(kDefaultNumberOfPredecessors);
652 successors_.reserve(kDefaultNumberOfSuccessors);
653 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
654 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000655
Vladimir Marko60584552015-09-03 13:35:12 +0000656 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100657 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000658 }
659
Vladimir Marko60584552015-09-03 13:35:12 +0000660 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100661 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000662 }
663
Vladimir Marko60584552015-09-03 13:35:12 +0000664 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
665 return ContainsElement(successors_, block, start_from);
666 }
667
668 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100669 return dominated_blocks_;
670 }
671
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100672 bool IsEntryBlock() const {
673 return graph_->GetEntryBlock() == this;
674 }
675
676 bool IsExitBlock() const {
677 return graph_->GetExitBlock() == this;
678 }
679
David Brazdil46e2a392015-03-16 17:31:52 +0000680 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000681 bool IsSingleTryBoundary() const;
682
683 // Returns true if this block emits nothing but a jump.
684 bool IsSingleJump() const {
685 HLoopInformation* loop_info = GetLoopInformation();
686 return (IsSingleGoto() || IsSingleTryBoundary())
687 // Back edges generate a suspend check.
688 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
689 }
David Brazdil46e2a392015-03-16 17:31:52 +0000690
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000691 void AddBackEdge(HBasicBlock* back_edge) {
692 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000693 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000694 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100695 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000696 loop_information_->AddBackEdge(back_edge);
697 }
698
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000699 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000700 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000701
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100702 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000703 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600704 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000705
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000706 HBasicBlock* GetDominator() const { return dominator_; }
707 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000708 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
709
710 void RemoveDominatedBlock(HBasicBlock* block) {
711 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100712 }
Vladimir Marko60584552015-09-03 13:35:12 +0000713
714 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
715 ReplaceElement(dominated_blocks_, existing, new_block);
716 }
717
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100718 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000719
720 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100721 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000722 }
723
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100724 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
725 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100726 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100727 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100728 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
729 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000730
731 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000732 successors_.push_back(block);
733 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000734 }
735
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100736 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
737 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100738 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000739 new_block->predecessors_.push_back(this);
740 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000741 }
742
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000743 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
744 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000745 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000746 new_block->successors_.push_back(this);
747 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000748 }
749
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100750 // Insert `this` between `predecessor` and `successor. This method
751 // preserves the indicies, and will update the first edge found between
752 // `predecessor` and `successor`.
753 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
754 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100755 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000756 successor->predecessors_[predecessor_index] = this;
757 predecessor->successors_[successor_index] = this;
758 successors_.push_back(successor);
759 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100760 }
761
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100762 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000763 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 }
765
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000766 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000767 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000768 }
769
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100770 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000771 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100772 }
773
774 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000775 predecessors_.push_back(block);
776 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100777 }
778
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100779 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000780 DCHECK_EQ(predecessors_.size(), 2u);
781 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100782 }
783
David Brazdil769c9e52015-04-27 13:54:09 +0100784 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000785 DCHECK_EQ(successors_.size(), 2u);
786 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100787 }
788
David Brazdilfc6a86a2015-06-26 10:33:45 +0000789 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000790 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100791 }
792
David Brazdilfc6a86a2015-06-26 10:33:45 +0000793 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000794 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100795 }
796
David Brazdilfc6a86a2015-06-26 10:33:45 +0000797 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000798 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100799 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000800 }
801
802 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000803 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100804 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805 }
806
807 // Returns whether the first occurrence of `predecessor` in the list of
808 // predecessors is at index `idx`.
809 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100810 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000811 return GetPredecessorIndexOf(predecessor) == idx;
812 }
813
David Brazdilffee3d32015-07-06 11:48:53 +0100814 // Returns the number of non-exceptional successors. SsaChecker ensures that
815 // these are stored at the beginning of the successor list.
816 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000817 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100818 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000819
David Brazdild7558da2015-09-22 13:04:14 +0100820 // Create a new block between this block and its predecessors. The new block
821 // is added to the graph, all predecessor edges are relinked to it and an edge
822 // is created to `this`. Returns the new empty block. Reverse post order or
823 // loop and try/catch information are not updated.
824 HBasicBlock* CreateImmediateDominator();
825
David Brazdilfc6a86a2015-06-26 10:33:45 +0000826 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100827 // created, latter block. Note that this method will add the block to the
828 // graph, create a Goto at the end of the former block and will create an edge
829 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100830 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000831 HBasicBlock* SplitBefore(HInstruction* cursor);
832
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000833 // Split the block into two blocks just after `cursor`. Returns the newly
834 // created block. Note that this method just updates raw block information,
835 // like predecessors, successors, dominators, and instruction list. It does not
836 // update the graph, reverse post order, loop information, nor make sure the
837 // blocks are consistent (for example ending with a control flow instruction).
838 HBasicBlock* SplitAfter(HInstruction* cursor);
839
840 // Merge `other` at the end of `this`. Successors and dominated blocks of
841 // `other` are changed to be successors and dominated blocks of `this`. Note
842 // that this method does not update the graph, reverse post order, loop
843 // information, nor make sure the blocks are consistent (for example ending
844 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100845 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846
847 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
848 // of `this` are moved to `other`.
849 // Note that this method does not update the graph, reverse post order, loop
850 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000851 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000852 void ReplaceWith(HBasicBlock* other);
853
David Brazdil2d7352b2015-04-20 14:52:42 +0100854 // Merge `other` at the end of `this`. This method updates loops, reverse post
855 // order, links to predecessors, successors, dominators and deletes the block
856 // from the graph. The two blocks must be successive, i.e. `this` the only
857 // predecessor of `other` and vice versa.
858 void MergeWith(HBasicBlock* other);
859
860 // Disconnects `this` from all its predecessors, successors and dominator,
861 // removes it from all loops it is included in and eventually from the graph.
862 // The block must not dominate any other block. Predecessors and successors
863 // are safely updated.
864 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000865
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000866 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100867 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100868 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100869 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100870 // Replace instruction `initial` with `replacement` within this block.
871 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
872 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100873 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100874 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000875 // RemoveInstruction and RemovePhi delete a given instruction from the respective
876 // instruction list. With 'ensure_safety' set to true, it verifies that the
877 // instruction is not in use and removes it from the use lists of its inputs.
878 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
879 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100880 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100881
882 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100883 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100884 }
885
Roland Levillain6b879dd2014-09-22 17:13:44 +0100886 bool IsLoopPreHeaderFirstPredecessor() const {
887 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100888 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100889 }
890
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100891 HLoopInformation* GetLoopInformation() const {
892 return loop_information_;
893 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000895 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100896 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000897 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 void SetInLoop(HLoopInformation* info) {
899 if (IsLoopHeader()) {
900 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100901 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100902 loop_information_ = info;
903 } else if (loop_information_->Contains(*info->GetHeader())) {
904 // Block is currently part of an outer loop. Make it part of this inner loop.
905 // Note that a non loop header having a loop information means this loop information
906 // has already been populated
907 loop_information_ = info;
908 } else {
909 // Block is part of an inner loop. Do not update the loop information.
910 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
911 // at this point, because this method is being called while populating `info`.
912 }
913 }
914
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000915 // Raw update of the loop information.
916 void SetLoopInformation(HLoopInformation* info) {
917 loop_information_ = info;
918 }
919
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100920 bool IsInLoop() const { return loop_information_ != nullptr; }
921
David Brazdilec16f792015-08-19 15:04:01 +0100922 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
923
924 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
925 try_catch_information_ = try_catch_information;
926 }
927
928 bool IsTryBlock() const {
929 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
930 }
931
932 bool IsCatchBlock() const {
933 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
934 }
David Brazdilffee3d32015-07-06 11:48:53 +0100935
936 // Returns the try entry that this block's successors should have. They will
937 // be in the same try, unless the block ends in a try boundary. In that case,
938 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100939 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100940
David Brazdild7558da2015-09-22 13:04:14 +0100941 bool HasThrowingInstructions() const;
942
David Brazdila4b8c212015-05-07 09:59:30 +0100943 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100944 bool Dominates(HBasicBlock* block) const;
945
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100946 size_t GetLifetimeStart() const { return lifetime_start_; }
947 size_t GetLifetimeEnd() const { return lifetime_end_; }
948
949 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
950 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
951
David Brazdil8d5b8b22015-03-24 10:51:52 +0000952 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000953 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100954 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000955 bool HasSinglePhi() const;
956
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000958 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000959 ArenaVector<HBasicBlock*> predecessors_;
960 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100961 HInstructionList instructions_;
962 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000963 HLoopInformation* loop_information_;
964 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000965 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100966 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100967 // The dex program counter of the first instruction of this block.
968 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100969 size_t lifetime_start_;
970 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100971 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100972
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000973 friend class HGraph;
974 friend class HInstruction;
975
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000976 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
977};
978
David Brazdilb2bd1c52015-03-25 11:17:37 +0000979// Iterates over the LoopInformation of all loops which contain 'block'
980// from the innermost to the outermost.
981class HLoopInformationOutwardIterator : public ValueObject {
982 public:
983 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
984 : current_(block.GetLoopInformation()) {}
985
986 bool Done() const { return current_ == nullptr; }
987
988 void Advance() {
989 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100990 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000991 }
992
993 HLoopInformation* Current() const {
994 DCHECK(!Done());
995 return current_;
996 }
997
998 private:
999 HLoopInformation* current_;
1000
1001 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1002};
1003
Alexandre Ramesef20f712015-06-09 10:29:30 +01001004#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001005 M(Above, Condition) \
1006 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001007 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001008 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001009 M(ArrayGet, Instruction) \
1010 M(ArrayLength, Instruction) \
1011 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001012 M(Below, Condition) \
1013 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001014 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001015 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001016 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001017 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001018 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001019 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001020 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001021 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001022 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001023 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001024 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001025 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001026 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001027 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001028 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001029 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001030 M(FloatConstant, Constant) \
1031 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(GreaterThan, Condition) \
1033 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001034 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001035 M(InstanceFieldGet, Instruction) \
1036 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001037 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001039 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001040 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001041 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001042 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001043 M(LessThan, Condition) \
1044 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001045 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001046 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001047 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001048 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(Local, Instruction) \
1050 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001051 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001052 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001053 M(Mul, BinaryOperation) \
1054 M(Neg, UnaryOperation) \
1055 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001056 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001057 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001058 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001059 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001060 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001061 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001062 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001063 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001064 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001065 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001066 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001067 M(Return, Instruction) \
1068 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001069 M(Shl, BinaryOperation) \
1070 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001071 M(StaticFieldGet, Instruction) \
1072 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001073 M(UnresolvedInstanceFieldGet, Instruction) \
1074 M(UnresolvedInstanceFieldSet, Instruction) \
1075 M(UnresolvedStaticFieldGet, Instruction) \
1076 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001077 M(StoreLocal, Instruction) \
1078 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001079 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001080 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001081 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001082 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001083 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001084 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001085 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001086
Alexandre Ramesef20f712015-06-09 10:29:30 +01001087#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1088
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001089#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001090#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001091#else
1092#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1093 M(Arm64IntermediateAddress, Instruction)
1094#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001095
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001096#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1097
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001098#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1099
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001100#ifndef ART_ENABLE_CODEGEN_x86
1101#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1102#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001103#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1104 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001105 M(X86LoadFromConstantTable, Instruction) \
1106 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001107#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001108
1109#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1110
1111#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1112 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1113 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1114 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001115 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001116 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001117 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1118 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1119
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001120#define FOR_EACH_INSTRUCTION(M) \
1121 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1122 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001123 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001124 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001125 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001126
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001127#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001128FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1129#undef FORWARD_DECLARATION
1130
Roland Levillainccc07a92014-09-16 14:48:16 +01001131#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001132 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1133 const char* DebugName() const OVERRIDE { return #type; } \
1134 const H##type* As##type() const OVERRIDE { return this; } \
1135 H##type* As##type() OVERRIDE { return this; } \
1136 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001137 return other->Is##type(); \
1138 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001139 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001140
David Brazdiled596192015-01-23 10:39:45 +00001141template <typename T> class HUseList;
1142
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001143template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001144class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001145 public:
David Brazdiled596192015-01-23 10:39:45 +00001146 HUseListNode* GetPrevious() const { return prev_; }
1147 HUseListNode* GetNext() const { return next_; }
1148 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001149 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001150 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001151
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001152 private:
David Brazdiled596192015-01-23 10:39:45 +00001153 HUseListNode(T user, size_t index)
1154 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1155
1156 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001157 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001158 HUseListNode<T>* prev_;
1159 HUseListNode<T>* next_;
1160
1161 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001162
1163 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1164};
1165
David Brazdiled596192015-01-23 10:39:45 +00001166template <typename T>
1167class HUseList : public ValueObject {
1168 public:
1169 HUseList() : first_(nullptr) {}
1170
1171 void Clear() {
1172 first_ = nullptr;
1173 }
1174
1175 // Adds a new entry at the beginning of the use list and returns
1176 // the newly created node.
1177 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001178 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001179 if (IsEmpty()) {
1180 first_ = new_node;
1181 } else {
1182 first_->prev_ = new_node;
1183 new_node->next_ = first_;
1184 first_ = new_node;
1185 }
1186 return new_node;
1187 }
1188
1189 HUseListNode<T>* GetFirst() const {
1190 return first_;
1191 }
1192
1193 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001194 DCHECK(node != nullptr);
1195 DCHECK(Contains(node));
1196
David Brazdiled596192015-01-23 10:39:45 +00001197 if (node->prev_ != nullptr) {
1198 node->prev_->next_ = node->next_;
1199 }
1200 if (node->next_ != nullptr) {
1201 node->next_->prev_ = node->prev_;
1202 }
1203 if (node == first_) {
1204 first_ = node->next_;
1205 }
1206 }
1207
David Brazdil1abb4192015-02-17 18:33:36 +00001208 bool Contains(const HUseListNode<T>* node) const {
1209 if (node == nullptr) {
1210 return false;
1211 }
1212 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1213 if (current == node) {
1214 return true;
1215 }
1216 }
1217 return false;
1218 }
1219
David Brazdiled596192015-01-23 10:39:45 +00001220 bool IsEmpty() const {
1221 return first_ == nullptr;
1222 }
1223
1224 bool HasOnlyOneUse() const {
1225 return first_ != nullptr && first_->next_ == nullptr;
1226 }
1227
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001228 size_t SizeSlow() const {
1229 size_t count = 0;
1230 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1231 ++count;
1232 }
1233 return count;
1234 }
1235
David Brazdiled596192015-01-23 10:39:45 +00001236 private:
1237 HUseListNode<T>* first_;
1238};
1239
1240template<typename T>
1241class HUseIterator : public ValueObject {
1242 public:
1243 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1244
1245 bool Done() const { return current_ == nullptr; }
1246
1247 void Advance() {
1248 DCHECK(!Done());
1249 current_ = current_->GetNext();
1250 }
1251
1252 HUseListNode<T>* Current() const {
1253 DCHECK(!Done());
1254 return current_;
1255 }
1256
1257 private:
1258 HUseListNode<T>* current_;
1259
1260 friend class HValue;
1261};
1262
David Brazdil1abb4192015-02-17 18:33:36 +00001263// This class is used by HEnvironment and HInstruction classes to record the
1264// instructions they use and pointers to the corresponding HUseListNodes kept
1265// by the used instructions.
1266template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001267class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001268 public:
1269 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1270 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1271
1272 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1273 : instruction_(old_record.instruction_), use_node_(use_node) {
1274 DCHECK(instruction_ != nullptr);
1275 DCHECK(use_node_ != nullptr);
1276 DCHECK(old_record.use_node_ == nullptr);
1277 }
1278
1279 HInstruction* GetInstruction() const { return instruction_; }
1280 HUseListNode<T>* GetUseNode() const { return use_node_; }
1281
1282 private:
1283 // Instruction used by the user.
1284 HInstruction* instruction_;
1285
1286 // Corresponding entry in the use list kept by 'instruction_'.
1287 HUseListNode<T>* use_node_;
1288};
1289
Aart Bik854a02b2015-07-14 16:07:00 -07001290/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001291 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001292 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001293 * For write/read dependences on fields/arrays, the dependence analysis uses
1294 * type disambiguation (e.g. a float field write cannot modify the value of an
1295 * integer field read) and the access type (e.g. a reference array write cannot
1296 * modify the value of a reference field read [although it may modify the
1297 * reference fetch prior to reading the field, which is represented by its own
1298 * write/read dependence]). The analysis makes conservative points-to
1299 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1300 * the same, and any reference read depends on any reference read without
1301 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001302 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001303 * The internal representation uses 38-bit and is described in the table below.
1304 * The first line indicates the side effect, and for field/array accesses the
1305 * second line indicates the type of the access (in the order of the
1306 * Primitive::Type enum).
1307 * The two numbered lines below indicate the bit position in the bitfield (read
1308 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001309 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001310 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1311 * +-------------+---------+---------+--------------+---------+---------+
1312 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1313 * | 3 |333333322|222222221| 1 |111111110|000000000|
1314 * | 7 |654321098|765432109| 8 |765432109|876543210|
1315 *
1316 * Note that, to ease the implementation, 'changes' bits are least significant
1317 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001318 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001319class SideEffects : public ValueObject {
1320 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001321 SideEffects() : flags_(0) {}
1322
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001323 static SideEffects None() {
1324 return SideEffects(0);
1325 }
1326
1327 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001328 return SideEffects(kAllChangeBits | kAllDependOnBits);
1329 }
1330
1331 static SideEffects AllChanges() {
1332 return SideEffects(kAllChangeBits);
1333 }
1334
1335 static SideEffects AllDependencies() {
1336 return SideEffects(kAllDependOnBits);
1337 }
1338
1339 static SideEffects AllExceptGCDependency() {
1340 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1341 }
1342
1343 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001344 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001345 }
1346
Aart Bik34c3ba92015-07-20 14:08:59 -07001347 static SideEffects AllWrites() {
1348 return SideEffects(kAllWrites);
1349 }
1350
1351 static SideEffects AllReads() {
1352 return SideEffects(kAllReads);
1353 }
1354
1355 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1356 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001357 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001358 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001359 }
1360
Aart Bik854a02b2015-07-14 16:07:00 -07001361 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1362 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001363 }
1364
Aart Bik34c3ba92015-07-20 14:08:59 -07001365 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1366 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001367 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001368 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001369 }
1370
1371 static SideEffects ArrayReadOfType(Primitive::Type type) {
1372 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1373 }
1374
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001375 static SideEffects CanTriggerGC() {
1376 return SideEffects(1ULL << kCanTriggerGCBit);
1377 }
1378
1379 static SideEffects DependsOnGC() {
1380 return SideEffects(1ULL << kDependsOnGCBit);
1381 }
1382
Aart Bik854a02b2015-07-14 16:07:00 -07001383 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001384 SideEffects Union(SideEffects other) const {
1385 return SideEffects(flags_ | other.flags_);
1386 }
1387
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001388 SideEffects Exclusion(SideEffects other) const {
1389 return SideEffects(flags_ & ~other.flags_);
1390 }
1391
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001392 void Add(SideEffects other) {
1393 flags_ |= other.flags_;
1394 }
1395
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001396 bool Includes(SideEffects other) const {
1397 return (other.flags_ & flags_) == other.flags_;
1398 }
1399
1400 bool HasSideEffects() const {
1401 return (flags_ & kAllChangeBits);
1402 }
1403
1404 bool HasDependencies() const {
1405 return (flags_ & kAllDependOnBits);
1406 }
1407
1408 // Returns true if there are no side effects or dependencies.
1409 bool DoesNothing() const {
1410 return flags_ == 0;
1411 }
1412
Aart Bik854a02b2015-07-14 16:07:00 -07001413 // Returns true if something is written.
1414 bool DoesAnyWrite() const {
1415 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001416 }
1417
Aart Bik854a02b2015-07-14 16:07:00 -07001418 // Returns true if something is read.
1419 bool DoesAnyRead() const {
1420 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001421 }
1422
Aart Bik854a02b2015-07-14 16:07:00 -07001423 // Returns true if potentially everything is written and read
1424 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001425 bool DoesAllReadWrite() const {
1426 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1427 }
1428
Aart Bik854a02b2015-07-14 16:07:00 -07001429 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001430 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001431 }
1432
1433 // Returns true if this may read something written by other.
1434 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001435 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1436 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001437 }
1438
1439 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001440 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001441 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001442 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001443 for (int s = kLastBit; s >= 0; s--) {
1444 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1445 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1446 // This is a bit for the GC side effect.
1447 if (current_bit_is_set) {
1448 flags += "GC";
1449 }
Aart Bik854a02b2015-07-14 16:07:00 -07001450 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001451 } else {
1452 // This is a bit for the array/field analysis.
1453 // The underscore character stands for the 'can trigger GC' bit.
1454 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1455 if (current_bit_is_set) {
1456 flags += kDebug[s];
1457 }
1458 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1459 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1460 flags += "|";
1461 }
1462 }
Aart Bik854a02b2015-07-14 16:07:00 -07001463 }
1464 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001465 }
1466
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001467 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001468
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001469 private:
1470 static constexpr int kFieldArrayAnalysisBits = 9;
1471
1472 static constexpr int kFieldWriteOffset = 0;
1473 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1474 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1475 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1476
1477 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1478
1479 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1480 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1481 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1482 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1483
1484 static constexpr int kLastBit = kDependsOnGCBit;
1485 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1486
1487 // Aliases.
1488
1489 static_assert(kChangeBits == kDependOnBits,
1490 "the 'change' bits should match the 'depend on' bits.");
1491
1492 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1493 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1494 static constexpr uint64_t kAllWrites =
1495 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1496 static constexpr uint64_t kAllReads =
1497 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001498
Aart Bik854a02b2015-07-14 16:07:00 -07001499 // Work around the fact that HIR aliases I/F and J/D.
1500 // TODO: remove this interceptor once HIR types are clean
1501 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1502 switch (type) {
1503 case Primitive::kPrimInt:
1504 case Primitive::kPrimFloat:
1505 return TypeFlag(Primitive::kPrimInt, offset) |
1506 TypeFlag(Primitive::kPrimFloat, offset);
1507 case Primitive::kPrimLong:
1508 case Primitive::kPrimDouble:
1509 return TypeFlag(Primitive::kPrimLong, offset) |
1510 TypeFlag(Primitive::kPrimDouble, offset);
1511 default:
1512 return TypeFlag(type, offset);
1513 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001514 }
1515
Aart Bik854a02b2015-07-14 16:07:00 -07001516 // Translates type to bit flag.
1517 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1518 CHECK_NE(type, Primitive::kPrimVoid);
1519 const uint64_t one = 1;
1520 const int shift = type; // 0-based consecutive enum
1521 DCHECK_LE(kFieldWriteOffset, shift);
1522 DCHECK_LT(shift, kArrayWriteOffset);
1523 return one << (type + offset);
1524 }
1525
1526 // Private constructor on direct flags value.
1527 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1528
1529 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530};
1531
David Brazdiled596192015-01-23 10:39:45 +00001532// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001533class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001534 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001535 HEnvironment(ArenaAllocator* arena,
1536 size_t number_of_vregs,
1537 const DexFile& dex_file,
1538 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001539 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001540 InvokeType invoke_type,
1541 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001542 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1543 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001544 parent_(nullptr),
1545 dex_file_(dex_file),
1546 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001547 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001548 invoke_type_(invoke_type),
1549 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001550 }
1551
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001552 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001553 : HEnvironment(arena,
1554 to_copy.Size(),
1555 to_copy.GetDexFile(),
1556 to_copy.GetMethodIdx(),
1557 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001558 to_copy.GetInvokeType(),
1559 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001560
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001561 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001562 if (parent_ != nullptr) {
1563 parent_->SetAndCopyParentChain(allocator, parent);
1564 } else {
1565 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1566 parent_->CopyFrom(parent);
1567 if (parent->GetParent() != nullptr) {
1568 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1569 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001570 }
David Brazdiled596192015-01-23 10:39:45 +00001571 }
1572
Vladimir Marko71bf8092015-09-15 15:33:14 +01001573 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001574 void CopyFrom(HEnvironment* environment);
1575
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001576 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1577 // input to the loop phi instead. This is for inserting instructions that
1578 // require an environment (like HDeoptimization) in the loop pre-header.
1579 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001580
1581 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001582 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001583 }
1584
1585 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001586 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001587 }
1588
David Brazdil1abb4192015-02-17 18:33:36 +00001589 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001590
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001591 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001592
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001593 HEnvironment* GetParent() const { return parent_; }
1594
1595 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001596 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001597 }
1598
1599 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001600 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001601 }
1602
1603 uint32_t GetDexPc() const {
1604 return dex_pc_;
1605 }
1606
1607 uint32_t GetMethodIdx() const {
1608 return method_idx_;
1609 }
1610
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001611 InvokeType GetInvokeType() const {
1612 return invoke_type_;
1613 }
1614
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001615 const DexFile& GetDexFile() const {
1616 return dex_file_;
1617 }
1618
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001619 HInstruction* GetHolder() const {
1620 return holder_;
1621 }
1622
David Brazdiled596192015-01-23 10:39:45 +00001623 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001624 // Record instructions' use entries of this environment for constant-time removal.
1625 // It should only be called by HInstruction when a new environment use is added.
1626 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1627 DCHECK(env_use->GetUser() == this);
1628 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001629 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001630 }
David Brazdiled596192015-01-23 10:39:45 +00001631
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001632 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1633 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001634 HEnvironment* parent_;
1635 const DexFile& dex_file_;
1636 const uint32_t method_idx_;
1637 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001638 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001639
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001640 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001641 HInstruction* const holder_;
1642
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001643 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001644
1645 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1646};
1647
Calin Juravleacf735c2015-02-12 15:25:22 +00001648class ReferenceTypeInfo : ValueObject {
1649 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001650 typedef Handle<mirror::Class> TypeHandle;
1651
Calin Juravle2e768302015-07-28 14:41:11 +00001652 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1653 // The constructor will check that the type_handle is valid.
1654 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001655 }
1656
Calin Juravle2e768302015-07-28 14:41:11 +00001657 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1658
1659 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1660 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001661 }
1662
Calin Juravle2e768302015-07-28 14:41:11 +00001663 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1664 return IsValidHandle(type_handle_);
1665 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001666
Calin Juravleacf735c2015-02-12 15:25:22 +00001667 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001668
1669 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1670 DCHECK(IsValid());
1671 return GetTypeHandle()->IsObjectClass();
1672 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001673
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001674 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001675 DCHECK(IsValid());
1676 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001677 }
1678
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001679 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1680 DCHECK(IsValid());
1681 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1682 }
1683
Mathieu Chartier90443472015-07-16 20:32:27 -07001684 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001685 DCHECK(IsValid());
1686 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001687 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001688
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001689 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001690 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001691 return GetTypeHandle()->IsArrayClass();
1692 }
1693
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001694 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1695 DCHECK(IsValid());
1696 return GetTypeHandle()->IsPrimitiveArray();
1697 }
1698
1699 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1700 DCHECK(IsValid());
1701 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1702 }
1703
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001704 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001705 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001706 if (!IsExact()) return false;
1707 if (!IsArrayClass()) return false;
1708 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1709 }
1710
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001711 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1712 DCHECK(IsValid());
1713 if (!IsExact()) return false;
1714 if (!IsArrayClass()) return false;
1715 if (!rti.IsArrayClass()) return false;
1716 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1717 rti.GetTypeHandle()->GetComponentType());
1718 }
1719
Calin Juravleacf735c2015-02-12 15:25:22 +00001720 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1721
Mathieu Chartier90443472015-07-16 20:32:27 -07001722 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001723 DCHECK(IsValid());
1724 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001725 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1726 }
1727
1728 // Returns true if the type information provide the same amount of details.
1729 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001730 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001731 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001732 if (!IsValid() && !rti.IsValid()) {
1733 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001734 return true;
1735 }
Calin Juravle2e768302015-07-28 14:41:11 +00001736 if (!IsValid() || !rti.IsValid()) {
1737 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001738 return false;
1739 }
Calin Juravle2e768302015-07-28 14:41:11 +00001740 return IsExact() == rti.IsExact()
1741 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001742 }
1743
1744 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001745 ReferenceTypeInfo();
1746 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001747
Calin Juravleacf735c2015-02-12 15:25:22 +00001748 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001749 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001750 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001751 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001752 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001753};
1754
1755std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1756
Vladimir Markof9f64412015-09-02 14:05:49 +01001757class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001758 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001759 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001760 : previous_(nullptr),
1761 next_(nullptr),
1762 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001763 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001764 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001765 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001766 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001767 locations_(nullptr),
1768 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001769 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001770 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001771 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001772
Dave Allison20dfc792014-06-16 20:44:29 -07001773 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001774
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001775#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001776 enum InstructionKind {
1777 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1778 };
1779#undef DECLARE_KIND
1780
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001781 HInstruction* GetNext() const { return next_; }
1782 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001783
Calin Juravle77520bc2015-01-12 18:45:46 +00001784 HInstruction* GetNextDisregardingMoves() const;
1785 HInstruction* GetPreviousDisregardingMoves() const;
1786
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001787 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001788 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001789 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001790 bool IsInBlock() const { return block_ != nullptr; }
1791 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001792 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001793
Roland Levillain6b879dd2014-09-22 17:13:44 +01001794 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001795 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001796
1797 virtual void Accept(HGraphVisitor* visitor) = 0;
1798 virtual const char* DebugName() const = 0;
1799
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001800 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001801 void SetRawInputAt(size_t index, HInstruction* input) {
1802 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1803 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001804
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001805 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001806
1807 uint32_t GetDexPc() const { return dex_pc_; }
1808
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001809 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001810
Roland Levillaine161a2a2014-10-03 12:45:18 +01001811 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001812 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001813
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001814 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001815 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001816
Calin Juravle10e244f2015-01-26 18:54:32 +00001817 // Does not apply for all instructions, but having this at top level greatly
1818 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001819 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001820 virtual bool CanBeNull() const {
1821 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1822 return true;
1823 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001824
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001825 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001826 return false;
1827 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001828
Calin Juravle2e768302015-07-28 14:41:11 +00001829 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001830
Calin Juravle61d544b2015-02-23 16:46:57 +00001831 ReferenceTypeInfo GetReferenceTypeInfo() const {
1832 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1833 return reference_type_info_;
1834 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001835
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001836 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001837 DCHECK(user != nullptr);
1838 HUseListNode<HInstruction*>* use =
1839 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1840 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001841 }
1842
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001843 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001844 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001845 HUseListNode<HEnvironment*>* env_use =
1846 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1847 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001848 }
1849
David Brazdil1abb4192015-02-17 18:33:36 +00001850 void RemoveAsUserOfInput(size_t input) {
1851 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1852 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1853 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001854
David Brazdil1abb4192015-02-17 18:33:36 +00001855 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1856 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001857
David Brazdiled596192015-01-23 10:39:45 +00001858 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1859 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001860 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001861 bool HasOnlyOneNonEnvironmentUse() const {
1862 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1863 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001864
Roland Levillain6c82d402014-10-13 16:10:27 +01001865 // Does this instruction strictly dominate `other_instruction`?
1866 // Returns false if this instruction and `other_instruction` are the same.
1867 // Aborts if this instruction and `other_instruction` are both phis.
1868 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001869
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001870 int GetId() const { return id_; }
1871 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001872
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001873 int GetSsaIndex() const { return ssa_index_; }
1874 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1875 bool HasSsaIndex() const { return ssa_index_ != -1; }
1876
1877 bool HasEnvironment() const { return environment_ != nullptr; }
1878 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001879 // Set the `environment_` field. Raw because this method does not
1880 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001881 void SetRawEnvironment(HEnvironment* environment) {
1882 DCHECK(environment_ == nullptr);
1883 DCHECK_EQ(environment->GetHolder(), this);
1884 environment_ = environment;
1885 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001886
1887 // Set the environment of this instruction, copying it from `environment`. While
1888 // copying, the uses lists are being updated.
1889 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001890 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001891 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001892 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001893 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001894 if (environment->GetParent() != nullptr) {
1895 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1896 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001897 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001898
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001899 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1900 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001901 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001902 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001903 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001904 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001905 if (environment->GetParent() != nullptr) {
1906 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1907 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001908 }
1909
Nicolas Geoffray39468442014-09-02 15:17:15 +01001910 // Returns the number of entries in the environment. Typically, that is the
1911 // number of dex registers in a method. It could be more in case of inlining.
1912 size_t EnvironmentSize() const;
1913
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001914 LocationSummary* GetLocations() const { return locations_; }
1915 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001916
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001917 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001918 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001919
Alexandre Rames188d4312015-04-09 18:30:21 +01001920 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1921 // uses of this instruction by `other` are *not* updated.
1922 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1923 ReplaceWith(other);
1924 other->ReplaceInput(this, use_index);
1925 }
1926
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001927 // Move `this` instruction before `cursor`.
1928 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001929
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001930#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001931 bool Is##type() const { return (As##type() != nullptr); } \
1932 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001933 virtual H##type* As##type() { return nullptr; }
1934
1935 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1936#undef INSTRUCTION_TYPE_CHECK
1937
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001938 // Returns whether the instruction can be moved within the graph.
1939 virtual bool CanBeMoved() const { return false; }
1940
1941 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001942 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001943 return false;
1944 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001945
1946 // Returns whether any data encoded in the two instructions is equal.
1947 // This method does not look at the inputs. Both instructions must be
1948 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001949 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001950 return false;
1951 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001952
1953 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001954 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001955 // 2) Their inputs are identical.
1956 bool Equals(HInstruction* other) const;
1957
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001958 virtual InstructionKind GetKind() const = 0;
1959
1960 virtual size_t ComputeHashCode() const {
1961 size_t result = GetKind();
1962 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1963 result = (result * 31) + InputAt(i)->GetId();
1964 }
1965 return result;
1966 }
1967
1968 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001969 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001970
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001971 size_t GetLifetimePosition() const { return lifetime_position_; }
1972 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1973 LiveInterval* GetLiveInterval() const { return live_interval_; }
1974 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1975 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1976
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001977 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1978
1979 // Returns whether the code generation of the instruction will require to have access
1980 // to the current method. Such instructions are:
1981 // (1): Instructions that require an environment, as calling the runtime requires
1982 // to walk the stack and have the current method stored at a specific stack address.
1983 // (2): Object literals like classes and strings, that are loaded from the dex cache
1984 // fields of the current method.
1985 bool NeedsCurrentMethod() const {
1986 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1987 }
1988
Vladimir Markodc151b22015-10-15 18:02:30 +01001989 // Returns whether the code generation of the instruction will require to have access
1990 // to the dex cache of the current method's declaring class via the current method.
1991 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001992
Mark Mendellc4701932015-04-10 13:18:51 -04001993 // Does this instruction have any use in an environment before
1994 // control flow hits 'other'?
1995 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1996
1997 // Remove all references to environment uses of this instruction.
1998 // The caller must ensure that this is safe to do.
1999 void RemoveEnvironmentUsers();
2000
David Brazdil1abb4192015-02-17 18:33:36 +00002001 protected:
2002 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2003 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2004
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002005 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002006 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2007
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002008 HInstruction* previous_;
2009 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002010 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002011 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002012
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002013 // An instruction gets an id when it is added to the graph.
2014 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002015 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002016 int id_;
2017
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002018 // When doing liveness analysis, instructions that have uses get an SSA index.
2019 int ssa_index_;
2020
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002021 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002022 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002023
2024 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002025 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002026
Nicolas Geoffray39468442014-09-02 15:17:15 +01002027 // The environment associated with this instruction. Not null if the instruction
2028 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002029 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002030
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002031 // Set by the code generator.
2032 LocationSummary* locations_;
2033
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002034 // Set by the liveness analysis.
2035 LiveInterval* live_interval_;
2036
2037 // Set by the liveness analysis, this is the position in a linear
2038 // order of blocks where this instruction's live interval start.
2039 size_t lifetime_position_;
2040
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002041 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002042
Calin Juravleacf735c2015-02-12 15:25:22 +00002043 // TODO: for primitive types this should be marked as invalid.
2044 ReferenceTypeInfo reference_type_info_;
2045
David Brazdil1abb4192015-02-17 18:33:36 +00002046 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002047 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002048 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002049 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002050 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002051
2052 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2053};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002054std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002056class HInputIterator : public ValueObject {
2057 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002058 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002059
2060 bool Done() const { return index_ == instruction_->InputCount(); }
2061 HInstruction* Current() const { return instruction_->InputAt(index_); }
2062 void Advance() { index_++; }
2063
2064 private:
2065 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002066 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002067
2068 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2069};
2070
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002071class HInstructionIterator : public ValueObject {
2072 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002073 explicit HInstructionIterator(const HInstructionList& instructions)
2074 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002075 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002076 }
2077
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002078 bool Done() const { return instruction_ == nullptr; }
2079 HInstruction* Current() const { return instruction_; }
2080 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002081 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002082 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002083 }
2084
2085 private:
2086 HInstruction* instruction_;
2087 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002088
2089 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002090};
2091
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002092class HBackwardInstructionIterator : public ValueObject {
2093 public:
2094 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2095 : instruction_(instructions.last_instruction_) {
2096 next_ = Done() ? nullptr : instruction_->GetPrevious();
2097 }
2098
2099 bool Done() const { return instruction_ == nullptr; }
2100 HInstruction* Current() const { return instruction_; }
2101 void Advance() {
2102 instruction_ = next_;
2103 next_ = Done() ? nullptr : instruction_->GetPrevious();
2104 }
2105
2106 private:
2107 HInstruction* instruction_;
2108 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002109
2110 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002111};
2112
Vladimir Markof9f64412015-09-02 14:05:49 +01002113template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114class HTemplateInstruction: public HInstruction {
2115 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002116 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002117 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002118 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002120 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002122 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002123 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2124 DCHECK_LT(i, N);
2125 return inputs_[i];
2126 }
David Brazdil1abb4192015-02-17 18:33:36 +00002127
2128 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002129 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002130 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002131 }
2132
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002133 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002134 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002135
2136 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002137};
2138
Vladimir Markof9f64412015-09-02 14:05:49 +01002139// HTemplateInstruction specialization for N=0.
2140template<>
2141class HTemplateInstruction<0>: public HInstruction {
2142 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002143 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002144 : HInstruction(side_effects, dex_pc) {}
2145
Vladimir Markof9f64412015-09-02 14:05:49 +01002146 virtual ~HTemplateInstruction() {}
2147
2148 size_t InputCount() const OVERRIDE { return 0; }
2149
2150 protected:
2151 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2152 LOG(FATAL) << "Unreachable";
2153 UNREACHABLE();
2154 }
2155
2156 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2157 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2158 LOG(FATAL) << "Unreachable";
2159 UNREACHABLE();
2160 }
2161
2162 private:
2163 friend class SsaBuilder;
2164};
2165
Dave Allison20dfc792014-06-16 20:44:29 -07002166template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002167class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002168 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002169 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002170 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002171 virtual ~HExpression() {}
2172
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002173 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002174
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002175 protected:
2176 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002177};
2178
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002179// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2180// instruction that branches to the exit block.
2181class HReturnVoid : public HTemplateInstruction<0> {
2182 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002183 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2184 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002185
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002186 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002187
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002188 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002189
2190 private:
2191 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2192};
2193
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002194// Represents dex's RETURN opcodes. A HReturn is a control flow
2195// instruction that branches to the exit block.
2196class HReturn : public HTemplateInstruction<1> {
2197 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002198 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2199 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002200 SetRawInputAt(0, value);
2201 }
2202
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002203 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002204
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002205 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002206
2207 private:
2208 DISALLOW_COPY_AND_ASSIGN(HReturn);
2209};
2210
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002211// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002212// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213// exit block.
2214class HExit : public HTemplateInstruction<0> {
2215 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002216 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002218 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002219
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002220 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221
2222 private:
2223 DISALLOW_COPY_AND_ASSIGN(HExit);
2224};
2225
2226// Jumps from one block to another.
2227class HGoto : public HTemplateInstruction<0> {
2228 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002229 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002230
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002231 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002233 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002234 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002235 }
2236
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002237 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002238
2239 private:
2240 DISALLOW_COPY_AND_ASSIGN(HGoto);
2241};
2242
Roland Levillain9867bc72015-08-05 10:21:34 +01002243class HConstant : public HExpression<0> {
2244 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002245 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2246 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002247
2248 bool CanBeMoved() const OVERRIDE { return true; }
2249
2250 virtual bool IsMinusOne() const { return false; }
2251 virtual bool IsZero() const { return false; }
2252 virtual bool IsOne() const { return false; }
2253
David Brazdil77a48ae2015-09-15 12:34:04 +00002254 virtual uint64_t GetValueAsUint64() const = 0;
2255
Roland Levillain9867bc72015-08-05 10:21:34 +01002256 DECLARE_INSTRUCTION(Constant);
2257
2258 private:
2259 DISALLOW_COPY_AND_ASSIGN(HConstant);
2260};
2261
2262class HNullConstant : public HConstant {
2263 public:
2264 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2265 return true;
2266 }
2267
David Brazdil77a48ae2015-09-15 12:34:04 +00002268 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2269
Roland Levillain9867bc72015-08-05 10:21:34 +01002270 size_t ComputeHashCode() const OVERRIDE { return 0; }
2271
2272 DECLARE_INSTRUCTION(NullConstant);
2273
2274 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002275 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002276
2277 friend class HGraph;
2278 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2279};
2280
2281// Constants of the type int. Those can be from Dex instructions, or
2282// synthesized (for example with the if-eqz instruction).
2283class HIntConstant : public HConstant {
2284 public:
2285 int32_t GetValue() const { return value_; }
2286
David Brazdil9f389d42015-10-01 14:32:56 +01002287 uint64_t GetValueAsUint64() const OVERRIDE {
2288 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2289 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002290
Roland Levillain9867bc72015-08-05 10:21:34 +01002291 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2292 DCHECK(other->IsIntConstant());
2293 return other->AsIntConstant()->value_ == value_;
2294 }
2295
2296 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2297
2298 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2299 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2300 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2301
2302 DECLARE_INSTRUCTION(IntConstant);
2303
2304 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002305 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2306 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2307 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2308 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002309
2310 const int32_t value_;
2311
2312 friend class HGraph;
2313 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2314 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2315 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2316};
2317
2318class HLongConstant : public HConstant {
2319 public:
2320 int64_t GetValue() const { return value_; }
2321
David Brazdil77a48ae2015-09-15 12:34:04 +00002322 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2323
Roland Levillain9867bc72015-08-05 10:21:34 +01002324 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2325 DCHECK(other->IsLongConstant());
2326 return other->AsLongConstant()->value_ == value_;
2327 }
2328
2329 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2330
2331 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2332 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2333 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2334
2335 DECLARE_INSTRUCTION(LongConstant);
2336
2337 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002338 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2339 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002340
2341 const int64_t value_;
2342
2343 friend class HGraph;
2344 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2345};
Dave Allison20dfc792014-06-16 20:44:29 -07002346
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002347// Conditional branch. A block ending with an HIf instruction must have
2348// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002349class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002350 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002351 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2352 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002353 SetRawInputAt(0, input);
2354 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002355
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002356 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002357
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002358 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002359 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002360 }
2361
2362 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002363 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002364 }
2365
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002366 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002367
2368 private:
2369 DISALLOW_COPY_AND_ASSIGN(HIf);
2370};
2371
David Brazdilfc6a86a2015-06-26 10:33:45 +00002372
2373// Abstract instruction which marks the beginning and/or end of a try block and
2374// links it to the respective exception handlers. Behaves the same as a Goto in
2375// non-exceptional control flow.
2376// Normal-flow successor is stored at index zero, exception handlers under
2377// higher indices in no particular order.
2378class HTryBoundary : public HTemplateInstruction<0> {
2379 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002380 enum BoundaryKind {
2381 kEntry,
2382 kExit,
2383 };
2384
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002385 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2386 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002387
2388 bool IsControlFlow() const OVERRIDE { return true; }
2389
2390 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002391 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002392
2393 // Returns whether `handler` is among its exception handlers (non-zero index
2394 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002395 bool HasExceptionHandler(const HBasicBlock& handler) const {
2396 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002397 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002398 }
2399
2400 // If not present already, adds `handler` to its block's list of exception
2401 // handlers.
2402 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002403 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002404 GetBlock()->AddSuccessor(handler);
2405 }
2406 }
2407
David Brazdil56e1acc2015-06-30 15:41:36 +01002408 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002409
David Brazdilffee3d32015-07-06 11:48:53 +01002410 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2411
David Brazdilfc6a86a2015-06-26 10:33:45 +00002412 DECLARE_INSTRUCTION(TryBoundary);
2413
2414 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002415 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002416
2417 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2418};
2419
David Brazdilffee3d32015-07-06 11:48:53 +01002420// Iterator over exception handlers of a given HTryBoundary, i.e. over
2421// exceptional successors of its basic block.
2422class HExceptionHandlerIterator : public ValueObject {
2423 public:
2424 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2425 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2426
Vladimir Marko60584552015-09-03 13:35:12 +00002427 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002428 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002429 size_t CurrentSuccessorIndex() const { return index_; }
2430 void Advance() { ++index_; }
2431
2432 private:
2433 const HBasicBlock& block_;
2434 size_t index_;
2435
2436 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2437};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002438
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002439// Deoptimize to interpreter, upon checking a condition.
2440class HDeoptimize : public HTemplateInstruction<1> {
2441 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002442 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2443 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002444 SetRawInputAt(0, cond);
2445 }
2446
2447 bool NeedsEnvironment() const OVERRIDE { return true; }
2448 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002449
2450 DECLARE_INSTRUCTION(Deoptimize);
2451
2452 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002453 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2454};
2455
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002456// Represents the ArtMethod that was passed as a first argument to
2457// the method. It is used by instructions that depend on it, like
2458// instructions that work with the dex cache.
2459class HCurrentMethod : public HExpression<0> {
2460 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002461 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2462 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002463
2464 DECLARE_INSTRUCTION(CurrentMethod);
2465
2466 private:
2467 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2468};
2469
Mark Mendellfe57faa2015-09-18 09:26:15 -04002470// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2471// have one successor for each entry in the switch table, and the final successor
2472// will be the block containing the next Dex opcode.
2473class HPackedSwitch : public HTemplateInstruction<1> {
2474 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002475 HPackedSwitch(int32_t start_value,
2476 uint32_t num_entries,
2477 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002478 uint32_t dex_pc = kNoDexPc)
2479 : HTemplateInstruction(SideEffects::None(), dex_pc),
2480 start_value_(start_value),
2481 num_entries_(num_entries) {
2482 SetRawInputAt(0, input);
2483 }
2484
2485 bool IsControlFlow() const OVERRIDE { return true; }
2486
2487 int32_t GetStartValue() const { return start_value_; }
2488
Vladimir Marko211c2112015-09-24 16:52:33 +01002489 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002490
2491 HBasicBlock* GetDefaultBlock() const {
2492 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002493 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002494 }
2495 DECLARE_INSTRUCTION(PackedSwitch);
2496
2497 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002498 const int32_t start_value_;
2499 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002500
2501 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2502};
2503
Roland Levillain88cb1752014-10-20 16:36:47 +01002504class HUnaryOperation : public HExpression<1> {
2505 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002506 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2507 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002508 SetRawInputAt(0, input);
2509 }
2510
2511 HInstruction* GetInput() const { return InputAt(0); }
2512 Primitive::Type GetResultType() const { return GetType(); }
2513
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002514 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002515 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002516 return true;
2517 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002518
Roland Levillain9240d6a2014-10-20 16:47:04 +01002519 // Try to statically evaluate `operation` and return a HConstant
2520 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002521 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002522 HConstant* TryStaticEvaluation() const;
2523
2524 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002525 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2526 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002527
Roland Levillain88cb1752014-10-20 16:36:47 +01002528 DECLARE_INSTRUCTION(UnaryOperation);
2529
2530 private:
2531 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2532};
2533
Dave Allison20dfc792014-06-16 20:44:29 -07002534class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002535 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002536 HBinaryOperation(Primitive::Type result_type,
2537 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002538 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002539 SideEffects side_effects = SideEffects::None(),
2540 uint32_t dex_pc = kNoDexPc)
2541 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002542 SetRawInputAt(0, left);
2543 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002544 }
2545
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002546 HInstruction* GetLeft() const { return InputAt(0); }
2547 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002548 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002549
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002550 virtual bool IsCommutative() const { return false; }
2551
2552 // Put constant on the right.
2553 // Returns whether order is changed.
2554 bool OrderInputsWithConstantOnTheRight() {
2555 HInstruction* left = InputAt(0);
2556 HInstruction* right = InputAt(1);
2557 if (left->IsConstant() && !right->IsConstant()) {
2558 ReplaceInput(right, 0);
2559 ReplaceInput(left, 1);
2560 return true;
2561 }
2562 return false;
2563 }
2564
2565 // Order inputs by instruction id, but favor constant on the right side.
2566 // This helps GVN for commutative ops.
2567 void OrderInputs() {
2568 DCHECK(IsCommutative());
2569 HInstruction* left = InputAt(0);
2570 HInstruction* right = InputAt(1);
2571 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2572 return;
2573 }
2574 if (OrderInputsWithConstantOnTheRight()) {
2575 return;
2576 }
2577 // Order according to instruction id.
2578 if (left->GetId() > right->GetId()) {
2579 ReplaceInput(right, 0);
2580 ReplaceInput(left, 1);
2581 }
2582 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002583
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002584 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002585 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002586 return true;
2587 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002588
Roland Levillain9240d6a2014-10-20 16:47:04 +01002589 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002590 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002591 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002592 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002593
2594 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002595 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2596 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2597 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2598 HLongConstant* y ATTRIBUTE_UNUSED) const {
2599 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2600 return nullptr;
2601 }
2602 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2603 HIntConstant* y ATTRIBUTE_UNUSED) const {
2604 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2605 return nullptr;
2606 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002607
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002608 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002609 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002610 HConstant* GetConstantRight() const;
2611
2612 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002613 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002614 HInstruction* GetLeastConstantLeft() const;
2615
Roland Levillainccc07a92014-09-16 14:48:16 +01002616 DECLARE_INSTRUCTION(BinaryOperation);
2617
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002618 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002619 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2620};
2621
Mark Mendellc4701932015-04-10 13:18:51 -04002622// The comparison bias applies for floating point operations and indicates how NaN
2623// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002624enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002625 kNoBias, // bias is not applicable (i.e. for long operation)
2626 kGtBias, // return 1 for NaN comparisons
2627 kLtBias, // return -1 for NaN comparisons
2628};
2629
Dave Allison20dfc792014-06-16 20:44:29 -07002630class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002631 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002632 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2633 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002634 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002635 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002636
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002637 bool NeedsMaterialization() const { return needs_materialization_; }
2638 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002639
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002640 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002641 // `instruction`, and disregard moves in between.
2642 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002643
Dave Allison20dfc792014-06-16 20:44:29 -07002644 DECLARE_INSTRUCTION(Condition);
2645
2646 virtual IfCondition GetCondition() const = 0;
2647
Mark Mendellc4701932015-04-10 13:18:51 -04002648 virtual IfCondition GetOppositeCondition() const = 0;
2649
Roland Levillain4fa13f62015-07-06 18:11:54 +01002650 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002651
2652 void SetBias(ComparisonBias bias) { bias_ = bias; }
2653
2654 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2655 return bias_ == other->AsCondition()->bias_;
2656 }
2657
Roland Levillain4fa13f62015-07-06 18:11:54 +01002658 bool IsFPConditionTrueIfNaN() const {
2659 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2660 IfCondition if_cond = GetCondition();
2661 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2662 }
2663
2664 bool IsFPConditionFalseIfNaN() const {
2665 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2666 IfCondition if_cond = GetCondition();
2667 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2668 }
2669
Dave Allison20dfc792014-06-16 20:44:29 -07002670 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002671 // For register allocation purposes, returns whether this instruction needs to be
2672 // materialized (that is, not just be in the processor flags).
2673 bool needs_materialization_;
2674
Mark Mendellc4701932015-04-10 13:18:51 -04002675 // Needed if we merge a HCompare into a HCondition.
2676 ComparisonBias bias_;
2677
Dave Allison20dfc792014-06-16 20:44:29 -07002678 DISALLOW_COPY_AND_ASSIGN(HCondition);
2679};
2680
2681// Instruction to check if two inputs are equal to each other.
2682class HEqual : public HCondition {
2683 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002684 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2685 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002686
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002687 bool IsCommutative() const OVERRIDE { return true; }
2688
Roland Levillain9867bc72015-08-05 10:21:34 +01002689 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002690 return GetBlock()->GetGraph()->GetIntConstant(
2691 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002692 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002693 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002694 return GetBlock()->GetGraph()->GetIntConstant(
2695 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002696 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002697
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002698 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002699
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002700 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002701 return kCondEQ;
2702 }
2703
Mark Mendellc4701932015-04-10 13:18:51 -04002704 IfCondition GetOppositeCondition() const OVERRIDE {
2705 return kCondNE;
2706 }
2707
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002708 private:
Aart Bike9f37602015-10-09 11:15:55 -07002709 template <typename T> bool Compute(T x, T y) const { return x == y; }
2710
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002711 DISALLOW_COPY_AND_ASSIGN(HEqual);
2712};
2713
Dave Allison20dfc792014-06-16 20:44:29 -07002714class HNotEqual : public HCondition {
2715 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002716 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2717 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002718
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002719 bool IsCommutative() const OVERRIDE { return true; }
2720
Roland Levillain9867bc72015-08-05 10:21:34 +01002721 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002722 return GetBlock()->GetGraph()->GetIntConstant(
2723 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002724 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002725 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002726 return GetBlock()->GetGraph()->GetIntConstant(
2727 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002728 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002729
Dave Allison20dfc792014-06-16 20:44:29 -07002730 DECLARE_INSTRUCTION(NotEqual);
2731
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002732 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002733 return kCondNE;
2734 }
2735
Mark Mendellc4701932015-04-10 13:18:51 -04002736 IfCondition GetOppositeCondition() const OVERRIDE {
2737 return kCondEQ;
2738 }
2739
Dave Allison20dfc792014-06-16 20:44:29 -07002740 private:
Aart Bike9f37602015-10-09 11:15:55 -07002741 template <typename T> bool Compute(T x, T y) const { return x != y; }
2742
Dave Allison20dfc792014-06-16 20:44:29 -07002743 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2744};
2745
2746class HLessThan : public HCondition {
2747 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002748 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2749 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002750
Roland Levillain9867bc72015-08-05 10:21:34 +01002751 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002752 return GetBlock()->GetGraph()->GetIntConstant(
2753 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002754 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002755 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 return GetBlock()->GetGraph()->GetIntConstant(
2757 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002758 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002759
Dave Allison20dfc792014-06-16 20:44:29 -07002760 DECLARE_INSTRUCTION(LessThan);
2761
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002762 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002763 return kCondLT;
2764 }
2765
Mark Mendellc4701932015-04-10 13:18:51 -04002766 IfCondition GetOppositeCondition() const OVERRIDE {
2767 return kCondGE;
2768 }
2769
Dave Allison20dfc792014-06-16 20:44:29 -07002770 private:
Aart Bike9f37602015-10-09 11:15:55 -07002771 template <typename T> bool Compute(T x, T y) const { return x < y; }
2772
Dave Allison20dfc792014-06-16 20:44:29 -07002773 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2774};
2775
2776class HLessThanOrEqual : public HCondition {
2777 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002778 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2779 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002780
Roland Levillain9867bc72015-08-05 10:21:34 +01002781 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002782 return GetBlock()->GetGraph()->GetIntConstant(
2783 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002784 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002785 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002786 return GetBlock()->GetGraph()->GetIntConstant(
2787 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002788 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002789
Dave Allison20dfc792014-06-16 20:44:29 -07002790 DECLARE_INSTRUCTION(LessThanOrEqual);
2791
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002792 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002793 return kCondLE;
2794 }
2795
Mark Mendellc4701932015-04-10 13:18:51 -04002796 IfCondition GetOppositeCondition() const OVERRIDE {
2797 return kCondGT;
2798 }
2799
Dave Allison20dfc792014-06-16 20:44:29 -07002800 private:
Aart Bike9f37602015-10-09 11:15:55 -07002801 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2802
Dave Allison20dfc792014-06-16 20:44:29 -07002803 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2804};
2805
2806class HGreaterThan : public HCondition {
2807 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002808 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2809 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002810
Roland Levillain9867bc72015-08-05 10:21:34 +01002811 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002812 return GetBlock()->GetGraph()->GetIntConstant(
2813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002814 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002815 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 return GetBlock()->GetGraph()->GetIntConstant(
2817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002818 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002819
Dave Allison20dfc792014-06-16 20:44:29 -07002820 DECLARE_INSTRUCTION(GreaterThan);
2821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002822 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002823 return kCondGT;
2824 }
2825
Mark Mendellc4701932015-04-10 13:18:51 -04002826 IfCondition GetOppositeCondition() const OVERRIDE {
2827 return kCondLE;
2828 }
2829
Dave Allison20dfc792014-06-16 20:44:29 -07002830 private:
Aart Bike9f37602015-10-09 11:15:55 -07002831 template <typename T> bool Compute(T x, T y) const { return x > y; }
2832
Dave Allison20dfc792014-06-16 20:44:29 -07002833 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2834};
2835
2836class HGreaterThanOrEqual : public HCondition {
2837 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002838 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2839 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002840
Roland Levillain9867bc72015-08-05 10:21:34 +01002841 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002842 return GetBlock()->GetGraph()->GetIntConstant(
2843 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002844 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002845 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002846 return GetBlock()->GetGraph()->GetIntConstant(
2847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002848 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002849
Dave Allison20dfc792014-06-16 20:44:29 -07002850 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2851
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002852 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002853 return kCondGE;
2854 }
2855
Mark Mendellc4701932015-04-10 13:18:51 -04002856 IfCondition GetOppositeCondition() const OVERRIDE {
2857 return kCondLT;
2858 }
2859
Dave Allison20dfc792014-06-16 20:44:29 -07002860 private:
Aart Bike9f37602015-10-09 11:15:55 -07002861 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2862
Dave Allison20dfc792014-06-16 20:44:29 -07002863 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2864};
2865
Aart Bike9f37602015-10-09 11:15:55 -07002866class HBelow : public HCondition {
2867 public:
2868 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2869 : HCondition(first, second, dex_pc) {}
2870
2871 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2872 return GetBlock()->GetGraph()->GetIntConstant(
2873 Compute(static_cast<uint32_t>(x->GetValue()),
2874 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2875 }
2876 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2877 return GetBlock()->GetGraph()->GetIntConstant(
2878 Compute(static_cast<uint64_t>(x->GetValue()),
2879 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2880 }
2881
2882 DECLARE_INSTRUCTION(Below);
2883
2884 IfCondition GetCondition() const OVERRIDE {
2885 return kCondB;
2886 }
2887
2888 IfCondition GetOppositeCondition() const OVERRIDE {
2889 return kCondAE;
2890 }
2891
2892 private:
2893 template <typename T> bool Compute(T x, T y) const { return x < y; }
2894
2895 DISALLOW_COPY_AND_ASSIGN(HBelow);
2896};
2897
2898class HBelowOrEqual : public HCondition {
2899 public:
2900 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2901 : HCondition(first, second, dex_pc) {}
2902
2903 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2904 return GetBlock()->GetGraph()->GetIntConstant(
2905 Compute(static_cast<uint32_t>(x->GetValue()),
2906 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2907 }
2908 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2909 return GetBlock()->GetGraph()->GetIntConstant(
2910 Compute(static_cast<uint64_t>(x->GetValue()),
2911 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2912 }
2913
2914 DECLARE_INSTRUCTION(BelowOrEqual);
2915
2916 IfCondition GetCondition() const OVERRIDE {
2917 return kCondBE;
2918 }
2919
2920 IfCondition GetOppositeCondition() const OVERRIDE {
2921 return kCondA;
2922 }
2923
2924 private:
2925 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2926
2927 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2928};
2929
2930class HAbove : public HCondition {
2931 public:
2932 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2933 : HCondition(first, second, dex_pc) {}
2934
2935 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2936 return GetBlock()->GetGraph()->GetIntConstant(
2937 Compute(static_cast<uint32_t>(x->GetValue()),
2938 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2939 }
2940 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2941 return GetBlock()->GetGraph()->GetIntConstant(
2942 Compute(static_cast<uint64_t>(x->GetValue()),
2943 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2944 }
2945
2946 DECLARE_INSTRUCTION(Above);
2947
2948 IfCondition GetCondition() const OVERRIDE {
2949 return kCondA;
2950 }
2951
2952 IfCondition GetOppositeCondition() const OVERRIDE {
2953 return kCondBE;
2954 }
2955
2956 private:
2957 template <typename T> bool Compute(T x, T y) const { return x > y; }
2958
2959 DISALLOW_COPY_AND_ASSIGN(HAbove);
2960};
2961
2962class HAboveOrEqual : public HCondition {
2963 public:
2964 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2965 : HCondition(first, second, dex_pc) {}
2966
2967 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2968 return GetBlock()->GetGraph()->GetIntConstant(
2969 Compute(static_cast<uint32_t>(x->GetValue()),
2970 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2971 }
2972 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2973 return GetBlock()->GetGraph()->GetIntConstant(
2974 Compute(static_cast<uint64_t>(x->GetValue()),
2975 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2976 }
2977
2978 DECLARE_INSTRUCTION(AboveOrEqual);
2979
2980 IfCondition GetCondition() const OVERRIDE {
2981 return kCondAE;
2982 }
2983
2984 IfCondition GetOppositeCondition() const OVERRIDE {
2985 return kCondB;
2986 }
2987
2988 private:
2989 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2990
2991 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
2992};
Dave Allison20dfc792014-06-16 20:44:29 -07002993
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002994// Instruction to check how two inputs compare to each other.
2995// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2996class HCompare : public HBinaryOperation {
2997 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002998 HCompare(Primitive::Type type,
2999 HInstruction* first,
3000 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003001 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003002 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003003 : HBinaryOperation(Primitive::kPrimInt,
3004 first,
3005 second,
3006 SideEffectsForArchRuntimeCalls(type),
3007 dex_pc),
3008 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003009 DCHECK_EQ(type, first->GetType());
3010 DCHECK_EQ(type, second->GetType());
3011 }
3012
Roland Levillain9867bc72015-08-05 10:21:34 +01003013 template <typename T>
3014 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003015
Roland Levillain9867bc72015-08-05 10:21:34 +01003016 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003017 return GetBlock()->GetGraph()->GetIntConstant(
3018 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003019 }
3020 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003021 return GetBlock()->GetGraph()->GetIntConstant(
3022 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003023 }
3024
Calin Juravleddb7df22014-11-25 20:56:51 +00003025 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3026 return bias_ == other->AsCompare()->bias_;
3027 }
3028
Mark Mendellc4701932015-04-10 13:18:51 -04003029 ComparisonBias GetBias() const { return bias_; }
3030
Roland Levillain4fa13f62015-07-06 18:11:54 +01003031 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003032
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003033
3034 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3035 // MIPS64 uses a runtime call for FP comparisons.
3036 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3037 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003038
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003039 DECLARE_INSTRUCTION(Compare);
3040
3041 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003042 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003043
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003044 DISALLOW_COPY_AND_ASSIGN(HCompare);
3045};
3046
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003047// A local in the graph. Corresponds to a Dex register.
3048class HLocal : public HTemplateInstruction<0> {
3049 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003050 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003051 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003052
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003053 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003054
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003055 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003056
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003057 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003058 // The Dex register number.
3059 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003060
3061 DISALLOW_COPY_AND_ASSIGN(HLocal);
3062};
3063
3064// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003065class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003066 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003067 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3068 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003069 SetRawInputAt(0, local);
3070 }
3071
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003072 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3073
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003074 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003075
3076 private:
3077 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3078};
3079
3080// Store a value in a given local. This instruction has two inputs: the value
3081// and the local.
3082class HStoreLocal : public HTemplateInstruction<2> {
3083 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003084 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3085 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003086 SetRawInputAt(0, local);
3087 SetRawInputAt(1, value);
3088 }
3089
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003090 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3091
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003092 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003093
3094 private:
3095 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3096};
3097
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003098class HFloatConstant : public HConstant {
3099 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003100 float GetValue() const { return value_; }
3101
David Brazdil77a48ae2015-09-15 12:34:04 +00003102 uint64_t GetValueAsUint64() const OVERRIDE {
3103 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3104 }
3105
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003106 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003107 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003108 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003109 }
3110
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003111 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003112
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003113 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003114 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003115 }
3116 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003117 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003118 }
3119 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003120 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3121 }
3122 bool IsNaN() const {
3123 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003124 }
3125
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003126 DECLARE_INSTRUCTION(FloatConstant);
3127
3128 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003129 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3130 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3131 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3132 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003133
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003134 const float value_;
3135
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003136 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003137 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003138 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003139 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3140};
3141
3142class HDoubleConstant : public HConstant {
3143 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003144 double GetValue() const { return value_; }
3145
David Brazdil77a48ae2015-09-15 12:34:04 +00003146 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3147
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003148 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003149 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003150 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003151 }
3152
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003153 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003154
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003155 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003156 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003157 }
3158 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003159 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003160 }
3161 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003162 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3163 }
3164 bool IsNaN() const {
3165 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003166 }
3167
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003168 DECLARE_INSTRUCTION(DoubleConstant);
3169
3170 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003171 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3172 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3173 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3174 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003175
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003176 const double value_;
3177
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003178 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003179 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003180 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003181 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3182};
3183
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003184enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003185#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003186#include "intrinsics_list.h"
3187 kNone,
3188 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3189#undef INTRINSICS_LIST
3190#undef OPTIMIZING_INTRINSICS
3191};
3192std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3193
Agi Csaki05f20562015-08-19 14:58:14 -07003194enum IntrinsicNeedsEnvironmentOrCache {
3195 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3196 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003197};
3198
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003199class HInvoke : public HInstruction {
3200 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003201 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003202
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003203 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003204
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003205 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003206 SetRawInputAt(index, argument);
3207 }
3208
Roland Levillain3e3d7332015-04-28 11:00:54 +01003209 // Return the number of arguments. This number can be lower than
3210 // the number of inputs returned by InputCount(), as some invoke
3211 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3212 // inputs at the end of their list of inputs.
3213 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3214
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003215 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003216
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003217
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003218 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003219 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003220
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003221 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3222
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003223 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003224 return intrinsic_;
3225 }
3226
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003227 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003228
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003229 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003230 return GetEnvironment()->GetParent() != nullptr;
3231 }
3232
3233 bool CanThrow() const OVERRIDE { return true; }
3234
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003235 uint32_t* GetIntrinsicOptimizations() {
3236 return &intrinsic_optimizations_;
3237 }
3238
3239 const uint32_t* GetIntrinsicOptimizations() const {
3240 return &intrinsic_optimizations_;
3241 }
3242
3243 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3244
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003245 DECLARE_INSTRUCTION(Invoke);
3246
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003247 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003248 HInvoke(ArenaAllocator* arena,
3249 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003250 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003251 Primitive::Type return_type,
3252 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003253 uint32_t dex_method_index,
3254 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003255 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003256 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003257 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003258 inputs_(number_of_arguments + number_of_other_inputs,
3259 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003260 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003261 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003262 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003263 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003264 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003265 }
3266
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003267 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003268 return inputs_[index];
3269 }
3270
David Brazdil1abb4192015-02-17 18:33:36 +00003271 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003272 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003273 }
3274
Roland Levillain3e3d7332015-04-28 11:00:54 +01003275 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003276 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003277 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003278 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003279 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003280 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003281
3282 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3283 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003284
3285 private:
3286 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3287};
3288
Calin Juravle175dc732015-08-25 15:42:32 +01003289class HInvokeUnresolved : public HInvoke {
3290 public:
3291 HInvokeUnresolved(ArenaAllocator* arena,
3292 uint32_t number_of_arguments,
3293 Primitive::Type return_type,
3294 uint32_t dex_pc,
3295 uint32_t dex_method_index,
3296 InvokeType invoke_type)
3297 : HInvoke(arena,
3298 number_of_arguments,
3299 0u /* number_of_other_inputs */,
3300 return_type,
3301 dex_pc,
3302 dex_method_index,
3303 invoke_type) {
3304 }
3305
3306 DECLARE_INSTRUCTION(InvokeUnresolved);
3307
3308 private:
3309 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3310};
3311
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003312class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003313 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003314 // Requirements of this method call regarding the class
3315 // initialization (clinit) check of its declaring class.
3316 enum class ClinitCheckRequirement {
3317 kNone, // Class already initialized.
3318 kExplicit, // Static call having explicit clinit check as last input.
3319 kImplicit, // Static call implicitly requiring a clinit check.
3320 };
3321
Vladimir Marko58155012015-08-19 12:49:41 +00003322 // Determines how to load the target ArtMethod*.
3323 enum class MethodLoadKind {
3324 // Use a String init ArtMethod* loaded from Thread entrypoints.
3325 kStringInit,
3326
3327 // Use the method's own ArtMethod* loaded by the register allocator.
3328 kRecursive,
3329
3330 // Use ArtMethod* at a known address, embed the direct address in the code.
3331 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3332 kDirectAddress,
3333
3334 // Use ArtMethod* at an address that will be known at link time, embed the direct
3335 // address in the code. If the image is relocatable, emit .patch_oat entry.
3336 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3337 // the image relocatable or not.
3338 kDirectAddressWithFixup,
3339
3340 // Load from resoved methods array in the dex cache using a PC-relative load.
3341 // Used when we need to use the dex cache, for example for invoke-static that
3342 // may cause class initialization (the entry may point to a resolution method),
3343 // and we know that we can access the dex cache arrays using a PC-relative load.
3344 kDexCachePcRelative,
3345
3346 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3347 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3348 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3349 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3350 kDexCacheViaMethod,
3351 };
3352
3353 // Determines the location of the code pointer.
3354 enum class CodePtrLocation {
3355 // Recursive call, use local PC-relative call instruction.
3356 kCallSelf,
3357
3358 // Use PC-relative call instruction patched at link time.
3359 // Used for calls within an oat file, boot->boot or app->app.
3360 kCallPCRelative,
3361
3362 // Call to a known target address, embed the direct address in code.
3363 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3364 kCallDirect,
3365
3366 // Call to a target address that will be known at link time, embed the direct
3367 // address in code. If the image is relocatable, emit .patch_oat entry.
3368 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3369 // the image relocatable or not.
3370 kCallDirectWithFixup,
3371
3372 // Use code pointer from the ArtMethod*.
3373 // Used when we don't know the target code. This is also the last-resort-kind used when
3374 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3375 kCallArtMethod,
3376 };
3377
3378 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003379 MethodLoadKind method_load_kind;
3380 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003381 // The method load data holds
3382 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3383 // Note that there are multiple string init methods, each having its own offset.
3384 // - the method address for kDirectAddress
3385 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003386 uint64_t method_load_data;
3387 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003388 };
3389
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003390 HInvokeStaticOrDirect(ArenaAllocator* arena,
3391 uint32_t number_of_arguments,
3392 Primitive::Type return_type,
3393 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003394 uint32_t method_index,
3395 MethodReference target_method,
3396 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003397 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003398 InvokeType invoke_type,
3399 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003400 : HInvoke(arena,
3401 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003402 // There is one extra argument for the HCurrentMethod node, and
3403 // potentially one other if the clinit check is explicit, and one other
3404 // if the method is a string factory.
3405 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003406 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003407 return_type,
3408 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003409 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003410 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003411 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003412 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003413 target_method_(target_method),
3414 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003415
Vladimir Markodc151b22015-10-15 18:02:30 +01003416 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
3417 dispatch_info_ = dispatch_info;
3418 }
3419
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003420 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003421 // We access the method via the dex cache so we can't do an implicit null check.
3422 // TODO: for intrinsics we can generate implicit null checks.
3423 return false;
3424 }
3425
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003426 bool CanBeNull() const OVERRIDE {
3427 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3428 }
3429
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003430 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003431 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3432 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3433 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003434 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003435 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003436 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003437 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003438 bool HasPcRelDexCache() const {
3439 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3440 }
Vladimir Marko58155012015-08-19 12:49:41 +00003441 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3442 MethodReference GetTargetMethod() const { return target_method_; }
3443
3444 int32_t GetStringInitOffset() const {
3445 DCHECK(IsStringInit());
3446 return dispatch_info_.method_load_data;
3447 }
3448
3449 uint64_t GetMethodAddress() const {
3450 DCHECK(HasMethodAddress());
3451 return dispatch_info_.method_load_data;
3452 }
3453
3454 uint32_t GetDexCacheArrayOffset() const {
3455 DCHECK(HasPcRelDexCache());
3456 return dispatch_info_.method_load_data;
3457 }
3458
3459 uint64_t GetDirectCodePtr() const {
3460 DCHECK(HasDirectCodePtr());
3461 return dispatch_info_.direct_code_ptr;
3462 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003463
Calin Juravle68ad6492015-08-18 17:08:12 +01003464 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3465
Roland Levillain4c0eb422015-04-24 16:43:49 +01003466 // Is this instruction a call to a static method?
3467 bool IsStatic() const {
3468 return GetInvokeType() == kStatic;
3469 }
3470
Roland Levillain3e3d7332015-04-28 11:00:54 +01003471 // Remove the art::HLoadClass instruction set as last input by
3472 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3473 // the initial art::HClinitCheck instruction (only relevant for
3474 // static calls with explicit clinit check).
3475 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003476 DCHECK(IsStaticWithExplicitClinitCheck());
3477 size_t last_input_index = InputCount() - 1;
3478 HInstruction* last_input = InputAt(last_input_index);
3479 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003480 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003481 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003482 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003483 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3484 DCHECK(IsStaticWithImplicitClinitCheck());
3485 }
3486
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003487 bool IsStringFactoryFor(HFakeString* str) const {
3488 if (!IsStringInit()) return false;
3489 // +1 for the current method.
3490 if (InputCount() == (number_of_arguments_ + 1)) return false;
3491 return InputAt(InputCount() - 1)->AsFakeString() == str;
3492 }
3493
3494 void RemoveFakeStringArgumentAsLastInput() {
3495 DCHECK(IsStringInit());
3496 size_t last_input_index = InputCount() - 1;
3497 HInstruction* last_input = InputAt(last_input_index);
3498 DCHECK(last_input != nullptr);
3499 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3500 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003501 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003502 }
3503
Roland Levillain4c0eb422015-04-24 16:43:49 +01003504 // Is this a call to a static method whose declaring class has an
3505 // explicit intialization check in the graph?
3506 bool IsStaticWithExplicitClinitCheck() const {
3507 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3508 }
3509
3510 // Is this a call to a static method whose declaring class has an
3511 // implicit intialization check requirement?
3512 bool IsStaticWithImplicitClinitCheck() const {
3513 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3514 }
3515
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003516 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003517
Roland Levillain4c0eb422015-04-24 16:43:49 +01003518 protected:
3519 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3520 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3521 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3522 HInstruction* input = input_record.GetInstruction();
3523 // `input` is the last input of a static invoke marked as having
3524 // an explicit clinit check. It must either be:
3525 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3526 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3527 DCHECK(input != nullptr);
3528 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3529 }
3530 return input_record;
3531 }
3532
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003533 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003534 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003535 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003536 // The target method may refer to different dex file or method index than the original
3537 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3538 // in derived class to increase visibility.
3539 MethodReference target_method_;
3540 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003541
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003542 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003543};
3544
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003545class HInvokeVirtual : public HInvoke {
3546 public:
3547 HInvokeVirtual(ArenaAllocator* arena,
3548 uint32_t number_of_arguments,
3549 Primitive::Type return_type,
3550 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003551 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003552 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003553 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003554 vtable_index_(vtable_index) {}
3555
Calin Juravle641547a2015-04-21 22:08:51 +01003556 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003557 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003558 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003559 }
3560
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003561 uint32_t GetVTableIndex() const { return vtable_index_; }
3562
3563 DECLARE_INSTRUCTION(InvokeVirtual);
3564
3565 private:
3566 const uint32_t vtable_index_;
3567
3568 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3569};
3570
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003571class HInvokeInterface : public HInvoke {
3572 public:
3573 HInvokeInterface(ArenaAllocator* arena,
3574 uint32_t number_of_arguments,
3575 Primitive::Type return_type,
3576 uint32_t dex_pc,
3577 uint32_t dex_method_index,
3578 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003579 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003580 imt_index_(imt_index) {}
3581
Calin Juravle641547a2015-04-21 22:08:51 +01003582 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003583 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003584 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003585 }
3586
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003587 uint32_t GetImtIndex() const { return imt_index_; }
3588 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3589
3590 DECLARE_INSTRUCTION(InvokeInterface);
3591
3592 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003593 const uint32_t imt_index_;
3594
3595 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3596};
3597
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003598class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003599 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003600 HNewInstance(HCurrentMethod* current_method,
3601 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003602 uint16_t type_index,
3603 const DexFile& dex_file,
Mingyao Yang7f43a3d2015-10-27 16:38:33 -07003604 bool can_throw,
3605 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003606 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003607 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003608 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003609 dex_file_(dex_file),
Mingyao Yang7f43a3d2015-10-27 16:38:33 -07003610 can_throw_(can_throw),
3611 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003612 entrypoint_(entrypoint) {
3613 SetRawInputAt(0, current_method);
3614 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003615
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003616 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003617 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003618
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003619 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003620 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yang7f43a3d2015-10-27 16:38:33 -07003621
3622 // It may throw when called on type that's not instantiable/accessible.
3623 bool CanThrow() const OVERRIDE { return can_throw_; }
3624
3625 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003626
Calin Juravle10e244f2015-01-26 18:54:32 +00003627 bool CanBeNull() const OVERRIDE { return false; }
3628
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003629 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3630
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003631 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003632
3633 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003634 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003635 const DexFile& dex_file_;
Mingyao Yang7f43a3d2015-10-27 16:38:33 -07003636 const bool can_throw_;
3637 const bool finalizable_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003638 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003639
3640 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3641};
3642
Roland Levillain88cb1752014-10-20 16:36:47 +01003643class HNeg : public HUnaryOperation {
3644 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003645 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3646 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003647
Roland Levillain9867bc72015-08-05 10:21:34 +01003648 template <typename T> T Compute(T x) const { return -x; }
3649
3650 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003651 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003652 }
3653 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003654 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003655 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003656
Roland Levillain88cb1752014-10-20 16:36:47 +01003657 DECLARE_INSTRUCTION(Neg);
3658
3659 private:
3660 DISALLOW_COPY_AND_ASSIGN(HNeg);
3661};
3662
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003663class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003664 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003665 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003666 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003667 uint32_t dex_pc,
3668 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003669 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003670 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003671 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003672 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003673 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003674 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003675 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003676 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003677 }
3678
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003679 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003680 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003681
3682 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003683 bool NeedsEnvironment() const OVERRIDE { return true; }
3684
Mingyao Yang0c365e62015-03-31 15:09:29 -07003685 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3686 bool CanThrow() const OVERRIDE { return true; }
3687
Calin Juravle10e244f2015-01-26 18:54:32 +00003688 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003689
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003690 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3691
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003692 DECLARE_INSTRUCTION(NewArray);
3693
3694 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003695 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003696 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003697 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003698
3699 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3700};
3701
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003702class HAdd : public HBinaryOperation {
3703 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003704 HAdd(Primitive::Type result_type,
3705 HInstruction* left,
3706 HInstruction* right,
3707 uint32_t dex_pc = kNoDexPc)
3708 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003709
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003710 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003711
Roland Levillain9867bc72015-08-05 10:21:34 +01003712 template <typename T> T Compute(T x, T y) const { return x + y; }
3713
3714 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003715 return GetBlock()->GetGraph()->GetIntConstant(
3716 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003717 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003718 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003719 return GetBlock()->GetGraph()->GetLongConstant(
3720 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003721 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003722
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003723 DECLARE_INSTRUCTION(Add);
3724
3725 private:
3726 DISALLOW_COPY_AND_ASSIGN(HAdd);
3727};
3728
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003729class HSub : public HBinaryOperation {
3730 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003731 HSub(Primitive::Type result_type,
3732 HInstruction* left,
3733 HInstruction* right,
3734 uint32_t dex_pc = kNoDexPc)
3735 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003736
Roland Levillain9867bc72015-08-05 10:21:34 +01003737 template <typename T> T Compute(T x, T y) const { return x - y; }
3738
3739 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003740 return GetBlock()->GetGraph()->GetIntConstant(
3741 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003742 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003743 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003744 return GetBlock()->GetGraph()->GetLongConstant(
3745 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003746 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003747
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003748 DECLARE_INSTRUCTION(Sub);
3749
3750 private:
3751 DISALLOW_COPY_AND_ASSIGN(HSub);
3752};
3753
Calin Juravle34bacdf2014-10-07 20:23:36 +01003754class HMul : public HBinaryOperation {
3755 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003756 HMul(Primitive::Type result_type,
3757 HInstruction* left,
3758 HInstruction* right,
3759 uint32_t dex_pc = kNoDexPc)
3760 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003761
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003762 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003763
Roland Levillain9867bc72015-08-05 10:21:34 +01003764 template <typename T> T Compute(T x, T y) const { return x * y; }
3765
3766 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003767 return GetBlock()->GetGraph()->GetIntConstant(
3768 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003769 }
3770 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003771 return GetBlock()->GetGraph()->GetLongConstant(
3772 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003773 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003774
3775 DECLARE_INSTRUCTION(Mul);
3776
3777 private:
3778 DISALLOW_COPY_AND_ASSIGN(HMul);
3779};
3780
Calin Juravle7c4954d2014-10-28 16:57:40 +00003781class HDiv : public HBinaryOperation {
3782 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003783 HDiv(Primitive::Type result_type,
3784 HInstruction* left,
3785 HInstruction* right,
3786 uint32_t dex_pc)
3787 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003788
Roland Levillain9867bc72015-08-05 10:21:34 +01003789 template <typename T>
3790 T Compute(T x, T y) const {
3791 // Our graph structure ensures we never have 0 for `y` during
3792 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003793 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003794 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003795 return (y == -1) ? -x : x / y;
3796 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003797
Roland Levillain9867bc72015-08-05 10:21:34 +01003798 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());
Calin Juravlebacfec32014-11-14 15:54:36 +00003805 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003806
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003807 static SideEffects SideEffectsForArchRuntimeCalls() {
3808 // The generated code can use a runtime call.
3809 return SideEffects::CanTriggerGC();
3810 }
3811
Calin Juravle7c4954d2014-10-28 16:57:40 +00003812 DECLARE_INSTRUCTION(Div);
3813
3814 private:
3815 DISALLOW_COPY_AND_ASSIGN(HDiv);
3816};
3817
Calin Juravlebacfec32014-11-14 15:54:36 +00003818class HRem : public HBinaryOperation {
3819 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003820 HRem(Primitive::Type result_type,
3821 HInstruction* left,
3822 HInstruction* right,
3823 uint32_t dex_pc)
3824 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003825
Roland Levillain9867bc72015-08-05 10:21:34 +01003826 template <typename T>
3827 T Compute(T x, T y) const {
3828 // Our graph structure ensures we never have 0 for `y` during
3829 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003830 DCHECK_NE(y, 0);
3831 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3832 return (y == -1) ? 0 : x % y;
3833 }
3834
Roland Levillain9867bc72015-08-05 10:21:34 +01003835 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003836 return GetBlock()->GetGraph()->GetIntConstant(
3837 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003838 }
3839 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003840 return GetBlock()->GetGraph()->GetLongConstant(
3841 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003842 }
3843
Calin Juravlebacfec32014-11-14 15:54:36 +00003844
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003845 static SideEffects SideEffectsForArchRuntimeCalls() {
3846 return SideEffects::CanTriggerGC();
3847 }
3848
Calin Juravlebacfec32014-11-14 15:54:36 +00003849 DECLARE_INSTRUCTION(Rem);
3850
3851 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003852 DISALLOW_COPY_AND_ASSIGN(HRem);
3853};
3854
Calin Juravled0d48522014-11-04 16:40:20 +00003855class HDivZeroCheck : public HExpression<1> {
3856 public:
3857 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003858 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003859 SetRawInputAt(0, value);
3860 }
3861
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003862 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3863
Calin Juravled0d48522014-11-04 16:40:20 +00003864 bool CanBeMoved() const OVERRIDE { return true; }
3865
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003866 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003867 return true;
3868 }
3869
3870 bool NeedsEnvironment() const OVERRIDE { return true; }
3871 bool CanThrow() const OVERRIDE { return true; }
3872
Calin Juravled0d48522014-11-04 16:40:20 +00003873 DECLARE_INSTRUCTION(DivZeroCheck);
3874
3875 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003876 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3877};
3878
Calin Juravle9aec02f2014-11-18 23:06:35 +00003879class HShl : public HBinaryOperation {
3880 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003881 HShl(Primitive::Type result_type,
3882 HInstruction* left,
3883 HInstruction* right,
3884 uint32_t dex_pc = kNoDexPc)
3885 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003886
Roland Levillain9867bc72015-08-05 10:21:34 +01003887 template <typename T, typename U, typename V>
3888 T Compute(T x, U y, V max_shift_value) const {
3889 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3890 "V is not the unsigned integer type corresponding to T");
3891 return x << (y & max_shift_value);
3892 }
3893
3894 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3895 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003896 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003897 }
3898 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3899 // case is handled as `x << static_cast<int>(y)`.
3900 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3901 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003902 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003903 }
3904 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3905 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003906 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003907 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003908
3909 DECLARE_INSTRUCTION(Shl);
3910
3911 private:
3912 DISALLOW_COPY_AND_ASSIGN(HShl);
3913};
3914
3915class HShr : public HBinaryOperation {
3916 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003917 HShr(Primitive::Type result_type,
3918 HInstruction* left,
3919 HInstruction* right,
3920 uint32_t dex_pc = kNoDexPc)
3921 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003922
Roland Levillain9867bc72015-08-05 10:21:34 +01003923 template <typename T, typename U, typename V>
3924 T Compute(T x, U y, V max_shift_value) const {
3925 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3926 "V is not the unsigned integer type corresponding to T");
3927 return x >> (y & max_shift_value);
3928 }
3929
3930 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3931 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003932 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003933 }
3934 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3935 // case is handled as `x >> static_cast<int>(y)`.
3936 HConstant* Evaluate(HLongConstant* x, HIntConstant* 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 }
3940 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3941 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003942 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003943 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003944
3945 DECLARE_INSTRUCTION(Shr);
3946
3947 private:
3948 DISALLOW_COPY_AND_ASSIGN(HShr);
3949};
3950
3951class HUShr : public HBinaryOperation {
3952 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003953 HUShr(Primitive::Type result_type,
3954 HInstruction* left,
3955 HInstruction* right,
3956 uint32_t dex_pc = kNoDexPc)
3957 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003958
Roland Levillain9867bc72015-08-05 10:21:34 +01003959 template <typename T, typename U, typename V>
3960 T Compute(T x, U y, V max_shift_value) const {
3961 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3962 "V is not the unsigned integer type corresponding to T");
3963 V ux = static_cast<V>(x);
3964 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003965 }
3966
Roland Levillain9867bc72015-08-05 10:21:34 +01003967 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3968 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003969 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003970 }
3971 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3972 // case is handled as `x >>> static_cast<int>(y)`.
3973 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3974 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003975 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003976 }
3977 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3978 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003979 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003980 }
3981
3982 DECLARE_INSTRUCTION(UShr);
3983
3984 private:
3985 DISALLOW_COPY_AND_ASSIGN(HUShr);
3986};
3987
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003988class HAnd : public HBinaryOperation {
3989 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003990 HAnd(Primitive::Type result_type,
3991 HInstruction* left,
3992 HInstruction* right,
3993 uint32_t dex_pc = kNoDexPc)
3994 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003995
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003996 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003997
Roland Levillain9867bc72015-08-05 10:21:34 +01003998 template <typename T, typename U>
3999 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4000
4001 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004002 return GetBlock()->GetGraph()->GetIntConstant(
4003 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004004 }
4005 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004006 return GetBlock()->GetGraph()->GetLongConstant(
4007 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004008 }
4009 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004010 return GetBlock()->GetGraph()->GetLongConstant(
4011 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004012 }
4013 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004014 return GetBlock()->GetGraph()->GetLongConstant(
4015 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004016 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004017
4018 DECLARE_INSTRUCTION(And);
4019
4020 private:
4021 DISALLOW_COPY_AND_ASSIGN(HAnd);
4022};
4023
4024class HOr : public HBinaryOperation {
4025 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004026 HOr(Primitive::Type result_type,
4027 HInstruction* left,
4028 HInstruction* right,
4029 uint32_t dex_pc = kNoDexPc)
4030 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004031
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004032 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004033
Roland Levillain9867bc72015-08-05 10:21:34 +01004034 template <typename T, typename U>
4035 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4036
4037 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004038 return GetBlock()->GetGraph()->GetIntConstant(
4039 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004040 }
4041 HConstant* Evaluate(HIntConstant* x, HLongConstant* 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, HIntConstant* 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 }
4049 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004050 return GetBlock()->GetGraph()->GetLongConstant(
4051 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004052 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004053
4054 DECLARE_INSTRUCTION(Or);
4055
4056 private:
4057 DISALLOW_COPY_AND_ASSIGN(HOr);
4058};
4059
4060class HXor : public HBinaryOperation {
4061 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004062 HXor(Primitive::Type result_type,
4063 HInstruction* left,
4064 HInstruction* right,
4065 uint32_t dex_pc = kNoDexPc)
4066 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004067
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004068 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004069
Roland Levillain9867bc72015-08-05 10:21:34 +01004070 template <typename T, typename U>
4071 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4072
4073 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004074 return GetBlock()->GetGraph()->GetIntConstant(
4075 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004076 }
4077 HConstant* Evaluate(HIntConstant* x, HLongConstant* 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, HIntConstant* 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 }
4085 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004086 return GetBlock()->GetGraph()->GetLongConstant(
4087 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004088 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004089
4090 DECLARE_INSTRUCTION(Xor);
4091
4092 private:
4093 DISALLOW_COPY_AND_ASSIGN(HXor);
4094};
4095
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004096// The value of a parameter in this method. Its location depends on
4097// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004098class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004099 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004100 HParameterValue(const DexFile& dex_file,
4101 uint16_t type_index,
4102 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004103 Primitive::Type parameter_type,
4104 bool is_this = false)
4105 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004106 dex_file_(dex_file),
4107 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004108 index_(index),
4109 is_this_(is_this),
4110 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004111
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004112 const DexFile& GetDexFile() const { return dex_file_; }
4113 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004114 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004115 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004116
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004117 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4118 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004119
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004120 DECLARE_INSTRUCTION(ParameterValue);
4121
4122 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004123 const DexFile& dex_file_;
4124 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004125 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004126 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004127 const uint8_t index_;
4128
Calin Juravle10e244f2015-01-26 18:54:32 +00004129 // Whether or not the parameter value corresponds to 'this' argument.
4130 const bool is_this_;
4131
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004132 bool can_be_null_;
4133
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004134 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4135};
4136
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004137class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004138 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004139 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4140 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004141
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004142 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004143 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004144 return true;
4145 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004146
Roland Levillain9867bc72015-08-05 10:21:34 +01004147 template <typename T> T Compute(T x) const { return ~x; }
4148
4149 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004150 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004151 }
4152 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004153 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004154 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004155
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004156 DECLARE_INSTRUCTION(Not);
4157
4158 private:
4159 DISALLOW_COPY_AND_ASSIGN(HNot);
4160};
4161
David Brazdil66d126e2015-04-03 16:02:44 +01004162class HBooleanNot : public HUnaryOperation {
4163 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004164 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4165 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004166
4167 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004168 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004169 return true;
4170 }
4171
Roland Levillain9867bc72015-08-05 10:21:34 +01004172 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004173 DCHECK(IsUint<1>(x));
4174 return !x;
4175 }
4176
Roland Levillain9867bc72015-08-05 10:21:34 +01004177 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004178 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004179 }
4180 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4181 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004182 UNREACHABLE();
4183 }
4184
4185 DECLARE_INSTRUCTION(BooleanNot);
4186
4187 private:
4188 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4189};
4190
Roland Levillaindff1f282014-11-05 14:15:05 +00004191class HTypeConversion : public HExpression<1> {
4192 public:
4193 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004194 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004195 : HExpression(result_type,
4196 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4197 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004198 SetRawInputAt(0, input);
4199 DCHECK_NE(input->GetType(), result_type);
4200 }
4201
4202 HInstruction* GetInput() const { return InputAt(0); }
4203 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4204 Primitive::Type GetResultType() const { return GetType(); }
4205
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004206 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004207 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004208
Roland Levillaindff1f282014-11-05 14:15:05 +00004209 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004210 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004211
Mark Mendelle82549b2015-05-06 10:55:34 -04004212 // Try to statically evaluate the conversion and return a HConstant
4213 // containing the result. If the input cannot be converted, return nullptr.
4214 HConstant* TryStaticEvaluation() const;
4215
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004216 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4217 Primitive::Type result_type) {
4218 // Some architectures may not require the 'GC' side effects, but at this point
4219 // in the compilation process we do not know what architecture we will
4220 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004221 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4222 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004223 return SideEffects::CanTriggerGC();
4224 }
4225 return SideEffects::None();
4226 }
4227
Roland Levillaindff1f282014-11-05 14:15:05 +00004228 DECLARE_INSTRUCTION(TypeConversion);
4229
4230 private:
4231 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4232};
4233
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004234static constexpr uint32_t kNoRegNumber = -1;
4235
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004236class HPhi : public HInstruction {
4237 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004238 HPhi(ArenaAllocator* arena,
4239 uint32_t reg_number,
4240 size_t number_of_inputs,
4241 Primitive::Type type,
4242 uint32_t dex_pc = kNoDexPc)
4243 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004244 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004245 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004246 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004247 is_live_(false),
4248 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004249 }
4250
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004251 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4252 static Primitive::Type ToPhiType(Primitive::Type type) {
4253 switch (type) {
4254 case Primitive::kPrimBoolean:
4255 case Primitive::kPrimByte:
4256 case Primitive::kPrimShort:
4257 case Primitive::kPrimChar:
4258 return Primitive::kPrimInt;
4259 default:
4260 return type;
4261 }
4262 }
4263
David Brazdilffee3d32015-07-06 11:48:53 +01004264 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4265
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004266 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004267
4268 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004269 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004270
Calin Juravle10e244f2015-01-26 18:54:32 +00004271 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004272 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004273
Calin Juravle10e244f2015-01-26 18:54:32 +00004274 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4275 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4276
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004277 uint32_t GetRegNumber() const { return reg_number_; }
4278
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004279 void SetDead() { is_live_ = false; }
4280 void SetLive() { is_live_ = true; }
4281 bool IsDead() const { return !is_live_; }
4282 bool IsLive() const { return is_live_; }
4283
David Brazdil77a48ae2015-09-15 12:34:04 +00004284 bool IsVRegEquivalentOf(HInstruction* other) const {
4285 return other != nullptr
4286 && other->IsPhi()
4287 && other->AsPhi()->GetBlock() == GetBlock()
4288 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4289 }
4290
Calin Juravlea4f88312015-04-16 12:57:19 +01004291 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4292 // An equivalent phi is a phi having the same dex register and type.
4293 // It assumes that phis with the same dex register are adjacent.
4294 HPhi* GetNextEquivalentPhiWithSameType() {
4295 HInstruction* next = GetNext();
4296 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4297 if (next->GetType() == GetType()) {
4298 return next->AsPhi();
4299 }
4300 next = next->GetNext();
4301 }
4302 return nullptr;
4303 }
4304
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004305 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004306
David Brazdil1abb4192015-02-17 18:33:36 +00004307 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004308 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004309 return inputs_[index];
4310 }
David Brazdil1abb4192015-02-17 18:33:36 +00004311
4312 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004313 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004314 }
4315
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004316 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004317 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004318 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004319 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004320 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004321 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004322
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004323 DISALLOW_COPY_AND_ASSIGN(HPhi);
4324};
4325
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004326class HNullCheck : public HExpression<1> {
4327 public:
4328 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004329 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004330 SetRawInputAt(0, value);
4331 }
4332
Calin Juravle10e244f2015-01-26 18:54:32 +00004333 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004334 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004335 return true;
4336 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004337
Calin Juravle10e244f2015-01-26 18:54:32 +00004338 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004339
Calin Juravle10e244f2015-01-26 18:54:32 +00004340 bool CanThrow() const OVERRIDE { return true; }
4341
4342 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004343
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004344
4345 DECLARE_INSTRUCTION(NullCheck);
4346
4347 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004348 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4349};
4350
4351class FieldInfo : public ValueObject {
4352 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004353 FieldInfo(MemberOffset field_offset,
4354 Primitive::Type field_type,
4355 bool is_volatile,
4356 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004357 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004358 const DexFile& dex_file,
4359 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004360 : field_offset_(field_offset),
4361 field_type_(field_type),
4362 is_volatile_(is_volatile),
4363 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004364 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004365 dex_file_(dex_file),
4366 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004367
4368 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004369 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004370 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004371 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004372 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004373 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004374 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004375
4376 private:
4377 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004378 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004379 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004380 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004381 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004382 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004383 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004384};
4385
4386class HInstanceFieldGet : public HExpression<1> {
4387 public:
4388 HInstanceFieldGet(HInstruction* value,
4389 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004390 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004391 bool is_volatile,
4392 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004393 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004394 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004395 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004396 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004397 : HExpression(field_type,
4398 SideEffects::FieldReadOfType(field_type, is_volatile),
4399 dex_pc),
4400 field_info_(field_offset,
4401 field_type,
4402 is_volatile,
4403 field_idx,
4404 declaring_class_def_index,
4405 dex_file,
4406 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004407 SetRawInputAt(0, value);
4408 }
4409
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004410 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004411
4412 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4413 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4414 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004415 }
4416
Calin Juravle641547a2015-04-21 22:08:51 +01004417 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4418 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004419 }
4420
4421 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004422 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4423 }
4424
Calin Juravle52c48962014-12-16 17:02:57 +00004425 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004426 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004427 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004428 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004429
4430 DECLARE_INSTRUCTION(InstanceFieldGet);
4431
4432 private:
4433 const FieldInfo field_info_;
4434
4435 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4436};
4437
4438class HInstanceFieldSet : public HTemplateInstruction<2> {
4439 public:
4440 HInstanceFieldSet(HInstruction* object,
4441 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004442 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004443 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004444 bool is_volatile,
4445 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004446 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004447 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004448 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004449 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004450 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4451 dex_pc),
4452 field_info_(field_offset,
4453 field_type,
4454 is_volatile,
4455 field_idx,
4456 declaring_class_def_index,
4457 dex_file,
4458 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004459 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004460 SetRawInputAt(0, object);
4461 SetRawInputAt(1, value);
4462 }
4463
Calin Juravle641547a2015-04-21 22:08:51 +01004464 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4465 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004466 }
4467
Calin Juravle52c48962014-12-16 17:02:57 +00004468 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004469 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004470 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004471 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004472 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004473 bool GetValueCanBeNull() const { return value_can_be_null_; }
4474 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004475
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476 DECLARE_INSTRUCTION(InstanceFieldSet);
4477
4478 private:
4479 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004480 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004481
4482 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4483};
4484
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004485class HArrayGet : public HExpression<2> {
4486 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004487 HArrayGet(HInstruction* array,
4488 HInstruction* index,
4489 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004490 uint32_t dex_pc,
4491 SideEffects additional_side_effects = SideEffects::None())
4492 : HExpression(type,
4493 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
4494 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004495 SetRawInputAt(0, array);
4496 SetRawInputAt(1, index);
4497 }
4498
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004499 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004500 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004501 return true;
4502 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004503 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004504 // TODO: We can be smarter here.
4505 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4506 // which generates the implicit null check. There are cases when these can be removed
4507 // to produce better code. If we ever add optimizations to do so we should allow an
4508 // implicit check here (as long as the address falls in the first page).
4509 return false;
4510 }
4511
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004512 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004513
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004514 HInstruction* GetArray() const { return InputAt(0); }
4515 HInstruction* GetIndex() const { return InputAt(1); }
4516
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004517 DECLARE_INSTRUCTION(ArrayGet);
4518
4519 private:
4520 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4521};
4522
4523class HArraySet : public HTemplateInstruction<3> {
4524 public:
4525 HArraySet(HInstruction* array,
4526 HInstruction* index,
4527 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004528 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004529 uint32_t dex_pc,
4530 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004531 : HTemplateInstruction(
4532 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004533 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4534 additional_side_effects),
4535 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004536 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004537 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004538 value_can_be_null_(true),
4539 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004540 SetRawInputAt(0, array);
4541 SetRawInputAt(1, index);
4542 SetRawInputAt(2, value);
4543 }
4544
Calin Juravle77520bc2015-01-12 18:45:46 +00004545 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004546 // We currently always call a runtime method to catch array store
4547 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004548 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004549 }
4550
Mingyao Yang81014cb2015-06-02 03:16:27 -07004551 // Can throw ArrayStoreException.
4552 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4553
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004554 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004555 // TODO: Same as for ArrayGet.
4556 return false;
4557 }
4558
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004559 void ClearNeedsTypeCheck() {
4560 needs_type_check_ = false;
4561 }
4562
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004563 void ClearValueCanBeNull() {
4564 value_can_be_null_ = false;
4565 }
4566
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004567 void SetStaticTypeOfArrayIsObjectArray() {
4568 static_type_of_array_is_object_array_ = true;
4569 }
4570
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004571 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004572 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004573 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004574
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004575 HInstruction* GetArray() const { return InputAt(0); }
4576 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004577 HInstruction* GetValue() const { return InputAt(2); }
4578
4579 Primitive::Type GetComponentType() const {
4580 // The Dex format does not type floating point index operations. Since the
4581 // `expected_component_type_` is set during building and can therefore not
4582 // be correct, we also check what is the value type. If it is a floating
4583 // point type, we must use that type.
4584 Primitive::Type value_type = GetValue()->GetType();
4585 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4586 ? value_type
4587 : expected_component_type_;
4588 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004589
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004590 Primitive::Type GetRawExpectedComponentType() const {
4591 return expected_component_type_;
4592 }
4593
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004594 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4595 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4596 }
4597
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004598 DECLARE_INSTRUCTION(ArraySet);
4599
4600 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004601 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004602 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004603 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004604 // Cached information for the reference_type_info_ so that codegen
4605 // does not need to inspect the static type.
4606 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004607
4608 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4609};
4610
4611class HArrayLength : public HExpression<1> {
4612 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004613 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004614 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004615 // Note that arrays do not change length, so the instruction does not
4616 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004617 SetRawInputAt(0, array);
4618 }
4619
Calin Juravle77520bc2015-01-12 18:45:46 +00004620 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004621 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004622 return true;
4623 }
Calin Juravle641547a2015-04-21 22:08:51 +01004624 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4625 return obj == InputAt(0);
4626 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004627
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004628 DECLARE_INSTRUCTION(ArrayLength);
4629
4630 private:
4631 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4632};
4633
4634class HBoundsCheck : public HExpression<2> {
4635 public:
4636 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004637 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004638 DCHECK(index->GetType() == Primitive::kPrimInt);
4639 SetRawInputAt(0, index);
4640 SetRawInputAt(1, length);
4641 }
4642
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004643 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004644 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004645 return true;
4646 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004647
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004648 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004649
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004650 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004651
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004652 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004653
4654 DECLARE_INSTRUCTION(BoundsCheck);
4655
4656 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004657 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4658};
4659
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004660/**
4661 * Some DEX instructions are folded into multiple HInstructions that need
4662 * to stay live until the last HInstruction. This class
4663 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004664 * HInstruction stays live. `index` represents the stack location index of the
4665 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004666 */
4667class HTemporary : public HTemplateInstruction<0> {
4668 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004669 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4670 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004671
4672 size_t GetIndex() const { return index_; }
4673
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004674 Primitive::Type GetType() const OVERRIDE {
4675 // The previous instruction is the one that will be stored in the temporary location.
4676 DCHECK(GetPrevious() != nullptr);
4677 return GetPrevious()->GetType();
4678 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004679
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004680 DECLARE_INSTRUCTION(Temporary);
4681
4682 private:
4683 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004684 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4685};
4686
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004687class HSuspendCheck : public HTemplateInstruction<0> {
4688 public:
4689 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004690 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004691
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004692 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004693 return true;
4694 }
4695
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004696 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4697 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004698
4699 DECLARE_INSTRUCTION(SuspendCheck);
4700
4701 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004702 // Only used for code generation, in order to share the same slow path between back edges
4703 // of a same loop.
4704 SlowPathCode* slow_path_;
4705
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004706 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4707};
4708
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004709/**
4710 * Instruction to load a Class object.
4711 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004712class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004713 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004714 HLoadClass(HCurrentMethod* current_method,
4715 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004716 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004717 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004718 uint32_t dex_pc,
4719 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004720 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004721 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004722 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004723 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004724 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004725 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004726 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004727 // Referrers class should not need access check. We never inline unverified
4728 // methods so we can't possibly end up in this situation.
4729 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004730 SetRawInputAt(0, current_method);
4731 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004732
4733 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004734
4735 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004736 // Note that we don't need to test for generate_clinit_check_.
4737 // Whether or not we need to generate the clinit check is processed in
4738 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004739 return other->AsLoadClass()->type_index_ == type_index_ &&
4740 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004741 }
4742
4743 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4744
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004745 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004746 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004747 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004748
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004749 bool NeedsEnvironment() const OVERRIDE {
4750 // Will call runtime and load the class if the class is not loaded yet.
4751 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004752 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004753 }
4754
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004755 bool MustGenerateClinitCheck() const {
4756 return generate_clinit_check_;
4757 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004758 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4759 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004760 }
4761
4762 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004763 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4764 }
4765
4766 bool NeedsAccessCheck() const {
4767 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004768 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004769
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004770 bool CanThrow() const OVERRIDE {
4771 // May call runtime and and therefore can throw.
4772 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004773 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004774 }
4775
Calin Juravleacf735c2015-02-12 15:25:22 +00004776 ReferenceTypeInfo GetLoadedClassRTI() {
4777 return loaded_class_rti_;
4778 }
4779
4780 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4781 // Make sure we only set exact types (the loaded class should never be merged).
4782 DCHECK(rti.IsExact());
4783 loaded_class_rti_ = rti;
4784 }
4785
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004786 const DexFile& GetDexFile() { return dex_file_; }
4787
Vladimir Markodc151b22015-10-15 18:02:30 +01004788 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004789
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004790 static SideEffects SideEffectsForArchRuntimeCalls() {
4791 return SideEffects::CanTriggerGC();
4792 }
4793
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004794 DECLARE_INSTRUCTION(LoadClass);
4795
4796 private:
4797 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004798 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004799 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004800 // Whether this instruction must generate the initialization check.
4801 // Used for code generation.
4802 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004803 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004804
Calin Juravleacf735c2015-02-12 15:25:22 +00004805 ReferenceTypeInfo loaded_class_rti_;
4806
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004807 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4808};
4809
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004810class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004811 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004812 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004813 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4814 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004815 SetRawInputAt(0, current_method);
4816 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004817
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004818 bool CanBeMoved() const OVERRIDE { return true; }
4819
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004820 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4821 return other->AsLoadString()->string_index_ == string_index_;
4822 }
4823
4824 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4825
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004826 uint32_t GetStringIndex() const { return string_index_; }
4827
4828 // TODO: Can we deopt or debug when we resolve a string?
4829 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004830 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004831 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004832
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004833 static SideEffects SideEffectsForArchRuntimeCalls() {
4834 return SideEffects::CanTriggerGC();
4835 }
4836
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004837 DECLARE_INSTRUCTION(LoadString);
4838
4839 private:
4840 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004841
4842 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4843};
4844
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004845/**
4846 * Performs an initialization check on its Class object input.
4847 */
4848class HClinitCheck : public HExpression<1> {
4849 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004850 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004851 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004852 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004853 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4854 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004855 SetRawInputAt(0, constant);
4856 }
4857
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004858 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004859 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004860 return true;
4861 }
4862
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004863 bool NeedsEnvironment() const OVERRIDE {
4864 // May call runtime to initialize the class.
4865 return true;
4866 }
4867
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004868
4869 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4870
4871 DECLARE_INSTRUCTION(ClinitCheck);
4872
4873 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004874 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4875};
4876
4877class HStaticFieldGet : public HExpression<1> {
4878 public:
4879 HStaticFieldGet(HInstruction* cls,
4880 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004881 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004882 bool is_volatile,
4883 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004884 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004885 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004886 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004887 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004888 : HExpression(field_type,
4889 SideEffects::FieldReadOfType(field_type, is_volatile),
4890 dex_pc),
4891 field_info_(field_offset,
4892 field_type,
4893 is_volatile,
4894 field_idx,
4895 declaring_class_def_index,
4896 dex_file,
4897 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004898 SetRawInputAt(0, cls);
4899 }
4900
Calin Juravle52c48962014-12-16 17:02:57 +00004901
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004902 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004903
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004904 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004905 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4906 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004907 }
4908
4909 size_t ComputeHashCode() const OVERRIDE {
4910 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4911 }
4912
Calin Juravle52c48962014-12-16 17:02:57 +00004913 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004914 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4915 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004916 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004917
4918 DECLARE_INSTRUCTION(StaticFieldGet);
4919
4920 private:
4921 const FieldInfo field_info_;
4922
4923 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4924};
4925
4926class HStaticFieldSet : public HTemplateInstruction<2> {
4927 public:
4928 HStaticFieldSet(HInstruction* cls,
4929 HInstruction* value,
4930 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004931 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004932 bool is_volatile,
4933 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004934 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004935 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004936 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004937 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004938 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4939 dex_pc),
4940 field_info_(field_offset,
4941 field_type,
4942 is_volatile,
4943 field_idx,
4944 declaring_class_def_index,
4945 dex_file,
4946 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004947 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004948 SetRawInputAt(0, cls);
4949 SetRawInputAt(1, value);
4950 }
4951
Calin Juravle52c48962014-12-16 17:02:57 +00004952 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004953 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4954 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004955 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004956
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004957 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004958 bool GetValueCanBeNull() const { return value_can_be_null_; }
4959 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004960
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004961 DECLARE_INSTRUCTION(StaticFieldSet);
4962
4963 private:
4964 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004965 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004966
4967 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4968};
4969
Calin Juravlee460d1d2015-09-29 04:52:17 +01004970class HUnresolvedInstanceFieldGet : public HExpression<1> {
4971 public:
4972 HUnresolvedInstanceFieldGet(HInstruction* obj,
4973 Primitive::Type field_type,
4974 uint32_t field_index,
4975 uint32_t dex_pc)
4976 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4977 field_index_(field_index) {
4978 SetRawInputAt(0, obj);
4979 }
4980
4981 bool NeedsEnvironment() const OVERRIDE { return true; }
4982 bool CanThrow() const OVERRIDE { return true; }
4983
4984 Primitive::Type GetFieldType() const { return GetType(); }
4985 uint32_t GetFieldIndex() const { return field_index_; }
4986
4987 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4988
4989 private:
4990 const uint32_t field_index_;
4991
4992 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4993};
4994
4995class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4996 public:
4997 HUnresolvedInstanceFieldSet(HInstruction* obj,
4998 HInstruction* value,
4999 Primitive::Type field_type,
5000 uint32_t field_index,
5001 uint32_t dex_pc)
5002 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5003 field_type_(field_type),
5004 field_index_(field_index) {
5005 DCHECK_EQ(field_type, value->GetType());
5006 SetRawInputAt(0, obj);
5007 SetRawInputAt(1, value);
5008 }
5009
5010 bool NeedsEnvironment() const OVERRIDE { return true; }
5011 bool CanThrow() const OVERRIDE { return true; }
5012
5013 Primitive::Type GetFieldType() const { return field_type_; }
5014 uint32_t GetFieldIndex() const { return field_index_; }
5015
5016 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5017
5018 private:
5019 const Primitive::Type field_type_;
5020 const uint32_t field_index_;
5021
5022 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5023};
5024
5025class HUnresolvedStaticFieldGet : public HExpression<0> {
5026 public:
5027 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5028 uint32_t field_index,
5029 uint32_t dex_pc)
5030 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5031 field_index_(field_index) {
5032 }
5033
5034 bool NeedsEnvironment() const OVERRIDE { return true; }
5035 bool CanThrow() const OVERRIDE { return true; }
5036
5037 Primitive::Type GetFieldType() const { return GetType(); }
5038 uint32_t GetFieldIndex() const { return field_index_; }
5039
5040 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5041
5042 private:
5043 const uint32_t field_index_;
5044
5045 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5046};
5047
5048class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5049 public:
5050 HUnresolvedStaticFieldSet(HInstruction* value,
5051 Primitive::Type field_type,
5052 uint32_t field_index,
5053 uint32_t dex_pc)
5054 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5055 field_type_(field_type),
5056 field_index_(field_index) {
5057 DCHECK_EQ(field_type, value->GetType());
5058 SetRawInputAt(0, value);
5059 }
5060
5061 bool NeedsEnvironment() const OVERRIDE { return true; }
5062 bool CanThrow() const OVERRIDE { return true; }
5063
5064 Primitive::Type GetFieldType() const { return field_type_; }
5065 uint32_t GetFieldIndex() const { return field_index_; }
5066
5067 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5068
5069 private:
5070 const Primitive::Type field_type_;
5071 const uint32_t field_index_;
5072
5073 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5074};
5075
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005076// Implement the move-exception DEX instruction.
5077class HLoadException : public HExpression<0> {
5078 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005079 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5080 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005081
David Brazdilbbd733e2015-08-18 17:48:17 +01005082 bool CanBeNull() const OVERRIDE { return false; }
5083
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005084 DECLARE_INSTRUCTION(LoadException);
5085
5086 private:
5087 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5088};
5089
David Brazdilcb1c0552015-08-04 16:22:25 +01005090// Implicit part of move-exception which clears thread-local exception storage.
5091// Must not be removed because the runtime expects the TLS to get cleared.
5092class HClearException : public HTemplateInstruction<0> {
5093 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005094 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5095 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005096
5097 DECLARE_INSTRUCTION(ClearException);
5098
5099 private:
5100 DISALLOW_COPY_AND_ASSIGN(HClearException);
5101};
5102
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005103class HThrow : public HTemplateInstruction<1> {
5104 public:
5105 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005106 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005107 SetRawInputAt(0, exception);
5108 }
5109
5110 bool IsControlFlow() const OVERRIDE { return true; }
5111
5112 bool NeedsEnvironment() const OVERRIDE { return true; }
5113
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005114 bool CanThrow() const OVERRIDE { return true; }
5115
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005116
5117 DECLARE_INSTRUCTION(Throw);
5118
5119 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005120 DISALLOW_COPY_AND_ASSIGN(HThrow);
5121};
5122
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005123/**
5124 * Implementation strategies for the code generator of a HInstanceOf
5125 * or `HCheckCast`.
5126 */
5127enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005128 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005129 kExactCheck, // Can do a single class compare.
5130 kClassHierarchyCheck, // Can just walk the super class chain.
5131 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5132 kInterfaceCheck, // No optimization yet when checking against an interface.
5133 kArrayObjectCheck, // Can just check if the array is not primitive.
5134 kArrayCheck // No optimization yet when checking against a generic array.
5135};
5136
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005137class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005138 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005139 HInstanceOf(HInstruction* object,
5140 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005141 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005142 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005143 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005144 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005145 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005146 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005147 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005148 SetRawInputAt(0, object);
5149 SetRawInputAt(1, constant);
5150 }
5151
5152 bool CanBeMoved() const OVERRIDE { return true; }
5153
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005154 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005155 return true;
5156 }
5157
5158 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005159 return false;
5160 }
5161
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005162 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5163
5164 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005165
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005166 // Used only in code generation.
5167 bool MustDoNullCheck() const { return must_do_null_check_; }
5168 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5169
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005170 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5171 return (check_kind == TypeCheckKind::kExactCheck)
5172 ? SideEffects::None()
5173 // Mips currently does runtime calls for any other checks.
5174 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005175 }
5176
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005177 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005178
5179 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005180 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005181 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005182
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005183 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5184};
5185
Calin Juravleb1498f62015-02-16 13:13:29 +00005186class HBoundType : public HExpression<1> {
5187 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005188 // Constructs an HBoundType with the given upper_bound.
5189 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005190 HBoundType(HInstruction* input,
5191 ReferenceTypeInfo upper_bound,
5192 bool upper_can_be_null,
5193 uint32_t dex_pc = kNoDexPc)
5194 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005195 upper_bound_(upper_bound),
5196 upper_can_be_null_(upper_can_be_null),
5197 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005198 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005199 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005200 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005201 }
5202
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005203 // GetUpper* should only be used in reference type propagation.
5204 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5205 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005206
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005207 void SetCanBeNull(bool can_be_null) {
5208 DCHECK(upper_can_be_null_ || !can_be_null);
5209 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005210 }
5211
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005212 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5213
Calin Juravleb1498f62015-02-16 13:13:29 +00005214 DECLARE_INSTRUCTION(BoundType);
5215
5216 private:
5217 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005218 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5219 // It is used to bound the type in cases like:
5220 // if (x instanceof ClassX) {
5221 // // uper_bound_ will be ClassX
5222 // }
5223 const ReferenceTypeInfo upper_bound_;
5224 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5225 // is false then can_be_null_ cannot be true).
5226 const bool upper_can_be_null_;
5227 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005228
5229 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5230};
5231
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005232class HCheckCast : public HTemplateInstruction<2> {
5233 public:
5234 HCheckCast(HInstruction* object,
5235 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005236 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005237 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005238 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005239 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005240 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005241 SetRawInputAt(0, object);
5242 SetRawInputAt(1, constant);
5243 }
5244
5245 bool CanBeMoved() const OVERRIDE { return true; }
5246
5247 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5248 return true;
5249 }
5250
5251 bool NeedsEnvironment() const OVERRIDE {
5252 // Instruction may throw a CheckCastError.
5253 return true;
5254 }
5255
5256 bool CanThrow() const OVERRIDE { return true; }
5257
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005258 bool MustDoNullCheck() const { return must_do_null_check_; }
5259 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005260 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005261
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005262 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005263
5264 DECLARE_INSTRUCTION(CheckCast);
5265
5266 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005267 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005268 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005269
5270 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005271};
5272
Calin Juravle27df7582015-04-17 19:12:31 +01005273class HMemoryBarrier : public HTemplateInstruction<0> {
5274 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005275 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005276 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005277 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005278 barrier_kind_(barrier_kind) {}
5279
5280 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5281
5282 DECLARE_INSTRUCTION(MemoryBarrier);
5283
5284 private:
5285 const MemBarrierKind barrier_kind_;
5286
5287 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5288};
5289
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005290class HMonitorOperation : public HTemplateInstruction<1> {
5291 public:
5292 enum OperationKind {
5293 kEnter,
5294 kExit,
5295 };
5296
5297 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005298 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005299 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5300 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005301 SetRawInputAt(0, object);
5302 }
5303
5304 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005305 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5306
5307 bool CanThrow() const OVERRIDE {
5308 // Verifier guarantees that monitor-exit cannot throw.
5309 // This is important because it allows the HGraphBuilder to remove
5310 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5311 return IsEnter();
5312 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005313
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005314
5315 bool IsEnter() const { return kind_ == kEnter; }
5316
5317 DECLARE_INSTRUCTION(MonitorOperation);
5318
Calin Juravle52c48962014-12-16 17:02:57 +00005319 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005320 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005321
5322 private:
5323 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5324};
5325
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005326/**
5327 * A HInstruction used as a marker for the replacement of new + <init>
5328 * of a String to a call to a StringFactory. Only baseline will see
5329 * the node at code generation, where it will be be treated as null.
5330 * When compiling non-baseline, `HFakeString` instructions are being removed
5331 * in the instruction simplifier.
5332 */
5333class HFakeString : public HTemplateInstruction<0> {
5334 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005335 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5336 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005337
5338 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5339
5340 DECLARE_INSTRUCTION(FakeString);
5341
5342 private:
5343 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5344};
5345
Vladimir Markof9f64412015-09-02 14:05:49 +01005346class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005347 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005348 MoveOperands(Location source,
5349 Location destination,
5350 Primitive::Type type,
5351 HInstruction* instruction)
5352 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005353
5354 Location GetSource() const { return source_; }
5355 Location GetDestination() const { return destination_; }
5356
5357 void SetSource(Location value) { source_ = value; }
5358 void SetDestination(Location value) { destination_ = value; }
5359
5360 // The parallel move resolver marks moves as "in-progress" by clearing the
5361 // destination (but not the source).
5362 Location MarkPending() {
5363 DCHECK(!IsPending());
5364 Location dest = destination_;
5365 destination_ = Location::NoLocation();
5366 return dest;
5367 }
5368
5369 void ClearPending(Location dest) {
5370 DCHECK(IsPending());
5371 destination_ = dest;
5372 }
5373
5374 bool IsPending() const {
5375 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5376 return destination_.IsInvalid() && !source_.IsInvalid();
5377 }
5378
5379 // True if this blocks a move from the given location.
5380 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005381 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005382 }
5383
5384 // A move is redundant if it's been eliminated, if its source and
5385 // destination are the same, or if its destination is unneeded.
5386 bool IsRedundant() const {
5387 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5388 }
5389
5390 // We clear both operands to indicate move that's been eliminated.
5391 void Eliminate() {
5392 source_ = destination_ = Location::NoLocation();
5393 }
5394
5395 bool IsEliminated() const {
5396 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5397 return source_.IsInvalid();
5398 }
5399
Alexey Frunze4dda3372015-06-01 18:31:49 -07005400 Primitive::Type GetType() const { return type_; }
5401
Nicolas Geoffray90218252015-04-15 11:56:51 +01005402 bool Is64BitMove() const {
5403 return Primitive::Is64BitType(type_);
5404 }
5405
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005406 HInstruction* GetInstruction() const { return instruction_; }
5407
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005408 private:
5409 Location source_;
5410 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005411 // The type this move is for.
5412 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005413 // The instruction this move is assocatied with. Null when this move is
5414 // for moving an input in the expected locations of user (including a phi user).
5415 // This is only used in debug mode, to ensure we do not connect interval siblings
5416 // in the same parallel move.
5417 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005418};
5419
5420static constexpr size_t kDefaultNumberOfMoves = 4;
5421
5422class HParallelMove : public HTemplateInstruction<0> {
5423 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005424 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005425 : HTemplateInstruction(SideEffects::None(), dex_pc),
5426 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5427 moves_.reserve(kDefaultNumberOfMoves);
5428 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005429
Nicolas Geoffray90218252015-04-15 11:56:51 +01005430 void AddMove(Location source,
5431 Location destination,
5432 Primitive::Type type,
5433 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005434 DCHECK(source.IsValid());
5435 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005436 if (kIsDebugBuild) {
5437 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005438 for (const MoveOperands& move : moves_) {
5439 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005440 // Special case the situation where the move is for the spill slot
5441 // of the instruction.
5442 if ((GetPrevious() == instruction)
5443 || ((GetPrevious() == nullptr)
5444 && instruction->IsPhi()
5445 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005446 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005447 << "Doing parallel moves for the same instruction.";
5448 } else {
5449 DCHECK(false) << "Doing parallel moves for the same instruction.";
5450 }
5451 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005452 }
5453 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005454 for (const MoveOperands& move : moves_) {
5455 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005456 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005457 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005458 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005459 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005460 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005461 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005462 }
5463
Vladimir Marko225b6462015-09-28 12:17:40 +01005464 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005465 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005466 }
5467
Vladimir Marko225b6462015-09-28 12:17:40 +01005468 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005469
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005470 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005471
5472 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005473 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005474
5475 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5476};
5477
Mark Mendell0616ae02015-04-17 12:49:27 -04005478} // namespace art
5479
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005480#ifdef ART_ENABLE_CODEGEN_arm64
5481#include "nodes_arm64.h"
5482#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005483#ifdef ART_ENABLE_CODEGEN_x86
5484#include "nodes_x86.h"
5485#endif
5486
5487namespace art {
5488
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005489class HGraphVisitor : public ValueObject {
5490 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005491 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5492 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005493
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005494 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005495 virtual void VisitBasicBlock(HBasicBlock* block);
5496
Roland Levillain633021e2014-10-01 14:12:25 +01005497 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005498 void VisitInsertionOrder();
5499
Roland Levillain633021e2014-10-01 14:12:25 +01005500 // Visit the graph following dominator tree reverse post-order.
5501 void VisitReversePostOrder();
5502
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005503 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005504
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005505 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005506#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005507 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5508
5509 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5510
5511#undef DECLARE_VISIT_INSTRUCTION
5512
5513 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005514 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005515
5516 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5517};
5518
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005519class HGraphDelegateVisitor : public HGraphVisitor {
5520 public:
5521 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5522 virtual ~HGraphDelegateVisitor() {}
5523
5524 // Visit functions that delegate to to super class.
5525#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005526 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005527
5528 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5529
5530#undef DECLARE_VISIT_INSTRUCTION
5531
5532 private:
5533 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5534};
5535
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005536class HInsertionOrderIterator : public ValueObject {
5537 public:
5538 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5539
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005540 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005541 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005542 void Advance() { ++index_; }
5543
5544 private:
5545 const HGraph& graph_;
5546 size_t index_;
5547
5548 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5549};
5550
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005551class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005552 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005553 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5554 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005555 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005556 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005557
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005558 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5559 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005560 void Advance() { ++index_; }
5561
5562 private:
5563 const HGraph& graph_;
5564 size_t index_;
5565
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005566 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005567};
5568
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005569class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005570 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005571 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005572 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005573 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005574 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005575 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005576
5577 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005578 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005579 void Advance() { --index_; }
5580
5581 private:
5582 const HGraph& graph_;
5583 size_t index_;
5584
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005585 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005586};
5587
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005588class HLinearPostOrderIterator : public ValueObject {
5589 public:
5590 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005591 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005592
5593 bool Done() const { return index_ == 0; }
5594
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005595 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005596
5597 void Advance() {
5598 --index_;
5599 DCHECK_GE(index_, 0U);
5600 }
5601
5602 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005603 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005604 size_t index_;
5605
5606 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5607};
5608
5609class HLinearOrderIterator : public ValueObject {
5610 public:
5611 explicit HLinearOrderIterator(const HGraph& graph)
5612 : order_(graph.GetLinearOrder()), index_(0) {}
5613
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005614 bool Done() const { return index_ == order_.size(); }
5615 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005616 void Advance() { ++index_; }
5617
5618 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005619 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005620 size_t index_;
5621
5622 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5623};
5624
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005625// Iterator over the blocks that art part of the loop. Includes blocks part
5626// of an inner loop. The order in which the blocks are iterated is on their
5627// block id.
5628class HBlocksInLoopIterator : public ValueObject {
5629 public:
5630 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5631 : blocks_in_loop_(info.GetBlocks()),
5632 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5633 index_(0) {
5634 if (!blocks_in_loop_.IsBitSet(index_)) {
5635 Advance();
5636 }
5637 }
5638
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005639 bool Done() const { return index_ == blocks_.size(); }
5640 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005641 void Advance() {
5642 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005643 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005644 if (blocks_in_loop_.IsBitSet(index_)) {
5645 break;
5646 }
5647 }
5648 }
5649
5650 private:
5651 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005652 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005653 size_t index_;
5654
5655 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5656};
5657
Mingyao Yang3584bce2015-05-19 16:01:59 -07005658// Iterator over the blocks that art part of the loop. Includes blocks part
5659// of an inner loop. The order in which the blocks are iterated is reverse
5660// post order.
5661class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5662 public:
5663 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5664 : blocks_in_loop_(info.GetBlocks()),
5665 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5666 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005667 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005668 Advance();
5669 }
5670 }
5671
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005672 bool Done() const { return index_ == blocks_.size(); }
5673 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005674 void Advance() {
5675 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005676 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5677 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005678 break;
5679 }
5680 }
5681 }
5682
5683 private:
5684 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005685 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005686 size_t index_;
5687
5688 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5689};
5690
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005691inline int64_t Int64FromConstant(HConstant* constant) {
5692 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5693 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5694 : constant->AsLongConstant()->GetValue();
5695}
5696
Vladimir Marko58155012015-08-19 12:49:41 +00005697inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5698 // For the purposes of the compiler, the dex files must actually be the same object
5699 // if we want to safely treat them as the same. This is especially important for JIT
5700 // as custom class loaders can open the same underlying file (or memory) multiple
5701 // times and provide different class resolution but no two class loaders should ever
5702 // use the same DexFile object - doing so is an unsupported hack that can lead to
5703 // all sorts of weird failures.
5704 return &lhs == &rhs;
5705}
5706
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005707} // namespace art
5708
5709#endif // ART_COMPILER_OPTIMIZING_NODES_H_