blob: cadb24c659da199765b2015fba24333c71a5c3bc [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
David Brazdil8d5b8b22015-03-24 10:51:52 +000024#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080025#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000026#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010027#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000028#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000029#include "handle.h"
30#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000031#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010032#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000033#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010035#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070036#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000037#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39namespace art {
40
David Brazdil1abb4192015-02-17 18:33:36 +000041class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010045class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010046class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070085 // All types.
86 kCondEQ, // ==
87 kCondNE, // !=
88 // Signed integers and floating-point numbers.
89 kCondLT, // <
90 kCondLE, // <=
91 kCondGT, // >
92 kCondGE, // >=
93 // Unsigned integers.
94 kCondB, // <
95 kCondBE, // <=
96 kCondA, // >
97 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070098};
99
Vladimir Markof9f64412015-09-02 14:05:49 +0100100class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100101 public:
102 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
103
104 void AddInstruction(HInstruction* instruction);
105 void RemoveInstruction(HInstruction* instruction);
106
David Brazdilc3d743f2015-04-22 13:40:50 +0100107 // Insert `instruction` before/after an existing instruction `cursor`.
108 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
109 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
110
Roland Levillain6b469232014-09-25 10:10:38 +0100111 // Return true if this list contains `instruction`.
112 bool Contains(HInstruction* instruction) const;
113
Roland Levillainccc07a92014-09-16 14:48:16 +0100114 // Return true if `instruction1` is found before `instruction2` in
115 // this instruction list and false otherwise. Abort if none
116 // of these instructions is found.
117 bool FoundBefore(const HInstruction* instruction1,
118 const HInstruction* instruction2) const;
119
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000120 bool IsEmpty() const { return first_instruction_ == nullptr; }
121 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
122
123 // Update the block of all instructions to be `block`.
124 void SetBlockOfInstructions(HBasicBlock* block) const;
125
126 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
127 void Add(const HInstructionList& instruction_list);
128
David Brazdil2d7352b2015-04-20 14:52:42 +0100129 // Return the number of instructions in the list. This is an expensive operation.
130 size_t CountSize() const;
131
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100132 private:
133 HInstruction* first_instruction_;
134 HInstruction* last_instruction_;
135
136 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000137 friend class HGraph;
138 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100140 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100141
142 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
143};
144
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000145// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100146class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100148 HGraph(ArenaAllocator* arena,
149 const DexFile& dex_file,
150 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100151 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700152 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100153 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100154 bool debuggable = false,
155 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000156 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100157 blocks_(arena->Adapter(kArenaAllocBlockList)),
158 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
159 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700160 entry_block_(nullptr),
161 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100162 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100163 number_of_vregs_(0),
164 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000165 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400166 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000167 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000168 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000169 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100170 dex_file_(dex_file),
171 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100172 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100173 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100174 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700175 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000176 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100177 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
178 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
179 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
180 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
181 cached_current_method_(nullptr) {
182 blocks_.reserve(kDefaultNumberOfBlocks);
183 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000184
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000185 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100186 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
187
David Brazdil69ba7b72015-06-23 18:27:30 +0100188 bool IsInSsaForm() const { return in_ssa_form_; }
189
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000190 HBasicBlock* GetEntryBlock() const { return entry_block_; }
191 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100192 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000193
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000194 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
195 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000196
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000197 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 // Try building the SSA form of this graph, with dominance computation and loop
200 // recognition. Returns whether it was successful in doing all these steps.
201 bool TryBuildingSsa() {
202 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000203 // The SSA builder requires loops to all be natural. Specifically, the dead phi
204 // elimination phase checks the consistency of the graph when doing a post-order
205 // visit for eliminating dead phis: a dead phi can only have loop header phi
206 // users remaining when being visited.
207 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100208 // Precompute per-block try membership before entering the SSA builder,
209 // which needs the information to build catch block phis from values of
210 // locals at throwing instructions inside try blocks.
211 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100213 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000214 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 }
216
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100217 void ComputeDominanceInformation();
218 void ClearDominanceInformation();
219
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000220 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100222 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100223 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000224
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000225 // Analyze all natural loops in this graph. Returns false if one
226 // loop is not natural, that is the header does not dominate the
227 // back edge.
228 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100229
David Brazdilffee3d32015-07-06 11:48:53 +0100230 // Iterate over blocks to compute try block membership. Needs reverse post
231 // order and loop information.
232 void ComputeTryBlockInformation();
233
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000234 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000235 // Returns the instruction used to replace the invoke expression or null if the
236 // invoke is for a void method.
237 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238
Mingyao Yang3584bce2015-05-19 16:01:59 -0700239 // Need to add a couple of blocks to test if the loop body is entered and
240 // put deoptimization instructions, etc.
241 void TransformLoopHeaderForBCE(HBasicBlock* header);
242
David Brazdil2d7352b2015-04-20 14:52:42 +0100243 // Removes `block` from the graph.
244 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000245
David Brazdilfc6a86a2015-06-26 10:33:45 +0000246 // Splits the edge between `block` and `successor` while preserving the
247 // indices in the predecessor/successor lists. If there are multiple edges
248 // between the blocks, the lowest indices are used.
249 // Returns the new block which is empty and has the same dex pc as `successor`.
250 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
251
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100252 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
253 void SimplifyLoop(HBasicBlock* header);
254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000255 int32_t GetNextInstructionId() {
256 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000257 return current_instruction_id_++;
258 }
259
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000260 int32_t GetCurrentInstructionId() const {
261 return current_instruction_id_;
262 }
263
264 void SetCurrentInstructionId(int32_t id) {
265 current_instruction_id_ = id;
266 }
267
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100268 uint16_t GetMaximumNumberOfOutVRegs() const {
269 return maximum_number_of_out_vregs_;
270 }
271
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000272 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
273 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100274 }
275
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100276 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
277 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
278 }
279
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000280 void UpdateTemporariesVRegSlots(size_t slots) {
281 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100282 }
283
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000284 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100285 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000286 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100287 }
288
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100289 void SetNumberOfVRegs(uint16_t number_of_vregs) {
290 number_of_vregs_ = number_of_vregs;
291 }
292
293 uint16_t GetNumberOfVRegs() const {
294 return number_of_vregs_;
295 }
296
297 void SetNumberOfInVRegs(uint16_t value) {
298 number_of_in_vregs_ = value;
299 }
300
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100301 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100302 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100303 return number_of_vregs_ - number_of_in_vregs_;
304 }
305
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100306 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100307 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100308 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100309
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100311 return linear_order_;
312 }
313
Mark Mendell1152c922015-04-24 17:06:35 -0400314 bool HasBoundsChecks() const {
315 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800316 }
317
Mark Mendell1152c922015-04-24 17:06:35 -0400318 void SetHasBoundsChecks(bool value) {
319 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800320 }
321
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100322 bool ShouldGenerateConstructorBarrier() const {
323 return should_generate_constructor_barrier_;
324 }
325
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000326 bool IsDebuggable() const { return debuggable_; }
327
David Brazdil8d5b8b22015-03-24 10:51:52 +0000328 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000329 // already, it is created and inserted into the graph. This method is only for
330 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600331 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000332
333 // TODO: This is problematic for the consistency of reference type propagation
334 // because it can be created anytime after the pass and thus it will be left
335 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600336 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000337
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600338 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
339 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000340 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600341 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
342 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000343 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600344 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
345 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000346 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600347 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
348 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000349 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000350
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100351 HCurrentMethod* GetCurrentMethod();
352
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000353 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100354
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100355 const DexFile& GetDexFile() const {
356 return dex_file_;
357 }
358
359 uint32_t GetMethodIdx() const {
360 return method_idx_;
361 }
362
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100363 InvokeType GetInvokeType() const {
364 return invoke_type_;
365 }
366
Mark Mendellc4701932015-04-10 13:18:51 -0400367 InstructionSet GetInstructionSet() const {
368 return instruction_set_;
369 }
370
David Brazdil77a48ae2015-09-15 12:34:04 +0000371 bool HasTryCatch() const { return has_try_catch_; }
372 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100373
David Brazdil2d7352b2015-04-20 14:52:42 +0100374 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100375 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000376 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100377 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000378
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000379 template <class InstructionType, typename ValueType>
380 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600381 ArenaSafeMap<ValueType, InstructionType*>* cache,
382 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000383 // Try to find an existing constant of the given value.
384 InstructionType* constant = nullptr;
385 auto cached_constant = cache->find(value);
386 if (cached_constant != cache->end()) {
387 constant = cached_constant->second;
388 }
389
390 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100391 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000392 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600393 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000394 cache->Overwrite(value, constant);
395 InsertConstant(constant);
396 }
397 return constant;
398 }
399
David Brazdil8d5b8b22015-03-24 10:51:52 +0000400 void InsertConstant(HConstant* instruction);
401
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000402 // Cache a float constant into the graph. This method should only be
403 // called by the SsaBuilder when creating "equivalent" instructions.
404 void CacheFloatConstant(HFloatConstant* constant);
405
406 // See CacheFloatConstant comment.
407 void CacheDoubleConstant(HDoubleConstant* constant);
408
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000409 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000410
411 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100412 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100414 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100415 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000416
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100417 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100418 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100419
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000420 HBasicBlock* entry_block_;
421 HBasicBlock* exit_block_;
422
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100423 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100424 uint16_t maximum_number_of_out_vregs_;
425
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100426 // The number of virtual registers in this method. Contains the parameters.
427 uint16_t number_of_vregs_;
428
429 // The number of virtual registers used by parameters of this method.
430 uint16_t number_of_in_vregs_;
431
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000432 // Number of vreg size slots that the temporaries use (used in baseline compiler).
433 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100434
Mark Mendell1152c922015-04-24 17:06:35 -0400435 // Has bounds checks. We can totally skip BCE if it's false.
436 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800437
David Brazdil77a48ae2015-09-15 12:34:04 +0000438 // Flag whether there are any try/catch blocks in the graph. We will skip
439 // try/catch-related passes if false.
440 bool has_try_catch_;
441
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000442 // Indicates whether the graph should be compiled in a way that
443 // ensures full debuggability. If false, we can apply more
444 // aggressive optimizations that may limit the level of debugging.
445 const bool debuggable_;
446
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000447 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000448 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000449
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100450 // The dex file from which the method is from.
451 const DexFile& dex_file_;
452
453 // The method index in the dex file.
454 const uint32_t method_idx_;
455
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100456 // If inlined, this encodes how the callee is being invoked.
457 const InvokeType invoke_type_;
458
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100459 // Whether the graph has been transformed to SSA form. Only used
460 // in debug mode to ensure we are not using properties only valid
461 // for non-SSA form (like the number of temporaries).
462 bool in_ssa_form_;
463
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100464 const bool should_generate_constructor_barrier_;
465
Mathieu Chartiere401d142015-04-22 13:56:20 -0700466 const InstructionSet instruction_set_;
467
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000468 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000469 HNullConstant* cached_null_constant_;
470 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000471 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000472 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000473 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000474
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100475 HCurrentMethod* cached_current_method_;
476
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000477 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100478 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000479 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000480 DISALLOW_COPY_AND_ASSIGN(HGraph);
481};
482
Vladimir Markof9f64412015-09-02 14:05:49 +0100483class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 public:
485 HLoopInformation(HBasicBlock* header, HGraph* graph)
486 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100487 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100488 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100489 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100490 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
491 back_edges_.reserve(kDefaultNumberOfBackEdges);
492 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493
494 HBasicBlock* GetHeader() const {
495 return header_;
496 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000497
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000498 void SetHeader(HBasicBlock* block) {
499 header_ = block;
500 }
501
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100502 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
503 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
504 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
505
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000506 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100507 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000508 }
509
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100510 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100511 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100512 }
513
David Brazdil46e2a392015-03-16 17:31:52 +0000514 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100515 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100516 }
517
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000518 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100519 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000520 }
521
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100523
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100524 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100525 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100526 }
527
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100528 // Returns the lifetime position of the back edge that has the
529 // greatest lifetime position.
530 size_t GetLifetimeEnd() const;
531
532 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100533 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100534 }
535
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100536 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100537 // that is the header dominates the back edge.
538 bool Populate();
539
David Brazdila4b8c212015-05-07 09:59:30 +0100540 // Reanalyzes the loop by removing loop info from its blocks and re-running
541 // Populate(). If there are no back edges left, the loop info is completely
542 // removed as well as its SuspendCheck instruction. It must be run on nested
543 // inner loops first.
544 void Update();
545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100546 // Returns whether this loop information contains `block`.
547 // Note that this loop information *must* be populated before entering this function.
548 bool Contains(const HBasicBlock& block) const;
549
550 // Returns whether this loop information is an inner loop of `other`.
551 // Note that `other` *must* be populated before entering this function.
552 bool IsIn(const HLoopInformation& other) const;
553
554 const ArenaBitVector& GetBlocks() const { return blocks_; }
555
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000556 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000557 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000558
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000559 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 // Internal recursive implementation of `Populate`.
561 void PopulateRecursive(HBasicBlock* block);
562
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100564 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100565 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100566 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567
568 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
569};
570
David Brazdilec16f792015-08-19 15:04:01 +0100571// Stores try/catch information for basic blocks.
572// Note that HGraph is constructed so that catch blocks cannot simultaneously
573// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100574class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100575 public:
576 // Try block information constructor.
577 explicit TryCatchInformation(const HTryBoundary& try_entry)
578 : try_entry_(&try_entry),
579 catch_dex_file_(nullptr),
580 catch_type_index_(DexFile::kDexNoIndex16) {
581 DCHECK(try_entry_ != nullptr);
582 }
583
584 // Catch block information constructor.
585 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
586 : try_entry_(nullptr),
587 catch_dex_file_(&dex_file),
588 catch_type_index_(catch_type_index) {}
589
590 bool IsTryBlock() const { return try_entry_ != nullptr; }
591
592 const HTryBoundary& GetTryEntry() const {
593 DCHECK(IsTryBlock());
594 return *try_entry_;
595 }
596
597 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
598
599 bool IsCatchAllTypeIndex() const {
600 DCHECK(IsCatchBlock());
601 return catch_type_index_ == DexFile::kDexNoIndex16;
602 }
603
604 uint16_t GetCatchTypeIndex() const {
605 DCHECK(IsCatchBlock());
606 return catch_type_index_;
607 }
608
609 const DexFile& GetCatchDexFile() const {
610 DCHECK(IsCatchBlock());
611 return *catch_dex_file_;
612 }
613
614 private:
615 // One of possibly several TryBoundary instructions entering the block's try.
616 // Only set for try blocks.
617 const HTryBoundary* try_entry_;
618
619 // Exception type information. Only set for catch blocks.
620 const DexFile* catch_dex_file_;
621 const uint16_t catch_type_index_;
622};
623
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100624static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100625static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100626
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000627// A block in a method. Contains the list of instructions represented
628// as a double linked list. Each block knows its predecessors and
629// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100630
Vladimir Markof9f64412015-09-02 14:05:49 +0100631class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600633 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000634 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000635 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
636 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000637 loop_information_(nullptr),
638 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000639 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100640 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100641 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100642 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000643 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000644 try_catch_information_(nullptr) {
645 predecessors_.reserve(kDefaultNumberOfPredecessors);
646 successors_.reserve(kDefaultNumberOfSuccessors);
647 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
648 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000649
Vladimir Marko60584552015-09-03 13:35:12 +0000650 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100651 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652 }
653
Vladimir Marko60584552015-09-03 13:35:12 +0000654 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100655 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656 }
657
Vladimir Marko60584552015-09-03 13:35:12 +0000658 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
659 return ContainsElement(successors_, block, start_from);
660 }
661
662 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100663 return dominated_blocks_;
664 }
665
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100666 bool IsEntryBlock() const {
667 return graph_->GetEntryBlock() == this;
668 }
669
670 bool IsExitBlock() const {
671 return graph_->GetExitBlock() == this;
672 }
673
David Brazdil46e2a392015-03-16 17:31:52 +0000674 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000675 bool IsSingleTryBoundary() const;
676
677 // Returns true if this block emits nothing but a jump.
678 bool IsSingleJump() const {
679 HLoopInformation* loop_info = GetLoopInformation();
680 return (IsSingleGoto() || IsSingleTryBoundary())
681 // Back edges generate a suspend check.
682 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
683 }
David Brazdil46e2a392015-03-16 17:31:52 +0000684
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000685 void AddBackEdge(HBasicBlock* back_edge) {
686 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000687 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000688 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100689 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000690 loop_information_->AddBackEdge(back_edge);
691 }
692
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000693 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000694 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000695
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100696 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000697 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600698 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000699
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000700 HBasicBlock* GetDominator() const { return dominator_; }
701 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000702 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
703
704 void RemoveDominatedBlock(HBasicBlock* block) {
705 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100706 }
Vladimir Marko60584552015-09-03 13:35:12 +0000707
708 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
709 ReplaceElement(dominated_blocks_, existing, new_block);
710 }
711
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100712 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000713
714 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100715 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000716 }
717
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100718 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
719 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100720 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100721 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100722 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
723 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000724
725 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000726 successors_.push_back(block);
727 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000728 }
729
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100730 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
731 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100732 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000733 new_block->predecessors_.push_back(this);
734 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000735 }
736
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000737 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
738 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000739 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000740 new_block->successors_.push_back(this);
741 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000742 }
743
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100744 // Insert `this` between `predecessor` and `successor. This method
745 // preserves the indicies, and will update the first edge found between
746 // `predecessor` and `successor`.
747 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
748 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100749 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000750 successor->predecessors_[predecessor_index] = this;
751 predecessor->successors_[successor_index] = this;
752 successors_.push_back(successor);
753 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100754 }
755
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100756 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000757 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100758 }
759
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000760 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000761 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000762 }
763
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000765 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100766 }
767
768 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000769 predecessors_.push_back(block);
770 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100771 }
772
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100773 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000774 DCHECK_EQ(predecessors_.size(), 2u);
775 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100776 }
777
David Brazdil769c9e52015-04-27 13:54:09 +0100778 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000779 DCHECK_EQ(successors_.size(), 2u);
780 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100781 }
782
David Brazdilfc6a86a2015-06-26 10:33:45 +0000783 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000784 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100785 }
786
David Brazdilfc6a86a2015-06-26 10:33:45 +0000787 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100789 }
790
David Brazdilfc6a86a2015-06-26 10:33:45 +0000791 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000792 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100793 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000794 }
795
796 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000797 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100798 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000799 }
800
801 // Returns whether the first occurrence of `predecessor` in the list of
802 // predecessors is at index `idx`.
803 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100804 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805 return GetPredecessorIndexOf(predecessor) == idx;
806 }
807
David Brazdilffee3d32015-07-06 11:48:53 +0100808 // Returns the number of non-exceptional successors. SsaChecker ensures that
809 // these are stored at the beginning of the successor list.
810 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000811 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100812 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813
David Brazdild7558da2015-09-22 13:04:14 +0100814 // Create a new block between this block and its predecessors. The new block
815 // is added to the graph, all predecessor edges are relinked to it and an edge
816 // is created to `this`. Returns the new empty block. Reverse post order or
817 // loop and try/catch information are not updated.
818 HBasicBlock* CreateImmediateDominator();
819
David Brazdilfc6a86a2015-06-26 10:33:45 +0000820 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100821 // created, latter block. Note that this method will add the block to the
822 // graph, create a Goto at the end of the former block and will create an edge
823 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100824 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000825 HBasicBlock* SplitBefore(HInstruction* cursor);
826
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000827 // Split the block into two blocks just after `cursor`. Returns the newly
828 // created block. Note that this method just updates raw block information,
829 // like predecessors, successors, dominators, and instruction list. It does not
830 // update the graph, reverse post order, loop information, nor make sure the
831 // blocks are consistent (for example ending with a control flow instruction).
832 HBasicBlock* SplitAfter(HInstruction* cursor);
833
834 // Merge `other` at the end of `this`. Successors and dominated blocks of
835 // `other` are changed to be successors and dominated blocks of `this`. Note
836 // that this method does not update the graph, reverse post order, loop
837 // information, nor make sure the blocks are consistent (for example ending
838 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100839 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000840
841 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
842 // of `this` are moved to `other`.
843 // Note that this method does not update the graph, reverse post order, loop
844 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000845 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846 void ReplaceWith(HBasicBlock* other);
847
David Brazdil2d7352b2015-04-20 14:52:42 +0100848 // Merge `other` at the end of `this`. This method updates loops, reverse post
849 // order, links to predecessors, successors, dominators and deletes the block
850 // from the graph. The two blocks must be successive, i.e. `this` the only
851 // predecessor of `other` and vice versa.
852 void MergeWith(HBasicBlock* other);
853
854 // Disconnects `this` from all its predecessors, successors and dominator,
855 // removes it from all loops it is included in and eventually from the graph.
856 // The block must not dominate any other block. Predecessors and successors
857 // are safely updated.
858 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000859
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000860 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100861 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100862 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100863 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100864 // Replace instruction `initial` with `replacement` within this block.
865 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
866 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100867 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100868 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000869 // RemoveInstruction and RemovePhi delete a given instruction from the respective
870 // instruction list. With 'ensure_safety' set to true, it verifies that the
871 // instruction is not in use and removes it from the use lists of its inputs.
872 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
873 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100874 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100875
876 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100877 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100878 }
879
Roland Levillain6b879dd2014-09-22 17:13:44 +0100880 bool IsLoopPreHeaderFirstPredecessor() const {
881 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100882 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100883 }
884
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100885 HLoopInformation* GetLoopInformation() const {
886 return loop_information_;
887 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000888
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000889 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100890 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000891 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100892 void SetInLoop(HLoopInformation* info) {
893 if (IsLoopHeader()) {
894 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100895 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100896 loop_information_ = info;
897 } else if (loop_information_->Contains(*info->GetHeader())) {
898 // Block is currently part of an outer loop. Make it part of this inner loop.
899 // Note that a non loop header having a loop information means this loop information
900 // has already been populated
901 loop_information_ = info;
902 } else {
903 // Block is part of an inner loop. Do not update the loop information.
904 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
905 // at this point, because this method is being called while populating `info`.
906 }
907 }
908
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000909 // Raw update of the loop information.
910 void SetLoopInformation(HLoopInformation* info) {
911 loop_information_ = info;
912 }
913
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100914 bool IsInLoop() const { return loop_information_ != nullptr; }
915
David Brazdilec16f792015-08-19 15:04:01 +0100916 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
917
918 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
919 try_catch_information_ = try_catch_information;
920 }
921
922 bool IsTryBlock() const {
923 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
924 }
925
926 bool IsCatchBlock() const {
927 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
928 }
David Brazdilffee3d32015-07-06 11:48:53 +0100929
930 // Returns the try entry that this block's successors should have. They will
931 // be in the same try, unless the block ends in a try boundary. In that case,
932 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100933 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100934
David Brazdild7558da2015-09-22 13:04:14 +0100935 bool HasThrowingInstructions() const;
936
David Brazdila4b8c212015-05-07 09:59:30 +0100937 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100938 bool Dominates(HBasicBlock* block) const;
939
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100940 size_t GetLifetimeStart() const { return lifetime_start_; }
941 size_t GetLifetimeEnd() const { return lifetime_end_; }
942
943 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
944 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
945
David Brazdil8d5b8b22015-03-24 10:51:52 +0000946 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000947 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100948 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000949 bool HasSinglePhi() const;
950
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000951 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000952 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000953 ArenaVector<HBasicBlock*> predecessors_;
954 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100955 HInstructionList instructions_;
956 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000957 HLoopInformation* loop_information_;
958 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000959 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100960 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100961 // The dex program counter of the first instruction of this block.
962 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100963 size_t lifetime_start_;
964 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100965 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100966
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000967 friend class HGraph;
968 friend class HInstruction;
969
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
971};
972
David Brazdilb2bd1c52015-03-25 11:17:37 +0000973// Iterates over the LoopInformation of all loops which contain 'block'
974// from the innermost to the outermost.
975class HLoopInformationOutwardIterator : public ValueObject {
976 public:
977 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
978 : current_(block.GetLoopInformation()) {}
979
980 bool Done() const { return current_ == nullptr; }
981
982 void Advance() {
983 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100984 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000985 }
986
987 HLoopInformation* Current() const {
988 DCHECK(!Done());
989 return current_;
990 }
991
992 private:
993 HLoopInformation* current_;
994
995 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
996};
997
Alexandre Ramesef20f712015-06-09 10:29:30 +0100998#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -0700999 M(Above, Condition) \
1000 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001001 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001002 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001003 M(ArrayGet, Instruction) \
1004 M(ArrayLength, Instruction) \
1005 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001006 M(Below, Condition) \
1007 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001008 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001009 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001010 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001011 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001012 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001013 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001014 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001015 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001016 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001017 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001018 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001019 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001020 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001021 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001022 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001023 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001024 M(FloatConstant, Constant) \
1025 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001026 M(GreaterThan, Condition) \
1027 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001028 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001029 M(InstanceFieldGet, Instruction) \
1030 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001031 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001033 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001034 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001035 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001036 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001037 M(LessThan, Condition) \
1038 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001039 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001040 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001041 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001042 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001043 M(Local, Instruction) \
1044 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001045 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001046 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001047 M(Mul, BinaryOperation) \
1048 M(Neg, UnaryOperation) \
1049 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001050 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001051 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001052 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001053 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001054 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001055 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001056 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001057 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001058 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001059 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001060 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001061 M(Return, Instruction) \
1062 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001063 M(Shl, BinaryOperation) \
1064 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001065 M(StaticFieldGet, Instruction) \
1066 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001067 M(UnresolvedInstanceFieldGet, Instruction) \
1068 M(UnresolvedInstanceFieldSet, Instruction) \
1069 M(UnresolvedStaticFieldGet, Instruction) \
1070 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001071 M(StoreLocal, Instruction) \
1072 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001073 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001074 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001075 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001076 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001077 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001078 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001079 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001080
Alexandre Ramesef20f712015-06-09 10:29:30 +01001081#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1082
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001083#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001084#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001085#else
1086#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1087 M(Arm64IntermediateAddress, Instruction)
1088#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001089
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001090#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1091
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001092#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1093
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001094#ifndef ART_ENABLE_CODEGEN_x86
1095#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1096#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001097#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1098 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001099 M(X86LoadFromConstantTable, Instruction) \
1100 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001101#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001102
1103#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1104
1105#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1106 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1107 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1108 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001109 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001110 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001111 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1112 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1113
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001114#define FOR_EACH_INSTRUCTION(M) \
1115 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1116 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001117 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001118 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001119 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001120
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001121#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001122FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1123#undef FORWARD_DECLARATION
1124
Roland Levillainccc07a92014-09-16 14:48:16 +01001125#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001126 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1127 const char* DebugName() const OVERRIDE { return #type; } \
1128 const H##type* As##type() const OVERRIDE { return this; } \
1129 H##type* As##type() OVERRIDE { return this; } \
1130 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001131 return other->Is##type(); \
1132 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001133 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001134
David Brazdiled596192015-01-23 10:39:45 +00001135template <typename T> class HUseList;
1136
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001137template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001138class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001139 public:
David Brazdiled596192015-01-23 10:39:45 +00001140 HUseListNode* GetPrevious() const { return prev_; }
1141 HUseListNode* GetNext() const { return next_; }
1142 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001143 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001144 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001145
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001146 private:
David Brazdiled596192015-01-23 10:39:45 +00001147 HUseListNode(T user, size_t index)
1148 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1149
1150 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001151 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001152 HUseListNode<T>* prev_;
1153 HUseListNode<T>* next_;
1154
1155 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001156
1157 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1158};
1159
David Brazdiled596192015-01-23 10:39:45 +00001160template <typename T>
1161class HUseList : public ValueObject {
1162 public:
1163 HUseList() : first_(nullptr) {}
1164
1165 void Clear() {
1166 first_ = nullptr;
1167 }
1168
1169 // Adds a new entry at the beginning of the use list and returns
1170 // the newly created node.
1171 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001172 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001173 if (IsEmpty()) {
1174 first_ = new_node;
1175 } else {
1176 first_->prev_ = new_node;
1177 new_node->next_ = first_;
1178 first_ = new_node;
1179 }
1180 return new_node;
1181 }
1182
1183 HUseListNode<T>* GetFirst() const {
1184 return first_;
1185 }
1186
1187 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001188 DCHECK(node != nullptr);
1189 DCHECK(Contains(node));
1190
David Brazdiled596192015-01-23 10:39:45 +00001191 if (node->prev_ != nullptr) {
1192 node->prev_->next_ = node->next_;
1193 }
1194 if (node->next_ != nullptr) {
1195 node->next_->prev_ = node->prev_;
1196 }
1197 if (node == first_) {
1198 first_ = node->next_;
1199 }
1200 }
1201
David Brazdil1abb4192015-02-17 18:33:36 +00001202 bool Contains(const HUseListNode<T>* node) const {
1203 if (node == nullptr) {
1204 return false;
1205 }
1206 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1207 if (current == node) {
1208 return true;
1209 }
1210 }
1211 return false;
1212 }
1213
David Brazdiled596192015-01-23 10:39:45 +00001214 bool IsEmpty() const {
1215 return first_ == nullptr;
1216 }
1217
1218 bool HasOnlyOneUse() const {
1219 return first_ != nullptr && first_->next_ == nullptr;
1220 }
1221
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001222 size_t SizeSlow() const {
1223 size_t count = 0;
1224 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1225 ++count;
1226 }
1227 return count;
1228 }
1229
David Brazdiled596192015-01-23 10:39:45 +00001230 private:
1231 HUseListNode<T>* first_;
1232};
1233
1234template<typename T>
1235class HUseIterator : public ValueObject {
1236 public:
1237 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1238
1239 bool Done() const { return current_ == nullptr; }
1240
1241 void Advance() {
1242 DCHECK(!Done());
1243 current_ = current_->GetNext();
1244 }
1245
1246 HUseListNode<T>* Current() const {
1247 DCHECK(!Done());
1248 return current_;
1249 }
1250
1251 private:
1252 HUseListNode<T>* current_;
1253
1254 friend class HValue;
1255};
1256
David Brazdil1abb4192015-02-17 18:33:36 +00001257// This class is used by HEnvironment and HInstruction classes to record the
1258// instructions they use and pointers to the corresponding HUseListNodes kept
1259// by the used instructions.
1260template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001261class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001262 public:
1263 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1264 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1265
1266 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1267 : instruction_(old_record.instruction_), use_node_(use_node) {
1268 DCHECK(instruction_ != nullptr);
1269 DCHECK(use_node_ != nullptr);
1270 DCHECK(old_record.use_node_ == nullptr);
1271 }
1272
1273 HInstruction* GetInstruction() const { return instruction_; }
1274 HUseListNode<T>* GetUseNode() const { return use_node_; }
1275
1276 private:
1277 // Instruction used by the user.
1278 HInstruction* instruction_;
1279
1280 // Corresponding entry in the use list kept by 'instruction_'.
1281 HUseListNode<T>* use_node_;
1282};
1283
Aart Bik854a02b2015-07-14 16:07:00 -07001284/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001285 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001286 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001287 * For write/read dependences on fields/arrays, the dependence analysis uses
1288 * type disambiguation (e.g. a float field write cannot modify the value of an
1289 * integer field read) and the access type (e.g. a reference array write cannot
1290 * modify the value of a reference field read [although it may modify the
1291 * reference fetch prior to reading the field, which is represented by its own
1292 * write/read dependence]). The analysis makes conservative points-to
1293 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1294 * the same, and any reference read depends on any reference read without
1295 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001296 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001297 * The internal representation uses 38-bit and is described in the table below.
1298 * The first line indicates the side effect, and for field/array accesses the
1299 * second line indicates the type of the access (in the order of the
1300 * Primitive::Type enum).
1301 * The two numbered lines below indicate the bit position in the bitfield (read
1302 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001303 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001304 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1305 * +-------------+---------+---------+--------------+---------+---------+
1306 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1307 * | 3 |333333322|222222221| 1 |111111110|000000000|
1308 * | 7 |654321098|765432109| 8 |765432109|876543210|
1309 *
1310 * Note that, to ease the implementation, 'changes' bits are least significant
1311 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001312 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001313class SideEffects : public ValueObject {
1314 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001315 SideEffects() : flags_(0) {}
1316
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001317 static SideEffects None() {
1318 return SideEffects(0);
1319 }
1320
1321 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001322 return SideEffects(kAllChangeBits | kAllDependOnBits);
1323 }
1324
1325 static SideEffects AllChanges() {
1326 return SideEffects(kAllChangeBits);
1327 }
1328
1329 static SideEffects AllDependencies() {
1330 return SideEffects(kAllDependOnBits);
1331 }
1332
1333 static SideEffects AllExceptGCDependency() {
1334 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1335 }
1336
1337 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001338 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001339 }
1340
Aart Bik34c3ba92015-07-20 14:08:59 -07001341 static SideEffects AllWrites() {
1342 return SideEffects(kAllWrites);
1343 }
1344
1345 static SideEffects AllReads() {
1346 return SideEffects(kAllReads);
1347 }
1348
1349 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1350 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001351 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001352 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001353 }
1354
Aart Bik854a02b2015-07-14 16:07:00 -07001355 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1356 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001357 }
1358
Aart Bik34c3ba92015-07-20 14:08:59 -07001359 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1360 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001361 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001362 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001363 }
1364
1365 static SideEffects ArrayReadOfType(Primitive::Type type) {
1366 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1367 }
1368
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001369 static SideEffects CanTriggerGC() {
1370 return SideEffects(1ULL << kCanTriggerGCBit);
1371 }
1372
1373 static SideEffects DependsOnGC() {
1374 return SideEffects(1ULL << kDependsOnGCBit);
1375 }
1376
Aart Bik854a02b2015-07-14 16:07:00 -07001377 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001378 SideEffects Union(SideEffects other) const {
1379 return SideEffects(flags_ | other.flags_);
1380 }
1381
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001382 SideEffects Exclusion(SideEffects other) const {
1383 return SideEffects(flags_ & ~other.flags_);
1384 }
1385
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001386 void Add(SideEffects other) {
1387 flags_ |= other.flags_;
1388 }
1389
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001390 bool Includes(SideEffects other) const {
1391 return (other.flags_ & flags_) == other.flags_;
1392 }
1393
1394 bool HasSideEffects() const {
1395 return (flags_ & kAllChangeBits);
1396 }
1397
1398 bool HasDependencies() const {
1399 return (flags_ & kAllDependOnBits);
1400 }
1401
1402 // Returns true if there are no side effects or dependencies.
1403 bool DoesNothing() const {
1404 return flags_ == 0;
1405 }
1406
Aart Bik854a02b2015-07-14 16:07:00 -07001407 // Returns true if something is written.
1408 bool DoesAnyWrite() const {
1409 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001410 }
1411
Aart Bik854a02b2015-07-14 16:07:00 -07001412 // Returns true if something is read.
1413 bool DoesAnyRead() const {
1414 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001415 }
1416
Aart Bik854a02b2015-07-14 16:07:00 -07001417 // Returns true if potentially everything is written and read
1418 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001419 bool DoesAllReadWrite() const {
1420 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1421 }
1422
Aart Bik854a02b2015-07-14 16:07:00 -07001423 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001424 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001425 }
1426
1427 // Returns true if this may read something written by other.
1428 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001429 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1430 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001431 }
1432
1433 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001434 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001435 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001436 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001437 for (int s = kLastBit; s >= 0; s--) {
1438 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1439 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1440 // This is a bit for the GC side effect.
1441 if (current_bit_is_set) {
1442 flags += "GC";
1443 }
Aart Bik854a02b2015-07-14 16:07:00 -07001444 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001445 } else {
1446 // This is a bit for the array/field analysis.
1447 // The underscore character stands for the 'can trigger GC' bit.
1448 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1449 if (current_bit_is_set) {
1450 flags += kDebug[s];
1451 }
1452 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1453 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1454 flags += "|";
1455 }
1456 }
Aart Bik854a02b2015-07-14 16:07:00 -07001457 }
1458 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001459 }
1460
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001461 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001462
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001463 private:
1464 static constexpr int kFieldArrayAnalysisBits = 9;
1465
1466 static constexpr int kFieldWriteOffset = 0;
1467 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1468 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1469 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1470
1471 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1472
1473 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1474 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1475 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1476 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1477
1478 static constexpr int kLastBit = kDependsOnGCBit;
1479 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1480
1481 // Aliases.
1482
1483 static_assert(kChangeBits == kDependOnBits,
1484 "the 'change' bits should match the 'depend on' bits.");
1485
1486 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1487 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1488 static constexpr uint64_t kAllWrites =
1489 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1490 static constexpr uint64_t kAllReads =
1491 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001492
Aart Bik854a02b2015-07-14 16:07:00 -07001493 // Work around the fact that HIR aliases I/F and J/D.
1494 // TODO: remove this interceptor once HIR types are clean
1495 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1496 switch (type) {
1497 case Primitive::kPrimInt:
1498 case Primitive::kPrimFloat:
1499 return TypeFlag(Primitive::kPrimInt, offset) |
1500 TypeFlag(Primitive::kPrimFloat, offset);
1501 case Primitive::kPrimLong:
1502 case Primitive::kPrimDouble:
1503 return TypeFlag(Primitive::kPrimLong, offset) |
1504 TypeFlag(Primitive::kPrimDouble, offset);
1505 default:
1506 return TypeFlag(type, offset);
1507 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001508 }
1509
Aart Bik854a02b2015-07-14 16:07:00 -07001510 // Translates type to bit flag.
1511 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1512 CHECK_NE(type, Primitive::kPrimVoid);
1513 const uint64_t one = 1;
1514 const int shift = type; // 0-based consecutive enum
1515 DCHECK_LE(kFieldWriteOffset, shift);
1516 DCHECK_LT(shift, kArrayWriteOffset);
1517 return one << (type + offset);
1518 }
1519
1520 // Private constructor on direct flags value.
1521 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1522
1523 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001524};
1525
David Brazdiled596192015-01-23 10:39:45 +00001526// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001527class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001528 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001529 HEnvironment(ArenaAllocator* arena,
1530 size_t number_of_vregs,
1531 const DexFile& dex_file,
1532 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001533 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001534 InvokeType invoke_type,
1535 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001536 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1537 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001538 parent_(nullptr),
1539 dex_file_(dex_file),
1540 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001541 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001542 invoke_type_(invoke_type),
1543 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001544 }
1545
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001546 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001547 : HEnvironment(arena,
1548 to_copy.Size(),
1549 to_copy.GetDexFile(),
1550 to_copy.GetMethodIdx(),
1551 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001552 to_copy.GetInvokeType(),
1553 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001554
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001555 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001556 if (parent_ != nullptr) {
1557 parent_->SetAndCopyParentChain(allocator, parent);
1558 } else {
1559 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1560 parent_->CopyFrom(parent);
1561 if (parent->GetParent() != nullptr) {
1562 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1563 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001564 }
David Brazdiled596192015-01-23 10:39:45 +00001565 }
1566
Vladimir Marko71bf8092015-09-15 15:33:14 +01001567 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001568 void CopyFrom(HEnvironment* environment);
1569
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001570 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1571 // input to the loop phi instead. This is for inserting instructions that
1572 // require an environment (like HDeoptimization) in the loop pre-header.
1573 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001574
1575 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001576 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001577 }
1578
1579 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001580 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001581 }
1582
David Brazdil1abb4192015-02-17 18:33:36 +00001583 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001584
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001585 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001586
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001587 HEnvironment* GetParent() const { return parent_; }
1588
1589 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001590 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001591 }
1592
1593 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001594 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001595 }
1596
1597 uint32_t GetDexPc() const {
1598 return dex_pc_;
1599 }
1600
1601 uint32_t GetMethodIdx() const {
1602 return method_idx_;
1603 }
1604
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001605 InvokeType GetInvokeType() const {
1606 return invoke_type_;
1607 }
1608
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001609 const DexFile& GetDexFile() const {
1610 return dex_file_;
1611 }
1612
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001613 HInstruction* GetHolder() const {
1614 return holder_;
1615 }
1616
David Brazdiled596192015-01-23 10:39:45 +00001617 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001618 // Record instructions' use entries of this environment for constant-time removal.
1619 // It should only be called by HInstruction when a new environment use is added.
1620 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1621 DCHECK(env_use->GetUser() == this);
1622 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001623 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001624 }
David Brazdiled596192015-01-23 10:39:45 +00001625
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001626 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1627 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001628 HEnvironment* parent_;
1629 const DexFile& dex_file_;
1630 const uint32_t method_idx_;
1631 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001632 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001633
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001634 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001635 HInstruction* const holder_;
1636
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001637 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001638
1639 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1640};
1641
Calin Juravleacf735c2015-02-12 15:25:22 +00001642class ReferenceTypeInfo : ValueObject {
1643 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001644 typedef Handle<mirror::Class> TypeHandle;
1645
Calin Juravle2e768302015-07-28 14:41:11 +00001646 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1647 // The constructor will check that the type_handle is valid.
1648 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001649 }
1650
Calin Juravle2e768302015-07-28 14:41:11 +00001651 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1652
1653 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1654 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001655 }
1656
Calin Juravle2e768302015-07-28 14:41:11 +00001657 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1658 return IsValidHandle(type_handle_);
1659 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001660
Calin Juravleacf735c2015-02-12 15:25:22 +00001661 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001662
1663 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1664 DCHECK(IsValid());
1665 return GetTypeHandle()->IsObjectClass();
1666 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001667
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001668 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001669 DCHECK(IsValid());
1670 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001671 }
1672
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001673 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1674 DCHECK(IsValid());
1675 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1676 }
1677
Mathieu Chartier90443472015-07-16 20:32:27 -07001678 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001679 DCHECK(IsValid());
1680 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001681 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001682
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001683 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001684 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001685 return GetTypeHandle()->IsArrayClass();
1686 }
1687
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001688 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1689 DCHECK(IsValid());
1690 return GetTypeHandle()->IsPrimitiveArray();
1691 }
1692
1693 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1694 DCHECK(IsValid());
1695 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1696 }
1697
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001698 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001699 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001700 if (!IsExact()) return false;
1701 if (!IsArrayClass()) return false;
1702 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1703 }
1704
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001705 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1706 DCHECK(IsValid());
1707 if (!IsExact()) return false;
1708 if (!IsArrayClass()) return false;
1709 if (!rti.IsArrayClass()) return false;
1710 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1711 rti.GetTypeHandle()->GetComponentType());
1712 }
1713
Calin Juravleacf735c2015-02-12 15:25:22 +00001714 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1715
Mathieu Chartier90443472015-07-16 20:32:27 -07001716 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001717 DCHECK(IsValid());
1718 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001719 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1720 }
1721
1722 // Returns true if the type information provide the same amount of details.
1723 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001724 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001725 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001726 if (!IsValid() && !rti.IsValid()) {
1727 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001728 return true;
1729 }
Calin Juravle2e768302015-07-28 14:41:11 +00001730 if (!IsValid() || !rti.IsValid()) {
1731 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001732 return false;
1733 }
Calin Juravle2e768302015-07-28 14:41:11 +00001734 return IsExact() == rti.IsExact()
1735 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001736 }
1737
1738 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001739 ReferenceTypeInfo();
1740 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001741
Calin Juravleacf735c2015-02-12 15:25:22 +00001742 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001743 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001744 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001745 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001746 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001747};
1748
1749std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1750
Vladimir Markof9f64412015-09-02 14:05:49 +01001751class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001752 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001753 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001754 : previous_(nullptr),
1755 next_(nullptr),
1756 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001757 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001758 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001759 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001760 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001761 locations_(nullptr),
1762 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001763 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001764 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001765 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001766
Dave Allison20dfc792014-06-16 20:44:29 -07001767 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001768
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001769#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001770 enum InstructionKind {
1771 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1772 };
1773#undef DECLARE_KIND
1774
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001775 HInstruction* GetNext() const { return next_; }
1776 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001777
Calin Juravle77520bc2015-01-12 18:45:46 +00001778 HInstruction* GetNextDisregardingMoves() const;
1779 HInstruction* GetPreviousDisregardingMoves() const;
1780
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001781 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001782 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001783 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001784 bool IsInBlock() const { return block_ != nullptr; }
1785 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001786 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001787
Roland Levillain6b879dd2014-09-22 17:13:44 +01001788 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001789 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001790
1791 virtual void Accept(HGraphVisitor* visitor) = 0;
1792 virtual const char* DebugName() const = 0;
1793
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001794 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001795 void SetRawInputAt(size_t index, HInstruction* input) {
1796 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1797 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001798
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001799 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001800
1801 uint32_t GetDexPc() const { return dex_pc_; }
1802
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001803 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001804
Roland Levillaine161a2a2014-10-03 12:45:18 +01001805 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001806 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001807
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001808 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001809 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001810
Calin Juravle10e244f2015-01-26 18:54:32 +00001811 // Does not apply for all instructions, but having this at top level greatly
1812 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001813 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001814 virtual bool CanBeNull() const {
1815 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1816 return true;
1817 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001818
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001819 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001820 return false;
1821 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001822
Calin Juravle2e768302015-07-28 14:41:11 +00001823 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001824
Calin Juravle61d544b2015-02-23 16:46:57 +00001825 ReferenceTypeInfo GetReferenceTypeInfo() const {
1826 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1827 return reference_type_info_;
1828 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001829
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001830 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001831 DCHECK(user != nullptr);
1832 HUseListNode<HInstruction*>* use =
1833 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1834 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001835 }
1836
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001837 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001838 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001839 HUseListNode<HEnvironment*>* env_use =
1840 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1841 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001842 }
1843
David Brazdil1abb4192015-02-17 18:33:36 +00001844 void RemoveAsUserOfInput(size_t input) {
1845 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1846 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1847 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001848
David Brazdil1abb4192015-02-17 18:33:36 +00001849 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1850 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001851
David Brazdiled596192015-01-23 10:39:45 +00001852 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1853 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001854 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001855 bool HasOnlyOneNonEnvironmentUse() const {
1856 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1857 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858
Roland Levillain6c82d402014-10-13 16:10:27 +01001859 // Does this instruction strictly dominate `other_instruction`?
1860 // Returns false if this instruction and `other_instruction` are the same.
1861 // Aborts if this instruction and `other_instruction` are both phis.
1862 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001863
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001864 int GetId() const { return id_; }
1865 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001866
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001867 int GetSsaIndex() const { return ssa_index_; }
1868 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1869 bool HasSsaIndex() const { return ssa_index_ != -1; }
1870
1871 bool HasEnvironment() const { return environment_ != nullptr; }
1872 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001873 // Set the `environment_` field. Raw because this method does not
1874 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001875 void SetRawEnvironment(HEnvironment* environment) {
1876 DCHECK(environment_ == nullptr);
1877 DCHECK_EQ(environment->GetHolder(), this);
1878 environment_ = environment;
1879 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001880
1881 // Set the environment of this instruction, copying it from `environment`. While
1882 // copying, the uses lists are being updated.
1883 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001884 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001885 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001886 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001887 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001888 if (environment->GetParent() != nullptr) {
1889 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1890 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001891 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001892
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001893 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1894 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001895 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001896 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001897 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001898 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001899 if (environment->GetParent() != nullptr) {
1900 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1901 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001902 }
1903
Nicolas Geoffray39468442014-09-02 15:17:15 +01001904 // Returns the number of entries in the environment. Typically, that is the
1905 // number of dex registers in a method. It could be more in case of inlining.
1906 size_t EnvironmentSize() const;
1907
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001908 LocationSummary* GetLocations() const { return locations_; }
1909 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001910
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001912 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913
Alexandre Rames188d4312015-04-09 18:30:21 +01001914 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1915 // uses of this instruction by `other` are *not* updated.
1916 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1917 ReplaceWith(other);
1918 other->ReplaceInput(this, use_index);
1919 }
1920
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001921 // Move `this` instruction before `cursor`.
1922 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001923
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001924#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001925 bool Is##type() const { return (As##type() != nullptr); } \
1926 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001927 virtual H##type* As##type() { return nullptr; }
1928
1929 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1930#undef INSTRUCTION_TYPE_CHECK
1931
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001932 // Returns whether the instruction can be moved within the graph.
1933 virtual bool CanBeMoved() const { return false; }
1934
1935 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001936 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001937 return false;
1938 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001939
1940 // Returns whether any data encoded in the two instructions is equal.
1941 // This method does not look at the inputs. Both instructions must be
1942 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001943 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001944 return false;
1945 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001946
1947 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001948 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001949 // 2) Their inputs are identical.
1950 bool Equals(HInstruction* other) const;
1951
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001952 virtual InstructionKind GetKind() const = 0;
1953
1954 virtual size_t ComputeHashCode() const {
1955 size_t result = GetKind();
1956 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1957 result = (result * 31) + InputAt(i)->GetId();
1958 }
1959 return result;
1960 }
1961
1962 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001963 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001964
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001965 size_t GetLifetimePosition() const { return lifetime_position_; }
1966 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1967 LiveInterval* GetLiveInterval() const { return live_interval_; }
1968 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1969 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1970
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001971 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1972
1973 // Returns whether the code generation of the instruction will require to have access
1974 // to the current method. Such instructions are:
1975 // (1): Instructions that require an environment, as calling the runtime requires
1976 // to walk the stack and have the current method stored at a specific stack address.
1977 // (2): Object literals like classes and strings, that are loaded from the dex cache
1978 // fields of the current method.
1979 bool NeedsCurrentMethod() const {
1980 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1981 }
1982
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001983 virtual bool NeedsDexCache() const { return false; }
1984
Mark Mendellc4701932015-04-10 13:18:51 -04001985 // Does this instruction have any use in an environment before
1986 // control flow hits 'other'?
1987 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1988
1989 // Remove all references to environment uses of this instruction.
1990 // The caller must ensure that this is safe to do.
1991 void RemoveEnvironmentUsers();
1992
David Brazdil1abb4192015-02-17 18:33:36 +00001993 protected:
1994 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1995 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1996
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001997 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001998 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1999
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002000 HInstruction* previous_;
2001 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002002 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002003 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002004
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002005 // An instruction gets an id when it is added to the graph.
2006 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002007 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002008 int id_;
2009
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002010 // When doing liveness analysis, instructions that have uses get an SSA index.
2011 int ssa_index_;
2012
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002013 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002014 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002015
2016 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002017 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002018
Nicolas Geoffray39468442014-09-02 15:17:15 +01002019 // The environment associated with this instruction. Not null if the instruction
2020 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002021 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002022
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002023 // Set by the code generator.
2024 LocationSummary* locations_;
2025
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002026 // Set by the liveness analysis.
2027 LiveInterval* live_interval_;
2028
2029 // Set by the liveness analysis, this is the position in a linear
2030 // order of blocks where this instruction's live interval start.
2031 size_t lifetime_position_;
2032
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002033 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002034
Calin Juravleacf735c2015-02-12 15:25:22 +00002035 // TODO: for primitive types this should be marked as invalid.
2036 ReferenceTypeInfo reference_type_info_;
2037
David Brazdil1abb4192015-02-17 18:33:36 +00002038 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002039 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002040 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002041 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002042 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002043
2044 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2045};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002046std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002047
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002048class HInputIterator : public ValueObject {
2049 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002050 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002051
2052 bool Done() const { return index_ == instruction_->InputCount(); }
2053 HInstruction* Current() const { return instruction_->InputAt(index_); }
2054 void Advance() { index_++; }
2055
2056 private:
2057 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002058 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002059
2060 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2061};
2062
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002063class HInstructionIterator : public ValueObject {
2064 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002065 explicit HInstructionIterator(const HInstructionList& instructions)
2066 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002067 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002068 }
2069
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002070 bool Done() const { return instruction_ == nullptr; }
2071 HInstruction* Current() const { return instruction_; }
2072 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002073 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002074 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002075 }
2076
2077 private:
2078 HInstruction* instruction_;
2079 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002080
2081 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002082};
2083
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002084class HBackwardInstructionIterator : public ValueObject {
2085 public:
2086 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2087 : instruction_(instructions.last_instruction_) {
2088 next_ = Done() ? nullptr : instruction_->GetPrevious();
2089 }
2090
2091 bool Done() const { return instruction_ == nullptr; }
2092 HInstruction* Current() const { return instruction_; }
2093 void Advance() {
2094 instruction_ = next_;
2095 next_ = Done() ? nullptr : instruction_->GetPrevious();
2096 }
2097
2098 private:
2099 HInstruction* instruction_;
2100 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002101
2102 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002103};
2104
Vladimir Markof9f64412015-09-02 14:05:49 +01002105template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002106class HTemplateInstruction: public HInstruction {
2107 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002108 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002109 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002110 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002111
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002112 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002114 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002115 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2116 DCHECK_LT(i, N);
2117 return inputs_[i];
2118 }
David Brazdil1abb4192015-02-17 18:33:36 +00002119
2120 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002121 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002122 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002123 }
2124
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002125 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002126 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002127
2128 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002129};
2130
Vladimir Markof9f64412015-09-02 14:05:49 +01002131// HTemplateInstruction specialization for N=0.
2132template<>
2133class HTemplateInstruction<0>: public HInstruction {
2134 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002135 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002136 : HInstruction(side_effects, dex_pc) {}
2137
Vladimir Markof9f64412015-09-02 14:05:49 +01002138 virtual ~HTemplateInstruction() {}
2139
2140 size_t InputCount() const OVERRIDE { return 0; }
2141
2142 protected:
2143 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2144 LOG(FATAL) << "Unreachable";
2145 UNREACHABLE();
2146 }
2147
2148 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2149 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2150 LOG(FATAL) << "Unreachable";
2151 UNREACHABLE();
2152 }
2153
2154 private:
2155 friend class SsaBuilder;
2156};
2157
Dave Allison20dfc792014-06-16 20:44:29 -07002158template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002159class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002160 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002161 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002162 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002163 virtual ~HExpression() {}
2164
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002165 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002166
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002167 protected:
2168 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002169};
2170
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002171// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2172// instruction that branches to the exit block.
2173class HReturnVoid : public HTemplateInstruction<0> {
2174 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002175 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2176 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002177
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002178 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002179
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002180 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181
2182 private:
2183 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2184};
2185
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002186// Represents dex's RETURN opcodes. A HReturn is a control flow
2187// instruction that branches to the exit block.
2188class HReturn : public HTemplateInstruction<1> {
2189 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002190 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2191 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002192 SetRawInputAt(0, value);
2193 }
2194
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002195 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002196
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002197 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002198
2199 private:
2200 DISALLOW_COPY_AND_ASSIGN(HReturn);
2201};
2202
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002203// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002204// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002205// exit block.
2206class HExit : public HTemplateInstruction<0> {
2207 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002208 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002209
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002210 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002211
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002212 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213
2214 private:
2215 DISALLOW_COPY_AND_ASSIGN(HExit);
2216};
2217
2218// Jumps from one block to another.
2219class HGoto : public HTemplateInstruction<0> {
2220 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002221 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002222
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002223 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002224
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002225 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002226 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002227 }
2228
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002229 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002230
2231 private:
2232 DISALLOW_COPY_AND_ASSIGN(HGoto);
2233};
2234
Roland Levillain9867bc72015-08-05 10:21:34 +01002235class HConstant : public HExpression<0> {
2236 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002237 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2238 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002239
2240 bool CanBeMoved() const OVERRIDE { return true; }
2241
2242 virtual bool IsMinusOne() const { return false; }
2243 virtual bool IsZero() const { return false; }
2244 virtual bool IsOne() const { return false; }
2245
David Brazdil77a48ae2015-09-15 12:34:04 +00002246 virtual uint64_t GetValueAsUint64() const = 0;
2247
Roland Levillain9867bc72015-08-05 10:21:34 +01002248 DECLARE_INSTRUCTION(Constant);
2249
2250 private:
2251 DISALLOW_COPY_AND_ASSIGN(HConstant);
2252};
2253
2254class HNullConstant : public HConstant {
2255 public:
2256 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2257 return true;
2258 }
2259
David Brazdil77a48ae2015-09-15 12:34:04 +00002260 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2261
Roland Levillain9867bc72015-08-05 10:21:34 +01002262 size_t ComputeHashCode() const OVERRIDE { return 0; }
2263
2264 DECLARE_INSTRUCTION(NullConstant);
2265
2266 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002267 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002268
2269 friend class HGraph;
2270 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2271};
2272
2273// Constants of the type int. Those can be from Dex instructions, or
2274// synthesized (for example with the if-eqz instruction).
2275class HIntConstant : public HConstant {
2276 public:
2277 int32_t GetValue() const { return value_; }
2278
David Brazdil9f389d42015-10-01 14:32:56 +01002279 uint64_t GetValueAsUint64() const OVERRIDE {
2280 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2281 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002282
Roland Levillain9867bc72015-08-05 10:21:34 +01002283 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2284 DCHECK(other->IsIntConstant());
2285 return other->AsIntConstant()->value_ == value_;
2286 }
2287
2288 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2289
2290 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2291 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2292 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2293
2294 DECLARE_INSTRUCTION(IntConstant);
2295
2296 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002297 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2298 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2299 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2300 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002301
2302 const int32_t value_;
2303
2304 friend class HGraph;
2305 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2306 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2307 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2308};
2309
2310class HLongConstant : public HConstant {
2311 public:
2312 int64_t GetValue() const { return value_; }
2313
David Brazdil77a48ae2015-09-15 12:34:04 +00002314 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2315
Roland Levillain9867bc72015-08-05 10:21:34 +01002316 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2317 DCHECK(other->IsLongConstant());
2318 return other->AsLongConstant()->value_ == value_;
2319 }
2320
2321 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2322
2323 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2324 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2325 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2326
2327 DECLARE_INSTRUCTION(LongConstant);
2328
2329 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002330 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2331 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002332
2333 const int64_t value_;
2334
2335 friend class HGraph;
2336 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2337};
Dave Allison20dfc792014-06-16 20:44:29 -07002338
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002339// Conditional branch. A block ending with an HIf instruction must have
2340// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002341class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002342 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002343 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2344 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002345 SetRawInputAt(0, input);
2346 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002347
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002348 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002349
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002350 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002351 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002352 }
2353
2354 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002355 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002356 }
2357
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002358 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002359
2360 private:
2361 DISALLOW_COPY_AND_ASSIGN(HIf);
2362};
2363
David Brazdilfc6a86a2015-06-26 10:33:45 +00002364
2365// Abstract instruction which marks the beginning and/or end of a try block and
2366// links it to the respective exception handlers. Behaves the same as a Goto in
2367// non-exceptional control flow.
2368// Normal-flow successor is stored at index zero, exception handlers under
2369// higher indices in no particular order.
2370class HTryBoundary : public HTemplateInstruction<0> {
2371 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002372 enum BoundaryKind {
2373 kEntry,
2374 kExit,
2375 };
2376
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002377 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2378 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002379
2380 bool IsControlFlow() const OVERRIDE { return true; }
2381
2382 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002383 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002384
2385 // Returns whether `handler` is among its exception handlers (non-zero index
2386 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002387 bool HasExceptionHandler(const HBasicBlock& handler) const {
2388 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002389 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002390 }
2391
2392 // If not present already, adds `handler` to its block's list of exception
2393 // handlers.
2394 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002395 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002396 GetBlock()->AddSuccessor(handler);
2397 }
2398 }
2399
David Brazdil56e1acc2015-06-30 15:41:36 +01002400 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002401
David Brazdilffee3d32015-07-06 11:48:53 +01002402 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2403
David Brazdilfc6a86a2015-06-26 10:33:45 +00002404 DECLARE_INSTRUCTION(TryBoundary);
2405
2406 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002407 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002408
2409 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2410};
2411
David Brazdilffee3d32015-07-06 11:48:53 +01002412// Iterator over exception handlers of a given HTryBoundary, i.e. over
2413// exceptional successors of its basic block.
2414class HExceptionHandlerIterator : public ValueObject {
2415 public:
2416 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2417 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2418
Vladimir Marko60584552015-09-03 13:35:12 +00002419 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002420 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002421 size_t CurrentSuccessorIndex() const { return index_; }
2422 void Advance() { ++index_; }
2423
2424 private:
2425 const HBasicBlock& block_;
2426 size_t index_;
2427
2428 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2429};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002430
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002431// Deoptimize to interpreter, upon checking a condition.
2432class HDeoptimize : public HTemplateInstruction<1> {
2433 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002434 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2435 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002436 SetRawInputAt(0, cond);
2437 }
2438
2439 bool NeedsEnvironment() const OVERRIDE { return true; }
2440 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002441
2442 DECLARE_INSTRUCTION(Deoptimize);
2443
2444 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002445 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2446};
2447
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002448// Represents the ArtMethod that was passed as a first argument to
2449// the method. It is used by instructions that depend on it, like
2450// instructions that work with the dex cache.
2451class HCurrentMethod : public HExpression<0> {
2452 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002453 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2454 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002455
2456 DECLARE_INSTRUCTION(CurrentMethod);
2457
2458 private:
2459 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2460};
2461
Mark Mendellfe57faa2015-09-18 09:26:15 -04002462// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2463// have one successor for each entry in the switch table, and the final successor
2464// will be the block containing the next Dex opcode.
2465class HPackedSwitch : public HTemplateInstruction<1> {
2466 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002467 HPackedSwitch(int32_t start_value,
2468 uint32_t num_entries,
2469 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002470 uint32_t dex_pc = kNoDexPc)
2471 : HTemplateInstruction(SideEffects::None(), dex_pc),
2472 start_value_(start_value),
2473 num_entries_(num_entries) {
2474 SetRawInputAt(0, input);
2475 }
2476
2477 bool IsControlFlow() const OVERRIDE { return true; }
2478
2479 int32_t GetStartValue() const { return start_value_; }
2480
Vladimir Marko211c2112015-09-24 16:52:33 +01002481 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002482
2483 HBasicBlock* GetDefaultBlock() const {
2484 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002485 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002486 }
2487 DECLARE_INSTRUCTION(PackedSwitch);
2488
2489 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002490 const int32_t start_value_;
2491 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002492
2493 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2494};
2495
Roland Levillain88cb1752014-10-20 16:36:47 +01002496class HUnaryOperation : public HExpression<1> {
2497 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002498 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2499 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002500 SetRawInputAt(0, input);
2501 }
2502
2503 HInstruction* GetInput() const { return InputAt(0); }
2504 Primitive::Type GetResultType() const { return GetType(); }
2505
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002506 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002507 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002508 return true;
2509 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002510
Roland Levillain9240d6a2014-10-20 16:47:04 +01002511 // Try to statically evaluate `operation` and return a HConstant
2512 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002513 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002514 HConstant* TryStaticEvaluation() const;
2515
2516 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002517 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2518 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002519
Roland Levillain88cb1752014-10-20 16:36:47 +01002520 DECLARE_INSTRUCTION(UnaryOperation);
2521
2522 private:
2523 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2524};
2525
Dave Allison20dfc792014-06-16 20:44:29 -07002526class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002527 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002528 HBinaryOperation(Primitive::Type result_type,
2529 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002530 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002531 SideEffects side_effects = SideEffects::None(),
2532 uint32_t dex_pc = kNoDexPc)
2533 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002534 SetRawInputAt(0, left);
2535 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002536 }
2537
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002538 HInstruction* GetLeft() const { return InputAt(0); }
2539 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002540 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002541
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002542 virtual bool IsCommutative() const { return false; }
2543
2544 // Put constant on the right.
2545 // Returns whether order is changed.
2546 bool OrderInputsWithConstantOnTheRight() {
2547 HInstruction* left = InputAt(0);
2548 HInstruction* right = InputAt(1);
2549 if (left->IsConstant() && !right->IsConstant()) {
2550 ReplaceInput(right, 0);
2551 ReplaceInput(left, 1);
2552 return true;
2553 }
2554 return false;
2555 }
2556
2557 // Order inputs by instruction id, but favor constant on the right side.
2558 // This helps GVN for commutative ops.
2559 void OrderInputs() {
2560 DCHECK(IsCommutative());
2561 HInstruction* left = InputAt(0);
2562 HInstruction* right = InputAt(1);
2563 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2564 return;
2565 }
2566 if (OrderInputsWithConstantOnTheRight()) {
2567 return;
2568 }
2569 // Order according to instruction id.
2570 if (left->GetId() > right->GetId()) {
2571 ReplaceInput(right, 0);
2572 ReplaceInput(left, 1);
2573 }
2574 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002575
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002576 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002577 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002578 return true;
2579 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002580
Roland Levillain9240d6a2014-10-20 16:47:04 +01002581 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002582 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002583 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002584 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002585
2586 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002587 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2588 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2589 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2590 HLongConstant* y ATTRIBUTE_UNUSED) const {
2591 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2592 return nullptr;
2593 }
2594 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2595 HIntConstant* y ATTRIBUTE_UNUSED) const {
2596 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2597 return nullptr;
2598 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002599
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002600 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002601 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002602 HConstant* GetConstantRight() const;
2603
2604 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002605 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002606 HInstruction* GetLeastConstantLeft() const;
2607
Roland Levillainccc07a92014-09-16 14:48:16 +01002608 DECLARE_INSTRUCTION(BinaryOperation);
2609
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002610 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002611 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2612};
2613
Mark Mendellc4701932015-04-10 13:18:51 -04002614// The comparison bias applies for floating point operations and indicates how NaN
2615// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002616enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002617 kNoBias, // bias is not applicable (i.e. for long operation)
2618 kGtBias, // return 1 for NaN comparisons
2619 kLtBias, // return -1 for NaN comparisons
2620};
2621
Dave Allison20dfc792014-06-16 20:44:29 -07002622class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002623 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002624 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2625 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002626 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002627 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002628
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002629 bool NeedsMaterialization() const { return needs_materialization_; }
2630 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002631
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002632 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002633 // `instruction`, and disregard moves in between.
2634 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002635
Dave Allison20dfc792014-06-16 20:44:29 -07002636 DECLARE_INSTRUCTION(Condition);
2637
2638 virtual IfCondition GetCondition() const = 0;
2639
Mark Mendellc4701932015-04-10 13:18:51 -04002640 virtual IfCondition GetOppositeCondition() const = 0;
2641
Roland Levillain4fa13f62015-07-06 18:11:54 +01002642 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002643
2644 void SetBias(ComparisonBias bias) { bias_ = bias; }
2645
2646 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2647 return bias_ == other->AsCondition()->bias_;
2648 }
2649
Roland Levillain4fa13f62015-07-06 18:11:54 +01002650 bool IsFPConditionTrueIfNaN() const {
2651 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2652 IfCondition if_cond = GetCondition();
2653 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2654 }
2655
2656 bool IsFPConditionFalseIfNaN() const {
2657 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2658 IfCondition if_cond = GetCondition();
2659 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2660 }
2661
Dave Allison20dfc792014-06-16 20:44:29 -07002662 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002663 // For register allocation purposes, returns whether this instruction needs to be
2664 // materialized (that is, not just be in the processor flags).
2665 bool needs_materialization_;
2666
Mark Mendellc4701932015-04-10 13:18:51 -04002667 // Needed if we merge a HCompare into a HCondition.
2668 ComparisonBias bias_;
2669
Dave Allison20dfc792014-06-16 20:44:29 -07002670 DISALLOW_COPY_AND_ASSIGN(HCondition);
2671};
2672
2673// Instruction to check if two inputs are equal to each other.
2674class HEqual : public HCondition {
2675 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002676 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2677 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002678
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002679 bool IsCommutative() const OVERRIDE { return true; }
2680
Roland Levillain9867bc72015-08-05 10:21:34 +01002681 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002682 return GetBlock()->GetGraph()->GetIntConstant(
2683 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002684 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002685 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002686 return GetBlock()->GetGraph()->GetIntConstant(
2687 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002688 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002689
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002690 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002691
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002692 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002693 return kCondEQ;
2694 }
2695
Mark Mendellc4701932015-04-10 13:18:51 -04002696 IfCondition GetOppositeCondition() const OVERRIDE {
2697 return kCondNE;
2698 }
2699
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002700 private:
Aart Bike9f37602015-10-09 11:15:55 -07002701 template <typename T> bool Compute(T x, T y) const { return x == y; }
2702
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002703 DISALLOW_COPY_AND_ASSIGN(HEqual);
2704};
2705
Dave Allison20dfc792014-06-16 20:44:29 -07002706class HNotEqual : public HCondition {
2707 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002708 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2709 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002710
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002711 bool IsCommutative() const OVERRIDE { return true; }
2712
Roland Levillain9867bc72015-08-05 10:21:34 +01002713 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002714 return GetBlock()->GetGraph()->GetIntConstant(
2715 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002716 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002717 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002718 return GetBlock()->GetGraph()->GetIntConstant(
2719 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002720 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002721
Dave Allison20dfc792014-06-16 20:44:29 -07002722 DECLARE_INSTRUCTION(NotEqual);
2723
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002724 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002725 return kCondNE;
2726 }
2727
Mark Mendellc4701932015-04-10 13:18:51 -04002728 IfCondition GetOppositeCondition() const OVERRIDE {
2729 return kCondEQ;
2730 }
2731
Dave Allison20dfc792014-06-16 20:44:29 -07002732 private:
Aart Bike9f37602015-10-09 11:15:55 -07002733 template <typename T> bool Compute(T x, T y) const { return x != y; }
2734
Dave Allison20dfc792014-06-16 20:44:29 -07002735 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2736};
2737
2738class HLessThan : public HCondition {
2739 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002740 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2741 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002742
Roland Levillain9867bc72015-08-05 10:21:34 +01002743 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002744 return GetBlock()->GetGraph()->GetIntConstant(
2745 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002746 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002747 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002748 return GetBlock()->GetGraph()->GetIntConstant(
2749 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002750 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002751
Dave Allison20dfc792014-06-16 20:44:29 -07002752 DECLARE_INSTRUCTION(LessThan);
2753
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002754 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002755 return kCondLT;
2756 }
2757
Mark Mendellc4701932015-04-10 13:18:51 -04002758 IfCondition GetOppositeCondition() const OVERRIDE {
2759 return kCondGE;
2760 }
2761
Dave Allison20dfc792014-06-16 20:44:29 -07002762 private:
Aart Bike9f37602015-10-09 11:15:55 -07002763 template <typename T> bool Compute(T x, T y) const { return x < y; }
2764
Dave Allison20dfc792014-06-16 20:44:29 -07002765 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2766};
2767
2768class HLessThanOrEqual : public HCondition {
2769 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002770 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2771 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002772
Roland Levillain9867bc72015-08-05 10:21:34 +01002773 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002774 return GetBlock()->GetGraph()->GetIntConstant(
2775 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002776 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002777 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002778 return GetBlock()->GetGraph()->GetIntConstant(
2779 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002780 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002781
Dave Allison20dfc792014-06-16 20:44:29 -07002782 DECLARE_INSTRUCTION(LessThanOrEqual);
2783
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002784 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002785 return kCondLE;
2786 }
2787
Mark Mendellc4701932015-04-10 13:18:51 -04002788 IfCondition GetOppositeCondition() const OVERRIDE {
2789 return kCondGT;
2790 }
2791
Dave Allison20dfc792014-06-16 20:44:29 -07002792 private:
Aart Bike9f37602015-10-09 11:15:55 -07002793 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2794
Dave Allison20dfc792014-06-16 20:44:29 -07002795 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2796};
2797
2798class HGreaterThan : public HCondition {
2799 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002800 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2801 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002802
Roland Levillain9867bc72015-08-05 10:21:34 +01002803 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002804 return GetBlock()->GetGraph()->GetIntConstant(
2805 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002806 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002807 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002808 return GetBlock()->GetGraph()->GetIntConstant(
2809 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002810 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002811
Dave Allison20dfc792014-06-16 20:44:29 -07002812 DECLARE_INSTRUCTION(GreaterThan);
2813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002814 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002815 return kCondGT;
2816 }
2817
Mark Mendellc4701932015-04-10 13:18:51 -04002818 IfCondition GetOppositeCondition() const OVERRIDE {
2819 return kCondLE;
2820 }
2821
Dave Allison20dfc792014-06-16 20:44:29 -07002822 private:
Aart Bike9f37602015-10-09 11:15:55 -07002823 template <typename T> bool Compute(T x, T y) const { return x > y; }
2824
Dave Allison20dfc792014-06-16 20:44:29 -07002825 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2826};
2827
2828class HGreaterThanOrEqual : public HCondition {
2829 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002830 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2831 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002832
Roland Levillain9867bc72015-08-05 10:21:34 +01002833 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002834 return GetBlock()->GetGraph()->GetIntConstant(
2835 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002836 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002837 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002838 return GetBlock()->GetGraph()->GetIntConstant(
2839 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002840 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002841
Dave Allison20dfc792014-06-16 20:44:29 -07002842 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2843
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002844 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002845 return kCondGE;
2846 }
2847
Mark Mendellc4701932015-04-10 13:18:51 -04002848 IfCondition GetOppositeCondition() const OVERRIDE {
2849 return kCondLT;
2850 }
2851
Dave Allison20dfc792014-06-16 20:44:29 -07002852 private:
Aart Bike9f37602015-10-09 11:15:55 -07002853 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2854
Dave Allison20dfc792014-06-16 20:44:29 -07002855 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2856};
2857
Aart Bike9f37602015-10-09 11:15:55 -07002858class HBelow : public HCondition {
2859 public:
2860 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2861 : HCondition(first, second, dex_pc) {}
2862
2863 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2864 return GetBlock()->GetGraph()->GetIntConstant(
2865 Compute(static_cast<uint32_t>(x->GetValue()),
2866 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2867 }
2868 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2869 return GetBlock()->GetGraph()->GetIntConstant(
2870 Compute(static_cast<uint64_t>(x->GetValue()),
2871 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2872 }
2873
2874 DECLARE_INSTRUCTION(Below);
2875
2876 IfCondition GetCondition() const OVERRIDE {
2877 return kCondB;
2878 }
2879
2880 IfCondition GetOppositeCondition() const OVERRIDE {
2881 return kCondAE;
2882 }
2883
2884 private:
2885 template <typename T> bool Compute(T x, T y) const { return x < y; }
2886
2887 DISALLOW_COPY_AND_ASSIGN(HBelow);
2888};
2889
2890class HBelowOrEqual : public HCondition {
2891 public:
2892 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2893 : HCondition(first, second, dex_pc) {}
2894
2895 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2896 return GetBlock()->GetGraph()->GetIntConstant(
2897 Compute(static_cast<uint32_t>(x->GetValue()),
2898 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2899 }
2900 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2901 return GetBlock()->GetGraph()->GetIntConstant(
2902 Compute(static_cast<uint64_t>(x->GetValue()),
2903 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2904 }
2905
2906 DECLARE_INSTRUCTION(BelowOrEqual);
2907
2908 IfCondition GetCondition() const OVERRIDE {
2909 return kCondBE;
2910 }
2911
2912 IfCondition GetOppositeCondition() const OVERRIDE {
2913 return kCondA;
2914 }
2915
2916 private:
2917 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2918
2919 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2920};
2921
2922class HAbove : public HCondition {
2923 public:
2924 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2925 : HCondition(first, second, dex_pc) {}
2926
2927 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2928 return GetBlock()->GetGraph()->GetIntConstant(
2929 Compute(static_cast<uint32_t>(x->GetValue()),
2930 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2931 }
2932 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2933 return GetBlock()->GetGraph()->GetIntConstant(
2934 Compute(static_cast<uint64_t>(x->GetValue()),
2935 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2936 }
2937
2938 DECLARE_INSTRUCTION(Above);
2939
2940 IfCondition GetCondition() const OVERRIDE {
2941 return kCondA;
2942 }
2943
2944 IfCondition GetOppositeCondition() const OVERRIDE {
2945 return kCondBE;
2946 }
2947
2948 private:
2949 template <typename T> bool Compute(T x, T y) const { return x > y; }
2950
2951 DISALLOW_COPY_AND_ASSIGN(HAbove);
2952};
2953
2954class HAboveOrEqual : public HCondition {
2955 public:
2956 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2957 : HCondition(first, second, dex_pc) {}
2958
2959 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2960 return GetBlock()->GetGraph()->GetIntConstant(
2961 Compute(static_cast<uint32_t>(x->GetValue()),
2962 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2963 }
2964 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2965 return GetBlock()->GetGraph()->GetIntConstant(
2966 Compute(static_cast<uint64_t>(x->GetValue()),
2967 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2968 }
2969
2970 DECLARE_INSTRUCTION(AboveOrEqual);
2971
2972 IfCondition GetCondition() const OVERRIDE {
2973 return kCondAE;
2974 }
2975
2976 IfCondition GetOppositeCondition() const OVERRIDE {
2977 return kCondB;
2978 }
2979
2980 private:
2981 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2982
2983 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
2984};
Dave Allison20dfc792014-06-16 20:44:29 -07002985
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002986// Instruction to check how two inputs compare to each other.
2987// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2988class HCompare : public HBinaryOperation {
2989 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002990 HCompare(Primitive::Type type,
2991 HInstruction* first,
2992 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002993 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002994 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002995 : HBinaryOperation(Primitive::kPrimInt,
2996 first,
2997 second,
2998 SideEffectsForArchRuntimeCalls(type),
2999 dex_pc),
3000 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003001 DCHECK_EQ(type, first->GetType());
3002 DCHECK_EQ(type, second->GetType());
3003 }
3004
Roland Levillain9867bc72015-08-05 10:21:34 +01003005 template <typename T>
3006 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003007
Roland Levillain9867bc72015-08-05 10:21:34 +01003008 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003009 return GetBlock()->GetGraph()->GetIntConstant(
3010 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003011 }
3012 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003013 return GetBlock()->GetGraph()->GetIntConstant(
3014 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003015 }
3016
Calin Juravleddb7df22014-11-25 20:56:51 +00003017 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3018 return bias_ == other->AsCompare()->bias_;
3019 }
3020
Mark Mendellc4701932015-04-10 13:18:51 -04003021 ComparisonBias GetBias() const { return bias_; }
3022
Roland Levillain4fa13f62015-07-06 18:11:54 +01003023 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003024
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003025
3026 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3027 // MIPS64 uses a runtime call for FP comparisons.
3028 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3029 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003030
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003031 DECLARE_INSTRUCTION(Compare);
3032
3033 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003034 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003035
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003036 DISALLOW_COPY_AND_ASSIGN(HCompare);
3037};
3038
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003039// A local in the graph. Corresponds to a Dex register.
3040class HLocal : public HTemplateInstruction<0> {
3041 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003042 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003043 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003044
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003045 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003046
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003047 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003048
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003049 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003050 // The Dex register number.
3051 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003052
3053 DISALLOW_COPY_AND_ASSIGN(HLocal);
3054};
3055
3056// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003057class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003058 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003059 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3060 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003061 SetRawInputAt(0, local);
3062 }
3063
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003064 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3065
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003066 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003067
3068 private:
3069 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3070};
3071
3072// Store a value in a given local. This instruction has two inputs: the value
3073// and the local.
3074class HStoreLocal : public HTemplateInstruction<2> {
3075 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003076 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3077 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003078 SetRawInputAt(0, local);
3079 SetRawInputAt(1, value);
3080 }
3081
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003082 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3083
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003084 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003085
3086 private:
3087 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3088};
3089
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003090class HFloatConstant : public HConstant {
3091 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003092 float GetValue() const { return value_; }
3093
David Brazdil77a48ae2015-09-15 12:34:04 +00003094 uint64_t GetValueAsUint64() const OVERRIDE {
3095 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3096 }
3097
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003098 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003099 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003100 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003101 }
3102
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003103 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003104
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003105 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003106 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003107 }
3108 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003109 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003110 }
3111 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003112 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3113 }
3114 bool IsNaN() const {
3115 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003116 }
3117
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003118 DECLARE_INSTRUCTION(FloatConstant);
3119
3120 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003121 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3122 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3123 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3124 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003125
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003126 const float value_;
3127
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003128 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003129 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003130 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003131 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3132};
3133
3134class HDoubleConstant : public HConstant {
3135 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003136 double GetValue() const { return value_; }
3137
David Brazdil77a48ae2015-09-15 12:34:04 +00003138 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3139
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003140 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003141 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003142 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003143 }
3144
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003145 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003146
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003147 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003148 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003149 }
3150 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003151 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003152 }
3153 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003154 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3155 }
3156 bool IsNaN() const {
3157 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003158 }
3159
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003160 DECLARE_INSTRUCTION(DoubleConstant);
3161
3162 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003163 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3164 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3165 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3166 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003167
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003168 const double value_;
3169
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003170 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003171 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003172 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003173 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3174};
3175
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003176enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003177#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003178#include "intrinsics_list.h"
3179 kNone,
3180 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3181#undef INTRINSICS_LIST
3182#undef OPTIMIZING_INTRINSICS
3183};
3184std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3185
Agi Csaki05f20562015-08-19 14:58:14 -07003186enum IntrinsicNeedsEnvironmentOrCache {
3187 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3188 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003189};
3190
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003191class HInvoke : public HInstruction {
3192 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003193 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003194
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003195 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003196
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003197 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003198 SetRawInputAt(index, argument);
3199 }
3200
Roland Levillain3e3d7332015-04-28 11:00:54 +01003201 // Return the number of arguments. This number can be lower than
3202 // the number of inputs returned by InputCount(), as some invoke
3203 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3204 // inputs at the end of their list of inputs.
3205 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3206
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003207 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003208
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003209
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003210 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003211 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003212
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003213 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3214
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003215 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003216 return intrinsic_;
3217 }
3218
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003219 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003220
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003221 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003222 return GetEnvironment()->GetParent() != nullptr;
3223 }
3224
3225 bool CanThrow() const OVERRIDE { return true; }
3226
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003227 uint32_t* GetIntrinsicOptimizations() {
3228 return &intrinsic_optimizations_;
3229 }
3230
3231 const uint32_t* GetIntrinsicOptimizations() const {
3232 return &intrinsic_optimizations_;
3233 }
3234
3235 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3236
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003237 DECLARE_INSTRUCTION(Invoke);
3238
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003239 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003240 HInvoke(ArenaAllocator* arena,
3241 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003242 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003243 Primitive::Type return_type,
3244 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003245 uint32_t dex_method_index,
3246 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003247 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003248 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003249 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003250 inputs_(number_of_arguments + number_of_other_inputs,
3251 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003252 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003253 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003254 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003255 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003256 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003257 }
3258
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003259 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003260 return inputs_[index];
3261 }
3262
David Brazdil1abb4192015-02-17 18:33:36 +00003263 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003264 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003265 }
3266
Roland Levillain3e3d7332015-04-28 11:00:54 +01003267 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003268 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003269 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003270 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003271 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003272 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003273
3274 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3275 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003276
3277 private:
3278 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3279};
3280
Calin Juravle175dc732015-08-25 15:42:32 +01003281class HInvokeUnresolved : public HInvoke {
3282 public:
3283 HInvokeUnresolved(ArenaAllocator* arena,
3284 uint32_t number_of_arguments,
3285 Primitive::Type return_type,
3286 uint32_t dex_pc,
3287 uint32_t dex_method_index,
3288 InvokeType invoke_type)
3289 : HInvoke(arena,
3290 number_of_arguments,
3291 0u /* number_of_other_inputs */,
3292 return_type,
3293 dex_pc,
3294 dex_method_index,
3295 invoke_type) {
3296 }
3297
3298 DECLARE_INSTRUCTION(InvokeUnresolved);
3299
3300 private:
3301 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3302};
3303
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003304class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003305 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003306 // Requirements of this method call regarding the class
3307 // initialization (clinit) check of its declaring class.
3308 enum class ClinitCheckRequirement {
3309 kNone, // Class already initialized.
3310 kExplicit, // Static call having explicit clinit check as last input.
3311 kImplicit, // Static call implicitly requiring a clinit check.
3312 };
3313
Vladimir Marko58155012015-08-19 12:49:41 +00003314 // Determines how to load the target ArtMethod*.
3315 enum class MethodLoadKind {
3316 // Use a String init ArtMethod* loaded from Thread entrypoints.
3317 kStringInit,
3318
3319 // Use the method's own ArtMethod* loaded by the register allocator.
3320 kRecursive,
3321
3322 // Use ArtMethod* at a known address, embed the direct address in the code.
3323 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3324 kDirectAddress,
3325
3326 // Use ArtMethod* at an address that will be known at link time, embed the direct
3327 // address in the code. If the image is relocatable, emit .patch_oat entry.
3328 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3329 // the image relocatable or not.
3330 kDirectAddressWithFixup,
3331
3332 // Load from resoved methods array in the dex cache using a PC-relative load.
3333 // Used when we need to use the dex cache, for example for invoke-static that
3334 // may cause class initialization (the entry may point to a resolution method),
3335 // and we know that we can access the dex cache arrays using a PC-relative load.
3336 kDexCachePcRelative,
3337
3338 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3339 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3340 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3341 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3342 kDexCacheViaMethod,
3343 };
3344
3345 // Determines the location of the code pointer.
3346 enum class CodePtrLocation {
3347 // Recursive call, use local PC-relative call instruction.
3348 kCallSelf,
3349
3350 // Use PC-relative call instruction patched at link time.
3351 // Used for calls within an oat file, boot->boot or app->app.
3352 kCallPCRelative,
3353
3354 // Call to a known target address, embed the direct address in code.
3355 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3356 kCallDirect,
3357
3358 // Call to a target address that will be known at link time, embed the direct
3359 // address in code. If the image is relocatable, emit .patch_oat entry.
3360 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3361 // the image relocatable or not.
3362 kCallDirectWithFixup,
3363
3364 // Use code pointer from the ArtMethod*.
3365 // Used when we don't know the target code. This is also the last-resort-kind used when
3366 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3367 kCallArtMethod,
3368 };
3369
3370 struct DispatchInfo {
3371 const MethodLoadKind method_load_kind;
3372 const CodePtrLocation code_ptr_location;
3373 // The method load data holds
3374 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3375 // Note that there are multiple string init methods, each having its own offset.
3376 // - the method address for kDirectAddress
3377 // - the dex cache arrays offset for kDexCachePcRel.
3378 const uint64_t method_load_data;
3379 const uint64_t direct_code_ptr;
3380 };
3381
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003382 HInvokeStaticOrDirect(ArenaAllocator* arena,
3383 uint32_t number_of_arguments,
3384 Primitive::Type return_type,
3385 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003386 uint32_t method_index,
3387 MethodReference target_method,
3388 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003389 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003390 InvokeType invoke_type,
3391 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003392 : HInvoke(arena,
3393 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003394 // There is one extra argument for the HCurrentMethod node, and
3395 // potentially one other if the clinit check is explicit, and one other
3396 // if the method is a string factory.
3397 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003398 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003399 return_type,
3400 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003401 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003402 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003403 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003404 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003405 target_method_(target_method),
3406 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003407
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003408 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003409 // We access the method via the dex cache so we can't do an implicit null check.
3410 // TODO: for intrinsics we can generate implicit null checks.
3411 return false;
3412 }
3413
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003414 bool CanBeNull() const OVERRIDE {
3415 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3416 }
3417
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003418 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003419 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3420 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3421 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003422 bool NeedsDexCache() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003423 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003424 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003425 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3426 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3427 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3428 MethodReference GetTargetMethod() const { return target_method_; }
3429
3430 int32_t GetStringInitOffset() const {
3431 DCHECK(IsStringInit());
3432 return dispatch_info_.method_load_data;
3433 }
3434
3435 uint64_t GetMethodAddress() const {
3436 DCHECK(HasMethodAddress());
3437 return dispatch_info_.method_load_data;
3438 }
3439
3440 uint32_t GetDexCacheArrayOffset() const {
3441 DCHECK(HasPcRelDexCache());
3442 return dispatch_info_.method_load_data;
3443 }
3444
3445 uint64_t GetDirectCodePtr() const {
3446 DCHECK(HasDirectCodePtr());
3447 return dispatch_info_.direct_code_ptr;
3448 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003449
Calin Juravle68ad6492015-08-18 17:08:12 +01003450 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3451
Roland Levillain4c0eb422015-04-24 16:43:49 +01003452 // Is this instruction a call to a static method?
3453 bool IsStatic() const {
3454 return GetInvokeType() == kStatic;
3455 }
3456
Roland Levillain3e3d7332015-04-28 11:00:54 +01003457 // Remove the art::HLoadClass instruction set as last input by
3458 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3459 // the initial art::HClinitCheck instruction (only relevant for
3460 // static calls with explicit clinit check).
3461 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003462 DCHECK(IsStaticWithExplicitClinitCheck());
3463 size_t last_input_index = InputCount() - 1;
3464 HInstruction* last_input = InputAt(last_input_index);
3465 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003466 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003467 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003468 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003469 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3470 DCHECK(IsStaticWithImplicitClinitCheck());
3471 }
3472
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003473 bool IsStringFactoryFor(HFakeString* str) const {
3474 if (!IsStringInit()) return false;
3475 // +1 for the current method.
3476 if (InputCount() == (number_of_arguments_ + 1)) return false;
3477 return InputAt(InputCount() - 1)->AsFakeString() == str;
3478 }
3479
3480 void RemoveFakeStringArgumentAsLastInput() {
3481 DCHECK(IsStringInit());
3482 size_t last_input_index = InputCount() - 1;
3483 HInstruction* last_input = InputAt(last_input_index);
3484 DCHECK(last_input != nullptr);
3485 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3486 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003487 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003488 }
3489
Roland Levillain4c0eb422015-04-24 16:43:49 +01003490 // Is this a call to a static method whose declaring class has an
3491 // explicit intialization check in the graph?
3492 bool IsStaticWithExplicitClinitCheck() const {
3493 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3494 }
3495
3496 // Is this a call to a static method whose declaring class has an
3497 // implicit intialization check requirement?
3498 bool IsStaticWithImplicitClinitCheck() const {
3499 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3500 }
3501
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003502 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003503
Roland Levillain4c0eb422015-04-24 16:43:49 +01003504 protected:
3505 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3506 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3507 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3508 HInstruction* input = input_record.GetInstruction();
3509 // `input` is the last input of a static invoke marked as having
3510 // an explicit clinit check. It must either be:
3511 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3512 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3513 DCHECK(input != nullptr);
3514 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3515 }
3516 return input_record;
3517 }
3518
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003519 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003520 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003521 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003522 // The target method may refer to different dex file or method index than the original
3523 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3524 // in derived class to increase visibility.
3525 MethodReference target_method_;
3526 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003527
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003528 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003529};
3530
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003531class HInvokeVirtual : public HInvoke {
3532 public:
3533 HInvokeVirtual(ArenaAllocator* arena,
3534 uint32_t number_of_arguments,
3535 Primitive::Type return_type,
3536 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003537 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003538 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003539 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003540 vtable_index_(vtable_index) {}
3541
Calin Juravle641547a2015-04-21 22:08:51 +01003542 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003543 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003544 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003545 }
3546
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003547 uint32_t GetVTableIndex() const { return vtable_index_; }
3548
3549 DECLARE_INSTRUCTION(InvokeVirtual);
3550
3551 private:
3552 const uint32_t vtable_index_;
3553
3554 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3555};
3556
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003557class HInvokeInterface : public HInvoke {
3558 public:
3559 HInvokeInterface(ArenaAllocator* arena,
3560 uint32_t number_of_arguments,
3561 Primitive::Type return_type,
3562 uint32_t dex_pc,
3563 uint32_t dex_method_index,
3564 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003565 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003566 imt_index_(imt_index) {}
3567
Calin Juravle641547a2015-04-21 22:08:51 +01003568 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003569 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003570 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003571 }
3572
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003573 uint32_t GetImtIndex() const { return imt_index_; }
3574 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3575
3576 DECLARE_INSTRUCTION(InvokeInterface);
3577
3578 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003579 const uint32_t imt_index_;
3580
3581 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3582};
3583
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003584class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003585 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003586 HNewInstance(HCurrentMethod* current_method,
3587 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003588 uint16_t type_index,
3589 const DexFile& dex_file,
3590 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003591 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003592 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003593 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003594 entrypoint_(entrypoint) {
3595 SetRawInputAt(0, current_method);
3596 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003597
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003598 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003599 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003600
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003601 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003602 bool NeedsEnvironment() const OVERRIDE { return true; }
3603 // It may throw when called on:
3604 // - interfaces
3605 // - abstract/innaccessible/unknown classes
3606 // TODO: optimize when possible.
3607 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003608
Calin Juravle10e244f2015-01-26 18:54:32 +00003609 bool CanBeNull() const OVERRIDE { return false; }
3610
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003611 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3612
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003613 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003614
3615 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003616 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003617 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003618 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003619
3620 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3621};
3622
Roland Levillain88cb1752014-10-20 16:36:47 +01003623class HNeg : public HUnaryOperation {
3624 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003625 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3626 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003627
Roland Levillain9867bc72015-08-05 10:21:34 +01003628 template <typename T> T Compute(T x) const { return -x; }
3629
3630 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003631 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003632 }
3633 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003634 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003635 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003636
Roland Levillain88cb1752014-10-20 16:36:47 +01003637 DECLARE_INSTRUCTION(Neg);
3638
3639 private:
3640 DISALLOW_COPY_AND_ASSIGN(HNeg);
3641};
3642
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003643class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003644 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003645 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003646 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003647 uint32_t dex_pc,
3648 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003649 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003650 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003651 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003652 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003653 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003654 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003655 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003656 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003657 }
3658
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003659 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003660 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003661
3662 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003663 bool NeedsEnvironment() const OVERRIDE { return true; }
3664
Mingyao Yang0c365e62015-03-31 15:09:29 -07003665 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3666 bool CanThrow() const OVERRIDE { return true; }
3667
Calin Juravle10e244f2015-01-26 18:54:32 +00003668 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003669
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003670 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3671
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003672 DECLARE_INSTRUCTION(NewArray);
3673
3674 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003675 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003676 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003677 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003678
3679 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3680};
3681
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003682class HAdd : public HBinaryOperation {
3683 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003684 HAdd(Primitive::Type result_type,
3685 HInstruction* left,
3686 HInstruction* right,
3687 uint32_t dex_pc = kNoDexPc)
3688 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003689
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003690 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003691
Roland Levillain9867bc72015-08-05 10:21:34 +01003692 template <typename T> T Compute(T x, T y) const { return x + y; }
3693
3694 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003695 return GetBlock()->GetGraph()->GetIntConstant(
3696 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003697 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003698 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003699 return GetBlock()->GetGraph()->GetLongConstant(
3700 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003701 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003702
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003703 DECLARE_INSTRUCTION(Add);
3704
3705 private:
3706 DISALLOW_COPY_AND_ASSIGN(HAdd);
3707};
3708
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003709class HSub : public HBinaryOperation {
3710 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003711 HSub(Primitive::Type result_type,
3712 HInstruction* left,
3713 HInstruction* right,
3714 uint32_t dex_pc = kNoDexPc)
3715 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003716
Roland Levillain9867bc72015-08-05 10:21:34 +01003717 template <typename T> T Compute(T x, T y) const { return x - y; }
3718
3719 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003720 return GetBlock()->GetGraph()->GetIntConstant(
3721 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003722 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003723 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003724 return GetBlock()->GetGraph()->GetLongConstant(
3725 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003726 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003727
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003728 DECLARE_INSTRUCTION(Sub);
3729
3730 private:
3731 DISALLOW_COPY_AND_ASSIGN(HSub);
3732};
3733
Calin Juravle34bacdf2014-10-07 20:23:36 +01003734class HMul : public HBinaryOperation {
3735 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003736 HMul(Primitive::Type result_type,
3737 HInstruction* left,
3738 HInstruction* right,
3739 uint32_t dex_pc = kNoDexPc)
3740 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003741
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003742 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003743
Roland Levillain9867bc72015-08-05 10:21:34 +01003744 template <typename T> T Compute(T x, T y) const { return x * y; }
3745
3746 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003747 return GetBlock()->GetGraph()->GetIntConstant(
3748 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003749 }
3750 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003751 return GetBlock()->GetGraph()->GetLongConstant(
3752 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003753 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003754
3755 DECLARE_INSTRUCTION(Mul);
3756
3757 private:
3758 DISALLOW_COPY_AND_ASSIGN(HMul);
3759};
3760
Calin Juravle7c4954d2014-10-28 16:57:40 +00003761class HDiv : public HBinaryOperation {
3762 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003763 HDiv(Primitive::Type result_type,
3764 HInstruction* left,
3765 HInstruction* right,
3766 uint32_t dex_pc)
3767 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003768
Roland Levillain9867bc72015-08-05 10:21:34 +01003769 template <typename T>
3770 T Compute(T x, T y) const {
3771 // Our graph structure ensures we never have 0 for `y` during
3772 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003773 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003774 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003775 return (y == -1) ? -x : x / y;
3776 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003777
Roland Levillain9867bc72015-08-05 10:21:34 +01003778 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003779 return GetBlock()->GetGraph()->GetIntConstant(
3780 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003781 }
3782 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003783 return GetBlock()->GetGraph()->GetLongConstant(
3784 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003785 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003786
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003787 static SideEffects SideEffectsForArchRuntimeCalls() {
3788 // The generated code can use a runtime call.
3789 return SideEffects::CanTriggerGC();
3790 }
3791
Calin Juravle7c4954d2014-10-28 16:57:40 +00003792 DECLARE_INSTRUCTION(Div);
3793
3794 private:
3795 DISALLOW_COPY_AND_ASSIGN(HDiv);
3796};
3797
Calin Juravlebacfec32014-11-14 15:54:36 +00003798class HRem : public HBinaryOperation {
3799 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003800 HRem(Primitive::Type result_type,
3801 HInstruction* left,
3802 HInstruction* right,
3803 uint32_t dex_pc)
3804 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003805
Roland Levillain9867bc72015-08-05 10:21:34 +01003806 template <typename T>
3807 T Compute(T x, T y) const {
3808 // Our graph structure ensures we never have 0 for `y` during
3809 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003810 DCHECK_NE(y, 0);
3811 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3812 return (y == -1) ? 0 : x % y;
3813 }
3814
Roland Levillain9867bc72015-08-05 10:21:34 +01003815 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003816 return GetBlock()->GetGraph()->GetIntConstant(
3817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003818 }
3819 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003820 return GetBlock()->GetGraph()->GetLongConstant(
3821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003822 }
3823
Calin Juravlebacfec32014-11-14 15:54:36 +00003824
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003825 static SideEffects SideEffectsForArchRuntimeCalls() {
3826 return SideEffects::CanTriggerGC();
3827 }
3828
Calin Juravlebacfec32014-11-14 15:54:36 +00003829 DECLARE_INSTRUCTION(Rem);
3830
3831 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003832 DISALLOW_COPY_AND_ASSIGN(HRem);
3833};
3834
Calin Juravled0d48522014-11-04 16:40:20 +00003835class HDivZeroCheck : public HExpression<1> {
3836 public:
3837 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003838 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003839 SetRawInputAt(0, value);
3840 }
3841
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003842 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3843
Calin Juravled0d48522014-11-04 16:40:20 +00003844 bool CanBeMoved() const OVERRIDE { return true; }
3845
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003846 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003847 return true;
3848 }
3849
3850 bool NeedsEnvironment() const OVERRIDE { return true; }
3851 bool CanThrow() const OVERRIDE { return true; }
3852
Calin Juravled0d48522014-11-04 16:40:20 +00003853 DECLARE_INSTRUCTION(DivZeroCheck);
3854
3855 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003856 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3857};
3858
Calin Juravle9aec02f2014-11-18 23:06:35 +00003859class HShl : public HBinaryOperation {
3860 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003861 HShl(Primitive::Type result_type,
3862 HInstruction* left,
3863 HInstruction* right,
3864 uint32_t dex_pc = kNoDexPc)
3865 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003866
Roland Levillain9867bc72015-08-05 10:21:34 +01003867 template <typename T, typename U, typename V>
3868 T Compute(T x, U y, V max_shift_value) const {
3869 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3870 "V is not the unsigned integer type corresponding to T");
3871 return x << (y & max_shift_value);
3872 }
3873
3874 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3875 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003876 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003877 }
3878 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3879 // case is handled as `x << static_cast<int>(y)`.
3880 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3881 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003882 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003883 }
3884 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3885 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003886 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003887 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003888
3889 DECLARE_INSTRUCTION(Shl);
3890
3891 private:
3892 DISALLOW_COPY_AND_ASSIGN(HShl);
3893};
3894
3895class HShr : public HBinaryOperation {
3896 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003897 HShr(Primitive::Type result_type,
3898 HInstruction* left,
3899 HInstruction* right,
3900 uint32_t dex_pc = kNoDexPc)
3901 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003902
Roland Levillain9867bc72015-08-05 10:21:34 +01003903 template <typename T, typename U, typename V>
3904 T Compute(T x, U y, V max_shift_value) const {
3905 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3906 "V is not the unsigned integer type corresponding to T");
3907 return x >> (y & max_shift_value);
3908 }
3909
3910 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3911 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003912 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003913 }
3914 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3915 // case is handled as `x >> static_cast<int>(y)`.
3916 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3917 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003918 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003919 }
3920 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3921 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003922 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003923 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003924
3925 DECLARE_INSTRUCTION(Shr);
3926
3927 private:
3928 DISALLOW_COPY_AND_ASSIGN(HShr);
3929};
3930
3931class HUShr : public HBinaryOperation {
3932 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003933 HUShr(Primitive::Type result_type,
3934 HInstruction* left,
3935 HInstruction* right,
3936 uint32_t dex_pc = kNoDexPc)
3937 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003938
Roland Levillain9867bc72015-08-05 10:21:34 +01003939 template <typename T, typename U, typename V>
3940 T Compute(T x, U y, V max_shift_value) const {
3941 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3942 "V is not the unsigned integer type corresponding to T");
3943 V ux = static_cast<V>(x);
3944 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003945 }
3946
Roland Levillain9867bc72015-08-05 10:21:34 +01003947 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3948 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003949 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003950 }
3951 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3952 // case is handled as `x >>> static_cast<int>(y)`.
3953 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3954 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003955 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003956 }
3957 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3958 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003959 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003960 }
3961
3962 DECLARE_INSTRUCTION(UShr);
3963
3964 private:
3965 DISALLOW_COPY_AND_ASSIGN(HUShr);
3966};
3967
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003968class HAnd : public HBinaryOperation {
3969 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003970 HAnd(Primitive::Type result_type,
3971 HInstruction* left,
3972 HInstruction* right,
3973 uint32_t dex_pc = kNoDexPc)
3974 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003975
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003976 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003977
Roland Levillain9867bc72015-08-05 10:21:34 +01003978 template <typename T, typename U>
3979 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3980
3981 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003982 return GetBlock()->GetGraph()->GetIntConstant(
3983 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003984 }
3985 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003986 return GetBlock()->GetGraph()->GetLongConstant(
3987 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003988 }
3989 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003990 return GetBlock()->GetGraph()->GetLongConstant(
3991 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 }
3993 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003994 return GetBlock()->GetGraph()->GetLongConstant(
3995 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003996 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003997
3998 DECLARE_INSTRUCTION(And);
3999
4000 private:
4001 DISALLOW_COPY_AND_ASSIGN(HAnd);
4002};
4003
4004class HOr : public HBinaryOperation {
4005 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004006 HOr(Primitive::Type result_type,
4007 HInstruction* left,
4008 HInstruction* right,
4009 uint32_t dex_pc = kNoDexPc)
4010 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004011
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004012 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004013
Roland Levillain9867bc72015-08-05 10:21:34 +01004014 template <typename T, typename U>
4015 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4016
4017 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004018 return GetBlock()->GetGraph()->GetIntConstant(
4019 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004020 }
4021 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004022 return GetBlock()->GetGraph()->GetLongConstant(
4023 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004024 }
4025 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004026 return GetBlock()->GetGraph()->GetLongConstant(
4027 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004028 }
4029 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004030 return GetBlock()->GetGraph()->GetLongConstant(
4031 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004032 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004033
4034 DECLARE_INSTRUCTION(Or);
4035
4036 private:
4037 DISALLOW_COPY_AND_ASSIGN(HOr);
4038};
4039
4040class HXor : public HBinaryOperation {
4041 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004042 HXor(Primitive::Type result_type,
4043 HInstruction* left,
4044 HInstruction* right,
4045 uint32_t dex_pc = kNoDexPc)
4046 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004047
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004048 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004049
Roland Levillain9867bc72015-08-05 10:21:34 +01004050 template <typename T, typename U>
4051 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4052
4053 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004054 return GetBlock()->GetGraph()->GetIntConstant(
4055 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004056 }
4057 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004058 return GetBlock()->GetGraph()->GetLongConstant(
4059 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004060 }
4061 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004062 return GetBlock()->GetGraph()->GetLongConstant(
4063 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004064 }
4065 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004066 return GetBlock()->GetGraph()->GetLongConstant(
4067 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004068 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004069
4070 DECLARE_INSTRUCTION(Xor);
4071
4072 private:
4073 DISALLOW_COPY_AND_ASSIGN(HXor);
4074};
4075
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004076// The value of a parameter in this method. Its location depends on
4077// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004078class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004079 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004080 HParameterValue(const DexFile& dex_file,
4081 uint16_t type_index,
4082 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004083 Primitive::Type parameter_type,
4084 bool is_this = false)
4085 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004086 dex_file_(dex_file),
4087 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004088 index_(index),
4089 is_this_(is_this),
4090 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004091
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004092 const DexFile& GetDexFile() const { return dex_file_; }
4093 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004094 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004095 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004096
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004097 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4098 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004099
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004100 DECLARE_INSTRUCTION(ParameterValue);
4101
4102 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004103 const DexFile& dex_file_;
4104 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004105 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004106 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004107 const uint8_t index_;
4108
Calin Juravle10e244f2015-01-26 18:54:32 +00004109 // Whether or not the parameter value corresponds to 'this' argument.
4110 const bool is_this_;
4111
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004112 bool can_be_null_;
4113
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004114 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4115};
4116
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004117class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004118 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004119 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4120 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004121
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004122 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004123 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004124 return true;
4125 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004126
Roland Levillain9867bc72015-08-05 10:21:34 +01004127 template <typename T> T Compute(T x) const { return ~x; }
4128
4129 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004130 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004131 }
4132 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004133 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004134 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004135
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004136 DECLARE_INSTRUCTION(Not);
4137
4138 private:
4139 DISALLOW_COPY_AND_ASSIGN(HNot);
4140};
4141
David Brazdil66d126e2015-04-03 16:02:44 +01004142class HBooleanNot : public HUnaryOperation {
4143 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004144 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4145 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004146
4147 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004148 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004149 return true;
4150 }
4151
Roland Levillain9867bc72015-08-05 10:21:34 +01004152 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004153 DCHECK(IsUint<1>(x));
4154 return !x;
4155 }
4156
Roland Levillain9867bc72015-08-05 10:21:34 +01004157 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004158 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004159 }
4160 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4161 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004162 UNREACHABLE();
4163 }
4164
4165 DECLARE_INSTRUCTION(BooleanNot);
4166
4167 private:
4168 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4169};
4170
Roland Levillaindff1f282014-11-05 14:15:05 +00004171class HTypeConversion : public HExpression<1> {
4172 public:
4173 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004174 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004175 : HExpression(result_type,
4176 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4177 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004178 SetRawInputAt(0, input);
4179 DCHECK_NE(input->GetType(), result_type);
4180 }
4181
4182 HInstruction* GetInput() const { return InputAt(0); }
4183 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4184 Primitive::Type GetResultType() const { return GetType(); }
4185
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004186 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004187 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004188
Roland Levillaindff1f282014-11-05 14:15:05 +00004189 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004190 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004191
Mark Mendelle82549b2015-05-06 10:55:34 -04004192 // Try to statically evaluate the conversion and return a HConstant
4193 // containing the result. If the input cannot be converted, return nullptr.
4194 HConstant* TryStaticEvaluation() const;
4195
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004196 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4197 Primitive::Type result_type) {
4198 // Some architectures may not require the 'GC' side effects, but at this point
4199 // in the compilation process we do not know what architecture we will
4200 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004201 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4202 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004203 return SideEffects::CanTriggerGC();
4204 }
4205 return SideEffects::None();
4206 }
4207
Roland Levillaindff1f282014-11-05 14:15:05 +00004208 DECLARE_INSTRUCTION(TypeConversion);
4209
4210 private:
4211 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4212};
4213
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004214static constexpr uint32_t kNoRegNumber = -1;
4215
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004216class HPhi : public HInstruction {
4217 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004218 HPhi(ArenaAllocator* arena,
4219 uint32_t reg_number,
4220 size_t number_of_inputs,
4221 Primitive::Type type,
4222 uint32_t dex_pc = kNoDexPc)
4223 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004224 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004225 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004226 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004227 is_live_(false),
4228 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004229 }
4230
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004231 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4232 static Primitive::Type ToPhiType(Primitive::Type type) {
4233 switch (type) {
4234 case Primitive::kPrimBoolean:
4235 case Primitive::kPrimByte:
4236 case Primitive::kPrimShort:
4237 case Primitive::kPrimChar:
4238 return Primitive::kPrimInt;
4239 default:
4240 return type;
4241 }
4242 }
4243
David Brazdilffee3d32015-07-06 11:48:53 +01004244 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4245
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004246 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004247
4248 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004249 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004250
Calin Juravle10e244f2015-01-26 18:54:32 +00004251 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004252 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004253
Calin Juravle10e244f2015-01-26 18:54:32 +00004254 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4255 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4256
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004257 uint32_t GetRegNumber() const { return reg_number_; }
4258
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004259 void SetDead() { is_live_ = false; }
4260 void SetLive() { is_live_ = true; }
4261 bool IsDead() const { return !is_live_; }
4262 bool IsLive() const { return is_live_; }
4263
David Brazdil77a48ae2015-09-15 12:34:04 +00004264 bool IsVRegEquivalentOf(HInstruction* other) const {
4265 return other != nullptr
4266 && other->IsPhi()
4267 && other->AsPhi()->GetBlock() == GetBlock()
4268 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4269 }
4270
Calin Juravlea4f88312015-04-16 12:57:19 +01004271 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4272 // An equivalent phi is a phi having the same dex register and type.
4273 // It assumes that phis with the same dex register are adjacent.
4274 HPhi* GetNextEquivalentPhiWithSameType() {
4275 HInstruction* next = GetNext();
4276 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4277 if (next->GetType() == GetType()) {
4278 return next->AsPhi();
4279 }
4280 next = next->GetNext();
4281 }
4282 return nullptr;
4283 }
4284
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004285 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004286
David Brazdil1abb4192015-02-17 18:33:36 +00004287 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004288 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004289 return inputs_[index];
4290 }
David Brazdil1abb4192015-02-17 18:33:36 +00004291
4292 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004293 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004294 }
4295
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004296 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004297 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004298 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004299 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004300 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004301 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004302
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004303 DISALLOW_COPY_AND_ASSIGN(HPhi);
4304};
4305
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004306class HNullCheck : public HExpression<1> {
4307 public:
4308 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004309 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004310 SetRawInputAt(0, value);
4311 }
4312
Calin Juravle10e244f2015-01-26 18:54:32 +00004313 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004314 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004315 return true;
4316 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004317
Calin Juravle10e244f2015-01-26 18:54:32 +00004318 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004319
Calin Juravle10e244f2015-01-26 18:54:32 +00004320 bool CanThrow() const OVERRIDE { return true; }
4321
4322 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004323
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004324
4325 DECLARE_INSTRUCTION(NullCheck);
4326
4327 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004328 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4329};
4330
4331class FieldInfo : public ValueObject {
4332 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004333 FieldInfo(MemberOffset field_offset,
4334 Primitive::Type field_type,
4335 bool is_volatile,
4336 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004337 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004338 const DexFile& dex_file,
4339 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004340 : field_offset_(field_offset),
4341 field_type_(field_type),
4342 is_volatile_(is_volatile),
4343 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004344 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004345 dex_file_(dex_file),
4346 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004347
4348 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004349 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004350 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004351 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004352 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004353 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004354 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004355
4356 private:
4357 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004358 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004359 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004360 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004361 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004362 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004363 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004364};
4365
4366class HInstanceFieldGet : public HExpression<1> {
4367 public:
4368 HInstanceFieldGet(HInstruction* value,
4369 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004370 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004371 bool is_volatile,
4372 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004373 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004374 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004375 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004376 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004377 : HExpression(field_type,
4378 SideEffects::FieldReadOfType(field_type, is_volatile),
4379 dex_pc),
4380 field_info_(field_offset,
4381 field_type,
4382 is_volatile,
4383 field_idx,
4384 declaring_class_def_index,
4385 dex_file,
4386 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004387 SetRawInputAt(0, value);
4388 }
4389
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004390 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004391
4392 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4393 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4394 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004395 }
4396
Calin Juravle641547a2015-04-21 22:08:51 +01004397 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4398 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004399 }
4400
4401 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004402 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4403 }
4404
Calin Juravle52c48962014-12-16 17:02:57 +00004405 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004406 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004407 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004408 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004409
4410 DECLARE_INSTRUCTION(InstanceFieldGet);
4411
4412 private:
4413 const FieldInfo field_info_;
4414
4415 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4416};
4417
4418class HInstanceFieldSet : public HTemplateInstruction<2> {
4419 public:
4420 HInstanceFieldSet(HInstruction* object,
4421 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004422 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004423 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004424 bool is_volatile,
4425 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004426 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004427 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004428 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004429 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004430 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4431 dex_pc),
4432 field_info_(field_offset,
4433 field_type,
4434 is_volatile,
4435 field_idx,
4436 declaring_class_def_index,
4437 dex_file,
4438 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004439 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004440 SetRawInputAt(0, object);
4441 SetRawInputAt(1, value);
4442 }
4443
Calin Juravle641547a2015-04-21 22:08:51 +01004444 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4445 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004446 }
4447
Calin Juravle52c48962014-12-16 17:02:57 +00004448 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004449 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004450 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004451 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004452 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004453 bool GetValueCanBeNull() const { return value_can_be_null_; }
4454 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004455
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004456 DECLARE_INSTRUCTION(InstanceFieldSet);
4457
4458 private:
4459 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004460 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004461
4462 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4463};
4464
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004465class HArrayGet : public HExpression<2> {
4466 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004467 HArrayGet(HInstruction* array,
4468 HInstruction* index,
4469 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004470 uint32_t dex_pc,
4471 SideEffects additional_side_effects = SideEffects::None())
4472 : HExpression(type,
4473 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
4474 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004475 SetRawInputAt(0, array);
4476 SetRawInputAt(1, index);
4477 }
4478
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004479 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004480 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004481 return true;
4482 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004483 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004484 // TODO: We can be smarter here.
4485 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4486 // which generates the implicit null check. There are cases when these can be removed
4487 // to produce better code. If we ever add optimizations to do so we should allow an
4488 // implicit check here (as long as the address falls in the first page).
4489 return false;
4490 }
4491
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004492 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004493
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004494 HInstruction* GetArray() const { return InputAt(0); }
4495 HInstruction* GetIndex() const { return InputAt(1); }
4496
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004497 DECLARE_INSTRUCTION(ArrayGet);
4498
4499 private:
4500 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4501};
4502
4503class HArraySet : public HTemplateInstruction<3> {
4504 public:
4505 HArraySet(HInstruction* array,
4506 HInstruction* index,
4507 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004508 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004509 uint32_t dex_pc,
4510 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004511 : HTemplateInstruction(
4512 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004513 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4514 additional_side_effects),
4515 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004516 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004517 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004518 value_can_be_null_(true),
4519 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004520 SetRawInputAt(0, array);
4521 SetRawInputAt(1, index);
4522 SetRawInputAt(2, value);
4523 }
4524
Calin Juravle77520bc2015-01-12 18:45:46 +00004525 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004526 // We currently always call a runtime method to catch array store
4527 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004528 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004529 }
4530
Mingyao Yang81014cb2015-06-02 03:16:27 -07004531 // Can throw ArrayStoreException.
4532 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4533
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004534 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004535 // TODO: Same as for ArrayGet.
4536 return false;
4537 }
4538
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004539 void ClearNeedsTypeCheck() {
4540 needs_type_check_ = false;
4541 }
4542
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004543 void ClearValueCanBeNull() {
4544 value_can_be_null_ = false;
4545 }
4546
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004547 void SetStaticTypeOfArrayIsObjectArray() {
4548 static_type_of_array_is_object_array_ = true;
4549 }
4550
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004551 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004552 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004553 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004554
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004555 HInstruction* GetArray() const { return InputAt(0); }
4556 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004557 HInstruction* GetValue() const { return InputAt(2); }
4558
4559 Primitive::Type GetComponentType() const {
4560 // The Dex format does not type floating point index operations. Since the
4561 // `expected_component_type_` is set during building and can therefore not
4562 // be correct, we also check what is the value type. If it is a floating
4563 // point type, we must use that type.
4564 Primitive::Type value_type = GetValue()->GetType();
4565 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4566 ? value_type
4567 : expected_component_type_;
4568 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004569
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004570 Primitive::Type GetRawExpectedComponentType() const {
4571 return expected_component_type_;
4572 }
4573
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004574 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4575 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4576 }
4577
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004578 DECLARE_INSTRUCTION(ArraySet);
4579
4580 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004581 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004582 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004583 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004584 // Cached information for the reference_type_info_ so that codegen
4585 // does not need to inspect the static type.
4586 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004587
4588 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4589};
4590
4591class HArrayLength : public HExpression<1> {
4592 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004593 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004594 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004595 // Note that arrays do not change length, so the instruction does not
4596 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597 SetRawInputAt(0, array);
4598 }
4599
Calin Juravle77520bc2015-01-12 18:45:46 +00004600 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004601 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004602 return true;
4603 }
Calin Juravle641547a2015-04-21 22:08:51 +01004604 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4605 return obj == InputAt(0);
4606 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004607
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004608 DECLARE_INSTRUCTION(ArrayLength);
4609
4610 private:
4611 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4612};
4613
4614class HBoundsCheck : public HExpression<2> {
4615 public:
4616 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004617 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004618 DCHECK(index->GetType() == Primitive::kPrimInt);
4619 SetRawInputAt(0, index);
4620 SetRawInputAt(1, length);
4621 }
4622
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004623 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004624 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004625 return true;
4626 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004627
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004628 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004629
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004630 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004631
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004632 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004633
4634 DECLARE_INSTRUCTION(BoundsCheck);
4635
4636 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004637 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4638};
4639
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004640/**
4641 * Some DEX instructions are folded into multiple HInstructions that need
4642 * to stay live until the last HInstruction. This class
4643 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004644 * HInstruction stays live. `index` represents the stack location index of the
4645 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004646 */
4647class HTemporary : public HTemplateInstruction<0> {
4648 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004649 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4650 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004651
4652 size_t GetIndex() const { return index_; }
4653
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004654 Primitive::Type GetType() const OVERRIDE {
4655 // The previous instruction is the one that will be stored in the temporary location.
4656 DCHECK(GetPrevious() != nullptr);
4657 return GetPrevious()->GetType();
4658 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004659
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004660 DECLARE_INSTRUCTION(Temporary);
4661
4662 private:
4663 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004664 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4665};
4666
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004667class HSuspendCheck : public HTemplateInstruction<0> {
4668 public:
4669 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004670 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004671
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004672 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004673 return true;
4674 }
4675
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004676 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4677 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004678
4679 DECLARE_INSTRUCTION(SuspendCheck);
4680
4681 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004682 // Only used for code generation, in order to share the same slow path between back edges
4683 // of a same loop.
4684 SlowPathCode* slow_path_;
4685
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004686 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4687};
4688
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004689/**
4690 * Instruction to load a Class object.
4691 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004692class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004693 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004694 HLoadClass(HCurrentMethod* current_method,
4695 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004696 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004697 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004698 uint32_t dex_pc,
4699 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004700 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004701 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004702 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004703 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004704 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004705 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004706 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004707 // Referrers class should not need access check. We never inline unverified
4708 // methods so we can't possibly end up in this situation.
4709 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004710 SetRawInputAt(0, current_method);
4711 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004712
4713 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004714
4715 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004716 // Note that we don't need to test for generate_clinit_check_.
4717 // Whether or not we need to generate the clinit check is processed in
4718 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004719 return other->AsLoadClass()->type_index_ == type_index_ &&
4720 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004721 }
4722
4723 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4724
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004725 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004726 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004727 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004728
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004729 bool NeedsEnvironment() const OVERRIDE {
4730 // Will call runtime and load the class if the class is not loaded yet.
4731 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004732 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004733 }
4734
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004735 bool MustGenerateClinitCheck() const {
4736 return generate_clinit_check_;
4737 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004738 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4739 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004740 }
4741
4742 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004743 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4744 }
4745
4746 bool NeedsAccessCheck() const {
4747 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004748 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004749
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004750 bool CanThrow() const OVERRIDE {
4751 // May call runtime and and therefore can throw.
4752 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004753 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004754 }
4755
Calin Juravleacf735c2015-02-12 15:25:22 +00004756 ReferenceTypeInfo GetLoadedClassRTI() {
4757 return loaded_class_rti_;
4758 }
4759
4760 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4761 // Make sure we only set exact types (the loaded class should never be merged).
4762 DCHECK(rti.IsExact());
4763 loaded_class_rti_ = rti;
4764 }
4765
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004766 const DexFile& GetDexFile() { return dex_file_; }
4767
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004768 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4769
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004770 static SideEffects SideEffectsForArchRuntimeCalls() {
4771 return SideEffects::CanTriggerGC();
4772 }
4773
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004774 DECLARE_INSTRUCTION(LoadClass);
4775
4776 private:
4777 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004778 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004779 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004780 // Whether this instruction must generate the initialization check.
4781 // Used for code generation.
4782 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004783 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004784
Calin Juravleacf735c2015-02-12 15:25:22 +00004785 ReferenceTypeInfo loaded_class_rti_;
4786
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004787 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4788};
4789
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004790class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004791 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004792 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004793 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4794 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004795 SetRawInputAt(0, current_method);
4796 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004797
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004798 bool CanBeMoved() const OVERRIDE { return true; }
4799
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004800 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4801 return other->AsLoadString()->string_index_ == string_index_;
4802 }
4803
4804 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4805
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004806 uint32_t GetStringIndex() const { return string_index_; }
4807
4808 // TODO: Can we deopt or debug when we resolve a string?
4809 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004810 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004811 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004812
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004813 static SideEffects SideEffectsForArchRuntimeCalls() {
4814 return SideEffects::CanTriggerGC();
4815 }
4816
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004817 DECLARE_INSTRUCTION(LoadString);
4818
4819 private:
4820 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004821
4822 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4823};
4824
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004825/**
4826 * Performs an initialization check on its Class object input.
4827 */
4828class HClinitCheck : public HExpression<1> {
4829 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004830 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004831 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004832 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004833 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4834 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004835 SetRawInputAt(0, constant);
4836 }
4837
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004838 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004839 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004840 return true;
4841 }
4842
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004843 bool NeedsEnvironment() const OVERRIDE {
4844 // May call runtime to initialize the class.
4845 return true;
4846 }
4847
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004848
4849 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4850
4851 DECLARE_INSTRUCTION(ClinitCheck);
4852
4853 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004854 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4855};
4856
4857class HStaticFieldGet : public HExpression<1> {
4858 public:
4859 HStaticFieldGet(HInstruction* cls,
4860 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004861 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004862 bool is_volatile,
4863 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004864 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004865 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004866 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004867 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004868 : HExpression(field_type,
4869 SideEffects::FieldReadOfType(field_type, is_volatile),
4870 dex_pc),
4871 field_info_(field_offset,
4872 field_type,
4873 is_volatile,
4874 field_idx,
4875 declaring_class_def_index,
4876 dex_file,
4877 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004878 SetRawInputAt(0, cls);
4879 }
4880
Calin Juravle52c48962014-12-16 17:02:57 +00004881
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004882 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004883
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004884 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004885 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4886 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004887 }
4888
4889 size_t ComputeHashCode() const OVERRIDE {
4890 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4891 }
4892
Calin Juravle52c48962014-12-16 17:02:57 +00004893 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004894 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4895 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004896 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004897
4898 DECLARE_INSTRUCTION(StaticFieldGet);
4899
4900 private:
4901 const FieldInfo field_info_;
4902
4903 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4904};
4905
4906class HStaticFieldSet : public HTemplateInstruction<2> {
4907 public:
4908 HStaticFieldSet(HInstruction* cls,
4909 HInstruction* value,
4910 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004911 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004912 bool is_volatile,
4913 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004914 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004915 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004916 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004917 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004918 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4919 dex_pc),
4920 field_info_(field_offset,
4921 field_type,
4922 is_volatile,
4923 field_idx,
4924 declaring_class_def_index,
4925 dex_file,
4926 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004927 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004928 SetRawInputAt(0, cls);
4929 SetRawInputAt(1, value);
4930 }
4931
Calin Juravle52c48962014-12-16 17:02:57 +00004932 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004933 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4934 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004935 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004936
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004937 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004938 bool GetValueCanBeNull() const { return value_can_be_null_; }
4939 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004940
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004941 DECLARE_INSTRUCTION(StaticFieldSet);
4942
4943 private:
4944 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004945 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004946
4947 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4948};
4949
Calin Juravlee460d1d2015-09-29 04:52:17 +01004950class HUnresolvedInstanceFieldGet : public HExpression<1> {
4951 public:
4952 HUnresolvedInstanceFieldGet(HInstruction* obj,
4953 Primitive::Type field_type,
4954 uint32_t field_index,
4955 uint32_t dex_pc)
4956 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4957 field_index_(field_index) {
4958 SetRawInputAt(0, obj);
4959 }
4960
4961 bool NeedsEnvironment() const OVERRIDE { return true; }
4962 bool CanThrow() const OVERRIDE { return true; }
4963
4964 Primitive::Type GetFieldType() const { return GetType(); }
4965 uint32_t GetFieldIndex() const { return field_index_; }
4966
4967 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4968
4969 private:
4970 const uint32_t field_index_;
4971
4972 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4973};
4974
4975class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4976 public:
4977 HUnresolvedInstanceFieldSet(HInstruction* obj,
4978 HInstruction* value,
4979 Primitive::Type field_type,
4980 uint32_t field_index,
4981 uint32_t dex_pc)
4982 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4983 field_type_(field_type),
4984 field_index_(field_index) {
4985 DCHECK_EQ(field_type, value->GetType());
4986 SetRawInputAt(0, obj);
4987 SetRawInputAt(1, value);
4988 }
4989
4990 bool NeedsEnvironment() const OVERRIDE { return true; }
4991 bool CanThrow() const OVERRIDE { return true; }
4992
4993 Primitive::Type GetFieldType() const { return field_type_; }
4994 uint32_t GetFieldIndex() const { return field_index_; }
4995
4996 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
4997
4998 private:
4999 const Primitive::Type field_type_;
5000 const uint32_t field_index_;
5001
5002 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5003};
5004
5005class HUnresolvedStaticFieldGet : public HExpression<0> {
5006 public:
5007 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5008 uint32_t field_index,
5009 uint32_t dex_pc)
5010 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5011 field_index_(field_index) {
5012 }
5013
5014 bool NeedsEnvironment() const OVERRIDE { return true; }
5015 bool CanThrow() const OVERRIDE { return true; }
5016
5017 Primitive::Type GetFieldType() const { return GetType(); }
5018 uint32_t GetFieldIndex() const { return field_index_; }
5019
5020 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5021
5022 private:
5023 const uint32_t field_index_;
5024
5025 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5026};
5027
5028class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5029 public:
5030 HUnresolvedStaticFieldSet(HInstruction* value,
5031 Primitive::Type field_type,
5032 uint32_t field_index,
5033 uint32_t dex_pc)
5034 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5035 field_type_(field_type),
5036 field_index_(field_index) {
5037 DCHECK_EQ(field_type, value->GetType());
5038 SetRawInputAt(0, value);
5039 }
5040
5041 bool NeedsEnvironment() const OVERRIDE { return true; }
5042 bool CanThrow() const OVERRIDE { return true; }
5043
5044 Primitive::Type GetFieldType() const { return field_type_; }
5045 uint32_t GetFieldIndex() const { return field_index_; }
5046
5047 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5048
5049 private:
5050 const Primitive::Type field_type_;
5051 const uint32_t field_index_;
5052
5053 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5054};
5055
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005056// Implement the move-exception DEX instruction.
5057class HLoadException : public HExpression<0> {
5058 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005059 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5060 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005061
David Brazdilbbd733e2015-08-18 17:48:17 +01005062 bool CanBeNull() const OVERRIDE { return false; }
5063
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005064 DECLARE_INSTRUCTION(LoadException);
5065
5066 private:
5067 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5068};
5069
David Brazdilcb1c0552015-08-04 16:22:25 +01005070// Implicit part of move-exception which clears thread-local exception storage.
5071// Must not be removed because the runtime expects the TLS to get cleared.
5072class HClearException : public HTemplateInstruction<0> {
5073 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005074 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5075 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005076
5077 DECLARE_INSTRUCTION(ClearException);
5078
5079 private:
5080 DISALLOW_COPY_AND_ASSIGN(HClearException);
5081};
5082
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005083class HThrow : public HTemplateInstruction<1> {
5084 public:
5085 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005086 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005087 SetRawInputAt(0, exception);
5088 }
5089
5090 bool IsControlFlow() const OVERRIDE { return true; }
5091
5092 bool NeedsEnvironment() const OVERRIDE { return true; }
5093
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005094 bool CanThrow() const OVERRIDE { return true; }
5095
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005096
5097 DECLARE_INSTRUCTION(Throw);
5098
5099 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005100 DISALLOW_COPY_AND_ASSIGN(HThrow);
5101};
5102
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005103/**
5104 * Implementation strategies for the code generator of a HInstanceOf
5105 * or `HCheckCast`.
5106 */
5107enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005108 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005109 kExactCheck, // Can do a single class compare.
5110 kClassHierarchyCheck, // Can just walk the super class chain.
5111 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5112 kInterfaceCheck, // No optimization yet when checking against an interface.
5113 kArrayObjectCheck, // Can just check if the array is not primitive.
5114 kArrayCheck // No optimization yet when checking against a generic array.
5115};
5116
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005117class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005118 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005119 HInstanceOf(HInstruction* object,
5120 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005121 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005122 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005123 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005124 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005125 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005126 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005127 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005128 SetRawInputAt(0, object);
5129 SetRawInputAt(1, constant);
5130 }
5131
5132 bool CanBeMoved() const OVERRIDE { return true; }
5133
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005134 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005135 return true;
5136 }
5137
5138 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005139 return false;
5140 }
5141
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005142 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5143
5144 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005145
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005146 // Used only in code generation.
5147 bool MustDoNullCheck() const { return must_do_null_check_; }
5148 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5149
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005150 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5151 return (check_kind == TypeCheckKind::kExactCheck)
5152 ? SideEffects::None()
5153 // Mips currently does runtime calls for any other checks.
5154 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005155 }
5156
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005157 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005158
5159 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005160 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005161 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005162
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005163 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5164};
5165
Calin Juravleb1498f62015-02-16 13:13:29 +00005166class HBoundType : public HExpression<1> {
5167 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005168 // Constructs an HBoundType with the given upper_bound.
5169 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005170 HBoundType(HInstruction* input,
5171 ReferenceTypeInfo upper_bound,
5172 bool upper_can_be_null,
5173 uint32_t dex_pc = kNoDexPc)
5174 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005175 upper_bound_(upper_bound),
5176 upper_can_be_null_(upper_can_be_null),
5177 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005178 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005179 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005180 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005181 }
5182
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005183 // GetUpper* should only be used in reference type propagation.
5184 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5185 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005186
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005187 void SetCanBeNull(bool can_be_null) {
5188 DCHECK(upper_can_be_null_ || !can_be_null);
5189 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005190 }
5191
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005192 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5193
Calin Juravleb1498f62015-02-16 13:13:29 +00005194 DECLARE_INSTRUCTION(BoundType);
5195
5196 private:
5197 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005198 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5199 // It is used to bound the type in cases like:
5200 // if (x instanceof ClassX) {
5201 // // uper_bound_ will be ClassX
5202 // }
5203 const ReferenceTypeInfo upper_bound_;
5204 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5205 // is false then can_be_null_ cannot be true).
5206 const bool upper_can_be_null_;
5207 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005208
5209 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5210};
5211
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005212class HCheckCast : public HTemplateInstruction<2> {
5213 public:
5214 HCheckCast(HInstruction* object,
5215 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005216 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005217 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005218 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005219 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005220 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005221 SetRawInputAt(0, object);
5222 SetRawInputAt(1, constant);
5223 }
5224
5225 bool CanBeMoved() const OVERRIDE { return true; }
5226
5227 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5228 return true;
5229 }
5230
5231 bool NeedsEnvironment() const OVERRIDE {
5232 // Instruction may throw a CheckCastError.
5233 return true;
5234 }
5235
5236 bool CanThrow() const OVERRIDE { return true; }
5237
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005238 bool MustDoNullCheck() const { return must_do_null_check_; }
5239 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005240 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005241
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005242 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005243
5244 DECLARE_INSTRUCTION(CheckCast);
5245
5246 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005247 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005248 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005249
5250 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005251};
5252
Calin Juravle27df7582015-04-17 19:12:31 +01005253class HMemoryBarrier : public HTemplateInstruction<0> {
5254 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005255 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005256 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005257 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005258 barrier_kind_(barrier_kind) {}
5259
5260 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5261
5262 DECLARE_INSTRUCTION(MemoryBarrier);
5263
5264 private:
5265 const MemBarrierKind barrier_kind_;
5266
5267 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5268};
5269
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005270class HMonitorOperation : public HTemplateInstruction<1> {
5271 public:
5272 enum OperationKind {
5273 kEnter,
5274 kExit,
5275 };
5276
5277 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005278 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005279 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5280 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005281 SetRawInputAt(0, object);
5282 }
5283
5284 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005285 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5286
5287 bool CanThrow() const OVERRIDE {
5288 // Verifier guarantees that monitor-exit cannot throw.
5289 // This is important because it allows the HGraphBuilder to remove
5290 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5291 return IsEnter();
5292 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005293
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005294
5295 bool IsEnter() const { return kind_ == kEnter; }
5296
5297 DECLARE_INSTRUCTION(MonitorOperation);
5298
Calin Juravle52c48962014-12-16 17:02:57 +00005299 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005300 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005301
5302 private:
5303 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5304};
5305
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005306/**
5307 * A HInstruction used as a marker for the replacement of new + <init>
5308 * of a String to a call to a StringFactory. Only baseline will see
5309 * the node at code generation, where it will be be treated as null.
5310 * When compiling non-baseline, `HFakeString` instructions are being removed
5311 * in the instruction simplifier.
5312 */
5313class HFakeString : public HTemplateInstruction<0> {
5314 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005315 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5316 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005317
5318 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5319
5320 DECLARE_INSTRUCTION(FakeString);
5321
5322 private:
5323 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5324};
5325
Vladimir Markof9f64412015-09-02 14:05:49 +01005326class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005327 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005328 MoveOperands(Location source,
5329 Location destination,
5330 Primitive::Type type,
5331 HInstruction* instruction)
5332 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005333
5334 Location GetSource() const { return source_; }
5335 Location GetDestination() const { return destination_; }
5336
5337 void SetSource(Location value) { source_ = value; }
5338 void SetDestination(Location value) { destination_ = value; }
5339
5340 // The parallel move resolver marks moves as "in-progress" by clearing the
5341 // destination (but not the source).
5342 Location MarkPending() {
5343 DCHECK(!IsPending());
5344 Location dest = destination_;
5345 destination_ = Location::NoLocation();
5346 return dest;
5347 }
5348
5349 void ClearPending(Location dest) {
5350 DCHECK(IsPending());
5351 destination_ = dest;
5352 }
5353
5354 bool IsPending() const {
5355 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5356 return destination_.IsInvalid() && !source_.IsInvalid();
5357 }
5358
5359 // True if this blocks a move from the given location.
5360 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005361 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005362 }
5363
5364 // A move is redundant if it's been eliminated, if its source and
5365 // destination are the same, or if its destination is unneeded.
5366 bool IsRedundant() const {
5367 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5368 }
5369
5370 // We clear both operands to indicate move that's been eliminated.
5371 void Eliminate() {
5372 source_ = destination_ = Location::NoLocation();
5373 }
5374
5375 bool IsEliminated() const {
5376 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5377 return source_.IsInvalid();
5378 }
5379
Alexey Frunze4dda3372015-06-01 18:31:49 -07005380 Primitive::Type GetType() const { return type_; }
5381
Nicolas Geoffray90218252015-04-15 11:56:51 +01005382 bool Is64BitMove() const {
5383 return Primitive::Is64BitType(type_);
5384 }
5385
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005386 HInstruction* GetInstruction() const { return instruction_; }
5387
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005388 private:
5389 Location source_;
5390 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005391 // The type this move is for.
5392 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005393 // The instruction this move is assocatied with. Null when this move is
5394 // for moving an input in the expected locations of user (including a phi user).
5395 // This is only used in debug mode, to ensure we do not connect interval siblings
5396 // in the same parallel move.
5397 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005398};
5399
5400static constexpr size_t kDefaultNumberOfMoves = 4;
5401
5402class HParallelMove : public HTemplateInstruction<0> {
5403 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005404 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005405 : HTemplateInstruction(SideEffects::None(), dex_pc),
5406 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5407 moves_.reserve(kDefaultNumberOfMoves);
5408 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005409
Nicolas Geoffray90218252015-04-15 11:56:51 +01005410 void AddMove(Location source,
5411 Location destination,
5412 Primitive::Type type,
5413 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005414 DCHECK(source.IsValid());
5415 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005416 if (kIsDebugBuild) {
5417 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005418 for (const MoveOperands& move : moves_) {
5419 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005420 // Special case the situation where the move is for the spill slot
5421 // of the instruction.
5422 if ((GetPrevious() == instruction)
5423 || ((GetPrevious() == nullptr)
5424 && instruction->IsPhi()
5425 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005426 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005427 << "Doing parallel moves for the same instruction.";
5428 } else {
5429 DCHECK(false) << "Doing parallel moves for the same instruction.";
5430 }
5431 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005432 }
5433 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005434 for (const MoveOperands& move : moves_) {
5435 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005436 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005437 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005438 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005439 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005440 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005441 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005442 }
5443
Vladimir Marko225b6462015-09-28 12:17:40 +01005444 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005445 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005446 }
5447
Vladimir Marko225b6462015-09-28 12:17:40 +01005448 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005449
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005450 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005451
5452 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005453 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005454
5455 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5456};
5457
Mark Mendell0616ae02015-04-17 12:49:27 -04005458} // namespace art
5459
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005460#ifdef ART_ENABLE_CODEGEN_arm64
5461#include "nodes_arm64.h"
5462#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005463#ifdef ART_ENABLE_CODEGEN_x86
5464#include "nodes_x86.h"
5465#endif
5466
5467namespace art {
5468
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005469class HGraphVisitor : public ValueObject {
5470 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005471 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5472 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005473
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005474 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005475 virtual void VisitBasicBlock(HBasicBlock* block);
5476
Roland Levillain633021e2014-10-01 14:12:25 +01005477 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005478 void VisitInsertionOrder();
5479
Roland Levillain633021e2014-10-01 14:12:25 +01005480 // Visit the graph following dominator tree reverse post-order.
5481 void VisitReversePostOrder();
5482
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005483 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005484
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005485 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005486#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005487 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5488
5489 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5490
5491#undef DECLARE_VISIT_INSTRUCTION
5492
5493 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005494 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005495
5496 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5497};
5498
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005499class HGraphDelegateVisitor : public HGraphVisitor {
5500 public:
5501 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5502 virtual ~HGraphDelegateVisitor() {}
5503
5504 // Visit functions that delegate to to super class.
5505#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005506 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005507
5508 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5509
5510#undef DECLARE_VISIT_INSTRUCTION
5511
5512 private:
5513 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5514};
5515
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005516class HInsertionOrderIterator : public ValueObject {
5517 public:
5518 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5519
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005520 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005521 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005522 void Advance() { ++index_; }
5523
5524 private:
5525 const HGraph& graph_;
5526 size_t index_;
5527
5528 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5529};
5530
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005531class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005532 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005533 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5534 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005535 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005536 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005537
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005538 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5539 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005540 void Advance() { ++index_; }
5541
5542 private:
5543 const HGraph& graph_;
5544 size_t index_;
5545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005546 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005547};
5548
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005549class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005550 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005551 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005552 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005553 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005554 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005555 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005556
5557 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005558 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005559 void Advance() { --index_; }
5560
5561 private:
5562 const HGraph& graph_;
5563 size_t index_;
5564
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005565 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005566};
5567
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005568class HLinearPostOrderIterator : public ValueObject {
5569 public:
5570 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005571 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005572
5573 bool Done() const { return index_ == 0; }
5574
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005575 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005576
5577 void Advance() {
5578 --index_;
5579 DCHECK_GE(index_, 0U);
5580 }
5581
5582 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005583 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005584 size_t index_;
5585
5586 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5587};
5588
5589class HLinearOrderIterator : public ValueObject {
5590 public:
5591 explicit HLinearOrderIterator(const HGraph& graph)
5592 : order_(graph.GetLinearOrder()), index_(0) {}
5593
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005594 bool Done() const { return index_ == order_.size(); }
5595 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005596 void Advance() { ++index_; }
5597
5598 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005599 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005600 size_t index_;
5601
5602 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5603};
5604
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005605// Iterator over the blocks that art part of the loop. Includes blocks part
5606// of an inner loop. The order in which the blocks are iterated is on their
5607// block id.
5608class HBlocksInLoopIterator : public ValueObject {
5609 public:
5610 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5611 : blocks_in_loop_(info.GetBlocks()),
5612 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5613 index_(0) {
5614 if (!blocks_in_loop_.IsBitSet(index_)) {
5615 Advance();
5616 }
5617 }
5618
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005619 bool Done() const { return index_ == blocks_.size(); }
5620 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005621 void Advance() {
5622 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005623 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005624 if (blocks_in_loop_.IsBitSet(index_)) {
5625 break;
5626 }
5627 }
5628 }
5629
5630 private:
5631 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005632 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005633 size_t index_;
5634
5635 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5636};
5637
Mingyao Yang3584bce2015-05-19 16:01:59 -07005638// Iterator over the blocks that art part of the loop. Includes blocks part
5639// of an inner loop. The order in which the blocks are iterated is reverse
5640// post order.
5641class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5642 public:
5643 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5644 : blocks_in_loop_(info.GetBlocks()),
5645 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5646 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005647 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005648 Advance();
5649 }
5650 }
5651
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005652 bool Done() const { return index_ == blocks_.size(); }
5653 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005654 void Advance() {
5655 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005656 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5657 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005658 break;
5659 }
5660 }
5661 }
5662
5663 private:
5664 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005665 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005666 size_t index_;
5667
5668 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5669};
5670
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005671inline int64_t Int64FromConstant(HConstant* constant) {
5672 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5673 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5674 : constant->AsLongConstant()->GetValue();
5675}
5676
Vladimir Marko58155012015-08-19 12:49:41 +00005677inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5678 // For the purposes of the compiler, the dex files must actually be the same object
5679 // if we want to safely treat them as the same. This is especially important for JIT
5680 // as custom class loaders can open the same underlying file (or memory) multiple
5681 // times and provide different class resolution but no two class loaders should ever
5682 // use the same DexFile object - doing so is an unsupported hack that can lead to
5683 // all sorts of weird failures.
5684 return &lhs == &rhs;
5685}
5686
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005687} // namespace art
5688
5689#endif // ART_COMPILER_OPTIMIZING_NODES_H_