blob: 2426f8b08d3cf7e9f0947bf6f1d7e3b7e260ac0b [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);
78
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010079static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
80
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060081static constexpr uint32_t kNoDexPc = -1;
82
Dave Allison20dfc792014-06-16 20:44:29 -070083enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070084 // All types.
85 kCondEQ, // ==
86 kCondNE, // !=
87 // Signed integers and floating-point numbers.
88 kCondLT, // <
89 kCondLE, // <=
90 kCondGT, // >
91 kCondGE, // >=
92 // Unsigned integers.
93 kCondB, // <
94 kCondBE, // <=
95 kCondA, // >
96 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070097};
98
Vladimir Markof9f64412015-09-02 14:05:49 +010099class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100100 public:
101 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
102
103 void AddInstruction(HInstruction* instruction);
104 void RemoveInstruction(HInstruction* instruction);
105
David Brazdilc3d743f2015-04-22 13:40:50 +0100106 // Insert `instruction` before/after an existing instruction `cursor`.
107 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
108 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
109
Roland Levillain6b469232014-09-25 10:10:38 +0100110 // Return true if this list contains `instruction`.
111 bool Contains(HInstruction* instruction) const;
112
Roland Levillainccc07a92014-09-16 14:48:16 +0100113 // Return true if `instruction1` is found before `instruction2` in
114 // this instruction list and false otherwise. Abort if none
115 // of these instructions is found.
116 bool FoundBefore(const HInstruction* instruction1,
117 const HInstruction* instruction2) const;
118
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000119 bool IsEmpty() const { return first_instruction_ == nullptr; }
120 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
121
122 // Update the block of all instructions to be `block`.
123 void SetBlockOfInstructions(HBasicBlock* block) const;
124
125 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
126 void Add(const HInstructionList& instruction_list);
127
David Brazdil2d7352b2015-04-20 14:52:42 +0100128 // Return the number of instructions in the list. This is an expensive operation.
129 size_t CountSize() const;
130
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100131 private:
132 HInstruction* first_instruction_;
133 HInstruction* last_instruction_;
134
135 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000136 friend class HGraph;
137 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100139 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140
141 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
142};
143
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000144// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100145class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000146 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100147 HGraph(ArenaAllocator* arena,
148 const DexFile& dex_file,
149 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100150 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100152 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100153 bool debuggable = false,
154 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000155 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100156 blocks_(arena->Adapter(kArenaAllocBlockList)),
157 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
158 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700159 entry_block_(nullptr),
160 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100161 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100162 number_of_vregs_(0),
163 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000164 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400165 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000166 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000167 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000168 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100169 dex_file_(dex_file),
170 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100171 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100172 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100173 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000175 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100176 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
177 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
178 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
179 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
180 cached_current_method_(nullptr) {
181 blocks_.reserve(kDefaultNumberOfBlocks);
182 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000183
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000184 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100185 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
186
David Brazdil69ba7b72015-06-23 18:27:30 +0100187 bool IsInSsaForm() const { return in_ssa_form_; }
188
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000189 HBasicBlock* GetEntryBlock() const { return entry_block_; }
190 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100191 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000192
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000193 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
194 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000195
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000196 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100197
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000198 // Try building the SSA form of this graph, with dominance computation and loop
199 // recognition. Returns whether it was successful in doing all these steps.
200 bool TryBuildingSsa() {
201 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000202 // The SSA builder requires loops to all be natural. Specifically, the dead phi
203 // elimination phase checks the consistency of the graph when doing a post-order
204 // visit for eliminating dead phis: a dead phi can only have loop header phi
205 // users remaining when being visited.
206 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100207 // Precompute per-block try membership before entering the SSA builder,
208 // which needs the information to build catch block phis from values of
209 // locals at throwing instructions inside try blocks.
210 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000211 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100212 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000213 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000214 }
215
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100216 void ComputeDominanceInformation();
217 void ClearDominanceInformation();
218
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000220 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100221 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100222 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000223
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000224 // Analyze all natural loops in this graph. Returns false if one
225 // loop is not natural, that is the header does not dominate the
226 // back edge.
227 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100228
David Brazdilffee3d32015-07-06 11:48:53 +0100229 // Iterate over blocks to compute try block membership. Needs reverse post
230 // order and loop information.
231 void ComputeTryBlockInformation();
232
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000233 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000234 // Returns the instruction used to replace the invoke expression or null if the
235 // invoke is for a void method.
236 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000237
Mingyao Yang3584bce2015-05-19 16:01:59 -0700238 // Need to add a couple of blocks to test if the loop body is entered and
239 // put deoptimization instructions, etc.
240 void TransformLoopHeaderForBCE(HBasicBlock* header);
241
David Brazdil2d7352b2015-04-20 14:52:42 +0100242 // Removes `block` from the graph.
243 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000244
David Brazdilfc6a86a2015-06-26 10:33:45 +0000245 // Splits the edge between `block` and `successor` while preserving the
246 // indices in the predecessor/successor lists. If there are multiple edges
247 // between the blocks, the lowest indices are used.
248 // Returns the new block which is empty and has the same dex pc as `successor`.
249 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
250
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100251 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
252 void SimplifyLoop(HBasicBlock* header);
253
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000254 int32_t GetNextInstructionId() {
255 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000256 return current_instruction_id_++;
257 }
258
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000259 int32_t GetCurrentInstructionId() const {
260 return current_instruction_id_;
261 }
262
263 void SetCurrentInstructionId(int32_t id) {
264 current_instruction_id_ = id;
265 }
266
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100267 uint16_t GetMaximumNumberOfOutVRegs() const {
268 return maximum_number_of_out_vregs_;
269 }
270
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000271 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
272 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100273 }
274
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100275 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
276 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
277 }
278
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000279 void UpdateTemporariesVRegSlots(size_t slots) {
280 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100281 }
282
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000283 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100284 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000285 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100286 }
287
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100288 void SetNumberOfVRegs(uint16_t number_of_vregs) {
289 number_of_vregs_ = number_of_vregs;
290 }
291
292 uint16_t GetNumberOfVRegs() const {
293 return number_of_vregs_;
294 }
295
296 void SetNumberOfInVRegs(uint16_t value) {
297 number_of_in_vregs_ = value;
298 }
299
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100300 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100301 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100302 return number_of_vregs_ - number_of_in_vregs_;
303 }
304
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100305 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100306 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100307 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100308
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100309 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100310 return linear_order_;
311 }
312
Mark Mendell1152c922015-04-24 17:06:35 -0400313 bool HasBoundsChecks() const {
314 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800315 }
316
Mark Mendell1152c922015-04-24 17:06:35 -0400317 void SetHasBoundsChecks(bool value) {
318 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800319 }
320
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100321 bool ShouldGenerateConstructorBarrier() const {
322 return should_generate_constructor_barrier_;
323 }
324
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000325 bool IsDebuggable() const { return debuggable_; }
326
David Brazdil8d5b8b22015-03-24 10:51:52 +0000327 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000328 // already, it is created and inserted into the graph. This method is only for
329 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600330 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000331
332 // TODO: This is problematic for the consistency of reference type propagation
333 // because it can be created anytime after the pass and thus it will be left
334 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600335 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000336
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600337 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
338 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000339 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600340 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
341 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600343 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
344 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000345 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600346 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
347 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000348 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000349
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100350 HCurrentMethod* GetCurrentMethod();
351
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000352 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100353
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100354 const DexFile& GetDexFile() const {
355 return dex_file_;
356 }
357
358 uint32_t GetMethodIdx() const {
359 return method_idx_;
360 }
361
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100362 InvokeType GetInvokeType() const {
363 return invoke_type_;
364 }
365
Mark Mendellc4701932015-04-10 13:18:51 -0400366 InstructionSet GetInstructionSet() const {
367 return instruction_set_;
368 }
369
David Brazdil77a48ae2015-09-15 12:34:04 +0000370 bool HasTryCatch() const { return has_try_catch_; }
371 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100372
David Brazdil2d7352b2015-04-20 14:52:42 +0100373 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100374 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000375 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100376 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000377
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000378 template <class InstructionType, typename ValueType>
379 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600380 ArenaSafeMap<ValueType, InstructionType*>* cache,
381 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000382 // Try to find an existing constant of the given value.
383 InstructionType* constant = nullptr;
384 auto cached_constant = cache->find(value);
385 if (cached_constant != cache->end()) {
386 constant = cached_constant->second;
387 }
388
389 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100390 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000391 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600392 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000393 cache->Overwrite(value, constant);
394 InsertConstant(constant);
395 }
396 return constant;
397 }
398
David Brazdil8d5b8b22015-03-24 10:51:52 +0000399 void InsertConstant(HConstant* instruction);
400
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000401 // Cache a float constant into the graph. This method should only be
402 // called by the SsaBuilder when creating "equivalent" instructions.
403 void CacheFloatConstant(HFloatConstant* constant);
404
405 // See CacheFloatConstant comment.
406 void CacheDoubleConstant(HDoubleConstant* constant);
407
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000408 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000409
410 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100411 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000412
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100413 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100414 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000415
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100416 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100417 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100418
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000419 HBasicBlock* entry_block_;
420 HBasicBlock* exit_block_;
421
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100422 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100423 uint16_t maximum_number_of_out_vregs_;
424
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100425 // The number of virtual registers in this method. Contains the parameters.
426 uint16_t number_of_vregs_;
427
428 // The number of virtual registers used by parameters of this method.
429 uint16_t number_of_in_vregs_;
430
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000431 // Number of vreg size slots that the temporaries use (used in baseline compiler).
432 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100433
Mark Mendell1152c922015-04-24 17:06:35 -0400434 // Has bounds checks. We can totally skip BCE if it's false.
435 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800436
David Brazdil77a48ae2015-09-15 12:34:04 +0000437 // Flag whether there are any try/catch blocks in the graph. We will skip
438 // try/catch-related passes if false.
439 bool has_try_catch_;
440
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000441 // Indicates whether the graph should be compiled in a way that
442 // ensures full debuggability. If false, we can apply more
443 // aggressive optimizations that may limit the level of debugging.
444 const bool debuggable_;
445
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000446 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000447 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000448
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100449 // The dex file from which the method is from.
450 const DexFile& dex_file_;
451
452 // The method index in the dex file.
453 const uint32_t method_idx_;
454
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100455 // If inlined, this encodes how the callee is being invoked.
456 const InvokeType invoke_type_;
457
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100458 // Whether the graph has been transformed to SSA form. Only used
459 // in debug mode to ensure we are not using properties only valid
460 // for non-SSA form (like the number of temporaries).
461 bool in_ssa_form_;
462
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100463 const bool should_generate_constructor_barrier_;
464
Mathieu Chartiere401d142015-04-22 13:56:20 -0700465 const InstructionSet instruction_set_;
466
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000467 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000468 HNullConstant* cached_null_constant_;
469 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000470 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000471 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000472 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000473
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100474 HCurrentMethod* cached_current_method_;
475
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000476 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100477 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000478 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000479 DISALLOW_COPY_AND_ASSIGN(HGraph);
480};
481
Vladimir Markof9f64412015-09-02 14:05:49 +0100482class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000483 public:
484 HLoopInformation(HBasicBlock* header, HGraph* graph)
485 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100486 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100487 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100488 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100489 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
490 back_edges_.reserve(kDefaultNumberOfBackEdges);
491 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100492
493 HBasicBlock* GetHeader() const {
494 return header_;
495 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000496
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000497 void SetHeader(HBasicBlock* block) {
498 header_ = block;
499 }
500
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100501 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
502 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
503 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
504
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000505 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100506 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000507 }
508
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100510 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100511 }
512
David Brazdil46e2a392015-03-16 17:31:52 +0000513 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100514 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100515 }
516
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000517 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100518 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000519 }
520
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100521 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100522
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100523 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100524 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100525 }
526
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100527 // Returns the lifetime position of the back edge that has the
528 // greatest lifetime position.
529 size_t GetLifetimeEnd() const;
530
531 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100532 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100533 }
534
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100535 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100536 // that is the header dominates the back edge.
537 bool Populate();
538
David Brazdila4b8c212015-05-07 09:59:30 +0100539 // Reanalyzes the loop by removing loop info from its blocks and re-running
540 // Populate(). If there are no back edges left, the loop info is completely
541 // removed as well as its SuspendCheck instruction. It must be run on nested
542 // inner loops first.
543 void Update();
544
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100545 // Returns whether this loop information contains `block`.
546 // Note that this loop information *must* be populated before entering this function.
547 bool Contains(const HBasicBlock& block) const;
548
549 // Returns whether this loop information is an inner loop of `other`.
550 // Note that `other` *must* be populated before entering this function.
551 bool IsIn(const HLoopInformation& other) const;
552
553 const ArenaBitVector& GetBlocks() const { return blocks_; }
554
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000555 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000556 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000557
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000558 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 // Internal recursive implementation of `Populate`.
560 void PopulateRecursive(HBasicBlock* block);
561
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000562 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100563 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100564 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000566
567 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
568};
569
David Brazdilec16f792015-08-19 15:04:01 +0100570// Stores try/catch information for basic blocks.
571// Note that HGraph is constructed so that catch blocks cannot simultaneously
572// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100573class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100574 public:
575 // Try block information constructor.
576 explicit TryCatchInformation(const HTryBoundary& try_entry)
577 : try_entry_(&try_entry),
578 catch_dex_file_(nullptr),
579 catch_type_index_(DexFile::kDexNoIndex16) {
580 DCHECK(try_entry_ != nullptr);
581 }
582
583 // Catch block information constructor.
584 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
585 : try_entry_(nullptr),
586 catch_dex_file_(&dex_file),
587 catch_type_index_(catch_type_index) {}
588
589 bool IsTryBlock() const { return try_entry_ != nullptr; }
590
591 const HTryBoundary& GetTryEntry() const {
592 DCHECK(IsTryBlock());
593 return *try_entry_;
594 }
595
596 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
597
598 bool IsCatchAllTypeIndex() const {
599 DCHECK(IsCatchBlock());
600 return catch_type_index_ == DexFile::kDexNoIndex16;
601 }
602
603 uint16_t GetCatchTypeIndex() const {
604 DCHECK(IsCatchBlock());
605 return catch_type_index_;
606 }
607
608 const DexFile& GetCatchDexFile() const {
609 DCHECK(IsCatchBlock());
610 return *catch_dex_file_;
611 }
612
613 private:
614 // One of possibly several TryBoundary instructions entering the block's try.
615 // Only set for try blocks.
616 const HTryBoundary* try_entry_;
617
618 // Exception type information. Only set for catch blocks.
619 const DexFile* catch_dex_file_;
620 const uint16_t catch_type_index_;
621};
622
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100623static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100624static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100625
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626// A block in a method. Contains the list of instructions represented
627// as a double linked list. Each block knows its predecessors and
628// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100629
Vladimir Markof9f64412015-09-02 14:05:49 +0100630class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000631 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600632 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000633 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000634 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
635 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000636 loop_information_(nullptr),
637 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000638 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100639 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100640 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100641 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000642 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000643 try_catch_information_(nullptr) {
644 predecessors_.reserve(kDefaultNumberOfPredecessors);
645 successors_.reserve(kDefaultNumberOfSuccessors);
646 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
647 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000648
Vladimir Marko60584552015-09-03 13:35:12 +0000649 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100650 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000651 }
652
Vladimir Marko60584552015-09-03 13:35:12 +0000653 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100654 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000655 }
656
Vladimir Marko60584552015-09-03 13:35:12 +0000657 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
658 return ContainsElement(successors_, block, start_from);
659 }
660
661 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100662 return dominated_blocks_;
663 }
664
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100665 bool IsEntryBlock() const {
666 return graph_->GetEntryBlock() == this;
667 }
668
669 bool IsExitBlock() const {
670 return graph_->GetExitBlock() == this;
671 }
672
David Brazdil46e2a392015-03-16 17:31:52 +0000673 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000674 bool IsSingleTryBoundary() const;
675
676 // Returns true if this block emits nothing but a jump.
677 bool IsSingleJump() const {
678 HLoopInformation* loop_info = GetLoopInformation();
679 return (IsSingleGoto() || IsSingleTryBoundary())
680 // Back edges generate a suspend check.
681 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
682 }
David Brazdil46e2a392015-03-16 17:31:52 +0000683
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000684 void AddBackEdge(HBasicBlock* back_edge) {
685 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000686 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000687 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100688 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000689 loop_information_->AddBackEdge(back_edge);
690 }
691
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000692 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000693 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000694
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100695 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000696 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600697 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000698
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000699 HBasicBlock* GetDominator() const { return dominator_; }
700 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000701 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
702
703 void RemoveDominatedBlock(HBasicBlock* block) {
704 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100705 }
Vladimir Marko60584552015-09-03 13:35:12 +0000706
707 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
708 ReplaceElement(dominated_blocks_, existing, new_block);
709 }
710
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100711 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000712
713 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100714 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000715 }
716
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
718 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100719 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100720 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100721 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
722 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000723
724 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000725 successors_.push_back(block);
726 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000727 }
728
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100729 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
730 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100731 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000732 new_block->predecessors_.push_back(this);
733 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000734 }
735
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000736 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
737 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000738 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000739 new_block->successors_.push_back(this);
740 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000741 }
742
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100743 // Insert `this` between `predecessor` and `successor. This method
744 // preserves the indicies, and will update the first edge found between
745 // `predecessor` and `successor`.
746 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
747 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100748 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000749 successor->predecessors_[predecessor_index] = this;
750 predecessor->successors_[successor_index] = this;
751 successors_.push_back(successor);
752 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100753 }
754
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100755 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000756 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100757 }
758
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000759 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000760 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000761 }
762
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000764 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100765 }
766
767 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000768 predecessors_.push_back(block);
769 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100770 }
771
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100772 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000773 DCHECK_EQ(predecessors_.size(), 2u);
774 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100775 }
776
David Brazdil769c9e52015-04-27 13:54:09 +0100777 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000778 DCHECK_EQ(successors_.size(), 2u);
779 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100780 }
781
David Brazdilfc6a86a2015-06-26 10:33:45 +0000782 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000783 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100784 }
785
David Brazdilfc6a86a2015-06-26 10:33:45 +0000786 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000787 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100788 }
789
David Brazdilfc6a86a2015-06-26 10:33:45 +0000790 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000791 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100792 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000793 }
794
795 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000796 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100797 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000798 }
799
800 // Returns whether the first occurrence of `predecessor` in the list of
801 // predecessors is at index `idx`.
802 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100803 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000804 return GetPredecessorIndexOf(predecessor) == idx;
805 }
806
David Brazdilffee3d32015-07-06 11:48:53 +0100807 // Returns the number of non-exceptional successors. SsaChecker ensures that
808 // these are stored at the beginning of the successor list.
809 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000810 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100811 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000812
David Brazdild7558da2015-09-22 13:04:14 +0100813 // Create a new block between this block and its predecessors. The new block
814 // is added to the graph, all predecessor edges are relinked to it and an edge
815 // is created to `this`. Returns the new empty block. Reverse post order or
816 // loop and try/catch information are not updated.
817 HBasicBlock* CreateImmediateDominator();
818
David Brazdilfc6a86a2015-06-26 10:33:45 +0000819 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100820 // created, latter block. Note that this method will add the block to the
821 // graph, create a Goto at the end of the former block and will create an edge
822 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100823 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000824 HBasicBlock* SplitBefore(HInstruction* cursor);
825
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000826 // Split the block into two blocks just after `cursor`. Returns the newly
827 // created block. Note that this method just updates raw block information,
828 // like predecessors, successors, dominators, and instruction list. It does not
829 // update the graph, reverse post order, loop information, nor make sure the
830 // blocks are consistent (for example ending with a control flow instruction).
831 HBasicBlock* SplitAfter(HInstruction* cursor);
832
833 // Merge `other` at the end of `this`. Successors and dominated blocks of
834 // `other` are changed to be successors and dominated blocks of `this`. Note
835 // that this method does not update the graph, reverse post order, loop
836 // information, nor make sure the blocks are consistent (for example ending
837 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100838 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000839
840 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
841 // of `this` are moved to `other`.
842 // Note that this method does not update the graph, reverse post order, loop
843 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000844 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000845 void ReplaceWith(HBasicBlock* other);
846
David Brazdil2d7352b2015-04-20 14:52:42 +0100847 // Merge `other` at the end of `this`. This method updates loops, reverse post
848 // order, links to predecessors, successors, dominators and deletes the block
849 // from the graph. The two blocks must be successive, i.e. `this` the only
850 // predecessor of `other` and vice versa.
851 void MergeWith(HBasicBlock* other);
852
853 // Disconnects `this` from all its predecessors, successors and dominator,
854 // removes it from all loops it is included in and eventually from the graph.
855 // The block must not dominate any other block. Predecessors and successors
856 // are safely updated.
857 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000858
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000859 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100860 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100861 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100862 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100863 // Replace instruction `initial` with `replacement` within this block.
864 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
865 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100866 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100867 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000868 // RemoveInstruction and RemovePhi delete a given instruction from the respective
869 // instruction list. With 'ensure_safety' set to true, it verifies that the
870 // instruction is not in use and removes it from the use lists of its inputs.
871 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
872 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100873 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100874
875 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100876 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100877 }
878
Roland Levillain6b879dd2014-09-22 17:13:44 +0100879 bool IsLoopPreHeaderFirstPredecessor() const {
880 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100881 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100882 }
883
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100884 HLoopInformation* GetLoopInformation() const {
885 return loop_information_;
886 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000887
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000888 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100889 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000890 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100891 void SetInLoop(HLoopInformation* info) {
892 if (IsLoopHeader()) {
893 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100894 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100895 loop_information_ = info;
896 } else if (loop_information_->Contains(*info->GetHeader())) {
897 // Block is currently part of an outer loop. Make it part of this inner loop.
898 // Note that a non loop header having a loop information means this loop information
899 // has already been populated
900 loop_information_ = info;
901 } else {
902 // Block is part of an inner loop. Do not update the loop information.
903 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
904 // at this point, because this method is being called while populating `info`.
905 }
906 }
907
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000908 // Raw update of the loop information.
909 void SetLoopInformation(HLoopInformation* info) {
910 loop_information_ = info;
911 }
912
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100913 bool IsInLoop() const { return loop_information_ != nullptr; }
914
David Brazdilec16f792015-08-19 15:04:01 +0100915 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
916
917 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
918 try_catch_information_ = try_catch_information;
919 }
920
921 bool IsTryBlock() const {
922 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
923 }
924
925 bool IsCatchBlock() const {
926 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
927 }
David Brazdilffee3d32015-07-06 11:48:53 +0100928
929 // Returns the try entry that this block's successors should have. They will
930 // be in the same try, unless the block ends in a try boundary. In that case,
931 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100932 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100933
David Brazdild7558da2015-09-22 13:04:14 +0100934 bool HasThrowingInstructions() const;
935
David Brazdila4b8c212015-05-07 09:59:30 +0100936 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100937 bool Dominates(HBasicBlock* block) const;
938
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939 size_t GetLifetimeStart() const { return lifetime_start_; }
940 size_t GetLifetimeEnd() const { return lifetime_end_; }
941
942 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
943 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
944
David Brazdil8d5b8b22015-03-24 10:51:52 +0000945 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000946 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100947 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000948 bool HasSinglePhi() const;
949
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000950 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000951 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000952 ArenaVector<HBasicBlock*> predecessors_;
953 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100954 HInstructionList instructions_;
955 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000956 HLoopInformation* loop_information_;
957 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000958 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100959 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100960 // The dex program counter of the first instruction of this block.
961 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100962 size_t lifetime_start_;
963 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100964 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100965
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000966 friend class HGraph;
967 friend class HInstruction;
968
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
970};
971
David Brazdilb2bd1c52015-03-25 11:17:37 +0000972// Iterates over the LoopInformation of all loops which contain 'block'
973// from the innermost to the outermost.
974class HLoopInformationOutwardIterator : public ValueObject {
975 public:
976 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
977 : current_(block.GetLoopInformation()) {}
978
979 bool Done() const { return current_ == nullptr; }
980
981 void Advance() {
982 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100983 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000984 }
985
986 HLoopInformation* Current() const {
987 DCHECK(!Done());
988 return current_;
989 }
990
991 private:
992 HLoopInformation* current_;
993
994 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
995};
996
Alexandre Ramesef20f712015-06-09 10:29:30 +0100997#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -0700998 M(Above, Condition) \
999 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001000 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001001 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001002 M(ArrayGet, Instruction) \
1003 M(ArrayLength, Instruction) \
1004 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001005 M(Below, Condition) \
1006 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001007 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001008 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001009 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001010 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001011 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001012 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001013 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001014 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001015 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001016 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001017 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001018 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001019 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001020 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001021 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001022 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001023 M(FloatConstant, Constant) \
1024 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001025 M(GreaterThan, Condition) \
1026 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001027 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001028 M(InstanceFieldGet, Instruction) \
1029 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001030 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001031 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001032 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001033 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001034 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001035 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001036 M(LessThan, Condition) \
1037 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001038 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001039 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001040 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001041 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001042 M(Local, Instruction) \
1043 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001044 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001045 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001046 M(Mul, BinaryOperation) \
1047 M(Neg, UnaryOperation) \
1048 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001050 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001051 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001052 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001053 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001054 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001055 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001056 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001057 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001058 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001059 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001060 M(Return, Instruction) \
1061 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001062 M(Shl, BinaryOperation) \
1063 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001064 M(StaticFieldGet, Instruction) \
1065 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001066 M(UnresolvedInstanceFieldGet, Instruction) \
1067 M(UnresolvedInstanceFieldSet, Instruction) \
1068 M(UnresolvedStaticFieldGet, Instruction) \
1069 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001070 M(StoreLocal, Instruction) \
1071 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001072 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001073 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001074 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001075 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001076 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001077 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001078 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001079
Alexandre Ramesef20f712015-06-09 10:29:30 +01001080#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1081
1082#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1083
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001084#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1085
Mark Mendell0616ae02015-04-17 12:49:27 -04001086#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1087 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001088 M(X86LoadFromConstantTable, Instruction) \
1089 M(X86PackedSwitch, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001090
1091#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1092
1093#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1094 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1095 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1096 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001097 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001098 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1099 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1100
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001101#define FOR_EACH_INSTRUCTION(M) \
1102 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1103 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001104 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001105 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001106 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001107
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001108#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001109FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1110#undef FORWARD_DECLARATION
1111
Roland Levillainccc07a92014-09-16 14:48:16 +01001112#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001113 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1114 const char* DebugName() const OVERRIDE { return #type; } \
1115 const H##type* As##type() const OVERRIDE { return this; } \
1116 H##type* As##type() OVERRIDE { return this; } \
1117 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001118 return other->Is##type(); \
1119 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001120 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001121
David Brazdiled596192015-01-23 10:39:45 +00001122template <typename T> class HUseList;
1123
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001124template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001125class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001126 public:
David Brazdiled596192015-01-23 10:39:45 +00001127 HUseListNode* GetPrevious() const { return prev_; }
1128 HUseListNode* GetNext() const { return next_; }
1129 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001130 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001131 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001132
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001133 private:
David Brazdiled596192015-01-23 10:39:45 +00001134 HUseListNode(T user, size_t index)
1135 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1136
1137 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001138 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001139 HUseListNode<T>* prev_;
1140 HUseListNode<T>* next_;
1141
1142 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001143
1144 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1145};
1146
David Brazdiled596192015-01-23 10:39:45 +00001147template <typename T>
1148class HUseList : public ValueObject {
1149 public:
1150 HUseList() : first_(nullptr) {}
1151
1152 void Clear() {
1153 first_ = nullptr;
1154 }
1155
1156 // Adds a new entry at the beginning of the use list and returns
1157 // the newly created node.
1158 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001159 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001160 if (IsEmpty()) {
1161 first_ = new_node;
1162 } else {
1163 first_->prev_ = new_node;
1164 new_node->next_ = first_;
1165 first_ = new_node;
1166 }
1167 return new_node;
1168 }
1169
1170 HUseListNode<T>* GetFirst() const {
1171 return first_;
1172 }
1173
1174 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001175 DCHECK(node != nullptr);
1176 DCHECK(Contains(node));
1177
David Brazdiled596192015-01-23 10:39:45 +00001178 if (node->prev_ != nullptr) {
1179 node->prev_->next_ = node->next_;
1180 }
1181 if (node->next_ != nullptr) {
1182 node->next_->prev_ = node->prev_;
1183 }
1184 if (node == first_) {
1185 first_ = node->next_;
1186 }
1187 }
1188
David Brazdil1abb4192015-02-17 18:33:36 +00001189 bool Contains(const HUseListNode<T>* node) const {
1190 if (node == nullptr) {
1191 return false;
1192 }
1193 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1194 if (current == node) {
1195 return true;
1196 }
1197 }
1198 return false;
1199 }
1200
David Brazdiled596192015-01-23 10:39:45 +00001201 bool IsEmpty() const {
1202 return first_ == nullptr;
1203 }
1204
1205 bool HasOnlyOneUse() const {
1206 return first_ != nullptr && first_->next_ == nullptr;
1207 }
1208
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001209 size_t SizeSlow() const {
1210 size_t count = 0;
1211 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1212 ++count;
1213 }
1214 return count;
1215 }
1216
David Brazdiled596192015-01-23 10:39:45 +00001217 private:
1218 HUseListNode<T>* first_;
1219};
1220
1221template<typename T>
1222class HUseIterator : public ValueObject {
1223 public:
1224 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1225
1226 bool Done() const { return current_ == nullptr; }
1227
1228 void Advance() {
1229 DCHECK(!Done());
1230 current_ = current_->GetNext();
1231 }
1232
1233 HUseListNode<T>* Current() const {
1234 DCHECK(!Done());
1235 return current_;
1236 }
1237
1238 private:
1239 HUseListNode<T>* current_;
1240
1241 friend class HValue;
1242};
1243
David Brazdil1abb4192015-02-17 18:33:36 +00001244// This class is used by HEnvironment and HInstruction classes to record the
1245// instructions they use and pointers to the corresponding HUseListNodes kept
1246// by the used instructions.
1247template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001248class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001249 public:
1250 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1251 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1252
1253 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1254 : instruction_(old_record.instruction_), use_node_(use_node) {
1255 DCHECK(instruction_ != nullptr);
1256 DCHECK(use_node_ != nullptr);
1257 DCHECK(old_record.use_node_ == nullptr);
1258 }
1259
1260 HInstruction* GetInstruction() const { return instruction_; }
1261 HUseListNode<T>* GetUseNode() const { return use_node_; }
1262
1263 private:
1264 // Instruction used by the user.
1265 HInstruction* instruction_;
1266
1267 // Corresponding entry in the use list kept by 'instruction_'.
1268 HUseListNode<T>* use_node_;
1269};
1270
Aart Bik854a02b2015-07-14 16:07:00 -07001271/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001272 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001273 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001274 * For write/read dependences on fields/arrays, the dependence analysis uses
1275 * type disambiguation (e.g. a float field write cannot modify the value of an
1276 * integer field read) and the access type (e.g. a reference array write cannot
1277 * modify the value of a reference field read [although it may modify the
1278 * reference fetch prior to reading the field, which is represented by its own
1279 * write/read dependence]). The analysis makes conservative points-to
1280 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1281 * the same, and any reference read depends on any reference read without
1282 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001283 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001284 * The internal representation uses 38-bit and is described in the table below.
1285 * The first line indicates the side effect, and for field/array accesses the
1286 * second line indicates the type of the access (in the order of the
1287 * Primitive::Type enum).
1288 * The two numbered lines below indicate the bit position in the bitfield (read
1289 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001290 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001291 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1292 * +-------------+---------+---------+--------------+---------+---------+
1293 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1294 * | 3 |333333322|222222221| 1 |111111110|000000000|
1295 * | 7 |654321098|765432109| 8 |765432109|876543210|
1296 *
1297 * Note that, to ease the implementation, 'changes' bits are least significant
1298 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001299 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001300class SideEffects : public ValueObject {
1301 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001302 SideEffects() : flags_(0) {}
1303
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001304 static SideEffects None() {
1305 return SideEffects(0);
1306 }
1307
1308 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001309 return SideEffects(kAllChangeBits | kAllDependOnBits);
1310 }
1311
1312 static SideEffects AllChanges() {
1313 return SideEffects(kAllChangeBits);
1314 }
1315
1316 static SideEffects AllDependencies() {
1317 return SideEffects(kAllDependOnBits);
1318 }
1319
1320 static SideEffects AllExceptGCDependency() {
1321 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1322 }
1323
1324 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001325 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001326 }
1327
Aart Bik34c3ba92015-07-20 14:08:59 -07001328 static SideEffects AllWrites() {
1329 return SideEffects(kAllWrites);
1330 }
1331
1332 static SideEffects AllReads() {
1333 return SideEffects(kAllReads);
1334 }
1335
1336 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1337 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001338 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001339 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001340 }
1341
Aart Bik854a02b2015-07-14 16:07:00 -07001342 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1343 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001344 }
1345
Aart Bik34c3ba92015-07-20 14:08:59 -07001346 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1347 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001348 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001349 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001350 }
1351
1352 static SideEffects ArrayReadOfType(Primitive::Type type) {
1353 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1354 }
1355
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001356 static SideEffects CanTriggerGC() {
1357 return SideEffects(1ULL << kCanTriggerGCBit);
1358 }
1359
1360 static SideEffects DependsOnGC() {
1361 return SideEffects(1ULL << kDependsOnGCBit);
1362 }
1363
Aart Bik854a02b2015-07-14 16:07:00 -07001364 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001365 SideEffects Union(SideEffects other) const {
1366 return SideEffects(flags_ | other.flags_);
1367 }
1368
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001369 SideEffects Exclusion(SideEffects other) const {
1370 return SideEffects(flags_ & ~other.flags_);
1371 }
1372
1373 bool Includes(SideEffects other) const {
1374 return (other.flags_ & flags_) == other.flags_;
1375 }
1376
1377 bool HasSideEffects() const {
1378 return (flags_ & kAllChangeBits);
1379 }
1380
1381 bool HasDependencies() const {
1382 return (flags_ & kAllDependOnBits);
1383 }
1384
1385 // Returns true if there are no side effects or dependencies.
1386 bool DoesNothing() const {
1387 return flags_ == 0;
1388 }
1389
Aart Bik854a02b2015-07-14 16:07:00 -07001390 // Returns true if something is written.
1391 bool DoesAnyWrite() const {
1392 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001393 }
1394
Aart Bik854a02b2015-07-14 16:07:00 -07001395 // Returns true if something is read.
1396 bool DoesAnyRead() const {
1397 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001398 }
1399
Aart Bik854a02b2015-07-14 16:07:00 -07001400 // Returns true if potentially everything is written and read
1401 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001402 bool DoesAllReadWrite() const {
1403 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1404 }
1405
Aart Bik854a02b2015-07-14 16:07:00 -07001406 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001407 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001408 }
1409
1410 // Returns true if this may read something written by other.
1411 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001412 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1413 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001414 }
1415
1416 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001417 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001418 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001419 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001420 for (int s = kLastBit; s >= 0; s--) {
1421 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1422 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1423 // This is a bit for the GC side effect.
1424 if (current_bit_is_set) {
1425 flags += "GC";
1426 }
Aart Bik854a02b2015-07-14 16:07:00 -07001427 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001428 } else {
1429 // This is a bit for the array/field analysis.
1430 // The underscore character stands for the 'can trigger GC' bit.
1431 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1432 if (current_bit_is_set) {
1433 flags += kDebug[s];
1434 }
1435 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1436 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1437 flags += "|";
1438 }
1439 }
Aart Bik854a02b2015-07-14 16:07:00 -07001440 }
1441 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001442 }
1443
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001444 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001445
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001446 private:
1447 static constexpr int kFieldArrayAnalysisBits = 9;
1448
1449 static constexpr int kFieldWriteOffset = 0;
1450 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1451 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1452 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1453
1454 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1455
1456 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1457 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1458 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1459 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1460
1461 static constexpr int kLastBit = kDependsOnGCBit;
1462 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1463
1464 // Aliases.
1465
1466 static_assert(kChangeBits == kDependOnBits,
1467 "the 'change' bits should match the 'depend on' bits.");
1468
1469 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1470 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1471 static constexpr uint64_t kAllWrites =
1472 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1473 static constexpr uint64_t kAllReads =
1474 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475
Aart Bik854a02b2015-07-14 16:07:00 -07001476 // Work around the fact that HIR aliases I/F and J/D.
1477 // TODO: remove this interceptor once HIR types are clean
1478 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1479 switch (type) {
1480 case Primitive::kPrimInt:
1481 case Primitive::kPrimFloat:
1482 return TypeFlag(Primitive::kPrimInt, offset) |
1483 TypeFlag(Primitive::kPrimFloat, offset);
1484 case Primitive::kPrimLong:
1485 case Primitive::kPrimDouble:
1486 return TypeFlag(Primitive::kPrimLong, offset) |
1487 TypeFlag(Primitive::kPrimDouble, offset);
1488 default:
1489 return TypeFlag(type, offset);
1490 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001491 }
1492
Aart Bik854a02b2015-07-14 16:07:00 -07001493 // Translates type to bit flag.
1494 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1495 CHECK_NE(type, Primitive::kPrimVoid);
1496 const uint64_t one = 1;
1497 const int shift = type; // 0-based consecutive enum
1498 DCHECK_LE(kFieldWriteOffset, shift);
1499 DCHECK_LT(shift, kArrayWriteOffset);
1500 return one << (type + offset);
1501 }
1502
1503 // Private constructor on direct flags value.
1504 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1505
1506 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001507};
1508
David Brazdiled596192015-01-23 10:39:45 +00001509// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001510class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001511 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001512 HEnvironment(ArenaAllocator* arena,
1513 size_t number_of_vregs,
1514 const DexFile& dex_file,
1515 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001516 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001517 InvokeType invoke_type,
1518 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001519 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1520 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001521 parent_(nullptr),
1522 dex_file_(dex_file),
1523 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001524 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001525 invoke_type_(invoke_type),
1526 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001527 }
1528
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001529 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001530 : HEnvironment(arena,
1531 to_copy.Size(),
1532 to_copy.GetDexFile(),
1533 to_copy.GetMethodIdx(),
1534 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001535 to_copy.GetInvokeType(),
1536 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001537
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001538 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001539 if (parent_ != nullptr) {
1540 parent_->SetAndCopyParentChain(allocator, parent);
1541 } else {
1542 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1543 parent_->CopyFrom(parent);
1544 if (parent->GetParent() != nullptr) {
1545 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1546 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001547 }
David Brazdiled596192015-01-23 10:39:45 +00001548 }
1549
Vladimir Marko71bf8092015-09-15 15:33:14 +01001550 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001551 void CopyFrom(HEnvironment* environment);
1552
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001553 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1554 // input to the loop phi instead. This is for inserting instructions that
1555 // require an environment (like HDeoptimization) in the loop pre-header.
1556 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001557
1558 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001559 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001560 }
1561
1562 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001563 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001564 }
1565
David Brazdil1abb4192015-02-17 18:33:36 +00001566 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001567
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001568 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001569
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001570 HEnvironment* GetParent() const { return parent_; }
1571
1572 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001573 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001574 }
1575
1576 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001577 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001578 }
1579
1580 uint32_t GetDexPc() const {
1581 return dex_pc_;
1582 }
1583
1584 uint32_t GetMethodIdx() const {
1585 return method_idx_;
1586 }
1587
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001588 InvokeType GetInvokeType() const {
1589 return invoke_type_;
1590 }
1591
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001592 const DexFile& GetDexFile() const {
1593 return dex_file_;
1594 }
1595
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001596 HInstruction* GetHolder() const {
1597 return holder_;
1598 }
1599
David Brazdiled596192015-01-23 10:39:45 +00001600 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001601 // Record instructions' use entries of this environment for constant-time removal.
1602 // It should only be called by HInstruction when a new environment use is added.
1603 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1604 DCHECK(env_use->GetUser() == this);
1605 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001606 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001607 }
David Brazdiled596192015-01-23 10:39:45 +00001608
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001609 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1610 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001611 HEnvironment* parent_;
1612 const DexFile& dex_file_;
1613 const uint32_t method_idx_;
1614 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001615 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001616
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001617 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001618 HInstruction* const holder_;
1619
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001620 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001621
1622 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1623};
1624
Calin Juravleacf735c2015-02-12 15:25:22 +00001625class ReferenceTypeInfo : ValueObject {
1626 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001627 typedef Handle<mirror::Class> TypeHandle;
1628
Calin Juravle2e768302015-07-28 14:41:11 +00001629 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1630 // The constructor will check that the type_handle is valid.
1631 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001632 }
1633
Calin Juravle2e768302015-07-28 14:41:11 +00001634 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1635
1636 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1637 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001638 }
1639
Calin Juravle2e768302015-07-28 14:41:11 +00001640 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1641 return IsValidHandle(type_handle_);
1642 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001643
Calin Juravleacf735c2015-02-12 15:25:22 +00001644 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001645
1646 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1647 DCHECK(IsValid());
1648 return GetTypeHandle()->IsObjectClass();
1649 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001650
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001651 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001652 DCHECK(IsValid());
1653 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001654 }
1655
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001656 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1657 DCHECK(IsValid());
1658 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1659 }
1660
Mathieu Chartier90443472015-07-16 20:32:27 -07001661 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001662 DCHECK(IsValid());
1663 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001664 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001665
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001666 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001667 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001668 return GetTypeHandle()->IsArrayClass();
1669 }
1670
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001671 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1672 DCHECK(IsValid());
1673 return GetTypeHandle()->IsPrimitiveArray();
1674 }
1675
1676 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1677 DCHECK(IsValid());
1678 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1679 }
1680
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001681 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001682 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001683 if (!IsExact()) return false;
1684 if (!IsArrayClass()) return false;
1685 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1686 }
1687
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001688 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1689 DCHECK(IsValid());
1690 if (!IsExact()) return false;
1691 if (!IsArrayClass()) return false;
1692 if (!rti.IsArrayClass()) return false;
1693 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1694 rti.GetTypeHandle()->GetComponentType());
1695 }
1696
Calin Juravleacf735c2015-02-12 15:25:22 +00001697 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1698
Mathieu Chartier90443472015-07-16 20:32:27 -07001699 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001700 DCHECK(IsValid());
1701 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001702 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1703 }
1704
1705 // Returns true if the type information provide the same amount of details.
1706 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001707 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001708 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001709 if (!IsValid() && !rti.IsValid()) {
1710 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001711 return true;
1712 }
Calin Juravle2e768302015-07-28 14:41:11 +00001713 if (!IsValid() || !rti.IsValid()) {
1714 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001715 return false;
1716 }
Calin Juravle2e768302015-07-28 14:41:11 +00001717 return IsExact() == rti.IsExact()
1718 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001719 }
1720
1721 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001722 ReferenceTypeInfo();
1723 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001724
Calin Juravleacf735c2015-02-12 15:25:22 +00001725 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001726 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001727 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001728 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001729 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001730};
1731
1732std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1733
Vladimir Markof9f64412015-09-02 14:05:49 +01001734class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001735 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001736 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001737 : previous_(nullptr),
1738 next_(nullptr),
1739 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001740 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001741 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001742 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001743 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001744 locations_(nullptr),
1745 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001746 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001747 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001748 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001749
Dave Allison20dfc792014-06-16 20:44:29 -07001750 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001751
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001752#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001753 enum InstructionKind {
1754 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1755 };
1756#undef DECLARE_KIND
1757
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001758 HInstruction* GetNext() const { return next_; }
1759 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001760
Calin Juravle77520bc2015-01-12 18:45:46 +00001761 HInstruction* GetNextDisregardingMoves() const;
1762 HInstruction* GetPreviousDisregardingMoves() const;
1763
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001764 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001765 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001766 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001767 bool IsInBlock() const { return block_ != nullptr; }
1768 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001769 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001770
Roland Levillain6b879dd2014-09-22 17:13:44 +01001771 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001772 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001773
1774 virtual void Accept(HGraphVisitor* visitor) = 0;
1775 virtual const char* DebugName() const = 0;
1776
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001777 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001778 void SetRawInputAt(size_t index, HInstruction* input) {
1779 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1780 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001781
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001782 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001783
1784 uint32_t GetDexPc() const { return dex_pc_; }
1785
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001786 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001787
Roland Levillaine161a2a2014-10-03 12:45:18 +01001788 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001789 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001790
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001791 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001792 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001793
Calin Juravle10e244f2015-01-26 18:54:32 +00001794 // Does not apply for all instructions, but having this at top level greatly
1795 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001796 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001797 virtual bool CanBeNull() const {
1798 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1799 return true;
1800 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001801
Calin Juravle641547a2015-04-21 22:08:51 +01001802 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1803 UNUSED(obj);
1804 return false;
1805 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001806
Calin Juravle2e768302015-07-28 14:41:11 +00001807 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001808
Calin Juravle61d544b2015-02-23 16:46:57 +00001809 ReferenceTypeInfo GetReferenceTypeInfo() const {
1810 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1811 return reference_type_info_;
1812 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001813
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001814 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001815 DCHECK(user != nullptr);
1816 HUseListNode<HInstruction*>* use =
1817 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1818 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001819 }
1820
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001821 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001822 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001823 HUseListNode<HEnvironment*>* env_use =
1824 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1825 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001826 }
1827
David Brazdil1abb4192015-02-17 18:33:36 +00001828 void RemoveAsUserOfInput(size_t input) {
1829 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1830 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1831 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001832
David Brazdil1abb4192015-02-17 18:33:36 +00001833 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1834 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001835
David Brazdiled596192015-01-23 10:39:45 +00001836 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1837 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001838 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001839 bool HasOnlyOneNonEnvironmentUse() const {
1840 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1841 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001842
Roland Levillain6c82d402014-10-13 16:10:27 +01001843 // Does this instruction strictly dominate `other_instruction`?
1844 // Returns false if this instruction and `other_instruction` are the same.
1845 // Aborts if this instruction and `other_instruction` are both phis.
1846 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001847
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001848 int GetId() const { return id_; }
1849 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001850
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001851 int GetSsaIndex() const { return ssa_index_; }
1852 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1853 bool HasSsaIndex() const { return ssa_index_ != -1; }
1854
1855 bool HasEnvironment() const { return environment_ != nullptr; }
1856 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001857 // Set the `environment_` field. Raw because this method does not
1858 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001859 void SetRawEnvironment(HEnvironment* environment) {
1860 DCHECK(environment_ == nullptr);
1861 DCHECK_EQ(environment->GetHolder(), this);
1862 environment_ = environment;
1863 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001864
1865 // Set the environment of this instruction, copying it from `environment`. While
1866 // copying, the uses lists are being updated.
1867 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001868 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001869 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001870 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001871 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001872 if (environment->GetParent() != nullptr) {
1873 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1874 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001875 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001876
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001877 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1878 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001879 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001880 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001881 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001882 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001883 if (environment->GetParent() != nullptr) {
1884 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1885 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001886 }
1887
Nicolas Geoffray39468442014-09-02 15:17:15 +01001888 // Returns the number of entries in the environment. Typically, that is the
1889 // number of dex registers in a method. It could be more in case of inlining.
1890 size_t EnvironmentSize() const;
1891
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001892 LocationSummary* GetLocations() const { return locations_; }
1893 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001894
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001895 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001896 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001897
Alexandre Rames188d4312015-04-09 18:30:21 +01001898 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1899 // uses of this instruction by `other` are *not* updated.
1900 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1901 ReplaceWith(other);
1902 other->ReplaceInput(this, use_index);
1903 }
1904
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001905 // Move `this` instruction before `cursor`.
1906 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001907
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001908#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001909 bool Is##type() const { return (As##type() != nullptr); } \
1910 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001911 virtual H##type* As##type() { return nullptr; }
1912
1913 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1914#undef INSTRUCTION_TYPE_CHECK
1915
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001916 // Returns whether the instruction can be moved within the graph.
1917 virtual bool CanBeMoved() const { return false; }
1918
1919 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001920 virtual bool InstructionTypeEquals(HInstruction* other) const {
1921 UNUSED(other);
1922 return false;
1923 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001924
1925 // Returns whether any data encoded in the two instructions is equal.
1926 // This method does not look at the inputs. Both instructions must be
1927 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001928 virtual bool InstructionDataEquals(HInstruction* other) const {
1929 UNUSED(other);
1930 return false;
1931 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001932
1933 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001934 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001935 // 2) Their inputs are identical.
1936 bool Equals(HInstruction* other) const;
1937
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001938 virtual InstructionKind GetKind() const = 0;
1939
1940 virtual size_t ComputeHashCode() const {
1941 size_t result = GetKind();
1942 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1943 result = (result * 31) + InputAt(i)->GetId();
1944 }
1945 return result;
1946 }
1947
1948 SideEffects GetSideEffects() const { return side_effects_; }
1949
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001950 size_t GetLifetimePosition() const { return lifetime_position_; }
1951 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1952 LiveInterval* GetLiveInterval() const { return live_interval_; }
1953 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1954 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1955
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001956 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1957
1958 // Returns whether the code generation of the instruction will require to have access
1959 // to the current method. Such instructions are:
1960 // (1): Instructions that require an environment, as calling the runtime requires
1961 // to walk the stack and have the current method stored at a specific stack address.
1962 // (2): Object literals like classes and strings, that are loaded from the dex cache
1963 // fields of the current method.
1964 bool NeedsCurrentMethod() const {
1965 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1966 }
1967
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001968 virtual bool NeedsDexCache() const { return false; }
1969
Mark Mendellc4701932015-04-10 13:18:51 -04001970 // Does this instruction have any use in an environment before
1971 // control flow hits 'other'?
1972 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1973
1974 // Remove all references to environment uses of this instruction.
1975 // The caller must ensure that this is safe to do.
1976 void RemoveEnvironmentUsers();
1977
David Brazdil1abb4192015-02-17 18:33:36 +00001978 protected:
1979 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1980 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1981
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001982 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001983 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1984
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001985 HInstruction* previous_;
1986 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001987 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001988 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001989
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001990 // An instruction gets an id when it is added to the graph.
1991 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001992 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001993 int id_;
1994
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001995 // When doing liveness analysis, instructions that have uses get an SSA index.
1996 int ssa_index_;
1997
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001998 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001999 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002000
2001 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002002 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002003
Nicolas Geoffray39468442014-09-02 15:17:15 +01002004 // The environment associated with this instruction. Not null if the instruction
2005 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002006 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002007
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008 // Set by the code generator.
2009 LocationSummary* locations_;
2010
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002011 // Set by the liveness analysis.
2012 LiveInterval* live_interval_;
2013
2014 // Set by the liveness analysis, this is the position in a linear
2015 // order of blocks where this instruction's live interval start.
2016 size_t lifetime_position_;
2017
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002018 const SideEffects side_effects_;
2019
Calin Juravleacf735c2015-02-12 15:25:22 +00002020 // TODO: for primitive types this should be marked as invalid.
2021 ReferenceTypeInfo reference_type_info_;
2022
David Brazdil1abb4192015-02-17 18:33:36 +00002023 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002024 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002025 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002026 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002027 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002028
2029 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2030};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002031std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002032
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002033class HInputIterator : public ValueObject {
2034 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002035 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002036
2037 bool Done() const { return index_ == instruction_->InputCount(); }
2038 HInstruction* Current() const { return instruction_->InputAt(index_); }
2039 void Advance() { index_++; }
2040
2041 private:
2042 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002043 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002044
2045 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2046};
2047
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002048class HInstructionIterator : public ValueObject {
2049 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002050 explicit HInstructionIterator(const HInstructionList& instructions)
2051 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002052 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002053 }
2054
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002055 bool Done() const { return instruction_ == nullptr; }
2056 HInstruction* Current() const { return instruction_; }
2057 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002058 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002059 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002060 }
2061
2062 private:
2063 HInstruction* instruction_;
2064 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002065
2066 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002067};
2068
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002069class HBackwardInstructionIterator : public ValueObject {
2070 public:
2071 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2072 : instruction_(instructions.last_instruction_) {
2073 next_ = Done() ? nullptr : instruction_->GetPrevious();
2074 }
2075
2076 bool Done() const { return instruction_ == nullptr; }
2077 HInstruction* Current() const { return instruction_; }
2078 void Advance() {
2079 instruction_ = next_;
2080 next_ = Done() ? nullptr : instruction_->GetPrevious();
2081 }
2082
2083 private:
2084 HInstruction* instruction_;
2085 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002086
2087 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002088};
2089
Vladimir Markof9f64412015-09-02 14:05:49 +01002090template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002091class HTemplateInstruction: public HInstruction {
2092 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002093 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002094 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002095 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002096
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002097 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002098
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002099 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002100 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2101 DCHECK_LT(i, N);
2102 return inputs_[i];
2103 }
David Brazdil1abb4192015-02-17 18:33:36 +00002104
2105 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002106 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002107 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002108 }
2109
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002110 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002111 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002112
2113 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114};
2115
Vladimir Markof9f64412015-09-02 14:05:49 +01002116// HTemplateInstruction specialization for N=0.
2117template<>
2118class HTemplateInstruction<0>: public HInstruction {
2119 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002120 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002121 : HInstruction(side_effects, dex_pc) {}
2122
Vladimir Markof9f64412015-09-02 14:05:49 +01002123 virtual ~HTemplateInstruction() {}
2124
2125 size_t InputCount() const OVERRIDE { return 0; }
2126
2127 protected:
2128 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2129 LOG(FATAL) << "Unreachable";
2130 UNREACHABLE();
2131 }
2132
2133 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2134 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2135 LOG(FATAL) << "Unreachable";
2136 UNREACHABLE();
2137 }
2138
2139 private:
2140 friend class SsaBuilder;
2141};
2142
Dave Allison20dfc792014-06-16 20:44:29 -07002143template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002144class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002145 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002146 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002147 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002148 virtual ~HExpression() {}
2149
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002150 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002151
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002152 protected:
2153 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002154};
2155
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2157// instruction that branches to the exit block.
2158class HReturnVoid : public HTemplateInstruction<0> {
2159 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002160 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2161 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002162
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002163 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002164
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002165 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002166
2167 private:
2168 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2169};
2170
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002171// Represents dex's RETURN opcodes. A HReturn is a control flow
2172// instruction that branches to the exit block.
2173class HReturn : public HTemplateInstruction<1> {
2174 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002175 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2176 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002177 SetRawInputAt(0, value);
2178 }
2179
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002180 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002181
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002182 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002183
2184 private:
2185 DISALLOW_COPY_AND_ASSIGN(HReturn);
2186};
2187
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002188// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002189// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002190// exit block.
2191class HExit : public HTemplateInstruction<0> {
2192 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002193 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002194
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002195 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002196
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002197 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002198
2199 private:
2200 DISALLOW_COPY_AND_ASSIGN(HExit);
2201};
2202
2203// Jumps from one block to another.
2204class HGoto : public HTemplateInstruction<0> {
2205 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002206 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002207
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002208 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002209
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002210 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002211 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002212 }
2213
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002214 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002215
2216 private:
2217 DISALLOW_COPY_AND_ASSIGN(HGoto);
2218};
2219
Roland Levillain9867bc72015-08-05 10:21:34 +01002220class HConstant : public HExpression<0> {
2221 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002222 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2223 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002224
2225 bool CanBeMoved() const OVERRIDE { return true; }
2226
2227 virtual bool IsMinusOne() const { return false; }
2228 virtual bool IsZero() const { return false; }
2229 virtual bool IsOne() const { return false; }
2230
David Brazdil77a48ae2015-09-15 12:34:04 +00002231 virtual uint64_t GetValueAsUint64() const = 0;
2232
Roland Levillain9867bc72015-08-05 10:21:34 +01002233 DECLARE_INSTRUCTION(Constant);
2234
2235 private:
2236 DISALLOW_COPY_AND_ASSIGN(HConstant);
2237};
2238
2239class HNullConstant : public HConstant {
2240 public:
2241 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2242 return true;
2243 }
2244
David Brazdil77a48ae2015-09-15 12:34:04 +00002245 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2246
Roland Levillain9867bc72015-08-05 10:21:34 +01002247 size_t ComputeHashCode() const OVERRIDE { return 0; }
2248
2249 DECLARE_INSTRUCTION(NullConstant);
2250
2251 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002252 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002253
2254 friend class HGraph;
2255 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2256};
2257
2258// Constants of the type int. Those can be from Dex instructions, or
2259// synthesized (for example with the if-eqz instruction).
2260class HIntConstant : public HConstant {
2261 public:
2262 int32_t GetValue() const { return value_; }
2263
David Brazdil9f389d42015-10-01 14:32:56 +01002264 uint64_t GetValueAsUint64() const OVERRIDE {
2265 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2266 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002267
Roland Levillain9867bc72015-08-05 10:21:34 +01002268 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2269 DCHECK(other->IsIntConstant());
2270 return other->AsIntConstant()->value_ == value_;
2271 }
2272
2273 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2274
2275 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2276 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2277 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2278
2279 DECLARE_INSTRUCTION(IntConstant);
2280
2281 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002282 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2283 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2284 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2285 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002286
2287 const int32_t value_;
2288
2289 friend class HGraph;
2290 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2291 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2292 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2293};
2294
2295class HLongConstant : public HConstant {
2296 public:
2297 int64_t GetValue() const { return value_; }
2298
David Brazdil77a48ae2015-09-15 12:34:04 +00002299 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2300
Roland Levillain9867bc72015-08-05 10:21:34 +01002301 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2302 DCHECK(other->IsLongConstant());
2303 return other->AsLongConstant()->value_ == value_;
2304 }
2305
2306 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2307
2308 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2309 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2310 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2311
2312 DECLARE_INSTRUCTION(LongConstant);
2313
2314 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002315 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2316 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002317
2318 const int64_t value_;
2319
2320 friend class HGraph;
2321 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2322};
Dave Allison20dfc792014-06-16 20:44:29 -07002323
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002324// Conditional branch. A block ending with an HIf instruction must have
2325// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002326class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002327 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002328 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2329 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002330 SetRawInputAt(0, input);
2331 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002332
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002333 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002334
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002335 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002336 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002337 }
2338
2339 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002340 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002341 }
2342
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002343 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002344
2345 private:
2346 DISALLOW_COPY_AND_ASSIGN(HIf);
2347};
2348
David Brazdilfc6a86a2015-06-26 10:33:45 +00002349
2350// Abstract instruction which marks the beginning and/or end of a try block and
2351// links it to the respective exception handlers. Behaves the same as a Goto in
2352// non-exceptional control flow.
2353// Normal-flow successor is stored at index zero, exception handlers under
2354// higher indices in no particular order.
2355class HTryBoundary : public HTemplateInstruction<0> {
2356 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002357 enum BoundaryKind {
2358 kEntry,
2359 kExit,
2360 };
2361
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002362 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2363 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002364
2365 bool IsControlFlow() const OVERRIDE { return true; }
2366
2367 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002368 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002369
2370 // Returns whether `handler` is among its exception handlers (non-zero index
2371 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002372 bool HasExceptionHandler(const HBasicBlock& handler) const {
2373 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002374 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002375 }
2376
2377 // If not present already, adds `handler` to its block's list of exception
2378 // handlers.
2379 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002380 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002381 GetBlock()->AddSuccessor(handler);
2382 }
2383 }
2384
David Brazdil56e1acc2015-06-30 15:41:36 +01002385 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002386
David Brazdilffee3d32015-07-06 11:48:53 +01002387 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2388
David Brazdilfc6a86a2015-06-26 10:33:45 +00002389 DECLARE_INSTRUCTION(TryBoundary);
2390
2391 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002392 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002393
2394 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2395};
2396
David Brazdilffee3d32015-07-06 11:48:53 +01002397// Iterator over exception handlers of a given HTryBoundary, i.e. over
2398// exceptional successors of its basic block.
2399class HExceptionHandlerIterator : public ValueObject {
2400 public:
2401 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2402 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2403
Vladimir Marko60584552015-09-03 13:35:12 +00002404 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002405 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002406 size_t CurrentSuccessorIndex() const { return index_; }
2407 void Advance() { ++index_; }
2408
2409 private:
2410 const HBasicBlock& block_;
2411 size_t index_;
2412
2413 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2414};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002415
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002416// Deoptimize to interpreter, upon checking a condition.
2417class HDeoptimize : public HTemplateInstruction<1> {
2418 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002419 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2420 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002421 SetRawInputAt(0, cond);
2422 }
2423
2424 bool NeedsEnvironment() const OVERRIDE { return true; }
2425 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002426
2427 DECLARE_INSTRUCTION(Deoptimize);
2428
2429 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002430 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2431};
2432
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002433// Represents the ArtMethod that was passed as a first argument to
2434// the method. It is used by instructions that depend on it, like
2435// instructions that work with the dex cache.
2436class HCurrentMethod : public HExpression<0> {
2437 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002438 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2439 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002440
2441 DECLARE_INSTRUCTION(CurrentMethod);
2442
2443 private:
2444 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2445};
2446
Mark Mendellfe57faa2015-09-18 09:26:15 -04002447// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2448// have one successor for each entry in the switch table, and the final successor
2449// will be the block containing the next Dex opcode.
2450class HPackedSwitch : public HTemplateInstruction<1> {
2451 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002452 HPackedSwitch(int32_t start_value,
2453 uint32_t num_entries,
2454 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002455 uint32_t dex_pc = kNoDexPc)
2456 : HTemplateInstruction(SideEffects::None(), dex_pc),
2457 start_value_(start_value),
2458 num_entries_(num_entries) {
2459 SetRawInputAt(0, input);
2460 }
2461
2462 bool IsControlFlow() const OVERRIDE { return true; }
2463
2464 int32_t GetStartValue() const { return start_value_; }
2465
Vladimir Marko211c2112015-09-24 16:52:33 +01002466 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002467
2468 HBasicBlock* GetDefaultBlock() const {
2469 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002470 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002471 }
2472 DECLARE_INSTRUCTION(PackedSwitch);
2473
2474 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002475 const int32_t start_value_;
2476 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002477
2478 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2479};
2480
Roland Levillain88cb1752014-10-20 16:36:47 +01002481class HUnaryOperation : public HExpression<1> {
2482 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002483 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2484 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002485 SetRawInputAt(0, input);
2486 }
2487
2488 HInstruction* GetInput() const { return InputAt(0); }
2489 Primitive::Type GetResultType() const { return GetType(); }
2490
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002491 bool CanBeMoved() const OVERRIDE { return true; }
2492 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002493 UNUSED(other);
2494 return true;
2495 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002496
Roland Levillain9240d6a2014-10-20 16:47:04 +01002497 // Try to statically evaluate `operation` and return a HConstant
2498 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002499 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002500 HConstant* TryStaticEvaluation() const;
2501
2502 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002503 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2504 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002505
Roland Levillain88cb1752014-10-20 16:36:47 +01002506 DECLARE_INSTRUCTION(UnaryOperation);
2507
2508 private:
2509 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2510};
2511
Dave Allison20dfc792014-06-16 20:44:29 -07002512class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002513 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002514 HBinaryOperation(Primitive::Type result_type,
2515 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002516 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002517 SideEffects side_effects = SideEffects::None(),
2518 uint32_t dex_pc = kNoDexPc)
2519 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002520 SetRawInputAt(0, left);
2521 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002522 }
2523
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002524 HInstruction* GetLeft() const { return InputAt(0); }
2525 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002526 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002527
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002528 virtual bool IsCommutative() const { return false; }
2529
2530 // Put constant on the right.
2531 // Returns whether order is changed.
2532 bool OrderInputsWithConstantOnTheRight() {
2533 HInstruction* left = InputAt(0);
2534 HInstruction* right = InputAt(1);
2535 if (left->IsConstant() && !right->IsConstant()) {
2536 ReplaceInput(right, 0);
2537 ReplaceInput(left, 1);
2538 return true;
2539 }
2540 return false;
2541 }
2542
2543 // Order inputs by instruction id, but favor constant on the right side.
2544 // This helps GVN for commutative ops.
2545 void OrderInputs() {
2546 DCHECK(IsCommutative());
2547 HInstruction* left = InputAt(0);
2548 HInstruction* right = InputAt(1);
2549 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2550 return;
2551 }
2552 if (OrderInputsWithConstantOnTheRight()) {
2553 return;
2554 }
2555 // Order according to instruction id.
2556 if (left->GetId() > right->GetId()) {
2557 ReplaceInput(right, 0);
2558 ReplaceInput(left, 1);
2559 }
2560 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002561
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002562 bool CanBeMoved() const OVERRIDE { return true; }
2563 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002564 UNUSED(other);
2565 return true;
2566 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002567
Roland Levillain9240d6a2014-10-20 16:47:04 +01002568 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002569 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002570 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002571 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002572
2573 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002574 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2575 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2576 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2577 HLongConstant* y ATTRIBUTE_UNUSED) const {
2578 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2579 return nullptr;
2580 }
2581 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2582 HIntConstant* y ATTRIBUTE_UNUSED) const {
2583 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2584 return nullptr;
2585 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002586
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002587 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002588 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002589 HConstant* GetConstantRight() const;
2590
2591 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002592 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002593 HInstruction* GetLeastConstantLeft() const;
2594
Roland Levillainccc07a92014-09-16 14:48:16 +01002595 DECLARE_INSTRUCTION(BinaryOperation);
2596
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002597 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002598 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2599};
2600
Mark Mendellc4701932015-04-10 13:18:51 -04002601// The comparison bias applies for floating point operations and indicates how NaN
2602// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002603enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002604 kNoBias, // bias is not applicable (i.e. for long operation)
2605 kGtBias, // return 1 for NaN comparisons
2606 kLtBias, // return -1 for NaN comparisons
2607};
2608
Dave Allison20dfc792014-06-16 20:44:29 -07002609class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002610 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002611 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2612 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002613 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002614 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002615
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002616 bool NeedsMaterialization() const { return needs_materialization_; }
2617 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002618
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002619 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002620 // `instruction`, and disregard moves in between.
2621 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002622
Dave Allison20dfc792014-06-16 20:44:29 -07002623 DECLARE_INSTRUCTION(Condition);
2624
2625 virtual IfCondition GetCondition() const = 0;
2626
Mark Mendellc4701932015-04-10 13:18:51 -04002627 virtual IfCondition GetOppositeCondition() const = 0;
2628
Roland Levillain4fa13f62015-07-06 18:11:54 +01002629 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002630
2631 void SetBias(ComparisonBias bias) { bias_ = bias; }
2632
2633 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2634 return bias_ == other->AsCondition()->bias_;
2635 }
2636
Roland Levillain4fa13f62015-07-06 18:11:54 +01002637 bool IsFPConditionTrueIfNaN() const {
2638 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2639 IfCondition if_cond = GetCondition();
2640 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2641 }
2642
2643 bool IsFPConditionFalseIfNaN() const {
2644 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2645 IfCondition if_cond = GetCondition();
2646 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2647 }
2648
Dave Allison20dfc792014-06-16 20:44:29 -07002649 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002650 // For register allocation purposes, returns whether this instruction needs to be
2651 // materialized (that is, not just be in the processor flags).
2652 bool needs_materialization_;
2653
Mark Mendellc4701932015-04-10 13:18:51 -04002654 // Needed if we merge a HCompare into a HCondition.
2655 ComparisonBias bias_;
2656
Dave Allison20dfc792014-06-16 20:44:29 -07002657 DISALLOW_COPY_AND_ASSIGN(HCondition);
2658};
2659
2660// Instruction to check if two inputs are equal to each other.
2661class HEqual : public HCondition {
2662 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002663 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2664 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002665
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002666 bool IsCommutative() const OVERRIDE { return true; }
2667
Roland Levillain9867bc72015-08-05 10:21:34 +01002668 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002669 return GetBlock()->GetGraph()->GetIntConstant(
2670 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002671 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002672 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002673 return GetBlock()->GetGraph()->GetIntConstant(
2674 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002675 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002676
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002677 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002678
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002679 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002680 return kCondEQ;
2681 }
2682
Mark Mendellc4701932015-04-10 13:18:51 -04002683 IfCondition GetOppositeCondition() const OVERRIDE {
2684 return kCondNE;
2685 }
2686
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002687 private:
Aart Bike9f37602015-10-09 11:15:55 -07002688 template <typename T> bool Compute(T x, T y) const { return x == y; }
2689
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002690 DISALLOW_COPY_AND_ASSIGN(HEqual);
2691};
2692
Dave Allison20dfc792014-06-16 20:44:29 -07002693class HNotEqual : public HCondition {
2694 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002695 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2696 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002697
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002698 bool IsCommutative() const OVERRIDE { return true; }
2699
Roland Levillain9867bc72015-08-05 10:21:34 +01002700 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002701 return GetBlock()->GetGraph()->GetIntConstant(
2702 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002703 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002704 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002705 return GetBlock()->GetGraph()->GetIntConstant(
2706 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002707 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002708
Dave Allison20dfc792014-06-16 20:44:29 -07002709 DECLARE_INSTRUCTION(NotEqual);
2710
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002711 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002712 return kCondNE;
2713 }
2714
Mark Mendellc4701932015-04-10 13:18:51 -04002715 IfCondition GetOppositeCondition() const OVERRIDE {
2716 return kCondEQ;
2717 }
2718
Dave Allison20dfc792014-06-16 20:44:29 -07002719 private:
Aart Bike9f37602015-10-09 11:15:55 -07002720 template <typename T> bool Compute(T x, T y) const { return x != y; }
2721
Dave Allison20dfc792014-06-16 20:44:29 -07002722 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2723};
2724
2725class HLessThan : public HCondition {
2726 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002727 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2728 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002729
Roland Levillain9867bc72015-08-05 10:21:34 +01002730 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002731 return GetBlock()->GetGraph()->GetIntConstant(
2732 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002733 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002734 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002735 return GetBlock()->GetGraph()->GetIntConstant(
2736 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002737 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002738
Dave Allison20dfc792014-06-16 20:44:29 -07002739 DECLARE_INSTRUCTION(LessThan);
2740
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002741 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002742 return kCondLT;
2743 }
2744
Mark Mendellc4701932015-04-10 13:18:51 -04002745 IfCondition GetOppositeCondition() const OVERRIDE {
2746 return kCondGE;
2747 }
2748
Dave Allison20dfc792014-06-16 20:44:29 -07002749 private:
Aart Bike9f37602015-10-09 11:15:55 -07002750 template <typename T> bool Compute(T x, T y) const { return x < y; }
2751
Dave Allison20dfc792014-06-16 20:44:29 -07002752 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2753};
2754
2755class HLessThanOrEqual : public HCondition {
2756 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002757 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2758 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002759
Roland Levillain9867bc72015-08-05 10:21:34 +01002760 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002761 return GetBlock()->GetGraph()->GetIntConstant(
2762 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002763 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002764 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002765 return GetBlock()->GetGraph()->GetIntConstant(
2766 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002767 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002768
Dave Allison20dfc792014-06-16 20:44:29 -07002769 DECLARE_INSTRUCTION(LessThanOrEqual);
2770
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002771 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002772 return kCondLE;
2773 }
2774
Mark Mendellc4701932015-04-10 13:18:51 -04002775 IfCondition GetOppositeCondition() const OVERRIDE {
2776 return kCondGT;
2777 }
2778
Dave Allison20dfc792014-06-16 20:44:29 -07002779 private:
Aart Bike9f37602015-10-09 11:15:55 -07002780 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2781
Dave Allison20dfc792014-06-16 20:44:29 -07002782 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2783};
2784
2785class HGreaterThan : public HCondition {
2786 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002787 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2788 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002789
Roland Levillain9867bc72015-08-05 10:21:34 +01002790 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002791 return GetBlock()->GetGraph()->GetIntConstant(
2792 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002793 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002794 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002795 return GetBlock()->GetGraph()->GetIntConstant(
2796 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002797 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002798
Dave Allison20dfc792014-06-16 20:44:29 -07002799 DECLARE_INSTRUCTION(GreaterThan);
2800
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002801 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002802 return kCondGT;
2803 }
2804
Mark Mendellc4701932015-04-10 13:18:51 -04002805 IfCondition GetOppositeCondition() const OVERRIDE {
2806 return kCondLE;
2807 }
2808
Dave Allison20dfc792014-06-16 20:44:29 -07002809 private:
Aart Bike9f37602015-10-09 11:15:55 -07002810 template <typename T> bool Compute(T x, T y) const { return x > y; }
2811
Dave Allison20dfc792014-06-16 20:44:29 -07002812 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2813};
2814
2815class HGreaterThanOrEqual : public HCondition {
2816 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002817 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2818 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002819
Roland Levillain9867bc72015-08-05 10:21:34 +01002820 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002821 return GetBlock()->GetGraph()->GetIntConstant(
2822 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002823 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002824 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002825 return GetBlock()->GetGraph()->GetIntConstant(
2826 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002827 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002828
Dave Allison20dfc792014-06-16 20:44:29 -07002829 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2830
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002831 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002832 return kCondGE;
2833 }
2834
Mark Mendellc4701932015-04-10 13:18:51 -04002835 IfCondition GetOppositeCondition() const OVERRIDE {
2836 return kCondLT;
2837 }
2838
Dave Allison20dfc792014-06-16 20:44:29 -07002839 private:
Aart Bike9f37602015-10-09 11:15:55 -07002840 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2841
Dave Allison20dfc792014-06-16 20:44:29 -07002842 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2843};
2844
Aart Bike9f37602015-10-09 11:15:55 -07002845class HBelow : public HCondition {
2846 public:
2847 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2848 : HCondition(first, second, dex_pc) {}
2849
2850 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2851 return GetBlock()->GetGraph()->GetIntConstant(
2852 Compute(static_cast<uint32_t>(x->GetValue()),
2853 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2854 }
2855 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2856 return GetBlock()->GetGraph()->GetIntConstant(
2857 Compute(static_cast<uint64_t>(x->GetValue()),
2858 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2859 }
2860
2861 DECLARE_INSTRUCTION(Below);
2862
2863 IfCondition GetCondition() const OVERRIDE {
2864 return kCondB;
2865 }
2866
2867 IfCondition GetOppositeCondition() const OVERRIDE {
2868 return kCondAE;
2869 }
2870
2871 private:
2872 template <typename T> bool Compute(T x, T y) const { return x < y; }
2873
2874 DISALLOW_COPY_AND_ASSIGN(HBelow);
2875};
2876
2877class HBelowOrEqual : public HCondition {
2878 public:
2879 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2880 : HCondition(first, second, dex_pc) {}
2881
2882 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2883 return GetBlock()->GetGraph()->GetIntConstant(
2884 Compute(static_cast<uint32_t>(x->GetValue()),
2885 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2886 }
2887 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2888 return GetBlock()->GetGraph()->GetIntConstant(
2889 Compute(static_cast<uint64_t>(x->GetValue()),
2890 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2891 }
2892
2893 DECLARE_INSTRUCTION(BelowOrEqual);
2894
2895 IfCondition GetCondition() const OVERRIDE {
2896 return kCondBE;
2897 }
2898
2899 IfCondition GetOppositeCondition() const OVERRIDE {
2900 return kCondA;
2901 }
2902
2903 private:
2904 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2905
2906 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2907};
2908
2909class HAbove : public HCondition {
2910 public:
2911 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2912 : HCondition(first, second, dex_pc) {}
2913
2914 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2915 return GetBlock()->GetGraph()->GetIntConstant(
2916 Compute(static_cast<uint32_t>(x->GetValue()),
2917 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2918 }
2919 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2920 return GetBlock()->GetGraph()->GetIntConstant(
2921 Compute(static_cast<uint64_t>(x->GetValue()),
2922 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2923 }
2924
2925 DECLARE_INSTRUCTION(Above);
2926
2927 IfCondition GetCondition() const OVERRIDE {
2928 return kCondA;
2929 }
2930
2931 IfCondition GetOppositeCondition() const OVERRIDE {
2932 return kCondBE;
2933 }
2934
2935 private:
2936 template <typename T> bool Compute(T x, T y) const { return x > y; }
2937
2938 DISALLOW_COPY_AND_ASSIGN(HAbove);
2939};
2940
2941class HAboveOrEqual : public HCondition {
2942 public:
2943 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2944 : HCondition(first, second, dex_pc) {}
2945
2946 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2947 return GetBlock()->GetGraph()->GetIntConstant(
2948 Compute(static_cast<uint32_t>(x->GetValue()),
2949 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2950 }
2951 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2952 return GetBlock()->GetGraph()->GetIntConstant(
2953 Compute(static_cast<uint64_t>(x->GetValue()),
2954 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2955 }
2956
2957 DECLARE_INSTRUCTION(AboveOrEqual);
2958
2959 IfCondition GetCondition() const OVERRIDE {
2960 return kCondAE;
2961 }
2962
2963 IfCondition GetOppositeCondition() const OVERRIDE {
2964 return kCondB;
2965 }
2966
2967 private:
2968 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2969
2970 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
2971};
Dave Allison20dfc792014-06-16 20:44:29 -07002972
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002973// Instruction to check how two inputs compare to each other.
2974// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2975class HCompare : public HBinaryOperation {
2976 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002977 HCompare(Primitive::Type type,
2978 HInstruction* first,
2979 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002980 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002981 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002982 : HBinaryOperation(Primitive::kPrimInt,
2983 first,
2984 second,
2985 SideEffectsForArchRuntimeCalls(type),
2986 dex_pc),
2987 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002988 DCHECK_EQ(type, first->GetType());
2989 DCHECK_EQ(type, second->GetType());
2990 }
2991
Roland Levillain9867bc72015-08-05 10:21:34 +01002992 template <typename T>
2993 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002994
Roland Levillain9867bc72015-08-05 10:21:34 +01002995 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002996 return GetBlock()->GetGraph()->GetIntConstant(
2997 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002998 }
2999 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003000 return GetBlock()->GetGraph()->GetIntConstant(
3001 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003002 }
3003
Calin Juravleddb7df22014-11-25 20:56:51 +00003004 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3005 return bias_ == other->AsCompare()->bias_;
3006 }
3007
Mark Mendellc4701932015-04-10 13:18:51 -04003008 ComparisonBias GetBias() const { return bias_; }
3009
Roland Levillain4fa13f62015-07-06 18:11:54 +01003010 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003011
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003012
3013 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3014 // MIPS64 uses a runtime call for FP comparisons.
3015 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3016 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003017
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003018 DECLARE_INSTRUCTION(Compare);
3019
3020 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003021 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003022
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003023 DISALLOW_COPY_AND_ASSIGN(HCompare);
3024};
3025
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003026// A local in the graph. Corresponds to a Dex register.
3027class HLocal : public HTemplateInstruction<0> {
3028 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003029 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003030 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003031
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003032 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003033
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003034 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003035
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003036 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003037 // The Dex register number.
3038 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003039
3040 DISALLOW_COPY_AND_ASSIGN(HLocal);
3041};
3042
3043// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003044class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003045 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003046 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3047 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003048 SetRawInputAt(0, local);
3049 }
3050
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003051 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3052
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003053 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003054
3055 private:
3056 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3057};
3058
3059// Store a value in a given local. This instruction has two inputs: the value
3060// and the local.
3061class HStoreLocal : public HTemplateInstruction<2> {
3062 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003063 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3064 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003065 SetRawInputAt(0, local);
3066 SetRawInputAt(1, value);
3067 }
3068
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003069 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3070
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003071 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003072
3073 private:
3074 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3075};
3076
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003077class HFloatConstant : public HConstant {
3078 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003079 float GetValue() const { return value_; }
3080
David Brazdil77a48ae2015-09-15 12:34:04 +00003081 uint64_t GetValueAsUint64() const OVERRIDE {
3082 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3083 }
3084
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003085 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003086 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003087 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003088 }
3089
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003090 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003091
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003092 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003093 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003094 }
3095 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003096 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003097 }
3098 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003099 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3100 }
3101 bool IsNaN() const {
3102 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003103 }
3104
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003105 DECLARE_INSTRUCTION(FloatConstant);
3106
3107 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003108 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3109 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3110 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3111 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003112
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003113 const float value_;
3114
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003115 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003116 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003117 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003118 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3119};
3120
3121class HDoubleConstant : public HConstant {
3122 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003123 double GetValue() const { return value_; }
3124
David Brazdil77a48ae2015-09-15 12:34:04 +00003125 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3126
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003127 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003128 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003129 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003130 }
3131
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003132 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003133
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003134 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003135 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003136 }
3137 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003138 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003139 }
3140 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003141 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3142 }
3143 bool IsNaN() const {
3144 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003145 }
3146
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003147 DECLARE_INSTRUCTION(DoubleConstant);
3148
3149 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003150 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3151 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3152 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3153 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003154
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003155 const double value_;
3156
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003157 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003158 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003159 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003160 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3161};
3162
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003163enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003164#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003165#include "intrinsics_list.h"
3166 kNone,
3167 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3168#undef INTRINSICS_LIST
3169#undef OPTIMIZING_INTRINSICS
3170};
3171std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3172
Agi Csaki05f20562015-08-19 14:58:14 -07003173enum IntrinsicNeedsEnvironmentOrCache {
3174 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3175 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003176};
3177
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003178class HInvoke : public HInstruction {
3179 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003180 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003181
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003182 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003183
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003184 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003185 SetRawInputAt(index, argument);
3186 }
3187
Roland Levillain3e3d7332015-04-28 11:00:54 +01003188 // Return the number of arguments. This number can be lower than
3189 // the number of inputs returned by InputCount(), as some invoke
3190 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3191 // inputs at the end of their list of inputs.
3192 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3193
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003194 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003195
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003196
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003197 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003198 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003199
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003200 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3201
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003202 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003203 return intrinsic_;
3204 }
3205
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003206 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003207
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003208 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003209 return GetEnvironment()->GetParent() != nullptr;
3210 }
3211
3212 bool CanThrow() const OVERRIDE { return true; }
3213
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003214 uint32_t* GetIntrinsicOptimizations() {
3215 return &intrinsic_optimizations_;
3216 }
3217
3218 const uint32_t* GetIntrinsicOptimizations() const {
3219 return &intrinsic_optimizations_;
3220 }
3221
3222 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3223
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003224 DECLARE_INSTRUCTION(Invoke);
3225
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003226 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003227 HInvoke(ArenaAllocator* arena,
3228 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003229 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003230 Primitive::Type return_type,
3231 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003232 uint32_t dex_method_index,
3233 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003234 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003235 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003236 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003237 inputs_(number_of_arguments + number_of_other_inputs,
3238 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003239 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003240 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003241 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003242 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003243 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003244 }
3245
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003246 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003247 return inputs_[index];
3248 }
3249
David Brazdil1abb4192015-02-17 18:33:36 +00003250 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003251 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003252 }
3253
Roland Levillain3e3d7332015-04-28 11:00:54 +01003254 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003255 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003256 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003257 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003258 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003259 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003260
3261 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3262 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003263
3264 private:
3265 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3266};
3267
Calin Juravle175dc732015-08-25 15:42:32 +01003268class HInvokeUnresolved : public HInvoke {
3269 public:
3270 HInvokeUnresolved(ArenaAllocator* arena,
3271 uint32_t number_of_arguments,
3272 Primitive::Type return_type,
3273 uint32_t dex_pc,
3274 uint32_t dex_method_index,
3275 InvokeType invoke_type)
3276 : HInvoke(arena,
3277 number_of_arguments,
3278 0u /* number_of_other_inputs */,
3279 return_type,
3280 dex_pc,
3281 dex_method_index,
3282 invoke_type) {
3283 }
3284
3285 DECLARE_INSTRUCTION(InvokeUnresolved);
3286
3287 private:
3288 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3289};
3290
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003291class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003292 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003293 // Requirements of this method call regarding the class
3294 // initialization (clinit) check of its declaring class.
3295 enum class ClinitCheckRequirement {
3296 kNone, // Class already initialized.
3297 kExplicit, // Static call having explicit clinit check as last input.
3298 kImplicit, // Static call implicitly requiring a clinit check.
3299 };
3300
Vladimir Marko58155012015-08-19 12:49:41 +00003301 // Determines how to load the target ArtMethod*.
3302 enum class MethodLoadKind {
3303 // Use a String init ArtMethod* loaded from Thread entrypoints.
3304 kStringInit,
3305
3306 // Use the method's own ArtMethod* loaded by the register allocator.
3307 kRecursive,
3308
3309 // Use ArtMethod* at a known address, embed the direct address in the code.
3310 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3311 kDirectAddress,
3312
3313 // Use ArtMethod* at an address that will be known at link time, embed the direct
3314 // address in the code. If the image is relocatable, emit .patch_oat entry.
3315 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3316 // the image relocatable or not.
3317 kDirectAddressWithFixup,
3318
3319 // Load from resoved methods array in the dex cache using a PC-relative load.
3320 // Used when we need to use the dex cache, for example for invoke-static that
3321 // may cause class initialization (the entry may point to a resolution method),
3322 // and we know that we can access the dex cache arrays using a PC-relative load.
3323 kDexCachePcRelative,
3324
3325 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3326 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3327 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3328 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3329 kDexCacheViaMethod,
3330 };
3331
3332 // Determines the location of the code pointer.
3333 enum class CodePtrLocation {
3334 // Recursive call, use local PC-relative call instruction.
3335 kCallSelf,
3336
3337 // Use PC-relative call instruction patched at link time.
3338 // Used for calls within an oat file, boot->boot or app->app.
3339 kCallPCRelative,
3340
3341 // Call to a known target address, embed the direct address in code.
3342 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3343 kCallDirect,
3344
3345 // Call to a target address that will be known at link time, embed the direct
3346 // address in code. If the image is relocatable, emit .patch_oat entry.
3347 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3348 // the image relocatable or not.
3349 kCallDirectWithFixup,
3350
3351 // Use code pointer from the ArtMethod*.
3352 // Used when we don't know the target code. This is also the last-resort-kind used when
3353 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3354 kCallArtMethod,
3355 };
3356
3357 struct DispatchInfo {
3358 const MethodLoadKind method_load_kind;
3359 const CodePtrLocation code_ptr_location;
3360 // The method load data holds
3361 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3362 // Note that there are multiple string init methods, each having its own offset.
3363 // - the method address for kDirectAddress
3364 // - the dex cache arrays offset for kDexCachePcRel.
3365 const uint64_t method_load_data;
3366 const uint64_t direct_code_ptr;
3367 };
3368
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003369 HInvokeStaticOrDirect(ArenaAllocator* arena,
3370 uint32_t number_of_arguments,
3371 Primitive::Type return_type,
3372 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003373 uint32_t method_index,
3374 MethodReference target_method,
3375 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003376 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003377 InvokeType invoke_type,
3378 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003379 : HInvoke(arena,
3380 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003381 // There is one extra argument for the HCurrentMethod node, and
3382 // potentially one other if the clinit check is explicit, and one other
3383 // if the method is a string factory.
3384 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003385 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003386 return_type,
3387 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003388 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003389 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003390 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003391 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003392 target_method_(target_method),
3393 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003394
Calin Juravle641547a2015-04-21 22:08:51 +01003395 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3396 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003397 // We access the method via the dex cache so we can't do an implicit null check.
3398 // TODO: for intrinsics we can generate implicit null checks.
3399 return false;
3400 }
3401
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003402 bool CanBeNull() const OVERRIDE {
3403 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3404 }
3405
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003406 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003407 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3408 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3409 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003410 bool NeedsDexCache() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003411 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003412 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003413 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3414 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3415 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3416 MethodReference GetTargetMethod() const { return target_method_; }
3417
3418 int32_t GetStringInitOffset() const {
3419 DCHECK(IsStringInit());
3420 return dispatch_info_.method_load_data;
3421 }
3422
3423 uint64_t GetMethodAddress() const {
3424 DCHECK(HasMethodAddress());
3425 return dispatch_info_.method_load_data;
3426 }
3427
3428 uint32_t GetDexCacheArrayOffset() const {
3429 DCHECK(HasPcRelDexCache());
3430 return dispatch_info_.method_load_data;
3431 }
3432
3433 uint64_t GetDirectCodePtr() const {
3434 DCHECK(HasDirectCodePtr());
3435 return dispatch_info_.direct_code_ptr;
3436 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003437
Calin Juravle68ad6492015-08-18 17:08:12 +01003438 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3439
Roland Levillain4c0eb422015-04-24 16:43:49 +01003440 // Is this instruction a call to a static method?
3441 bool IsStatic() const {
3442 return GetInvokeType() == kStatic;
3443 }
3444
Roland Levillain3e3d7332015-04-28 11:00:54 +01003445 // Remove the art::HLoadClass instruction set as last input by
3446 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3447 // the initial art::HClinitCheck instruction (only relevant for
3448 // static calls with explicit clinit check).
3449 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003450 DCHECK(IsStaticWithExplicitClinitCheck());
3451 size_t last_input_index = InputCount() - 1;
3452 HInstruction* last_input = InputAt(last_input_index);
3453 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003454 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003455 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003456 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003457 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3458 DCHECK(IsStaticWithImplicitClinitCheck());
3459 }
3460
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003461 bool IsStringFactoryFor(HFakeString* str) const {
3462 if (!IsStringInit()) return false;
3463 // +1 for the current method.
3464 if (InputCount() == (number_of_arguments_ + 1)) return false;
3465 return InputAt(InputCount() - 1)->AsFakeString() == str;
3466 }
3467
3468 void RemoveFakeStringArgumentAsLastInput() {
3469 DCHECK(IsStringInit());
3470 size_t last_input_index = InputCount() - 1;
3471 HInstruction* last_input = InputAt(last_input_index);
3472 DCHECK(last_input != nullptr);
3473 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3474 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003475 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003476 }
3477
Roland Levillain4c0eb422015-04-24 16:43:49 +01003478 // Is this a call to a static method whose declaring class has an
3479 // explicit intialization check in the graph?
3480 bool IsStaticWithExplicitClinitCheck() const {
3481 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3482 }
3483
3484 // Is this a call to a static method whose declaring class has an
3485 // implicit intialization check requirement?
3486 bool IsStaticWithImplicitClinitCheck() const {
3487 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3488 }
3489
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003490 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003491
Roland Levillain4c0eb422015-04-24 16:43:49 +01003492 protected:
3493 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3494 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3495 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3496 HInstruction* input = input_record.GetInstruction();
3497 // `input` is the last input of a static invoke marked as having
3498 // an explicit clinit check. It must either be:
3499 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3500 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3501 DCHECK(input != nullptr);
3502 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3503 }
3504 return input_record;
3505 }
3506
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003507 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003508 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003509 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003510 // The target method may refer to different dex file or method index than the original
3511 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3512 // in derived class to increase visibility.
3513 MethodReference target_method_;
3514 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003515
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003516 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003517};
3518
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003519class HInvokeVirtual : public HInvoke {
3520 public:
3521 HInvokeVirtual(ArenaAllocator* arena,
3522 uint32_t number_of_arguments,
3523 Primitive::Type return_type,
3524 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003525 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003526 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003527 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003528 vtable_index_(vtable_index) {}
3529
Calin Juravle641547a2015-04-21 22:08:51 +01003530 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003531 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003532 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003533 }
3534
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003535 uint32_t GetVTableIndex() const { return vtable_index_; }
3536
3537 DECLARE_INSTRUCTION(InvokeVirtual);
3538
3539 private:
3540 const uint32_t vtable_index_;
3541
3542 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3543};
3544
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003545class HInvokeInterface : public HInvoke {
3546 public:
3547 HInvokeInterface(ArenaAllocator* arena,
3548 uint32_t number_of_arguments,
3549 Primitive::Type return_type,
3550 uint32_t dex_pc,
3551 uint32_t dex_method_index,
3552 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003553 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003554 imt_index_(imt_index) {}
3555
Calin Juravle641547a2015-04-21 22:08:51 +01003556 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003557 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003558 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003559 }
3560
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003561 uint32_t GetImtIndex() const { return imt_index_; }
3562 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3563
3564 DECLARE_INSTRUCTION(InvokeInterface);
3565
3566 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003567 const uint32_t imt_index_;
3568
3569 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3570};
3571
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003572class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003573 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003574 HNewInstance(HCurrentMethod* current_method,
3575 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003576 uint16_t type_index,
3577 const DexFile& dex_file,
3578 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003579 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003580 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003581 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003582 entrypoint_(entrypoint) {
3583 SetRawInputAt(0, current_method);
3584 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003585
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003586 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003587 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003588
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003589 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003590 bool NeedsEnvironment() const OVERRIDE { return true; }
3591 // It may throw when called on:
3592 // - interfaces
3593 // - abstract/innaccessible/unknown classes
3594 // TODO: optimize when possible.
3595 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003596
Calin Juravle10e244f2015-01-26 18:54:32 +00003597 bool CanBeNull() const OVERRIDE { return false; }
3598
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003599 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3600
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003601 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003602
3603 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003604 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003605 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003606 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003607
3608 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3609};
3610
Roland Levillain88cb1752014-10-20 16:36:47 +01003611class HNeg : public HUnaryOperation {
3612 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003613 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3614 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003615
Roland Levillain9867bc72015-08-05 10:21:34 +01003616 template <typename T> T Compute(T x) const { return -x; }
3617
3618 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003619 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003620 }
3621 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003622 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003623 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003624
Roland Levillain88cb1752014-10-20 16:36:47 +01003625 DECLARE_INSTRUCTION(Neg);
3626
3627 private:
3628 DISALLOW_COPY_AND_ASSIGN(HNeg);
3629};
3630
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003631class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003632 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003633 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003634 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003635 uint32_t dex_pc,
3636 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003637 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003638 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003639 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003640 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003641 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003642 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003643 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003644 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003645 }
3646
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003647 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003648 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003649
3650 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003651 bool NeedsEnvironment() const OVERRIDE { return true; }
3652
Mingyao Yang0c365e62015-03-31 15:09:29 -07003653 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3654 bool CanThrow() const OVERRIDE { return true; }
3655
Calin Juravle10e244f2015-01-26 18:54:32 +00003656 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003657
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003658 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3659
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003660 DECLARE_INSTRUCTION(NewArray);
3661
3662 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003663 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003664 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003665 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003666
3667 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3668};
3669
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003670class HAdd : public HBinaryOperation {
3671 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003672 HAdd(Primitive::Type result_type,
3673 HInstruction* left,
3674 HInstruction* right,
3675 uint32_t dex_pc = kNoDexPc)
3676 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003677
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003678 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003679
Roland Levillain9867bc72015-08-05 10:21:34 +01003680 template <typename T> T Compute(T x, T y) const { return x + y; }
3681
3682 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003683 return GetBlock()->GetGraph()->GetIntConstant(
3684 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003685 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003686 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003687 return GetBlock()->GetGraph()->GetLongConstant(
3688 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003689 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003690
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003691 DECLARE_INSTRUCTION(Add);
3692
3693 private:
3694 DISALLOW_COPY_AND_ASSIGN(HAdd);
3695};
3696
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003697class HSub : public HBinaryOperation {
3698 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003699 HSub(Primitive::Type result_type,
3700 HInstruction* left,
3701 HInstruction* right,
3702 uint32_t dex_pc = kNoDexPc)
3703 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003704
Roland Levillain9867bc72015-08-05 10:21:34 +01003705 template <typename T> T Compute(T x, T y) const { return x - y; }
3706
3707 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003708 return GetBlock()->GetGraph()->GetIntConstant(
3709 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003710 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003711 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003712 return GetBlock()->GetGraph()->GetLongConstant(
3713 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003714 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003715
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003716 DECLARE_INSTRUCTION(Sub);
3717
3718 private:
3719 DISALLOW_COPY_AND_ASSIGN(HSub);
3720};
3721
Calin Juravle34bacdf2014-10-07 20:23:36 +01003722class HMul : public HBinaryOperation {
3723 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003724 HMul(Primitive::Type result_type,
3725 HInstruction* left,
3726 HInstruction* right,
3727 uint32_t dex_pc = kNoDexPc)
3728 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003729
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003730 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003731
Roland Levillain9867bc72015-08-05 10:21:34 +01003732 template <typename T> T Compute(T x, T y) const { return x * y; }
3733
3734 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003735 return GetBlock()->GetGraph()->GetIntConstant(
3736 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003737 }
3738 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003739 return GetBlock()->GetGraph()->GetLongConstant(
3740 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003741 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003742
3743 DECLARE_INSTRUCTION(Mul);
3744
3745 private:
3746 DISALLOW_COPY_AND_ASSIGN(HMul);
3747};
3748
Calin Juravle7c4954d2014-10-28 16:57:40 +00003749class HDiv : public HBinaryOperation {
3750 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003751 HDiv(Primitive::Type result_type,
3752 HInstruction* left,
3753 HInstruction* right,
3754 uint32_t dex_pc)
3755 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003756
Roland Levillain9867bc72015-08-05 10:21:34 +01003757 template <typename T>
3758 T Compute(T x, T y) const {
3759 // Our graph structure ensures we never have 0 for `y` during
3760 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003761 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003762 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003763 return (y == -1) ? -x : x / y;
3764 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003765
Roland Levillain9867bc72015-08-05 10:21:34 +01003766 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003767 return GetBlock()->GetGraph()->GetIntConstant(
3768 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003769 }
3770 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003771 return GetBlock()->GetGraph()->GetLongConstant(
3772 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003773 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003774
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003775 static SideEffects SideEffectsForArchRuntimeCalls() {
3776 // The generated code can use a runtime call.
3777 return SideEffects::CanTriggerGC();
3778 }
3779
Calin Juravle7c4954d2014-10-28 16:57:40 +00003780 DECLARE_INSTRUCTION(Div);
3781
3782 private:
3783 DISALLOW_COPY_AND_ASSIGN(HDiv);
3784};
3785
Calin Juravlebacfec32014-11-14 15:54:36 +00003786class HRem : public HBinaryOperation {
3787 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003788 HRem(Primitive::Type result_type,
3789 HInstruction* left,
3790 HInstruction* right,
3791 uint32_t dex_pc)
3792 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003793
Roland Levillain9867bc72015-08-05 10:21:34 +01003794 template <typename T>
3795 T Compute(T x, T y) const {
3796 // Our graph structure ensures we never have 0 for `y` during
3797 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003798 DCHECK_NE(y, 0);
3799 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3800 return (y == -1) ? 0 : x % y;
3801 }
3802
Roland Levillain9867bc72015-08-05 10:21:34 +01003803 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003804 return GetBlock()->GetGraph()->GetIntConstant(
3805 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003806 }
3807 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003808 return GetBlock()->GetGraph()->GetLongConstant(
3809 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003810 }
3811
Calin Juravlebacfec32014-11-14 15:54:36 +00003812
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003813 static SideEffects SideEffectsForArchRuntimeCalls() {
3814 return SideEffects::CanTriggerGC();
3815 }
3816
Calin Juravlebacfec32014-11-14 15:54:36 +00003817 DECLARE_INSTRUCTION(Rem);
3818
3819 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003820 DISALLOW_COPY_AND_ASSIGN(HRem);
3821};
3822
Calin Juravled0d48522014-11-04 16:40:20 +00003823class HDivZeroCheck : public HExpression<1> {
3824 public:
3825 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003826 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003827 SetRawInputAt(0, value);
3828 }
3829
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003830 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3831
Calin Juravled0d48522014-11-04 16:40:20 +00003832 bool CanBeMoved() const OVERRIDE { return true; }
3833
3834 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3835 UNUSED(other);
3836 return true;
3837 }
3838
3839 bool NeedsEnvironment() const OVERRIDE { return true; }
3840 bool CanThrow() const OVERRIDE { return true; }
3841
Calin Juravled0d48522014-11-04 16:40:20 +00003842 DECLARE_INSTRUCTION(DivZeroCheck);
3843
3844 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003845 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3846};
3847
Calin Juravle9aec02f2014-11-18 23:06:35 +00003848class HShl : public HBinaryOperation {
3849 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003850 HShl(Primitive::Type result_type,
3851 HInstruction* left,
3852 HInstruction* right,
3853 uint32_t dex_pc = kNoDexPc)
3854 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003855
Roland Levillain9867bc72015-08-05 10:21:34 +01003856 template <typename T, typename U, typename V>
3857 T Compute(T x, U y, V max_shift_value) const {
3858 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3859 "V is not the unsigned integer type corresponding to T");
3860 return x << (y & max_shift_value);
3861 }
3862
3863 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3864 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003865 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003866 }
3867 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3868 // case is handled as `x << static_cast<int>(y)`.
3869 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3870 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003871 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003872 }
3873 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3874 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003875 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003876 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003877
3878 DECLARE_INSTRUCTION(Shl);
3879
3880 private:
3881 DISALLOW_COPY_AND_ASSIGN(HShl);
3882};
3883
3884class HShr : public HBinaryOperation {
3885 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003886 HShr(Primitive::Type result_type,
3887 HInstruction* left,
3888 HInstruction* right,
3889 uint32_t dex_pc = kNoDexPc)
3890 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003891
Roland Levillain9867bc72015-08-05 10:21:34 +01003892 template <typename T, typename U, typename V>
3893 T Compute(T x, U y, V max_shift_value) const {
3894 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3895 "V is not the unsigned integer type corresponding to T");
3896 return x >> (y & max_shift_value);
3897 }
3898
3899 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3900 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003901 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003902 }
3903 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3904 // case is handled as `x >> static_cast<int>(y)`.
3905 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3906 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003907 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003908 }
3909 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3910 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003911 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003912 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003913
3914 DECLARE_INSTRUCTION(Shr);
3915
3916 private:
3917 DISALLOW_COPY_AND_ASSIGN(HShr);
3918};
3919
3920class HUShr : public HBinaryOperation {
3921 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003922 HUShr(Primitive::Type result_type,
3923 HInstruction* left,
3924 HInstruction* right,
3925 uint32_t dex_pc = kNoDexPc)
3926 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003927
Roland Levillain9867bc72015-08-05 10:21:34 +01003928 template <typename T, typename U, typename V>
3929 T Compute(T x, U y, V max_shift_value) const {
3930 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3931 "V is not the unsigned integer type corresponding to T");
3932 V ux = static_cast<V>(x);
3933 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003934 }
3935
Roland Levillain9867bc72015-08-05 10:21:34 +01003936 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3937 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003938 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003939 }
3940 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3941 // case is handled as `x >>> static_cast<int>(y)`.
3942 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3943 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003944 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003945 }
3946 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3947 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003948 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003949 }
3950
3951 DECLARE_INSTRUCTION(UShr);
3952
3953 private:
3954 DISALLOW_COPY_AND_ASSIGN(HUShr);
3955};
3956
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003957class HAnd : public HBinaryOperation {
3958 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003959 HAnd(Primitive::Type result_type,
3960 HInstruction* left,
3961 HInstruction* right,
3962 uint32_t dex_pc = kNoDexPc)
3963 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003964
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003965 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003966
Roland Levillain9867bc72015-08-05 10:21:34 +01003967 template <typename T, typename U>
3968 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3969
3970 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003971 return GetBlock()->GetGraph()->GetIntConstant(
3972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003973 }
3974 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003975 return GetBlock()->GetGraph()->GetLongConstant(
3976 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003977 }
3978 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003979 return GetBlock()->GetGraph()->GetLongConstant(
3980 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003981 }
3982 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003983 return GetBlock()->GetGraph()->GetLongConstant(
3984 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003985 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003986
3987 DECLARE_INSTRUCTION(And);
3988
3989 private:
3990 DISALLOW_COPY_AND_ASSIGN(HAnd);
3991};
3992
3993class HOr : public HBinaryOperation {
3994 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003995 HOr(Primitive::Type result_type,
3996 HInstruction* left,
3997 HInstruction* right,
3998 uint32_t dex_pc = kNoDexPc)
3999 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004000
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004001 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004002
Roland Levillain9867bc72015-08-05 10:21:34 +01004003 template <typename T, typename U>
4004 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4005
4006 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004007 return GetBlock()->GetGraph()->GetIntConstant(
4008 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004009 }
4010 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004011 return GetBlock()->GetGraph()->GetLongConstant(
4012 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004013 }
4014 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004015 return GetBlock()->GetGraph()->GetLongConstant(
4016 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004017 }
4018 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004019 return GetBlock()->GetGraph()->GetLongConstant(
4020 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004021 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004022
4023 DECLARE_INSTRUCTION(Or);
4024
4025 private:
4026 DISALLOW_COPY_AND_ASSIGN(HOr);
4027};
4028
4029class HXor : public HBinaryOperation {
4030 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004031 HXor(Primitive::Type result_type,
4032 HInstruction* left,
4033 HInstruction* right,
4034 uint32_t dex_pc = kNoDexPc)
4035 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004036
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004037 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004038
Roland Levillain9867bc72015-08-05 10:21:34 +01004039 template <typename T, typename U>
4040 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4041
4042 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004043 return GetBlock()->GetGraph()->GetIntConstant(
4044 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004045 }
4046 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004047 return GetBlock()->GetGraph()->GetLongConstant(
4048 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004049 }
4050 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004051 return GetBlock()->GetGraph()->GetLongConstant(
4052 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004053 }
4054 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004055 return GetBlock()->GetGraph()->GetLongConstant(
4056 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004057 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004058
4059 DECLARE_INSTRUCTION(Xor);
4060
4061 private:
4062 DISALLOW_COPY_AND_ASSIGN(HXor);
4063};
4064
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004065// The value of a parameter in this method. Its location depends on
4066// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004067class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004068 public:
Calin Juravlec05aca72015-10-13 13:10:33 +00004069 HParameterValue(uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 Primitive::Type parameter_type,
4071 bool is_this = false)
4072 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004073 index_(index),
4074 is_this_(is_this),
4075 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004076
4077 uint8_t GetIndex() const { return index_; }
4078
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004079 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4080 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004081
Calin Juravlec05aca72015-10-13 13:10:33 +00004082 bool IsThis() const { return is_this_; }
4083
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004084 DECLARE_INSTRUCTION(ParameterValue);
4085
4086 private:
4087 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004088 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004089 const uint8_t index_;
4090
Calin Juravle10e244f2015-01-26 18:54:32 +00004091 // Whether or not the parameter value corresponds to 'this' argument.
4092 const bool is_this_;
4093
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004094 bool can_be_null_;
4095
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004096 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4097};
4098
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004099class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004100 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004101 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4102 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004103
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004104 bool CanBeMoved() const OVERRIDE { return true; }
4105 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004106 UNUSED(other);
4107 return true;
4108 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004109
Roland Levillain9867bc72015-08-05 10:21:34 +01004110 template <typename T> T Compute(T x) const { return ~x; }
4111
4112 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004113 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004114 }
4115 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004116 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004117 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004118
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004119 DECLARE_INSTRUCTION(Not);
4120
4121 private:
4122 DISALLOW_COPY_AND_ASSIGN(HNot);
4123};
4124
David Brazdil66d126e2015-04-03 16:02:44 +01004125class HBooleanNot : public HUnaryOperation {
4126 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004127 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4128 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004129
4130 bool CanBeMoved() const OVERRIDE { return true; }
4131 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4132 UNUSED(other);
4133 return true;
4134 }
4135
Roland Levillain9867bc72015-08-05 10:21:34 +01004136 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004137 DCHECK(IsUint<1>(x));
4138 return !x;
4139 }
4140
Roland Levillain9867bc72015-08-05 10:21:34 +01004141 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004142 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004143 }
4144 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4145 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004146 UNREACHABLE();
4147 }
4148
4149 DECLARE_INSTRUCTION(BooleanNot);
4150
4151 private:
4152 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4153};
4154
Roland Levillaindff1f282014-11-05 14:15:05 +00004155class HTypeConversion : public HExpression<1> {
4156 public:
4157 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004158 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004159 : HExpression(result_type,
4160 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4161 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004162 SetRawInputAt(0, input);
4163 DCHECK_NE(input->GetType(), result_type);
4164 }
4165
4166 HInstruction* GetInput() const { return InputAt(0); }
4167 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4168 Primitive::Type GetResultType() const { return GetType(); }
4169
Roland Levillain624279f2014-12-04 11:54:28 +00004170 // Required by the x86 and ARM code generators when producing calls
4171 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004172
Roland Levillaindff1f282014-11-05 14:15:05 +00004173 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004174 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004175
Mark Mendelle82549b2015-05-06 10:55:34 -04004176 // Try to statically evaluate the conversion and return a HConstant
4177 // containing the result. If the input cannot be converted, return nullptr.
4178 HConstant* TryStaticEvaluation() const;
4179
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004180 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4181 Primitive::Type result_type) {
4182 // Some architectures may not require the 'GC' side effects, but at this point
4183 // in the compilation process we do not know what architecture we will
4184 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004185 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4186 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004187 return SideEffects::CanTriggerGC();
4188 }
4189 return SideEffects::None();
4190 }
4191
Roland Levillaindff1f282014-11-05 14:15:05 +00004192 DECLARE_INSTRUCTION(TypeConversion);
4193
4194 private:
4195 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4196};
4197
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004198static constexpr uint32_t kNoRegNumber = -1;
4199
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004200class HPhi : public HInstruction {
4201 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004202 HPhi(ArenaAllocator* arena,
4203 uint32_t reg_number,
4204 size_t number_of_inputs,
4205 Primitive::Type type,
4206 uint32_t dex_pc = kNoDexPc)
4207 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004208 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004209 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004210 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004211 is_live_(false),
4212 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004213 }
4214
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004215 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4216 static Primitive::Type ToPhiType(Primitive::Type type) {
4217 switch (type) {
4218 case Primitive::kPrimBoolean:
4219 case Primitive::kPrimByte:
4220 case Primitive::kPrimShort:
4221 case Primitive::kPrimChar:
4222 return Primitive::kPrimInt;
4223 default:
4224 return type;
4225 }
4226 }
4227
David Brazdilffee3d32015-07-06 11:48:53 +01004228 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4229
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004230 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004231
4232 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004233 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004234
Calin Juravle10e244f2015-01-26 18:54:32 +00004235 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004236 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004237
Calin Juravle10e244f2015-01-26 18:54:32 +00004238 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4239 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4240
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004241 uint32_t GetRegNumber() const { return reg_number_; }
4242
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004243 void SetDead() { is_live_ = false; }
4244 void SetLive() { is_live_ = true; }
4245 bool IsDead() const { return !is_live_; }
4246 bool IsLive() const { return is_live_; }
4247
David Brazdil77a48ae2015-09-15 12:34:04 +00004248 bool IsVRegEquivalentOf(HInstruction* other) const {
4249 return other != nullptr
4250 && other->IsPhi()
4251 && other->AsPhi()->GetBlock() == GetBlock()
4252 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4253 }
4254
Calin Juravlea4f88312015-04-16 12:57:19 +01004255 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4256 // An equivalent phi is a phi having the same dex register and type.
4257 // It assumes that phis with the same dex register are adjacent.
4258 HPhi* GetNextEquivalentPhiWithSameType() {
4259 HInstruction* next = GetNext();
4260 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4261 if (next->GetType() == GetType()) {
4262 return next->AsPhi();
4263 }
4264 next = next->GetNext();
4265 }
4266 return nullptr;
4267 }
4268
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004269 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004270
David Brazdil1abb4192015-02-17 18:33:36 +00004271 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004272 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004273 return inputs_[index];
4274 }
David Brazdil1abb4192015-02-17 18:33:36 +00004275
4276 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004277 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004278 }
4279
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004280 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004281 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004282 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004283 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004284 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004285 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004286
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004287 DISALLOW_COPY_AND_ASSIGN(HPhi);
4288};
4289
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004290class HNullCheck : public HExpression<1> {
4291 public:
4292 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004293 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004294 SetRawInputAt(0, value);
4295 }
4296
Calin Juravle10e244f2015-01-26 18:54:32 +00004297 bool CanBeMoved() const OVERRIDE { return true; }
4298 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004299 UNUSED(other);
4300 return true;
4301 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004302
Calin Juravle10e244f2015-01-26 18:54:32 +00004303 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004304
Calin Juravle10e244f2015-01-26 18:54:32 +00004305 bool CanThrow() const OVERRIDE { return true; }
4306
4307 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004308
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004309
4310 DECLARE_INSTRUCTION(NullCheck);
4311
4312 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004313 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4314};
4315
4316class FieldInfo : public ValueObject {
4317 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004318 FieldInfo(MemberOffset field_offset,
4319 Primitive::Type field_type,
4320 bool is_volatile,
4321 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004322 const DexFile& dex_file,
4323 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004324 : field_offset_(field_offset),
4325 field_type_(field_type),
4326 is_volatile_(is_volatile),
4327 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004328 dex_file_(dex_file),
4329 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004330
4331 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004332 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004333 uint32_t GetFieldIndex() const { return index_; }
4334 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004335 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004336 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004337
4338 private:
4339 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004340 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004341 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004342 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004343 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004344 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004345};
4346
4347class HInstanceFieldGet : public HExpression<1> {
4348 public:
4349 HInstanceFieldGet(HInstruction* value,
4350 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004351 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004352 bool is_volatile,
4353 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004354 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004355 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004356 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004357 : HExpression(
4358 field_type,
4359 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
4360 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004361 SetRawInputAt(0, value);
4362 }
4363
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004364 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004365
4366 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4367 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4368 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004369 }
4370
Calin Juravle641547a2015-04-21 22:08:51 +01004371 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4372 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004373 }
4374
4375 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004376 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4377 }
4378
Calin Juravle52c48962014-12-16 17:02:57 +00004379 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004380 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004381 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004382 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004383
4384 DECLARE_INSTRUCTION(InstanceFieldGet);
4385
4386 private:
4387 const FieldInfo field_info_;
4388
4389 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4390};
4391
4392class HInstanceFieldSet : public HTemplateInstruction<2> {
4393 public:
4394 HInstanceFieldSet(HInstruction* object,
4395 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004396 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004397 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004398 bool is_volatile,
4399 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004400 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004401 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004402 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004403 : HTemplateInstruction(
4404 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
4405 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004406 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004407 SetRawInputAt(0, object);
4408 SetRawInputAt(1, value);
4409 }
4410
Calin Juravle641547a2015-04-21 22:08:51 +01004411 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4412 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004413 }
4414
Calin Juravle52c48962014-12-16 17:02:57 +00004415 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004416 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004417 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004418 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004419 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004420 bool GetValueCanBeNull() const { return value_can_be_null_; }
4421 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004422
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004423 DECLARE_INSTRUCTION(InstanceFieldSet);
4424
4425 private:
4426 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004427 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004428
4429 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4430};
4431
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004432class HArrayGet : public HExpression<2> {
4433 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004434 HArrayGet(HInstruction* array,
4435 HInstruction* index,
4436 Primitive::Type type,
Calin Juravle154746b2015-10-06 15:46:54 +01004437 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004438 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004439 SetRawInputAt(0, array);
4440 SetRawInputAt(1, index);
4441 }
4442
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004443 bool CanBeMoved() const OVERRIDE { return true; }
4444 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004445 UNUSED(other);
4446 return true;
4447 }
Calin Juravle641547a2015-04-21 22:08:51 +01004448 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4449 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004450 // TODO: We can be smarter here.
4451 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4452 // which generates the implicit null check. There are cases when these can be removed
4453 // to produce better code. If we ever add optimizations to do so we should allow an
4454 // implicit check here (as long as the address falls in the first page).
4455 return false;
4456 }
4457
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004458 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004459
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004460 HInstruction* GetArray() const { return InputAt(0); }
4461 HInstruction* GetIndex() const { return InputAt(1); }
4462
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004463 DECLARE_INSTRUCTION(ArrayGet);
4464
4465 private:
4466 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4467};
4468
4469class HArraySet : public HTemplateInstruction<3> {
4470 public:
4471 HArraySet(HInstruction* array,
4472 HInstruction* index,
4473 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004474 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004475 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004476 : HTemplateInstruction(
4477 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004478 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004479 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004480 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004481 value_can_be_null_(true),
4482 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004483 SetRawInputAt(0, array);
4484 SetRawInputAt(1, index);
4485 SetRawInputAt(2, value);
4486 }
4487
Calin Juravle77520bc2015-01-12 18:45:46 +00004488 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004489 // We currently always call a runtime method to catch array store
4490 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004491 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004492 }
4493
Mingyao Yang81014cb2015-06-02 03:16:27 -07004494 // Can throw ArrayStoreException.
4495 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4496
Calin Juravle641547a2015-04-21 22:08:51 +01004497 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4498 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004499 // TODO: Same as for ArrayGet.
4500 return false;
4501 }
4502
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004503 void ClearNeedsTypeCheck() {
4504 needs_type_check_ = false;
4505 }
4506
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004507 void ClearValueCanBeNull() {
4508 value_can_be_null_ = false;
4509 }
4510
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004511 void SetStaticTypeOfArrayIsObjectArray() {
4512 static_type_of_array_is_object_array_ = true;
4513 }
4514
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004515 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004516 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004517 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004518
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004519 HInstruction* GetArray() const { return InputAt(0); }
4520 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004521 HInstruction* GetValue() const { return InputAt(2); }
4522
4523 Primitive::Type GetComponentType() const {
4524 // The Dex format does not type floating point index operations. Since the
4525 // `expected_component_type_` is set during building and can therefore not
4526 // be correct, we also check what is the value type. If it is a floating
4527 // point type, we must use that type.
4528 Primitive::Type value_type = GetValue()->GetType();
4529 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4530 ? value_type
4531 : expected_component_type_;
4532 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004533
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004534 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4535 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4536 }
4537
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004538 DECLARE_INSTRUCTION(ArraySet);
4539
4540 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004541 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004542 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004543 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004544 // Cached information for the reference_type_info_ so that codegen
4545 // does not need to inspect the static type.
4546 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004547
4548 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4549};
4550
4551class HArrayLength : public HExpression<1> {
4552 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004553 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004554 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004555 // Note that arrays do not change length, so the instruction does not
4556 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004557 SetRawInputAt(0, array);
4558 }
4559
Calin Juravle77520bc2015-01-12 18:45:46 +00004560 bool CanBeMoved() const OVERRIDE { return true; }
4561 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004562 UNUSED(other);
4563 return true;
4564 }
Calin Juravle641547a2015-04-21 22:08:51 +01004565 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4566 return obj == InputAt(0);
4567 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004568
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004569 DECLARE_INSTRUCTION(ArrayLength);
4570
4571 private:
4572 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4573};
4574
4575class HBoundsCheck : public HExpression<2> {
4576 public:
4577 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004578 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004579 DCHECK(index->GetType() == Primitive::kPrimInt);
4580 SetRawInputAt(0, index);
4581 SetRawInputAt(1, length);
4582 }
4583
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004584 bool CanBeMoved() const OVERRIDE { return true; }
4585 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004586 UNUSED(other);
4587 return true;
4588 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004589
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004590 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004591
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004592 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004593
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594
4595 DECLARE_INSTRUCTION(BoundsCheck);
4596
4597 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004598 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4599};
4600
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004601/**
4602 * Some DEX instructions are folded into multiple HInstructions that need
4603 * to stay live until the last HInstruction. This class
4604 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004605 * HInstruction stays live. `index` represents the stack location index of the
4606 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607 */
4608class HTemporary : public HTemplateInstruction<0> {
4609 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004610 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4611 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004612
4613 size_t GetIndex() const { return index_; }
4614
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004615 Primitive::Type GetType() const OVERRIDE {
4616 // The previous instruction is the one that will be stored in the temporary location.
4617 DCHECK(GetPrevious() != nullptr);
4618 return GetPrevious()->GetType();
4619 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004620
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004621 DECLARE_INSTRUCTION(Temporary);
4622
4623 private:
4624 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004625 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4626};
4627
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004628class HSuspendCheck : public HTemplateInstruction<0> {
4629 public:
4630 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004631 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004632
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004633 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004634 return true;
4635 }
4636
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004637 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4638 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004639
4640 DECLARE_INSTRUCTION(SuspendCheck);
4641
4642 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004643 // Only used for code generation, in order to share the same slow path between back edges
4644 // of a same loop.
4645 SlowPathCode* slow_path_;
4646
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004647 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4648};
4649
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004650/**
4651 * Instruction to load a Class object.
4652 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004653class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004654 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004655 HLoadClass(HCurrentMethod* current_method,
4656 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004657 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004658 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004659 uint32_t dex_pc,
4660 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004661 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004662 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004663 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004664 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004665 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004666 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004667 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004668 // Referrers class should not need access check. We never inline unverified
4669 // methods so we can't possibly end up in this situation.
4670 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004671 SetRawInputAt(0, current_method);
4672 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004673
4674 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004675
4676 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004677 // Note that we don't need to test for generate_clinit_check_.
4678 // Whether or not we need to generate the clinit check is processed in
4679 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004680 return other->AsLoadClass()->type_index_ == type_index_ &&
4681 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004682 }
4683
4684 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4685
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004686 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004687 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004688 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004689
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004690 bool NeedsEnvironment() const OVERRIDE {
4691 // Will call runtime and load the class if the class is not loaded yet.
4692 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004693 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004694 }
4695
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004696 bool MustGenerateClinitCheck() const {
4697 return generate_clinit_check_;
4698 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004699 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4700 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004701 }
4702
4703 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004704 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4705 }
4706
4707 bool NeedsAccessCheck() const {
4708 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004709 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004710
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004711 bool CanThrow() const OVERRIDE {
4712 // May call runtime and and therefore can throw.
4713 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004714 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004715 }
4716
Calin Juravleacf735c2015-02-12 15:25:22 +00004717 ReferenceTypeInfo GetLoadedClassRTI() {
4718 return loaded_class_rti_;
4719 }
4720
4721 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4722 // Make sure we only set exact types (the loaded class should never be merged).
4723 DCHECK(rti.IsExact());
4724 loaded_class_rti_ = rti;
4725 }
4726
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004727 const DexFile& GetDexFile() { return dex_file_; }
4728
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004729 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4730
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004731 static SideEffects SideEffectsForArchRuntimeCalls() {
4732 return SideEffects::CanTriggerGC();
4733 }
4734
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004735 DECLARE_INSTRUCTION(LoadClass);
4736
4737 private:
4738 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004739 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004740 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004741 // Whether this instruction must generate the initialization check.
4742 // Used for code generation.
4743 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004744 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004745
Calin Juravleacf735c2015-02-12 15:25:22 +00004746 ReferenceTypeInfo loaded_class_rti_;
4747
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004748 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4749};
4750
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004751class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004752 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004753 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004754 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4755 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004756 SetRawInputAt(0, current_method);
4757 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004758
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004759 bool CanBeMoved() const OVERRIDE { return true; }
4760
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004761 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4762 return other->AsLoadString()->string_index_ == string_index_;
4763 }
4764
4765 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4766
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004767 uint32_t GetStringIndex() const { return string_index_; }
4768
4769 // TODO: Can we deopt or debug when we resolve a string?
4770 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004771 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004772 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004773
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004774 static SideEffects SideEffectsForArchRuntimeCalls() {
4775 return SideEffects::CanTriggerGC();
4776 }
4777
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004778 DECLARE_INSTRUCTION(LoadString);
4779
4780 private:
4781 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004782
4783 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4784};
4785
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004786/**
4787 * Performs an initialization check on its Class object input.
4788 */
4789class HClinitCheck : public HExpression<1> {
4790 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004791 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004792 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004793 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004794 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4795 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004796 SetRawInputAt(0, constant);
4797 }
4798
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004799 bool CanBeMoved() const OVERRIDE { return true; }
4800 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4801 UNUSED(other);
4802 return true;
4803 }
4804
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004805 bool NeedsEnvironment() const OVERRIDE {
4806 // May call runtime to initialize the class.
4807 return true;
4808 }
4809
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004810
4811 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4812
4813 DECLARE_INSTRUCTION(ClinitCheck);
4814
4815 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004816 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4817};
4818
4819class HStaticFieldGet : public HExpression<1> {
4820 public:
4821 HStaticFieldGet(HInstruction* cls,
4822 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004823 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004824 bool is_volatile,
4825 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004826 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004827 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004828 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004829 : HExpression(
4830 field_type,
4831 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
4832 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004833 SetRawInputAt(0, cls);
4834 }
4835
Calin Juravle52c48962014-12-16 17:02:57 +00004836
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004837 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004838
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004839 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004840 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4841 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004842 }
4843
4844 size_t ComputeHashCode() const OVERRIDE {
4845 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4846 }
4847
Calin Juravle52c48962014-12-16 17:02:57 +00004848 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004849 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4850 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004851 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004852
4853 DECLARE_INSTRUCTION(StaticFieldGet);
4854
4855 private:
4856 const FieldInfo field_info_;
4857
4858 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4859};
4860
4861class HStaticFieldSet : public HTemplateInstruction<2> {
4862 public:
4863 HStaticFieldSet(HInstruction* cls,
4864 HInstruction* value,
4865 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004866 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004867 bool is_volatile,
4868 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004869 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004870 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004871 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004872 : HTemplateInstruction(
4873 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
4874 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004875 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004876 SetRawInputAt(0, cls);
4877 SetRawInputAt(1, value);
4878 }
4879
Calin Juravle52c48962014-12-16 17:02:57 +00004880 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004881 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4882 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004883 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004884
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004885 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004886 bool GetValueCanBeNull() const { return value_can_be_null_; }
4887 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004888
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004889 DECLARE_INSTRUCTION(StaticFieldSet);
4890
4891 private:
4892 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004893 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004894
4895 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4896};
4897
Calin Juravlee460d1d2015-09-29 04:52:17 +01004898class HUnresolvedInstanceFieldGet : public HExpression<1> {
4899 public:
4900 HUnresolvedInstanceFieldGet(HInstruction* obj,
4901 Primitive::Type field_type,
4902 uint32_t field_index,
4903 uint32_t dex_pc)
4904 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4905 field_index_(field_index) {
4906 SetRawInputAt(0, obj);
4907 }
4908
4909 bool NeedsEnvironment() const OVERRIDE { return true; }
4910 bool CanThrow() const OVERRIDE { return true; }
4911
4912 Primitive::Type GetFieldType() const { return GetType(); }
4913 uint32_t GetFieldIndex() const { return field_index_; }
4914
4915 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4916
4917 private:
4918 const uint32_t field_index_;
4919
4920 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4921};
4922
4923class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4924 public:
4925 HUnresolvedInstanceFieldSet(HInstruction* obj,
4926 HInstruction* value,
4927 Primitive::Type field_type,
4928 uint32_t field_index,
4929 uint32_t dex_pc)
4930 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4931 field_type_(field_type),
4932 field_index_(field_index) {
4933 DCHECK_EQ(field_type, value->GetType());
4934 SetRawInputAt(0, obj);
4935 SetRawInputAt(1, value);
4936 }
4937
4938 bool NeedsEnvironment() const OVERRIDE { return true; }
4939 bool CanThrow() const OVERRIDE { return true; }
4940
4941 Primitive::Type GetFieldType() const { return field_type_; }
4942 uint32_t GetFieldIndex() const { return field_index_; }
4943
4944 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
4945
4946 private:
4947 const Primitive::Type field_type_;
4948 const uint32_t field_index_;
4949
4950 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
4951};
4952
4953class HUnresolvedStaticFieldGet : public HExpression<0> {
4954 public:
4955 HUnresolvedStaticFieldGet(Primitive::Type field_type,
4956 uint32_t field_index,
4957 uint32_t dex_pc)
4958 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4959 field_index_(field_index) {
4960 }
4961
4962 bool NeedsEnvironment() const OVERRIDE { return true; }
4963 bool CanThrow() const OVERRIDE { return true; }
4964
4965 Primitive::Type GetFieldType() const { return GetType(); }
4966 uint32_t GetFieldIndex() const { return field_index_; }
4967
4968 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
4969
4970 private:
4971 const uint32_t field_index_;
4972
4973 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
4974};
4975
4976class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
4977 public:
4978 HUnresolvedStaticFieldSet(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, value);
4987 }
4988
4989 bool NeedsEnvironment() const OVERRIDE { return true; }
4990 bool CanThrow() const OVERRIDE { return true; }
4991
4992 Primitive::Type GetFieldType() const { return field_type_; }
4993 uint32_t GetFieldIndex() const { return field_index_; }
4994
4995 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
4996
4997 private:
4998 const Primitive::Type field_type_;
4999 const uint32_t field_index_;
5000
5001 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5002};
5003
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005004// Implement the move-exception DEX instruction.
5005class HLoadException : public HExpression<0> {
5006 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005007 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5008 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005009
David Brazdilbbd733e2015-08-18 17:48:17 +01005010 bool CanBeNull() const OVERRIDE { return false; }
5011
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005012 DECLARE_INSTRUCTION(LoadException);
5013
5014 private:
5015 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5016};
5017
David Brazdilcb1c0552015-08-04 16:22:25 +01005018// Implicit part of move-exception which clears thread-local exception storage.
5019// Must not be removed because the runtime expects the TLS to get cleared.
5020class HClearException : public HTemplateInstruction<0> {
5021 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005022 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5023 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005024
5025 DECLARE_INSTRUCTION(ClearException);
5026
5027 private:
5028 DISALLOW_COPY_AND_ASSIGN(HClearException);
5029};
5030
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005031class HThrow : public HTemplateInstruction<1> {
5032 public:
5033 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005034 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005035 SetRawInputAt(0, exception);
5036 }
5037
5038 bool IsControlFlow() const OVERRIDE { return true; }
5039
5040 bool NeedsEnvironment() const OVERRIDE { return true; }
5041
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005042 bool CanThrow() const OVERRIDE { return true; }
5043
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005044
5045 DECLARE_INSTRUCTION(Throw);
5046
5047 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005048 DISALLOW_COPY_AND_ASSIGN(HThrow);
5049};
5050
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005051/**
5052 * Implementation strategies for the code generator of a HInstanceOf
5053 * or `HCheckCast`.
5054 */
5055enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005056 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005057 kExactCheck, // Can do a single class compare.
5058 kClassHierarchyCheck, // Can just walk the super class chain.
5059 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5060 kInterfaceCheck, // No optimization yet when checking against an interface.
5061 kArrayObjectCheck, // Can just check if the array is not primitive.
5062 kArrayCheck // No optimization yet when checking against a generic array.
5063};
5064
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005065class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005066 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005067 HInstanceOf(HInstruction* object,
5068 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005069 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005070 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005071 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005072 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005073 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005074 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005075 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005076 SetRawInputAt(0, object);
5077 SetRawInputAt(1, constant);
5078 }
5079
5080 bool CanBeMoved() const OVERRIDE { return true; }
5081
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005082 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005083 return true;
5084 }
5085
5086 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005087 return false;
5088 }
5089
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005090 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5091
5092 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005093
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005094 // Used only in code generation.
5095 bool MustDoNullCheck() const { return must_do_null_check_; }
5096 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5097
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005098 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5099 return (check_kind == TypeCheckKind::kExactCheck)
5100 ? SideEffects::None()
5101 // Mips currently does runtime calls for any other checks.
5102 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005103 }
5104
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005105 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005106
5107 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005108 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005109 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005110
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005111 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5112};
5113
Calin Juravleb1498f62015-02-16 13:13:29 +00005114class HBoundType : public HExpression<1> {
5115 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005116 // Constructs an HBoundType with the given upper_bound.
5117 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005118 HBoundType(HInstruction* input,
5119 ReferenceTypeInfo upper_bound,
5120 bool upper_can_be_null,
5121 uint32_t dex_pc = kNoDexPc)
5122 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005123 upper_bound_(upper_bound),
5124 upper_can_be_null_(upper_can_be_null),
5125 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005126 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005127 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005128 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005129 }
5130
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005131 // GetUpper* should only be used in reference type propagation.
5132 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5133 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005134
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005135 void SetCanBeNull(bool can_be_null) {
5136 DCHECK(upper_can_be_null_ || !can_be_null);
5137 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005138 }
5139
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005140 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5141
Calin Juravleb1498f62015-02-16 13:13:29 +00005142 DECLARE_INSTRUCTION(BoundType);
5143
5144 private:
5145 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005146 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5147 // It is used to bound the type in cases like:
5148 // if (x instanceof ClassX) {
5149 // // uper_bound_ will be ClassX
5150 // }
5151 const ReferenceTypeInfo upper_bound_;
5152 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5153 // is false then can_be_null_ cannot be true).
5154 const bool upper_can_be_null_;
5155 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005156
5157 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5158};
5159
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005160class HCheckCast : public HTemplateInstruction<2> {
5161 public:
5162 HCheckCast(HInstruction* object,
5163 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005164 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005165 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005166 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005167 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005168 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005169 SetRawInputAt(0, object);
5170 SetRawInputAt(1, constant);
5171 }
5172
5173 bool CanBeMoved() const OVERRIDE { return true; }
5174
5175 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5176 return true;
5177 }
5178
5179 bool NeedsEnvironment() const OVERRIDE {
5180 // Instruction may throw a CheckCastError.
5181 return true;
5182 }
5183
5184 bool CanThrow() const OVERRIDE { return true; }
5185
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005186 bool MustDoNullCheck() const { return must_do_null_check_; }
5187 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005188 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005189
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005190 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005191
5192 DECLARE_INSTRUCTION(CheckCast);
5193
5194 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005195 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005196 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005197
5198 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005199};
5200
Calin Juravle27df7582015-04-17 19:12:31 +01005201class HMemoryBarrier : public HTemplateInstruction<0> {
5202 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005203 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005204 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005205 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005206 barrier_kind_(barrier_kind) {}
5207
5208 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5209
5210 DECLARE_INSTRUCTION(MemoryBarrier);
5211
5212 private:
5213 const MemBarrierKind barrier_kind_;
5214
5215 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5216};
5217
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005218class HMonitorOperation : public HTemplateInstruction<1> {
5219 public:
5220 enum OperationKind {
5221 kEnter,
5222 kExit,
5223 };
5224
5225 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005226 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005227 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5228 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005229 SetRawInputAt(0, object);
5230 }
5231
5232 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005233 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5234
5235 bool CanThrow() const OVERRIDE {
5236 // Verifier guarantees that monitor-exit cannot throw.
5237 // This is important because it allows the HGraphBuilder to remove
5238 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5239 return IsEnter();
5240 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005241
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005242
5243 bool IsEnter() const { return kind_ == kEnter; }
5244
5245 DECLARE_INSTRUCTION(MonitorOperation);
5246
Calin Juravle52c48962014-12-16 17:02:57 +00005247 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005248 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005249
5250 private:
5251 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5252};
5253
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005254/**
5255 * A HInstruction used as a marker for the replacement of new + <init>
5256 * of a String to a call to a StringFactory. Only baseline will see
5257 * the node at code generation, where it will be be treated as null.
5258 * When compiling non-baseline, `HFakeString` instructions are being removed
5259 * in the instruction simplifier.
5260 */
5261class HFakeString : public HTemplateInstruction<0> {
5262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005263 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5264 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005265
5266 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5267
5268 DECLARE_INSTRUCTION(FakeString);
5269
5270 private:
5271 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5272};
5273
Vladimir Markof9f64412015-09-02 14:05:49 +01005274class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005275 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005276 MoveOperands(Location source,
5277 Location destination,
5278 Primitive::Type type,
5279 HInstruction* instruction)
5280 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005281
5282 Location GetSource() const { return source_; }
5283 Location GetDestination() const { return destination_; }
5284
5285 void SetSource(Location value) { source_ = value; }
5286 void SetDestination(Location value) { destination_ = value; }
5287
5288 // The parallel move resolver marks moves as "in-progress" by clearing the
5289 // destination (but not the source).
5290 Location MarkPending() {
5291 DCHECK(!IsPending());
5292 Location dest = destination_;
5293 destination_ = Location::NoLocation();
5294 return dest;
5295 }
5296
5297 void ClearPending(Location dest) {
5298 DCHECK(IsPending());
5299 destination_ = dest;
5300 }
5301
5302 bool IsPending() const {
5303 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5304 return destination_.IsInvalid() && !source_.IsInvalid();
5305 }
5306
5307 // True if this blocks a move from the given location.
5308 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005309 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005310 }
5311
5312 // A move is redundant if it's been eliminated, if its source and
5313 // destination are the same, or if its destination is unneeded.
5314 bool IsRedundant() const {
5315 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5316 }
5317
5318 // We clear both operands to indicate move that's been eliminated.
5319 void Eliminate() {
5320 source_ = destination_ = Location::NoLocation();
5321 }
5322
5323 bool IsEliminated() const {
5324 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5325 return source_.IsInvalid();
5326 }
5327
Alexey Frunze4dda3372015-06-01 18:31:49 -07005328 Primitive::Type GetType() const { return type_; }
5329
Nicolas Geoffray90218252015-04-15 11:56:51 +01005330 bool Is64BitMove() const {
5331 return Primitive::Is64BitType(type_);
5332 }
5333
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005334 HInstruction* GetInstruction() const { return instruction_; }
5335
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005336 private:
5337 Location source_;
5338 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005339 // The type this move is for.
5340 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005341 // The instruction this move is assocatied with. Null when this move is
5342 // for moving an input in the expected locations of user (including a phi user).
5343 // This is only used in debug mode, to ensure we do not connect interval siblings
5344 // in the same parallel move.
5345 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005346};
5347
5348static constexpr size_t kDefaultNumberOfMoves = 4;
5349
5350class HParallelMove : public HTemplateInstruction<0> {
5351 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005352 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005353 : HTemplateInstruction(SideEffects::None(), dex_pc),
5354 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5355 moves_.reserve(kDefaultNumberOfMoves);
5356 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005357
Nicolas Geoffray90218252015-04-15 11:56:51 +01005358 void AddMove(Location source,
5359 Location destination,
5360 Primitive::Type type,
5361 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005362 DCHECK(source.IsValid());
5363 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005364 if (kIsDebugBuild) {
5365 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005366 for (const MoveOperands& move : moves_) {
5367 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005368 // Special case the situation where the move is for the spill slot
5369 // of the instruction.
5370 if ((GetPrevious() == instruction)
5371 || ((GetPrevious() == nullptr)
5372 && instruction->IsPhi()
5373 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005374 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005375 << "Doing parallel moves for the same instruction.";
5376 } else {
5377 DCHECK(false) << "Doing parallel moves for the same instruction.";
5378 }
5379 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005380 }
5381 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005382 for (const MoveOperands& move : moves_) {
5383 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005384 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005385 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005386 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005387 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005388 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005389 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005390 }
5391
Vladimir Marko225b6462015-09-28 12:17:40 +01005392 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005393 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005394 }
5395
Vladimir Marko225b6462015-09-28 12:17:40 +01005396 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005397
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005398 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005399
5400 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005401 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005402
5403 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5404};
5405
Mark Mendell0616ae02015-04-17 12:49:27 -04005406} // namespace art
5407
5408#ifdef ART_ENABLE_CODEGEN_x86
5409#include "nodes_x86.h"
5410#endif
5411
5412namespace art {
5413
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005414class HGraphVisitor : public ValueObject {
5415 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005416 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5417 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005418
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005419 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005420 virtual void VisitBasicBlock(HBasicBlock* block);
5421
Roland Levillain633021e2014-10-01 14:12:25 +01005422 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005423 void VisitInsertionOrder();
5424
Roland Levillain633021e2014-10-01 14:12:25 +01005425 // Visit the graph following dominator tree reverse post-order.
5426 void VisitReversePostOrder();
5427
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005428 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005429
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005430 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005431#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005432 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5433
5434 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5435
5436#undef DECLARE_VISIT_INSTRUCTION
5437
5438 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005439 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005440
5441 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5442};
5443
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005444class HGraphDelegateVisitor : public HGraphVisitor {
5445 public:
5446 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5447 virtual ~HGraphDelegateVisitor() {}
5448
5449 // Visit functions that delegate to to super class.
5450#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005451 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005452
5453 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5454
5455#undef DECLARE_VISIT_INSTRUCTION
5456
5457 private:
5458 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5459};
5460
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005461class HInsertionOrderIterator : public ValueObject {
5462 public:
5463 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5464
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005465 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005466 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005467 void Advance() { ++index_; }
5468
5469 private:
5470 const HGraph& graph_;
5471 size_t index_;
5472
5473 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5474};
5475
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005476class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005477 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005478 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5479 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005480 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005481 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005482
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005483 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5484 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005485 void Advance() { ++index_; }
5486
5487 private:
5488 const HGraph& graph_;
5489 size_t index_;
5490
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005491 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005492};
5493
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005494class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005495 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005496 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005497 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005498 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005499 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005500 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005501
5502 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005503 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005504 void Advance() { --index_; }
5505
5506 private:
5507 const HGraph& graph_;
5508 size_t index_;
5509
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005510 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005511};
5512
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005513class HLinearPostOrderIterator : public ValueObject {
5514 public:
5515 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005516 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005517
5518 bool Done() const { return index_ == 0; }
5519
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005520 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005521
5522 void Advance() {
5523 --index_;
5524 DCHECK_GE(index_, 0U);
5525 }
5526
5527 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005528 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005529 size_t index_;
5530
5531 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5532};
5533
5534class HLinearOrderIterator : public ValueObject {
5535 public:
5536 explicit HLinearOrderIterator(const HGraph& graph)
5537 : order_(graph.GetLinearOrder()), index_(0) {}
5538
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005539 bool Done() const { return index_ == order_.size(); }
5540 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005541 void Advance() { ++index_; }
5542
5543 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005544 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005545 size_t index_;
5546
5547 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5548};
5549
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005550// Iterator over the blocks that art part of the loop. Includes blocks part
5551// of an inner loop. The order in which the blocks are iterated is on their
5552// block id.
5553class HBlocksInLoopIterator : public ValueObject {
5554 public:
5555 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5556 : blocks_in_loop_(info.GetBlocks()),
5557 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5558 index_(0) {
5559 if (!blocks_in_loop_.IsBitSet(index_)) {
5560 Advance();
5561 }
5562 }
5563
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005564 bool Done() const { return index_ == blocks_.size(); }
5565 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005566 void Advance() {
5567 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005568 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005569 if (blocks_in_loop_.IsBitSet(index_)) {
5570 break;
5571 }
5572 }
5573 }
5574
5575 private:
5576 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005577 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005578 size_t index_;
5579
5580 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5581};
5582
Mingyao Yang3584bce2015-05-19 16:01:59 -07005583// Iterator over the blocks that art part of the loop. Includes blocks part
5584// of an inner loop. The order in which the blocks are iterated is reverse
5585// post order.
5586class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5587 public:
5588 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5589 : blocks_in_loop_(info.GetBlocks()),
5590 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5591 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005592 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005593 Advance();
5594 }
5595 }
5596
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005597 bool Done() const { return index_ == blocks_.size(); }
5598 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005599 void Advance() {
5600 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005601 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5602 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005603 break;
5604 }
5605 }
5606 }
5607
5608 private:
5609 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005610 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005611 size_t index_;
5612
5613 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5614};
5615
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005616inline int64_t Int64FromConstant(HConstant* constant) {
5617 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5618 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5619 : constant->AsLongConstant()->GetValue();
5620}
5621
Vladimir Marko58155012015-08-19 12:49:41 +00005622inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5623 // For the purposes of the compiler, the dex files must actually be the same object
5624 // if we want to safely treat them as the same. This is especially important for JIT
5625 // as custom class loaders can open the same underlying file (or memory) multiple
5626 // times and provide different class resolution but no two class loaders should ever
5627 // use the same DexFile object - doing so is an unsupported hack that can lead to
5628 // all sorts of weird failures.
5629 return &lhs == &rhs;
5630}
5631
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005632} // namespace art
5633
5634#endif // ART_COMPILER_OPTIMIZING_NODES_H_