blob: 0db1ba2419573c89d73b732ad3656157fc2f1eea [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
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001802 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001803 return false;
1804 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001805
Calin Juravle2e768302015-07-28 14:41:11 +00001806 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001807
Calin Juravle61d544b2015-02-23 16:46:57 +00001808 ReferenceTypeInfo GetReferenceTypeInfo() const {
1809 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1810 return reference_type_info_;
1811 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001812
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001813 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001814 DCHECK(user != nullptr);
1815 HUseListNode<HInstruction*>* use =
1816 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1817 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001818 }
1819
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001820 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001821 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001822 HUseListNode<HEnvironment*>* env_use =
1823 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1824 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001825 }
1826
David Brazdil1abb4192015-02-17 18:33:36 +00001827 void RemoveAsUserOfInput(size_t input) {
1828 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1829 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1830 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001831
David Brazdil1abb4192015-02-17 18:33:36 +00001832 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1833 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001834
David Brazdiled596192015-01-23 10:39:45 +00001835 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1836 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001837 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001838 bool HasOnlyOneNonEnvironmentUse() const {
1839 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1840 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001841
Roland Levillain6c82d402014-10-13 16:10:27 +01001842 // Does this instruction strictly dominate `other_instruction`?
1843 // Returns false if this instruction and `other_instruction` are the same.
1844 // Aborts if this instruction and `other_instruction` are both phis.
1845 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001846
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001847 int GetId() const { return id_; }
1848 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001849
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001850 int GetSsaIndex() const { return ssa_index_; }
1851 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1852 bool HasSsaIndex() const { return ssa_index_ != -1; }
1853
1854 bool HasEnvironment() const { return environment_ != nullptr; }
1855 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001856 // Set the `environment_` field. Raw because this method does not
1857 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001858 void SetRawEnvironment(HEnvironment* environment) {
1859 DCHECK(environment_ == nullptr);
1860 DCHECK_EQ(environment->GetHolder(), this);
1861 environment_ = environment;
1862 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001863
1864 // Set the environment of this instruction, copying it from `environment`. While
1865 // copying, the uses lists are being updated.
1866 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001867 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001868 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001869 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001870 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001871 if (environment->GetParent() != nullptr) {
1872 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1873 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001874 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001875
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001876 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1877 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001878 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001879 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001880 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001881 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001882 if (environment->GetParent() != nullptr) {
1883 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1884 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001885 }
1886
Nicolas Geoffray39468442014-09-02 15:17:15 +01001887 // Returns the number of entries in the environment. Typically, that is the
1888 // number of dex registers in a method. It could be more in case of inlining.
1889 size_t EnvironmentSize() const;
1890
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001891 LocationSummary* GetLocations() const { return locations_; }
1892 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001893
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001894 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001895 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001896
Alexandre Rames188d4312015-04-09 18:30:21 +01001897 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1898 // uses of this instruction by `other` are *not* updated.
1899 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1900 ReplaceWith(other);
1901 other->ReplaceInput(this, use_index);
1902 }
1903
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001904 // Move `this` instruction before `cursor`.
1905 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001906
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001907#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001908 bool Is##type() const { return (As##type() != nullptr); } \
1909 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001910 virtual H##type* As##type() { return nullptr; }
1911
1912 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1913#undef INSTRUCTION_TYPE_CHECK
1914
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001915 // Returns whether the instruction can be moved within the graph.
1916 virtual bool CanBeMoved() const { return false; }
1917
1918 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001919 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001920 return false;
1921 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001922
1923 // Returns whether any data encoded in the two instructions is equal.
1924 // This method does not look at the inputs. Both instructions must be
1925 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001926 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001927 return false;
1928 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001929
1930 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001931 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001932 // 2) Their inputs are identical.
1933 bool Equals(HInstruction* other) const;
1934
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001935 virtual InstructionKind GetKind() const = 0;
1936
1937 virtual size_t ComputeHashCode() const {
1938 size_t result = GetKind();
1939 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1940 result = (result * 31) + InputAt(i)->GetId();
1941 }
1942 return result;
1943 }
1944
1945 SideEffects GetSideEffects() const { return side_effects_; }
1946
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001947 size_t GetLifetimePosition() const { return lifetime_position_; }
1948 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1949 LiveInterval* GetLiveInterval() const { return live_interval_; }
1950 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1951 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1952
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001953 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1954
1955 // Returns whether the code generation of the instruction will require to have access
1956 // to the current method. Such instructions are:
1957 // (1): Instructions that require an environment, as calling the runtime requires
1958 // to walk the stack and have the current method stored at a specific stack address.
1959 // (2): Object literals like classes and strings, that are loaded from the dex cache
1960 // fields of the current method.
1961 bool NeedsCurrentMethod() const {
1962 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1963 }
1964
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001965 virtual bool NeedsDexCache() const { return false; }
1966
Mark Mendellc4701932015-04-10 13:18:51 -04001967 // Does this instruction have any use in an environment before
1968 // control flow hits 'other'?
1969 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1970
1971 // Remove all references to environment uses of this instruction.
1972 // The caller must ensure that this is safe to do.
1973 void RemoveEnvironmentUsers();
1974
David Brazdil1abb4192015-02-17 18:33:36 +00001975 protected:
1976 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1977 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1978
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001979 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001980 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1981
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001982 HInstruction* previous_;
1983 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001984 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001985 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001986
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001987 // An instruction gets an id when it is added to the graph.
1988 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001989 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001990 int id_;
1991
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001992 // When doing liveness analysis, instructions that have uses get an SSA index.
1993 int ssa_index_;
1994
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001995 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001996 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001997
1998 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001999 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002000
Nicolas Geoffray39468442014-09-02 15:17:15 +01002001 // The environment associated with this instruction. Not null if the instruction
2002 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002003 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002004
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002005 // Set by the code generator.
2006 LocationSummary* locations_;
2007
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002008 // Set by the liveness analysis.
2009 LiveInterval* live_interval_;
2010
2011 // Set by the liveness analysis, this is the position in a linear
2012 // order of blocks where this instruction's live interval start.
2013 size_t lifetime_position_;
2014
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002015 const SideEffects side_effects_;
2016
Calin Juravleacf735c2015-02-12 15:25:22 +00002017 // TODO: for primitive types this should be marked as invalid.
2018 ReferenceTypeInfo reference_type_info_;
2019
David Brazdil1abb4192015-02-17 18:33:36 +00002020 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002021 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002022 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002023 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002024 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002025
2026 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2027};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002028std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002029
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002030class HInputIterator : public ValueObject {
2031 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002032 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002033
2034 bool Done() const { return index_ == instruction_->InputCount(); }
2035 HInstruction* Current() const { return instruction_->InputAt(index_); }
2036 void Advance() { index_++; }
2037
2038 private:
2039 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002040 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002041
2042 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2043};
2044
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002045class HInstructionIterator : public ValueObject {
2046 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002047 explicit HInstructionIterator(const HInstructionList& instructions)
2048 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002049 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002050 }
2051
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002052 bool Done() const { return instruction_ == nullptr; }
2053 HInstruction* Current() const { return instruction_; }
2054 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002056 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002057 }
2058
2059 private:
2060 HInstruction* instruction_;
2061 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002062
2063 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002064};
2065
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002066class HBackwardInstructionIterator : public ValueObject {
2067 public:
2068 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2069 : instruction_(instructions.last_instruction_) {
2070 next_ = Done() ? nullptr : instruction_->GetPrevious();
2071 }
2072
2073 bool Done() const { return instruction_ == nullptr; }
2074 HInstruction* Current() const { return instruction_; }
2075 void Advance() {
2076 instruction_ = next_;
2077 next_ = Done() ? nullptr : instruction_->GetPrevious();
2078 }
2079
2080 private:
2081 HInstruction* instruction_;
2082 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002083
2084 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002085};
2086
Vladimir Markof9f64412015-09-02 14:05:49 +01002087template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002088class HTemplateInstruction: public HInstruction {
2089 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002090 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002091 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002092 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002093
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002094 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002095
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002096 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002097 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2098 DCHECK_LT(i, N);
2099 return inputs_[i];
2100 }
David Brazdil1abb4192015-02-17 18:33:36 +00002101
2102 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002103 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002104 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002105 }
2106
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002107 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002108 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002109
2110 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002111};
2112
Vladimir Markof9f64412015-09-02 14:05:49 +01002113// HTemplateInstruction specialization for N=0.
2114template<>
2115class HTemplateInstruction<0>: public HInstruction {
2116 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002117 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002118 : HInstruction(side_effects, dex_pc) {}
2119
Vladimir Markof9f64412015-09-02 14:05:49 +01002120 virtual ~HTemplateInstruction() {}
2121
2122 size_t InputCount() const OVERRIDE { return 0; }
2123
2124 protected:
2125 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2126 LOG(FATAL) << "Unreachable";
2127 UNREACHABLE();
2128 }
2129
2130 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2131 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2132 LOG(FATAL) << "Unreachable";
2133 UNREACHABLE();
2134 }
2135
2136 private:
2137 friend class SsaBuilder;
2138};
2139
Dave Allison20dfc792014-06-16 20:44:29 -07002140template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002141class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002142 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002143 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002144 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002145 virtual ~HExpression() {}
2146
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002147 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002148
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002149 protected:
2150 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002151};
2152
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002153// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2154// instruction that branches to the exit block.
2155class HReturnVoid : public HTemplateInstruction<0> {
2156 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002157 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2158 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002159
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002160 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002161
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002162 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002163
2164 private:
2165 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2166};
2167
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002168// Represents dex's RETURN opcodes. A HReturn is a control flow
2169// instruction that branches to the exit block.
2170class HReturn : public HTemplateInstruction<1> {
2171 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002172 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2173 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002174 SetRawInputAt(0, value);
2175 }
2176
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002177 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002178
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002179 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002180
2181 private:
2182 DISALLOW_COPY_AND_ASSIGN(HReturn);
2183};
2184
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002186// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002187// exit block.
2188class HExit : public HTemplateInstruction<0> {
2189 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002190 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002191
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002192 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002193
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002194 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002195
2196 private:
2197 DISALLOW_COPY_AND_ASSIGN(HExit);
2198};
2199
2200// Jumps from one block to another.
2201class HGoto : public HTemplateInstruction<0> {
2202 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002203 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002204
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002205 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002206
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002207 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002208 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002209 }
2210
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002211 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002212
2213 private:
2214 DISALLOW_COPY_AND_ASSIGN(HGoto);
2215};
2216
Roland Levillain9867bc72015-08-05 10:21:34 +01002217class HConstant : public HExpression<0> {
2218 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002219 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2220 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002221
2222 bool CanBeMoved() const OVERRIDE { return true; }
2223
2224 virtual bool IsMinusOne() const { return false; }
2225 virtual bool IsZero() const { return false; }
2226 virtual bool IsOne() const { return false; }
2227
David Brazdil77a48ae2015-09-15 12:34:04 +00002228 virtual uint64_t GetValueAsUint64() const = 0;
2229
Roland Levillain9867bc72015-08-05 10:21:34 +01002230 DECLARE_INSTRUCTION(Constant);
2231
2232 private:
2233 DISALLOW_COPY_AND_ASSIGN(HConstant);
2234};
2235
2236class HNullConstant : public HConstant {
2237 public:
2238 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2239 return true;
2240 }
2241
David Brazdil77a48ae2015-09-15 12:34:04 +00002242 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2243
Roland Levillain9867bc72015-08-05 10:21:34 +01002244 size_t ComputeHashCode() const OVERRIDE { return 0; }
2245
2246 DECLARE_INSTRUCTION(NullConstant);
2247
2248 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002249 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002250
2251 friend class HGraph;
2252 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2253};
2254
2255// Constants of the type int. Those can be from Dex instructions, or
2256// synthesized (for example with the if-eqz instruction).
2257class HIntConstant : public HConstant {
2258 public:
2259 int32_t GetValue() const { return value_; }
2260
David Brazdil9f389d42015-10-01 14:32:56 +01002261 uint64_t GetValueAsUint64() const OVERRIDE {
2262 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2263 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002264
Roland Levillain9867bc72015-08-05 10:21:34 +01002265 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2266 DCHECK(other->IsIntConstant());
2267 return other->AsIntConstant()->value_ == value_;
2268 }
2269
2270 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2271
2272 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2273 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2274 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2275
2276 DECLARE_INSTRUCTION(IntConstant);
2277
2278 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002279 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2280 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2281 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2282 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002283
2284 const int32_t value_;
2285
2286 friend class HGraph;
2287 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2288 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2289 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2290};
2291
2292class HLongConstant : public HConstant {
2293 public:
2294 int64_t GetValue() const { return value_; }
2295
David Brazdil77a48ae2015-09-15 12:34:04 +00002296 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2297
Roland Levillain9867bc72015-08-05 10:21:34 +01002298 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2299 DCHECK(other->IsLongConstant());
2300 return other->AsLongConstant()->value_ == value_;
2301 }
2302
2303 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2304
2305 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2306 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2307 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2308
2309 DECLARE_INSTRUCTION(LongConstant);
2310
2311 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002312 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2313 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002314
2315 const int64_t value_;
2316
2317 friend class HGraph;
2318 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2319};
Dave Allison20dfc792014-06-16 20:44:29 -07002320
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002321// Conditional branch. A block ending with an HIf instruction must have
2322// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002323class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002324 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002325 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2326 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002327 SetRawInputAt(0, input);
2328 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002329
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002330 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002331
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002332 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002333 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002334 }
2335
2336 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002337 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002338 }
2339
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002340 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002341
2342 private:
2343 DISALLOW_COPY_AND_ASSIGN(HIf);
2344};
2345
David Brazdilfc6a86a2015-06-26 10:33:45 +00002346
2347// Abstract instruction which marks the beginning and/or end of a try block and
2348// links it to the respective exception handlers. Behaves the same as a Goto in
2349// non-exceptional control flow.
2350// Normal-flow successor is stored at index zero, exception handlers under
2351// higher indices in no particular order.
2352class HTryBoundary : public HTemplateInstruction<0> {
2353 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002354 enum BoundaryKind {
2355 kEntry,
2356 kExit,
2357 };
2358
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002359 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2360 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002361
2362 bool IsControlFlow() const OVERRIDE { return true; }
2363
2364 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002365 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002366
2367 // Returns whether `handler` is among its exception handlers (non-zero index
2368 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002369 bool HasExceptionHandler(const HBasicBlock& handler) const {
2370 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002371 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002372 }
2373
2374 // If not present already, adds `handler` to its block's list of exception
2375 // handlers.
2376 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002377 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002378 GetBlock()->AddSuccessor(handler);
2379 }
2380 }
2381
David Brazdil56e1acc2015-06-30 15:41:36 +01002382 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002383
David Brazdilffee3d32015-07-06 11:48:53 +01002384 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2385
David Brazdilfc6a86a2015-06-26 10:33:45 +00002386 DECLARE_INSTRUCTION(TryBoundary);
2387
2388 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002389 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002390
2391 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2392};
2393
David Brazdilffee3d32015-07-06 11:48:53 +01002394// Iterator over exception handlers of a given HTryBoundary, i.e. over
2395// exceptional successors of its basic block.
2396class HExceptionHandlerIterator : public ValueObject {
2397 public:
2398 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2399 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2400
Vladimir Marko60584552015-09-03 13:35:12 +00002401 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002402 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002403 size_t CurrentSuccessorIndex() const { return index_; }
2404 void Advance() { ++index_; }
2405
2406 private:
2407 const HBasicBlock& block_;
2408 size_t index_;
2409
2410 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2411};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002412
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002413// Deoptimize to interpreter, upon checking a condition.
2414class HDeoptimize : public HTemplateInstruction<1> {
2415 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002416 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2417 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002418 SetRawInputAt(0, cond);
2419 }
2420
2421 bool NeedsEnvironment() const OVERRIDE { return true; }
2422 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002423
2424 DECLARE_INSTRUCTION(Deoptimize);
2425
2426 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002427 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2428};
2429
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002430// Represents the ArtMethod that was passed as a first argument to
2431// the method. It is used by instructions that depend on it, like
2432// instructions that work with the dex cache.
2433class HCurrentMethod : public HExpression<0> {
2434 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002435 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2436 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002437
2438 DECLARE_INSTRUCTION(CurrentMethod);
2439
2440 private:
2441 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2442};
2443
Mark Mendellfe57faa2015-09-18 09:26:15 -04002444// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2445// have one successor for each entry in the switch table, and the final successor
2446// will be the block containing the next Dex opcode.
2447class HPackedSwitch : public HTemplateInstruction<1> {
2448 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002449 HPackedSwitch(int32_t start_value,
2450 uint32_t num_entries,
2451 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002452 uint32_t dex_pc = kNoDexPc)
2453 : HTemplateInstruction(SideEffects::None(), dex_pc),
2454 start_value_(start_value),
2455 num_entries_(num_entries) {
2456 SetRawInputAt(0, input);
2457 }
2458
2459 bool IsControlFlow() const OVERRIDE { return true; }
2460
2461 int32_t GetStartValue() const { return start_value_; }
2462
Vladimir Marko211c2112015-09-24 16:52:33 +01002463 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002464
2465 HBasicBlock* GetDefaultBlock() const {
2466 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002467 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002468 }
2469 DECLARE_INSTRUCTION(PackedSwitch);
2470
2471 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002472 const int32_t start_value_;
2473 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002474
2475 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2476};
2477
Roland Levillain88cb1752014-10-20 16:36:47 +01002478class HUnaryOperation : public HExpression<1> {
2479 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002480 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2481 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002482 SetRawInputAt(0, input);
2483 }
2484
2485 HInstruction* GetInput() const { return InputAt(0); }
2486 Primitive::Type GetResultType() const { return GetType(); }
2487
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002488 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002489 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002490 return true;
2491 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002492
Roland Levillain9240d6a2014-10-20 16:47:04 +01002493 // Try to statically evaluate `operation` and return a HConstant
2494 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002495 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002496 HConstant* TryStaticEvaluation() const;
2497
2498 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002499 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2500 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002501
Roland Levillain88cb1752014-10-20 16:36:47 +01002502 DECLARE_INSTRUCTION(UnaryOperation);
2503
2504 private:
2505 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2506};
2507
Dave Allison20dfc792014-06-16 20:44:29 -07002508class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002509 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002510 HBinaryOperation(Primitive::Type result_type,
2511 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002512 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002513 SideEffects side_effects = SideEffects::None(),
2514 uint32_t dex_pc = kNoDexPc)
2515 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002516 SetRawInputAt(0, left);
2517 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002518 }
2519
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002520 HInstruction* GetLeft() const { return InputAt(0); }
2521 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002522 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002523
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002524 virtual bool IsCommutative() const { return false; }
2525
2526 // Put constant on the right.
2527 // Returns whether order is changed.
2528 bool OrderInputsWithConstantOnTheRight() {
2529 HInstruction* left = InputAt(0);
2530 HInstruction* right = InputAt(1);
2531 if (left->IsConstant() && !right->IsConstant()) {
2532 ReplaceInput(right, 0);
2533 ReplaceInput(left, 1);
2534 return true;
2535 }
2536 return false;
2537 }
2538
2539 // Order inputs by instruction id, but favor constant on the right side.
2540 // This helps GVN for commutative ops.
2541 void OrderInputs() {
2542 DCHECK(IsCommutative());
2543 HInstruction* left = InputAt(0);
2544 HInstruction* right = InputAt(1);
2545 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2546 return;
2547 }
2548 if (OrderInputsWithConstantOnTheRight()) {
2549 return;
2550 }
2551 // Order according to instruction id.
2552 if (left->GetId() > right->GetId()) {
2553 ReplaceInput(right, 0);
2554 ReplaceInput(left, 1);
2555 }
2556 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002557
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002558 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002559 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002560 return true;
2561 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002562
Roland Levillain9240d6a2014-10-20 16:47:04 +01002563 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002564 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002565 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002566 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002567
2568 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002569 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2570 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2571 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2572 HLongConstant* y ATTRIBUTE_UNUSED) const {
2573 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2574 return nullptr;
2575 }
2576 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2577 HIntConstant* y ATTRIBUTE_UNUSED) const {
2578 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2579 return nullptr;
2580 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002581
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002582 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002583 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002584 HConstant* GetConstantRight() const;
2585
2586 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002587 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002588 HInstruction* GetLeastConstantLeft() const;
2589
Roland Levillainccc07a92014-09-16 14:48:16 +01002590 DECLARE_INSTRUCTION(BinaryOperation);
2591
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002592 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002593 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2594};
2595
Mark Mendellc4701932015-04-10 13:18:51 -04002596// The comparison bias applies for floating point operations and indicates how NaN
2597// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002598enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002599 kNoBias, // bias is not applicable (i.e. for long operation)
2600 kGtBias, // return 1 for NaN comparisons
2601 kLtBias, // return -1 for NaN comparisons
2602};
2603
Dave Allison20dfc792014-06-16 20:44:29 -07002604class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002605 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002606 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2607 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002608 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002609 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002610
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002611 bool NeedsMaterialization() const { return needs_materialization_; }
2612 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002613
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002614 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002615 // `instruction`, and disregard moves in between.
2616 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002617
Dave Allison20dfc792014-06-16 20:44:29 -07002618 DECLARE_INSTRUCTION(Condition);
2619
2620 virtual IfCondition GetCondition() const = 0;
2621
Mark Mendellc4701932015-04-10 13:18:51 -04002622 virtual IfCondition GetOppositeCondition() const = 0;
2623
Roland Levillain4fa13f62015-07-06 18:11:54 +01002624 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002625
2626 void SetBias(ComparisonBias bias) { bias_ = bias; }
2627
2628 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2629 return bias_ == other->AsCondition()->bias_;
2630 }
2631
Roland Levillain4fa13f62015-07-06 18:11:54 +01002632 bool IsFPConditionTrueIfNaN() const {
2633 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2634 IfCondition if_cond = GetCondition();
2635 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2636 }
2637
2638 bool IsFPConditionFalseIfNaN() const {
2639 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2640 IfCondition if_cond = GetCondition();
2641 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2642 }
2643
Dave Allison20dfc792014-06-16 20:44:29 -07002644 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002645 // For register allocation purposes, returns whether this instruction needs to be
2646 // materialized (that is, not just be in the processor flags).
2647 bool needs_materialization_;
2648
Mark Mendellc4701932015-04-10 13:18:51 -04002649 // Needed if we merge a HCompare into a HCondition.
2650 ComparisonBias bias_;
2651
Dave Allison20dfc792014-06-16 20:44:29 -07002652 DISALLOW_COPY_AND_ASSIGN(HCondition);
2653};
2654
2655// Instruction to check if two inputs are equal to each other.
2656class HEqual : public HCondition {
2657 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002658 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2659 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002660
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002661 bool IsCommutative() const OVERRIDE { return true; }
2662
Roland Levillain9867bc72015-08-05 10:21:34 +01002663 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002664 return GetBlock()->GetGraph()->GetIntConstant(
2665 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002666 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002667 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002668 return GetBlock()->GetGraph()->GetIntConstant(
2669 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002670 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002671
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002672 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002673
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002674 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002675 return kCondEQ;
2676 }
2677
Mark Mendellc4701932015-04-10 13:18:51 -04002678 IfCondition GetOppositeCondition() const OVERRIDE {
2679 return kCondNE;
2680 }
2681
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002682 private:
Aart Bike9f37602015-10-09 11:15:55 -07002683 template <typename T> bool Compute(T x, T y) const { return x == y; }
2684
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002685 DISALLOW_COPY_AND_ASSIGN(HEqual);
2686};
2687
Dave Allison20dfc792014-06-16 20:44:29 -07002688class HNotEqual : public HCondition {
2689 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002690 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2691 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002692
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002693 bool IsCommutative() const OVERRIDE { return true; }
2694
Roland Levillain9867bc72015-08-05 10:21:34 +01002695 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002696 return GetBlock()->GetGraph()->GetIntConstant(
2697 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002698 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002699 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002700 return GetBlock()->GetGraph()->GetIntConstant(
2701 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002702 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002703
Dave Allison20dfc792014-06-16 20:44:29 -07002704 DECLARE_INSTRUCTION(NotEqual);
2705
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002706 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002707 return kCondNE;
2708 }
2709
Mark Mendellc4701932015-04-10 13:18:51 -04002710 IfCondition GetOppositeCondition() const OVERRIDE {
2711 return kCondEQ;
2712 }
2713
Dave Allison20dfc792014-06-16 20:44:29 -07002714 private:
Aart Bike9f37602015-10-09 11:15:55 -07002715 template <typename T> bool Compute(T x, T y) const { return x != y; }
2716
Dave Allison20dfc792014-06-16 20:44:29 -07002717 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2718};
2719
2720class HLessThan : public HCondition {
2721 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002722 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2723 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002724
Roland Levillain9867bc72015-08-05 10:21:34 +01002725 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002726 return GetBlock()->GetGraph()->GetIntConstant(
2727 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002728 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002729 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002730 return GetBlock()->GetGraph()->GetIntConstant(
2731 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002732 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002733
Dave Allison20dfc792014-06-16 20:44:29 -07002734 DECLARE_INSTRUCTION(LessThan);
2735
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002736 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002737 return kCondLT;
2738 }
2739
Mark Mendellc4701932015-04-10 13:18:51 -04002740 IfCondition GetOppositeCondition() const OVERRIDE {
2741 return kCondGE;
2742 }
2743
Dave Allison20dfc792014-06-16 20:44:29 -07002744 private:
Aart Bike9f37602015-10-09 11:15:55 -07002745 template <typename T> bool Compute(T x, T y) const { return x < y; }
2746
Dave Allison20dfc792014-06-16 20:44:29 -07002747 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2748};
2749
2750class HLessThanOrEqual : public HCondition {
2751 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002752 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2753 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002754
Roland Levillain9867bc72015-08-05 10:21:34 +01002755 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 return GetBlock()->GetGraph()->GetIntConstant(
2757 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002758 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002759 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002760 return GetBlock()->GetGraph()->GetIntConstant(
2761 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002762 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002763
Dave Allison20dfc792014-06-16 20:44:29 -07002764 DECLARE_INSTRUCTION(LessThanOrEqual);
2765
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002766 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002767 return kCondLE;
2768 }
2769
Mark Mendellc4701932015-04-10 13:18:51 -04002770 IfCondition GetOppositeCondition() const OVERRIDE {
2771 return kCondGT;
2772 }
2773
Dave Allison20dfc792014-06-16 20:44:29 -07002774 private:
Aart Bike9f37602015-10-09 11:15:55 -07002775 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2776
Dave Allison20dfc792014-06-16 20:44:29 -07002777 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2778};
2779
2780class HGreaterThan : public HCondition {
2781 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002782 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2783 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002784
Roland Levillain9867bc72015-08-05 10:21:34 +01002785 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002786 return GetBlock()->GetGraph()->GetIntConstant(
2787 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002788 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002789 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002790 return GetBlock()->GetGraph()->GetIntConstant(
2791 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002792 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002793
Dave Allison20dfc792014-06-16 20:44:29 -07002794 DECLARE_INSTRUCTION(GreaterThan);
2795
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002796 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002797 return kCondGT;
2798 }
2799
Mark Mendellc4701932015-04-10 13:18:51 -04002800 IfCondition GetOppositeCondition() const OVERRIDE {
2801 return kCondLE;
2802 }
2803
Dave Allison20dfc792014-06-16 20:44:29 -07002804 private:
Aart Bike9f37602015-10-09 11:15:55 -07002805 template <typename T> bool Compute(T x, T y) const { return x > y; }
2806
Dave Allison20dfc792014-06-16 20:44:29 -07002807 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2808};
2809
2810class HGreaterThanOrEqual : public HCondition {
2811 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002812 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2813 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002814
Roland Levillain9867bc72015-08-05 10:21:34 +01002815 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 return GetBlock()->GetGraph()->GetIntConstant(
2817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002818 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002819 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002820 return GetBlock()->GetGraph()->GetIntConstant(
2821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002822 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002823
Dave Allison20dfc792014-06-16 20:44:29 -07002824 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002826 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002827 return kCondGE;
2828 }
2829
Mark Mendellc4701932015-04-10 13:18:51 -04002830 IfCondition GetOppositeCondition() const OVERRIDE {
2831 return kCondLT;
2832 }
2833
Dave Allison20dfc792014-06-16 20:44:29 -07002834 private:
Aart Bike9f37602015-10-09 11:15:55 -07002835 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2836
Dave Allison20dfc792014-06-16 20:44:29 -07002837 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2838};
2839
Aart Bike9f37602015-10-09 11:15:55 -07002840class HBelow : public HCondition {
2841 public:
2842 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2843 : HCondition(first, second, dex_pc) {}
2844
2845 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2846 return GetBlock()->GetGraph()->GetIntConstant(
2847 Compute(static_cast<uint32_t>(x->GetValue()),
2848 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2849 }
2850 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2851 return GetBlock()->GetGraph()->GetIntConstant(
2852 Compute(static_cast<uint64_t>(x->GetValue()),
2853 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2854 }
2855
2856 DECLARE_INSTRUCTION(Below);
2857
2858 IfCondition GetCondition() const OVERRIDE {
2859 return kCondB;
2860 }
2861
2862 IfCondition GetOppositeCondition() const OVERRIDE {
2863 return kCondAE;
2864 }
2865
2866 private:
2867 template <typename T> bool Compute(T x, T y) const { return x < y; }
2868
2869 DISALLOW_COPY_AND_ASSIGN(HBelow);
2870};
2871
2872class HBelowOrEqual : public HCondition {
2873 public:
2874 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2875 : HCondition(first, second, dex_pc) {}
2876
2877 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2878 return GetBlock()->GetGraph()->GetIntConstant(
2879 Compute(static_cast<uint32_t>(x->GetValue()),
2880 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2881 }
2882 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2883 return GetBlock()->GetGraph()->GetIntConstant(
2884 Compute(static_cast<uint64_t>(x->GetValue()),
2885 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2886 }
2887
2888 DECLARE_INSTRUCTION(BelowOrEqual);
2889
2890 IfCondition GetCondition() const OVERRIDE {
2891 return kCondBE;
2892 }
2893
2894 IfCondition GetOppositeCondition() const OVERRIDE {
2895 return kCondA;
2896 }
2897
2898 private:
2899 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2900
2901 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2902};
2903
2904class HAbove : public HCondition {
2905 public:
2906 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2907 : HCondition(first, second, dex_pc) {}
2908
2909 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2910 return GetBlock()->GetGraph()->GetIntConstant(
2911 Compute(static_cast<uint32_t>(x->GetValue()),
2912 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2913 }
2914 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2915 return GetBlock()->GetGraph()->GetIntConstant(
2916 Compute(static_cast<uint64_t>(x->GetValue()),
2917 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2918 }
2919
2920 DECLARE_INSTRUCTION(Above);
2921
2922 IfCondition GetCondition() const OVERRIDE {
2923 return kCondA;
2924 }
2925
2926 IfCondition GetOppositeCondition() const OVERRIDE {
2927 return kCondBE;
2928 }
2929
2930 private:
2931 template <typename T> bool Compute(T x, T y) const { return x > y; }
2932
2933 DISALLOW_COPY_AND_ASSIGN(HAbove);
2934};
2935
2936class HAboveOrEqual : public HCondition {
2937 public:
2938 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2939 : HCondition(first, second, dex_pc) {}
2940
2941 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2942 return GetBlock()->GetGraph()->GetIntConstant(
2943 Compute(static_cast<uint32_t>(x->GetValue()),
2944 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2945 }
2946 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2947 return GetBlock()->GetGraph()->GetIntConstant(
2948 Compute(static_cast<uint64_t>(x->GetValue()),
2949 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2950 }
2951
2952 DECLARE_INSTRUCTION(AboveOrEqual);
2953
2954 IfCondition GetCondition() const OVERRIDE {
2955 return kCondAE;
2956 }
2957
2958 IfCondition GetOppositeCondition() const OVERRIDE {
2959 return kCondB;
2960 }
2961
2962 private:
2963 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2964
2965 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
2966};
Dave Allison20dfc792014-06-16 20:44:29 -07002967
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002968// Instruction to check how two inputs compare to each other.
2969// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2970class HCompare : public HBinaryOperation {
2971 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002972 HCompare(Primitive::Type type,
2973 HInstruction* first,
2974 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002975 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002976 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002977 : HBinaryOperation(Primitive::kPrimInt,
2978 first,
2979 second,
2980 SideEffectsForArchRuntimeCalls(type),
2981 dex_pc),
2982 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002983 DCHECK_EQ(type, first->GetType());
2984 DCHECK_EQ(type, second->GetType());
2985 }
2986
Roland Levillain9867bc72015-08-05 10:21:34 +01002987 template <typename T>
2988 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002989
Roland Levillain9867bc72015-08-05 10:21:34 +01002990 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002991 return GetBlock()->GetGraph()->GetIntConstant(
2992 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002993 }
2994 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002995 return GetBlock()->GetGraph()->GetIntConstant(
2996 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002997 }
2998
Calin Juravleddb7df22014-11-25 20:56:51 +00002999 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3000 return bias_ == other->AsCompare()->bias_;
3001 }
3002
Mark Mendellc4701932015-04-10 13:18:51 -04003003 ComparisonBias GetBias() const { return bias_; }
3004
Roland Levillain4fa13f62015-07-06 18:11:54 +01003005 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003006
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003007
3008 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3009 // MIPS64 uses a runtime call for FP comparisons.
3010 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3011 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003012
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003013 DECLARE_INSTRUCTION(Compare);
3014
3015 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003016 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003017
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003018 DISALLOW_COPY_AND_ASSIGN(HCompare);
3019};
3020
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003021// A local in the graph. Corresponds to a Dex register.
3022class HLocal : public HTemplateInstruction<0> {
3023 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003024 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003025 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003026
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003027 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003028
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003029 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003030
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003031 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003032 // The Dex register number.
3033 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003034
3035 DISALLOW_COPY_AND_ASSIGN(HLocal);
3036};
3037
3038// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003039class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003040 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003041 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3042 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003043 SetRawInputAt(0, local);
3044 }
3045
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003046 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3047
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003048 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003049
3050 private:
3051 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3052};
3053
3054// Store a value in a given local. This instruction has two inputs: the value
3055// and the local.
3056class HStoreLocal : public HTemplateInstruction<2> {
3057 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003058 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3059 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003060 SetRawInputAt(0, local);
3061 SetRawInputAt(1, value);
3062 }
3063
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003064 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3065
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003066 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003067
3068 private:
3069 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3070};
3071
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003072class HFloatConstant : public HConstant {
3073 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003074 float GetValue() const { return value_; }
3075
David Brazdil77a48ae2015-09-15 12:34:04 +00003076 uint64_t GetValueAsUint64() const OVERRIDE {
3077 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3078 }
3079
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003080 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003081 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003082 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003083 }
3084
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003085 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003086
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003087 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003088 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003089 }
3090 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003091 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003092 }
3093 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003094 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3095 }
3096 bool IsNaN() const {
3097 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003098 }
3099
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003100 DECLARE_INSTRUCTION(FloatConstant);
3101
3102 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003103 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3104 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3105 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3106 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003107
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003108 const float value_;
3109
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003110 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003111 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003112 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003113 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3114};
3115
3116class HDoubleConstant : public HConstant {
3117 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003118 double GetValue() const { return value_; }
3119
David Brazdil77a48ae2015-09-15 12:34:04 +00003120 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3121
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003122 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003123 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003124 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003125 }
3126
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003127 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003128
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003129 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003130 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003131 }
3132 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003133 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003134 }
3135 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003136 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3137 }
3138 bool IsNaN() const {
3139 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003140 }
3141
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003142 DECLARE_INSTRUCTION(DoubleConstant);
3143
3144 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003145 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3146 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3147 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3148 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003149
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003150 const double value_;
3151
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003152 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003153 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003154 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003155 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3156};
3157
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003158enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003159#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003160#include "intrinsics_list.h"
3161 kNone,
3162 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3163#undef INTRINSICS_LIST
3164#undef OPTIMIZING_INTRINSICS
3165};
3166std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3167
Agi Csaki05f20562015-08-19 14:58:14 -07003168enum IntrinsicNeedsEnvironmentOrCache {
3169 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3170 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003171};
3172
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003173class HInvoke : public HInstruction {
3174 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003175 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003176
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003177 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003178
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003179 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003180 SetRawInputAt(index, argument);
3181 }
3182
Roland Levillain3e3d7332015-04-28 11:00:54 +01003183 // Return the number of arguments. This number can be lower than
3184 // the number of inputs returned by InputCount(), as some invoke
3185 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3186 // inputs at the end of their list of inputs.
3187 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3188
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003189 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003190
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003191
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003192 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003193 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003194
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003195 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3196
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003197 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003198 return intrinsic_;
3199 }
3200
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003201 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003202
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003203 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003204 return GetEnvironment()->GetParent() != nullptr;
3205 }
3206
3207 bool CanThrow() const OVERRIDE { return true; }
3208
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003209 uint32_t* GetIntrinsicOptimizations() {
3210 return &intrinsic_optimizations_;
3211 }
3212
3213 const uint32_t* GetIntrinsicOptimizations() const {
3214 return &intrinsic_optimizations_;
3215 }
3216
3217 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3218
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003219 DECLARE_INSTRUCTION(Invoke);
3220
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003221 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003222 HInvoke(ArenaAllocator* arena,
3223 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003224 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003225 Primitive::Type return_type,
3226 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003227 uint32_t dex_method_index,
3228 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003229 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003230 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003231 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003232 inputs_(number_of_arguments + number_of_other_inputs,
3233 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003234 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003235 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003236 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003237 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003238 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003239 }
3240
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003241 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003242 return inputs_[index];
3243 }
3244
David Brazdil1abb4192015-02-17 18:33:36 +00003245 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003246 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003247 }
3248
Roland Levillain3e3d7332015-04-28 11:00:54 +01003249 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003250 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003251 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003252 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003253 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003254 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003255
3256 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3257 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003258
3259 private:
3260 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3261};
3262
Calin Juravle175dc732015-08-25 15:42:32 +01003263class HInvokeUnresolved : public HInvoke {
3264 public:
3265 HInvokeUnresolved(ArenaAllocator* arena,
3266 uint32_t number_of_arguments,
3267 Primitive::Type return_type,
3268 uint32_t dex_pc,
3269 uint32_t dex_method_index,
3270 InvokeType invoke_type)
3271 : HInvoke(arena,
3272 number_of_arguments,
3273 0u /* number_of_other_inputs */,
3274 return_type,
3275 dex_pc,
3276 dex_method_index,
3277 invoke_type) {
3278 }
3279
3280 DECLARE_INSTRUCTION(InvokeUnresolved);
3281
3282 private:
3283 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3284};
3285
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003286class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003287 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003288 // Requirements of this method call regarding the class
3289 // initialization (clinit) check of its declaring class.
3290 enum class ClinitCheckRequirement {
3291 kNone, // Class already initialized.
3292 kExplicit, // Static call having explicit clinit check as last input.
3293 kImplicit, // Static call implicitly requiring a clinit check.
3294 };
3295
Vladimir Marko58155012015-08-19 12:49:41 +00003296 // Determines how to load the target ArtMethod*.
3297 enum class MethodLoadKind {
3298 // Use a String init ArtMethod* loaded from Thread entrypoints.
3299 kStringInit,
3300
3301 // Use the method's own ArtMethod* loaded by the register allocator.
3302 kRecursive,
3303
3304 // Use ArtMethod* at a known address, embed the direct address in the code.
3305 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3306 kDirectAddress,
3307
3308 // Use ArtMethod* at an address that will be known at link time, embed the direct
3309 // address in the code. If the image is relocatable, emit .patch_oat entry.
3310 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3311 // the image relocatable or not.
3312 kDirectAddressWithFixup,
3313
3314 // Load from resoved methods array in the dex cache using a PC-relative load.
3315 // Used when we need to use the dex cache, for example for invoke-static that
3316 // may cause class initialization (the entry may point to a resolution method),
3317 // and we know that we can access the dex cache arrays using a PC-relative load.
3318 kDexCachePcRelative,
3319
3320 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3321 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3322 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3323 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3324 kDexCacheViaMethod,
3325 };
3326
3327 // Determines the location of the code pointer.
3328 enum class CodePtrLocation {
3329 // Recursive call, use local PC-relative call instruction.
3330 kCallSelf,
3331
3332 // Use PC-relative call instruction patched at link time.
3333 // Used for calls within an oat file, boot->boot or app->app.
3334 kCallPCRelative,
3335
3336 // Call to a known target address, embed the direct address in code.
3337 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3338 kCallDirect,
3339
3340 // Call to a target address that will be known at link time, embed the direct
3341 // address in code. If the image is relocatable, emit .patch_oat entry.
3342 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3343 // the image relocatable or not.
3344 kCallDirectWithFixup,
3345
3346 // Use code pointer from the ArtMethod*.
3347 // Used when we don't know the target code. This is also the last-resort-kind used when
3348 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3349 kCallArtMethod,
3350 };
3351
3352 struct DispatchInfo {
3353 const MethodLoadKind method_load_kind;
3354 const CodePtrLocation code_ptr_location;
3355 // The method load data holds
3356 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3357 // Note that there are multiple string init methods, each having its own offset.
3358 // - the method address for kDirectAddress
3359 // - the dex cache arrays offset for kDexCachePcRel.
3360 const uint64_t method_load_data;
3361 const uint64_t direct_code_ptr;
3362 };
3363
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003364 HInvokeStaticOrDirect(ArenaAllocator* arena,
3365 uint32_t number_of_arguments,
3366 Primitive::Type return_type,
3367 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003368 uint32_t method_index,
3369 MethodReference target_method,
3370 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003371 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003372 InvokeType invoke_type,
3373 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003374 : HInvoke(arena,
3375 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003376 // There is one extra argument for the HCurrentMethod node, and
3377 // potentially one other if the clinit check is explicit, and one other
3378 // if the method is a string factory.
3379 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003380 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003381 return_type,
3382 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003383 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003384 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003385 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003386 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003387 target_method_(target_method),
3388 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003389
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003390 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003391 // We access the method via the dex cache so we can't do an implicit null check.
3392 // TODO: for intrinsics we can generate implicit null checks.
3393 return false;
3394 }
3395
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003396 bool CanBeNull() const OVERRIDE {
3397 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3398 }
3399
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003400 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003401 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3402 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3403 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003404 bool NeedsDexCache() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003405 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003406 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003407 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3408 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3409 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3410 MethodReference GetTargetMethod() const { return target_method_; }
3411
3412 int32_t GetStringInitOffset() const {
3413 DCHECK(IsStringInit());
3414 return dispatch_info_.method_load_data;
3415 }
3416
3417 uint64_t GetMethodAddress() const {
3418 DCHECK(HasMethodAddress());
3419 return dispatch_info_.method_load_data;
3420 }
3421
3422 uint32_t GetDexCacheArrayOffset() const {
3423 DCHECK(HasPcRelDexCache());
3424 return dispatch_info_.method_load_data;
3425 }
3426
3427 uint64_t GetDirectCodePtr() const {
3428 DCHECK(HasDirectCodePtr());
3429 return dispatch_info_.direct_code_ptr;
3430 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003431
Calin Juravle68ad6492015-08-18 17:08:12 +01003432 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3433
Roland Levillain4c0eb422015-04-24 16:43:49 +01003434 // Is this instruction a call to a static method?
3435 bool IsStatic() const {
3436 return GetInvokeType() == kStatic;
3437 }
3438
Roland Levillain3e3d7332015-04-28 11:00:54 +01003439 // Remove the art::HLoadClass instruction set as last input by
3440 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3441 // the initial art::HClinitCheck instruction (only relevant for
3442 // static calls with explicit clinit check).
3443 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003444 DCHECK(IsStaticWithExplicitClinitCheck());
3445 size_t last_input_index = InputCount() - 1;
3446 HInstruction* last_input = InputAt(last_input_index);
3447 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003448 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003449 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003450 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003451 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3452 DCHECK(IsStaticWithImplicitClinitCheck());
3453 }
3454
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003455 bool IsStringFactoryFor(HFakeString* str) const {
3456 if (!IsStringInit()) return false;
3457 // +1 for the current method.
3458 if (InputCount() == (number_of_arguments_ + 1)) return false;
3459 return InputAt(InputCount() - 1)->AsFakeString() == str;
3460 }
3461
3462 void RemoveFakeStringArgumentAsLastInput() {
3463 DCHECK(IsStringInit());
3464 size_t last_input_index = InputCount() - 1;
3465 HInstruction* last_input = InputAt(last_input_index);
3466 DCHECK(last_input != nullptr);
3467 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3468 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003469 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003470 }
3471
Roland Levillain4c0eb422015-04-24 16:43:49 +01003472 // Is this a call to a static method whose declaring class has an
3473 // explicit intialization check in the graph?
3474 bool IsStaticWithExplicitClinitCheck() const {
3475 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3476 }
3477
3478 // Is this a call to a static method whose declaring class has an
3479 // implicit intialization check requirement?
3480 bool IsStaticWithImplicitClinitCheck() const {
3481 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3482 }
3483
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003484 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003485
Roland Levillain4c0eb422015-04-24 16:43:49 +01003486 protected:
3487 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3488 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3489 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3490 HInstruction* input = input_record.GetInstruction();
3491 // `input` is the last input of a static invoke marked as having
3492 // an explicit clinit check. It must either be:
3493 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3494 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3495 DCHECK(input != nullptr);
3496 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3497 }
3498 return input_record;
3499 }
3500
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003501 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003502 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003503 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003504 // The target method may refer to different dex file or method index than the original
3505 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3506 // in derived class to increase visibility.
3507 MethodReference target_method_;
3508 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003509
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003510 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003511};
3512
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003513class HInvokeVirtual : public HInvoke {
3514 public:
3515 HInvokeVirtual(ArenaAllocator* arena,
3516 uint32_t number_of_arguments,
3517 Primitive::Type return_type,
3518 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003519 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003520 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003521 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003522 vtable_index_(vtable_index) {}
3523
Calin Juravle641547a2015-04-21 22:08:51 +01003524 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003525 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003526 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003527 }
3528
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003529 uint32_t GetVTableIndex() const { return vtable_index_; }
3530
3531 DECLARE_INSTRUCTION(InvokeVirtual);
3532
3533 private:
3534 const uint32_t vtable_index_;
3535
3536 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3537};
3538
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003539class HInvokeInterface : public HInvoke {
3540 public:
3541 HInvokeInterface(ArenaAllocator* arena,
3542 uint32_t number_of_arguments,
3543 Primitive::Type return_type,
3544 uint32_t dex_pc,
3545 uint32_t dex_method_index,
3546 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003547 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003548 imt_index_(imt_index) {}
3549
Calin Juravle641547a2015-04-21 22:08:51 +01003550 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003551 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003552 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003553 }
3554
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003555 uint32_t GetImtIndex() const { return imt_index_; }
3556 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3557
3558 DECLARE_INSTRUCTION(InvokeInterface);
3559
3560 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003561 const uint32_t imt_index_;
3562
3563 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3564};
3565
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003566class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003567 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003568 HNewInstance(HCurrentMethod* current_method,
3569 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003570 uint16_t type_index,
3571 const DexFile& dex_file,
3572 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003573 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003574 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003575 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003576 entrypoint_(entrypoint) {
3577 SetRawInputAt(0, current_method);
3578 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003579
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003580 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003581 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003582
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003583 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003584 bool NeedsEnvironment() const OVERRIDE { return true; }
3585 // It may throw when called on:
3586 // - interfaces
3587 // - abstract/innaccessible/unknown classes
3588 // TODO: optimize when possible.
3589 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003590
Calin Juravle10e244f2015-01-26 18:54:32 +00003591 bool CanBeNull() const OVERRIDE { return false; }
3592
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003593 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3594
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003595 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003596
3597 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003598 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003599 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003600 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003601
3602 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3603};
3604
Roland Levillain88cb1752014-10-20 16:36:47 +01003605class HNeg : public HUnaryOperation {
3606 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003607 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3608 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003609
Roland Levillain9867bc72015-08-05 10:21:34 +01003610 template <typename T> T Compute(T x) const { return -x; }
3611
3612 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003613 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003614 }
3615 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003616 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003617 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003618
Roland Levillain88cb1752014-10-20 16:36:47 +01003619 DECLARE_INSTRUCTION(Neg);
3620
3621 private:
3622 DISALLOW_COPY_AND_ASSIGN(HNeg);
3623};
3624
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003625class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003626 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003627 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003628 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003629 uint32_t dex_pc,
3630 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003631 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003632 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003633 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003634 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003635 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003636 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003637 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003638 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003639 }
3640
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003641 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003642 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003643
3644 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003645 bool NeedsEnvironment() const OVERRIDE { return true; }
3646
Mingyao Yang0c365e62015-03-31 15:09:29 -07003647 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3648 bool CanThrow() const OVERRIDE { return true; }
3649
Calin Juravle10e244f2015-01-26 18:54:32 +00003650 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003651
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003652 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3653
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003654 DECLARE_INSTRUCTION(NewArray);
3655
3656 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003657 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003658 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003659 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003660
3661 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3662};
3663
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003664class HAdd : public HBinaryOperation {
3665 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003666 HAdd(Primitive::Type result_type,
3667 HInstruction* left,
3668 HInstruction* right,
3669 uint32_t dex_pc = kNoDexPc)
3670 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003671
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003672 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003673
Roland Levillain9867bc72015-08-05 10:21:34 +01003674 template <typename T> T Compute(T x, T y) const { return x + y; }
3675
3676 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003677 return GetBlock()->GetGraph()->GetIntConstant(
3678 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003679 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003680 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003681 return GetBlock()->GetGraph()->GetLongConstant(
3682 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003683 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003684
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003685 DECLARE_INSTRUCTION(Add);
3686
3687 private:
3688 DISALLOW_COPY_AND_ASSIGN(HAdd);
3689};
3690
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003691class HSub : public HBinaryOperation {
3692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003693 HSub(Primitive::Type result_type,
3694 HInstruction* left,
3695 HInstruction* right,
3696 uint32_t dex_pc = kNoDexPc)
3697 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003698
Roland Levillain9867bc72015-08-05 10:21:34 +01003699 template <typename T> T Compute(T x, T y) const { return x - y; }
3700
3701 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003702 return GetBlock()->GetGraph()->GetIntConstant(
3703 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003704 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003705 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003706 return GetBlock()->GetGraph()->GetLongConstant(
3707 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003708 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003709
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003710 DECLARE_INSTRUCTION(Sub);
3711
3712 private:
3713 DISALLOW_COPY_AND_ASSIGN(HSub);
3714};
3715
Calin Juravle34bacdf2014-10-07 20:23:36 +01003716class HMul : public HBinaryOperation {
3717 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003718 HMul(Primitive::Type result_type,
3719 HInstruction* left,
3720 HInstruction* right,
3721 uint32_t dex_pc = kNoDexPc)
3722 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003723
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003724 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003725
Roland Levillain9867bc72015-08-05 10:21:34 +01003726 template <typename T> T Compute(T x, T y) const { return x * y; }
3727
3728 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003729 return GetBlock()->GetGraph()->GetIntConstant(
3730 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003731 }
3732 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003733 return GetBlock()->GetGraph()->GetLongConstant(
3734 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003735 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003736
3737 DECLARE_INSTRUCTION(Mul);
3738
3739 private:
3740 DISALLOW_COPY_AND_ASSIGN(HMul);
3741};
3742
Calin Juravle7c4954d2014-10-28 16:57:40 +00003743class HDiv : public HBinaryOperation {
3744 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003745 HDiv(Primitive::Type result_type,
3746 HInstruction* left,
3747 HInstruction* right,
3748 uint32_t dex_pc)
3749 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003750
Roland Levillain9867bc72015-08-05 10:21:34 +01003751 template <typename T>
3752 T Compute(T x, T y) const {
3753 // Our graph structure ensures we never have 0 for `y` during
3754 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003755 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003756 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003757 return (y == -1) ? -x : x / y;
3758 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003759
Roland Levillain9867bc72015-08-05 10:21:34 +01003760 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003761 return GetBlock()->GetGraph()->GetIntConstant(
3762 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003763 }
3764 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003765 return GetBlock()->GetGraph()->GetLongConstant(
3766 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003767 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003768
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003769 static SideEffects SideEffectsForArchRuntimeCalls() {
3770 // The generated code can use a runtime call.
3771 return SideEffects::CanTriggerGC();
3772 }
3773
Calin Juravle7c4954d2014-10-28 16:57:40 +00003774 DECLARE_INSTRUCTION(Div);
3775
3776 private:
3777 DISALLOW_COPY_AND_ASSIGN(HDiv);
3778};
3779
Calin Juravlebacfec32014-11-14 15:54:36 +00003780class HRem : public HBinaryOperation {
3781 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003782 HRem(Primitive::Type result_type,
3783 HInstruction* left,
3784 HInstruction* right,
3785 uint32_t dex_pc)
3786 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003787
Roland Levillain9867bc72015-08-05 10:21:34 +01003788 template <typename T>
3789 T Compute(T x, T y) const {
3790 // Our graph structure ensures we never have 0 for `y` during
3791 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003792 DCHECK_NE(y, 0);
3793 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3794 return (y == -1) ? 0 : x % y;
3795 }
3796
Roland Levillain9867bc72015-08-05 10:21:34 +01003797 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003798 return GetBlock()->GetGraph()->GetIntConstant(
3799 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003800 }
3801 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003802 return GetBlock()->GetGraph()->GetLongConstant(
3803 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003804 }
3805
Calin Juravlebacfec32014-11-14 15:54:36 +00003806
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003807 static SideEffects SideEffectsForArchRuntimeCalls() {
3808 return SideEffects::CanTriggerGC();
3809 }
3810
Calin Juravlebacfec32014-11-14 15:54:36 +00003811 DECLARE_INSTRUCTION(Rem);
3812
3813 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003814 DISALLOW_COPY_AND_ASSIGN(HRem);
3815};
3816
Calin Juravled0d48522014-11-04 16:40:20 +00003817class HDivZeroCheck : public HExpression<1> {
3818 public:
3819 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003820 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003821 SetRawInputAt(0, value);
3822 }
3823
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003824 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3825
Calin Juravled0d48522014-11-04 16:40:20 +00003826 bool CanBeMoved() const OVERRIDE { return true; }
3827
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003828 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003829 return true;
3830 }
3831
3832 bool NeedsEnvironment() const OVERRIDE { return true; }
3833 bool CanThrow() const OVERRIDE { return true; }
3834
Calin Juravled0d48522014-11-04 16:40:20 +00003835 DECLARE_INSTRUCTION(DivZeroCheck);
3836
3837 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003838 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3839};
3840
Calin Juravle9aec02f2014-11-18 23:06:35 +00003841class HShl : public HBinaryOperation {
3842 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003843 HShl(Primitive::Type result_type,
3844 HInstruction* left,
3845 HInstruction* right,
3846 uint32_t dex_pc = kNoDexPc)
3847 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003848
Roland Levillain9867bc72015-08-05 10:21:34 +01003849 template <typename T, typename U, typename V>
3850 T Compute(T x, U y, V max_shift_value) const {
3851 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3852 "V is not the unsigned integer type corresponding to T");
3853 return x << (y & max_shift_value);
3854 }
3855
3856 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3857 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003858 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003859 }
3860 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3861 // case is handled as `x << static_cast<int>(y)`.
3862 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3863 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003864 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003865 }
3866 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3867 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003868 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003869 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003870
3871 DECLARE_INSTRUCTION(Shl);
3872
3873 private:
3874 DISALLOW_COPY_AND_ASSIGN(HShl);
3875};
3876
3877class HShr : public HBinaryOperation {
3878 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003879 HShr(Primitive::Type result_type,
3880 HInstruction* left,
3881 HInstruction* right,
3882 uint32_t dex_pc = kNoDexPc)
3883 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003884
Roland Levillain9867bc72015-08-05 10:21:34 +01003885 template <typename T, typename U, typename V>
3886 T Compute(T x, U y, V max_shift_value) const {
3887 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3888 "V is not the unsigned integer type corresponding to T");
3889 return x >> (y & max_shift_value);
3890 }
3891
3892 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3893 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003894 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003895 }
3896 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3897 // case is handled as `x >> static_cast<int>(y)`.
3898 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3899 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003900 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003901 }
3902 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3903 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003904 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003905 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003906
3907 DECLARE_INSTRUCTION(Shr);
3908
3909 private:
3910 DISALLOW_COPY_AND_ASSIGN(HShr);
3911};
3912
3913class HUShr : public HBinaryOperation {
3914 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003915 HUShr(Primitive::Type result_type,
3916 HInstruction* left,
3917 HInstruction* right,
3918 uint32_t dex_pc = kNoDexPc)
3919 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003920
Roland Levillain9867bc72015-08-05 10:21:34 +01003921 template <typename T, typename U, typename V>
3922 T Compute(T x, U y, V max_shift_value) const {
3923 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3924 "V is not the unsigned integer type corresponding to T");
3925 V ux = static_cast<V>(x);
3926 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003927 }
3928
Roland Levillain9867bc72015-08-05 10:21:34 +01003929 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3930 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003931 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003932 }
3933 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3934 // case is handled as `x >>> static_cast<int>(y)`.
3935 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3936 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003937 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003938 }
3939 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3940 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003941 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003942 }
3943
3944 DECLARE_INSTRUCTION(UShr);
3945
3946 private:
3947 DISALLOW_COPY_AND_ASSIGN(HUShr);
3948};
3949
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003950class HAnd : public HBinaryOperation {
3951 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003952 HAnd(Primitive::Type result_type,
3953 HInstruction* left,
3954 HInstruction* right,
3955 uint32_t dex_pc = kNoDexPc)
3956 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003957
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003958 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003959
Roland Levillain9867bc72015-08-05 10:21:34 +01003960 template <typename T, typename U>
3961 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3962
3963 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003964 return GetBlock()->GetGraph()->GetIntConstant(
3965 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003966 }
3967 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003968 return GetBlock()->GetGraph()->GetLongConstant(
3969 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003970 }
3971 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003972 return GetBlock()->GetGraph()->GetLongConstant(
3973 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003974 }
3975 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003976 return GetBlock()->GetGraph()->GetLongConstant(
3977 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003978 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003979
3980 DECLARE_INSTRUCTION(And);
3981
3982 private:
3983 DISALLOW_COPY_AND_ASSIGN(HAnd);
3984};
3985
3986class HOr : public HBinaryOperation {
3987 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003988 HOr(Primitive::Type result_type,
3989 HInstruction* left,
3990 HInstruction* right,
3991 uint32_t dex_pc = kNoDexPc)
3992 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003993
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003994 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003995
Roland Levillain9867bc72015-08-05 10:21:34 +01003996 template <typename T, typename U>
3997 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3998
3999 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004000 return GetBlock()->GetGraph()->GetIntConstant(
4001 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004002 }
4003 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004004 return GetBlock()->GetGraph()->GetLongConstant(
4005 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004006 }
4007 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004008 return GetBlock()->GetGraph()->GetLongConstant(
4009 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004010 }
4011 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004012 return GetBlock()->GetGraph()->GetLongConstant(
4013 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004014 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004015
4016 DECLARE_INSTRUCTION(Or);
4017
4018 private:
4019 DISALLOW_COPY_AND_ASSIGN(HOr);
4020};
4021
4022class HXor : public HBinaryOperation {
4023 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004024 HXor(Primitive::Type result_type,
4025 HInstruction* left,
4026 HInstruction* right,
4027 uint32_t dex_pc = kNoDexPc)
4028 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004029
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004030 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004031
Roland Levillain9867bc72015-08-05 10:21:34 +01004032 template <typename T, typename U>
4033 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4034
4035 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004036 return GetBlock()->GetGraph()->GetIntConstant(
4037 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004038 }
4039 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004040 return GetBlock()->GetGraph()->GetLongConstant(
4041 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004042 }
4043 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004044 return GetBlock()->GetGraph()->GetLongConstant(
4045 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004046 }
4047 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004048 return GetBlock()->GetGraph()->GetLongConstant(
4049 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004050 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004051
4052 DECLARE_INSTRUCTION(Xor);
4053
4054 private:
4055 DISALLOW_COPY_AND_ASSIGN(HXor);
4056};
4057
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004058// The value of a parameter in this method. Its location depends on
4059// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004060class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004061 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004062 HParameterValue(const DexFile& dex_file,
4063 uint16_t type_index,
4064 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004065 Primitive::Type parameter_type,
4066 bool is_this = false)
4067 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004068 dex_file_(dex_file),
4069 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004070 index_(index),
4071 is_this_(is_this),
4072 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004073
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004074 const DexFile& GetDexFile() const { return dex_file_; }
4075 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004076 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004077 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004078
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
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004082 DECLARE_INSTRUCTION(ParameterValue);
4083
4084 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004085 const DexFile& dex_file_;
4086 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004087 // 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; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004105 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004106 return true;
4107 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004108
Roland Levillain9867bc72015-08-05 10:21:34 +01004109 template <typename T> T Compute(T x) const { return ~x; }
4110
4111 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004112 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004113 }
4114 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004115 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004116 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004117
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004118 DECLARE_INSTRUCTION(Not);
4119
4120 private:
4121 DISALLOW_COPY_AND_ASSIGN(HNot);
4122};
4123
David Brazdil66d126e2015-04-03 16:02:44 +01004124class HBooleanNot : public HUnaryOperation {
4125 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004126 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4127 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004128
4129 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004130 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004131 return true;
4132 }
4133
Roland Levillain9867bc72015-08-05 10:21:34 +01004134 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004135 DCHECK(IsUint<1>(x));
4136 return !x;
4137 }
4138
Roland Levillain9867bc72015-08-05 10:21:34 +01004139 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004140 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004141 }
4142 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4143 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004144 UNREACHABLE();
4145 }
4146
4147 DECLARE_INSTRUCTION(BooleanNot);
4148
4149 private:
4150 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4151};
4152
Roland Levillaindff1f282014-11-05 14:15:05 +00004153class HTypeConversion : public HExpression<1> {
4154 public:
4155 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004156 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004157 : HExpression(result_type,
4158 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4159 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004160 SetRawInputAt(0, input);
4161 DCHECK_NE(input->GetType(), result_type);
4162 }
4163
4164 HInstruction* GetInput() const { return InputAt(0); }
4165 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4166 Primitive::Type GetResultType() const { return GetType(); }
4167
Roland Levillain624279f2014-12-04 11:54:28 +00004168 // Required by the x86 and ARM code generators when producing calls
4169 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004170
Roland Levillaindff1f282014-11-05 14:15:05 +00004171 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004172 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004173
Mark Mendelle82549b2015-05-06 10:55:34 -04004174 // Try to statically evaluate the conversion and return a HConstant
4175 // containing the result. If the input cannot be converted, return nullptr.
4176 HConstant* TryStaticEvaluation() const;
4177
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004178 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4179 Primitive::Type result_type) {
4180 // Some architectures may not require the 'GC' side effects, but at this point
4181 // in the compilation process we do not know what architecture we will
4182 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004183 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4184 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004185 return SideEffects::CanTriggerGC();
4186 }
4187 return SideEffects::None();
4188 }
4189
Roland Levillaindff1f282014-11-05 14:15:05 +00004190 DECLARE_INSTRUCTION(TypeConversion);
4191
4192 private:
4193 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4194};
4195
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004196static constexpr uint32_t kNoRegNumber = -1;
4197
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004198class HPhi : public HInstruction {
4199 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004200 HPhi(ArenaAllocator* arena,
4201 uint32_t reg_number,
4202 size_t number_of_inputs,
4203 Primitive::Type type,
4204 uint32_t dex_pc = kNoDexPc)
4205 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004206 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004207 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004208 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004209 is_live_(false),
4210 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004211 }
4212
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004213 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4214 static Primitive::Type ToPhiType(Primitive::Type type) {
4215 switch (type) {
4216 case Primitive::kPrimBoolean:
4217 case Primitive::kPrimByte:
4218 case Primitive::kPrimShort:
4219 case Primitive::kPrimChar:
4220 return Primitive::kPrimInt;
4221 default:
4222 return type;
4223 }
4224 }
4225
David Brazdilffee3d32015-07-06 11:48:53 +01004226 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4227
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004228 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004229
4230 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004231 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004232
Calin Juravle10e244f2015-01-26 18:54:32 +00004233 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004234 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004235
Calin Juravle10e244f2015-01-26 18:54:32 +00004236 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4237 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4238
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004239 uint32_t GetRegNumber() const { return reg_number_; }
4240
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004241 void SetDead() { is_live_ = false; }
4242 void SetLive() { is_live_ = true; }
4243 bool IsDead() const { return !is_live_; }
4244 bool IsLive() const { return is_live_; }
4245
David Brazdil77a48ae2015-09-15 12:34:04 +00004246 bool IsVRegEquivalentOf(HInstruction* other) const {
4247 return other != nullptr
4248 && other->IsPhi()
4249 && other->AsPhi()->GetBlock() == GetBlock()
4250 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4251 }
4252
Calin Juravlea4f88312015-04-16 12:57:19 +01004253 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4254 // An equivalent phi is a phi having the same dex register and type.
4255 // It assumes that phis with the same dex register are adjacent.
4256 HPhi* GetNextEquivalentPhiWithSameType() {
4257 HInstruction* next = GetNext();
4258 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4259 if (next->GetType() == GetType()) {
4260 return next->AsPhi();
4261 }
4262 next = next->GetNext();
4263 }
4264 return nullptr;
4265 }
4266
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004267 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004268
David Brazdil1abb4192015-02-17 18:33:36 +00004269 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004270 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004271 return inputs_[index];
4272 }
David Brazdil1abb4192015-02-17 18:33:36 +00004273
4274 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004275 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004276 }
4277
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004278 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004279 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004280 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004281 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004282 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004283 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004284
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004285 DISALLOW_COPY_AND_ASSIGN(HPhi);
4286};
4287
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004288class HNullCheck : public HExpression<1> {
4289 public:
4290 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004291 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004292 SetRawInputAt(0, value);
4293 }
4294
Calin Juravle10e244f2015-01-26 18:54:32 +00004295 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004296 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004297 return true;
4298 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004299
Calin Juravle10e244f2015-01-26 18:54:32 +00004300 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004301
Calin Juravle10e244f2015-01-26 18:54:32 +00004302 bool CanThrow() const OVERRIDE { return true; }
4303
4304 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004305
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004306
4307 DECLARE_INSTRUCTION(NullCheck);
4308
4309 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004310 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4311};
4312
4313class FieldInfo : public ValueObject {
4314 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004315 FieldInfo(MemberOffset field_offset,
4316 Primitive::Type field_type,
4317 bool is_volatile,
4318 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004319 const DexFile& dex_file,
4320 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004321 : field_offset_(field_offset),
4322 field_type_(field_type),
4323 is_volatile_(is_volatile),
4324 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004325 dex_file_(dex_file),
4326 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004327
4328 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004329 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004330 uint32_t GetFieldIndex() const { return index_; }
4331 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004332 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004333 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004334
4335 private:
4336 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004337 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004338 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004339 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004340 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004341 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004342};
4343
4344class HInstanceFieldGet : public HExpression<1> {
4345 public:
4346 HInstanceFieldGet(HInstruction* value,
4347 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004348 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004349 bool is_volatile,
4350 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004351 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004352 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004353 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004354 : HExpression(
4355 field_type,
4356 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
4357 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004358 SetRawInputAt(0, value);
4359 }
4360
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004361 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004362
4363 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4364 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4365 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004366 }
4367
Calin Juravle641547a2015-04-21 22:08:51 +01004368 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4369 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004370 }
4371
4372 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004373 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4374 }
4375
Calin Juravle52c48962014-12-16 17:02:57 +00004376 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004377 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004378 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004379 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004380
4381 DECLARE_INSTRUCTION(InstanceFieldGet);
4382
4383 private:
4384 const FieldInfo field_info_;
4385
4386 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4387};
4388
4389class HInstanceFieldSet : public HTemplateInstruction<2> {
4390 public:
4391 HInstanceFieldSet(HInstruction* object,
4392 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004393 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004394 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004395 bool is_volatile,
4396 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004397 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004398 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004399 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004400 : HTemplateInstruction(
4401 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
4402 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004403 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004404 SetRawInputAt(0, object);
4405 SetRawInputAt(1, value);
4406 }
4407
Calin Juravle641547a2015-04-21 22:08:51 +01004408 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4409 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004410 }
4411
Calin Juravle52c48962014-12-16 17:02:57 +00004412 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004413 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004414 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004415 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004416 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004417 bool GetValueCanBeNull() const { return value_can_be_null_; }
4418 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004419
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004420 DECLARE_INSTRUCTION(InstanceFieldSet);
4421
4422 private:
4423 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004424 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004425
4426 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4427};
4428
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004429class HArrayGet : public HExpression<2> {
4430 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004431 HArrayGet(HInstruction* array,
4432 HInstruction* index,
4433 Primitive::Type type,
Calin Juravle154746b2015-10-06 15:46:54 +01004434 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004435 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004436 SetRawInputAt(0, array);
4437 SetRawInputAt(1, index);
4438 }
4439
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004440 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004441 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004442 return true;
4443 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004444 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004445 // TODO: We can be smarter here.
4446 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4447 // which generates the implicit null check. There are cases when these can be removed
4448 // to produce better code. If we ever add optimizations to do so we should allow an
4449 // implicit check here (as long as the address falls in the first page).
4450 return false;
4451 }
4452
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004453 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004454
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004455 HInstruction* GetArray() const { return InputAt(0); }
4456 HInstruction* GetIndex() const { return InputAt(1); }
4457
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004458 DECLARE_INSTRUCTION(ArrayGet);
4459
4460 private:
4461 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4462};
4463
4464class HArraySet : public HTemplateInstruction<3> {
4465 public:
4466 HArraySet(HInstruction* array,
4467 HInstruction* index,
4468 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004469 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004470 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004471 : HTemplateInstruction(
4472 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004473 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004474 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004475 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004476 value_can_be_null_(true),
4477 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004478 SetRawInputAt(0, array);
4479 SetRawInputAt(1, index);
4480 SetRawInputAt(2, value);
4481 }
4482
Calin Juravle77520bc2015-01-12 18:45:46 +00004483 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004484 // We currently always call a runtime method to catch array store
4485 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004486 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004487 }
4488
Mingyao Yang81014cb2015-06-02 03:16:27 -07004489 // Can throw ArrayStoreException.
4490 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4491
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004492 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004493 // TODO: Same as for ArrayGet.
4494 return false;
4495 }
4496
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004497 void ClearNeedsTypeCheck() {
4498 needs_type_check_ = false;
4499 }
4500
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004501 void ClearValueCanBeNull() {
4502 value_can_be_null_ = false;
4503 }
4504
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004505 void SetStaticTypeOfArrayIsObjectArray() {
4506 static_type_of_array_is_object_array_ = true;
4507 }
4508
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004509 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004510 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004511 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004512
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004513 HInstruction* GetArray() const { return InputAt(0); }
4514 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004515 HInstruction* GetValue() const { return InputAt(2); }
4516
4517 Primitive::Type GetComponentType() const {
4518 // The Dex format does not type floating point index operations. Since the
4519 // `expected_component_type_` is set during building and can therefore not
4520 // be correct, we also check what is the value type. If it is a floating
4521 // point type, we must use that type.
4522 Primitive::Type value_type = GetValue()->GetType();
4523 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4524 ? value_type
4525 : expected_component_type_;
4526 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004527
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004528 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4529 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4530 }
4531
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004532 DECLARE_INSTRUCTION(ArraySet);
4533
4534 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004535 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004536 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004537 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004538 // Cached information for the reference_type_info_ so that codegen
4539 // does not need to inspect the static type.
4540 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004541
4542 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4543};
4544
4545class HArrayLength : public HExpression<1> {
4546 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004547 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004548 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004549 // Note that arrays do not change length, so the instruction does not
4550 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004551 SetRawInputAt(0, array);
4552 }
4553
Calin Juravle77520bc2015-01-12 18:45:46 +00004554 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004555 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004556 return true;
4557 }
Calin Juravle641547a2015-04-21 22:08:51 +01004558 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4559 return obj == InputAt(0);
4560 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004561
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004562 DECLARE_INSTRUCTION(ArrayLength);
4563
4564 private:
4565 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4566};
4567
4568class HBoundsCheck : public HExpression<2> {
4569 public:
4570 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004571 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004572 DCHECK(index->GetType() == Primitive::kPrimInt);
4573 SetRawInputAt(0, index);
4574 SetRawInputAt(1, length);
4575 }
4576
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004577 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004578 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004579 return true;
4580 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004581
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004582 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004583
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004584 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004585
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004586
4587 DECLARE_INSTRUCTION(BoundsCheck);
4588
4589 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004590 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4591};
4592
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004593/**
4594 * Some DEX instructions are folded into multiple HInstructions that need
4595 * to stay live until the last HInstruction. This class
4596 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004597 * HInstruction stays live. `index` represents the stack location index of the
4598 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004599 */
4600class HTemporary : public HTemplateInstruction<0> {
4601 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004602 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4603 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004604
4605 size_t GetIndex() const { return index_; }
4606
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004607 Primitive::Type GetType() const OVERRIDE {
4608 // The previous instruction is the one that will be stored in the temporary location.
4609 DCHECK(GetPrevious() != nullptr);
4610 return GetPrevious()->GetType();
4611 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004612
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004613 DECLARE_INSTRUCTION(Temporary);
4614
4615 private:
4616 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004617 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4618};
4619
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004620class HSuspendCheck : public HTemplateInstruction<0> {
4621 public:
4622 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004623 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004624
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004625 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004626 return true;
4627 }
4628
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004629 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4630 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004631
4632 DECLARE_INSTRUCTION(SuspendCheck);
4633
4634 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004635 // Only used for code generation, in order to share the same slow path between back edges
4636 // of a same loop.
4637 SlowPathCode* slow_path_;
4638
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004639 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4640};
4641
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004642/**
4643 * Instruction to load a Class object.
4644 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004645class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004646 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004647 HLoadClass(HCurrentMethod* current_method,
4648 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004649 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004650 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004651 uint32_t dex_pc,
4652 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004653 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004654 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004655 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004656 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004657 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004658 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004659 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004660 // Referrers class should not need access check. We never inline unverified
4661 // methods so we can't possibly end up in this situation.
4662 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004663 SetRawInputAt(0, current_method);
4664 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004665
4666 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004667
4668 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004669 // Note that we don't need to test for generate_clinit_check_.
4670 // Whether or not we need to generate the clinit check is processed in
4671 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004672 return other->AsLoadClass()->type_index_ == type_index_ &&
4673 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004674 }
4675
4676 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4677
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004678 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004679 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004680 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004681
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004682 bool NeedsEnvironment() const OVERRIDE {
4683 // Will call runtime and load the class if the class is not loaded yet.
4684 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004685 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004686 }
4687
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004688 bool MustGenerateClinitCheck() const {
4689 return generate_clinit_check_;
4690 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004691 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4692 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004693 }
4694
4695 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004696 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4697 }
4698
4699 bool NeedsAccessCheck() const {
4700 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004701 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004702
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004703 bool CanThrow() const OVERRIDE {
4704 // May call runtime and and therefore can throw.
4705 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004706 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004707 }
4708
Calin Juravleacf735c2015-02-12 15:25:22 +00004709 ReferenceTypeInfo GetLoadedClassRTI() {
4710 return loaded_class_rti_;
4711 }
4712
4713 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4714 // Make sure we only set exact types (the loaded class should never be merged).
4715 DCHECK(rti.IsExact());
4716 loaded_class_rti_ = rti;
4717 }
4718
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004719 const DexFile& GetDexFile() { return dex_file_; }
4720
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004721 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4722
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004723 static SideEffects SideEffectsForArchRuntimeCalls() {
4724 return SideEffects::CanTriggerGC();
4725 }
4726
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004727 DECLARE_INSTRUCTION(LoadClass);
4728
4729 private:
4730 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004731 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004732 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004733 // Whether this instruction must generate the initialization check.
4734 // Used for code generation.
4735 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004736 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004737
Calin Juravleacf735c2015-02-12 15:25:22 +00004738 ReferenceTypeInfo loaded_class_rti_;
4739
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004740 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4741};
4742
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004743class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004744 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004745 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004746 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4747 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004748 SetRawInputAt(0, current_method);
4749 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004750
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004751 bool CanBeMoved() const OVERRIDE { return true; }
4752
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004753 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4754 return other->AsLoadString()->string_index_ == string_index_;
4755 }
4756
4757 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4758
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004759 uint32_t GetStringIndex() const { return string_index_; }
4760
4761 // TODO: Can we deopt or debug when we resolve a string?
4762 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004763 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004764 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004765
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004766 static SideEffects SideEffectsForArchRuntimeCalls() {
4767 return SideEffects::CanTriggerGC();
4768 }
4769
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004770 DECLARE_INSTRUCTION(LoadString);
4771
4772 private:
4773 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004774
4775 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4776};
4777
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004778/**
4779 * Performs an initialization check on its Class object input.
4780 */
4781class HClinitCheck : public HExpression<1> {
4782 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004783 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004784 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004785 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004786 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4787 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004788 SetRawInputAt(0, constant);
4789 }
4790
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004791 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004792 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004793 return true;
4794 }
4795
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004796 bool NeedsEnvironment() const OVERRIDE {
4797 // May call runtime to initialize the class.
4798 return true;
4799 }
4800
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004801
4802 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4803
4804 DECLARE_INSTRUCTION(ClinitCheck);
4805
4806 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004807 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4808};
4809
4810class HStaticFieldGet : public HExpression<1> {
4811 public:
4812 HStaticFieldGet(HInstruction* cls,
4813 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004814 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004815 bool is_volatile,
4816 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004817 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004818 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004819 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004820 : HExpression(
4821 field_type,
4822 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
4823 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004824 SetRawInputAt(0, cls);
4825 }
4826
Calin Juravle52c48962014-12-16 17:02:57 +00004827
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004828 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004829
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004830 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004831 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4832 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004833 }
4834
4835 size_t ComputeHashCode() const OVERRIDE {
4836 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4837 }
4838
Calin Juravle52c48962014-12-16 17:02:57 +00004839 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004840 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4841 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004842 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004843
4844 DECLARE_INSTRUCTION(StaticFieldGet);
4845
4846 private:
4847 const FieldInfo field_info_;
4848
4849 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4850};
4851
4852class HStaticFieldSet : public HTemplateInstruction<2> {
4853 public:
4854 HStaticFieldSet(HInstruction* cls,
4855 HInstruction* value,
4856 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004857 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004858 bool is_volatile,
4859 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004860 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004861 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004862 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004863 : HTemplateInstruction(
4864 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
4865 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004866 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004867 SetRawInputAt(0, cls);
4868 SetRawInputAt(1, value);
4869 }
4870
Calin Juravle52c48962014-12-16 17:02:57 +00004871 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004872 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4873 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004874 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004875
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004876 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004877 bool GetValueCanBeNull() const { return value_can_be_null_; }
4878 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004879
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004880 DECLARE_INSTRUCTION(StaticFieldSet);
4881
4882 private:
4883 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004884 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004885
4886 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4887};
4888
Calin Juravlee460d1d2015-09-29 04:52:17 +01004889class HUnresolvedInstanceFieldGet : public HExpression<1> {
4890 public:
4891 HUnresolvedInstanceFieldGet(HInstruction* obj,
4892 Primitive::Type field_type,
4893 uint32_t field_index,
4894 uint32_t dex_pc)
4895 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4896 field_index_(field_index) {
4897 SetRawInputAt(0, obj);
4898 }
4899
4900 bool NeedsEnvironment() const OVERRIDE { return true; }
4901 bool CanThrow() const OVERRIDE { return true; }
4902
4903 Primitive::Type GetFieldType() const { return GetType(); }
4904 uint32_t GetFieldIndex() const { return field_index_; }
4905
4906 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4907
4908 private:
4909 const uint32_t field_index_;
4910
4911 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4912};
4913
4914class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4915 public:
4916 HUnresolvedInstanceFieldSet(HInstruction* obj,
4917 HInstruction* value,
4918 Primitive::Type field_type,
4919 uint32_t field_index,
4920 uint32_t dex_pc)
4921 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4922 field_type_(field_type),
4923 field_index_(field_index) {
4924 DCHECK_EQ(field_type, value->GetType());
4925 SetRawInputAt(0, obj);
4926 SetRawInputAt(1, value);
4927 }
4928
4929 bool NeedsEnvironment() const OVERRIDE { return true; }
4930 bool CanThrow() const OVERRIDE { return true; }
4931
4932 Primitive::Type GetFieldType() const { return field_type_; }
4933 uint32_t GetFieldIndex() const { return field_index_; }
4934
4935 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
4936
4937 private:
4938 const Primitive::Type field_type_;
4939 const uint32_t field_index_;
4940
4941 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
4942};
4943
4944class HUnresolvedStaticFieldGet : public HExpression<0> {
4945 public:
4946 HUnresolvedStaticFieldGet(Primitive::Type field_type,
4947 uint32_t field_index,
4948 uint32_t dex_pc)
4949 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4950 field_index_(field_index) {
4951 }
4952
4953 bool NeedsEnvironment() const OVERRIDE { return true; }
4954 bool CanThrow() const OVERRIDE { return true; }
4955
4956 Primitive::Type GetFieldType() const { return GetType(); }
4957 uint32_t GetFieldIndex() const { return field_index_; }
4958
4959 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
4960
4961 private:
4962 const uint32_t field_index_;
4963
4964 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
4965};
4966
4967class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
4968 public:
4969 HUnresolvedStaticFieldSet(HInstruction* value,
4970 Primitive::Type field_type,
4971 uint32_t field_index,
4972 uint32_t dex_pc)
4973 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4974 field_type_(field_type),
4975 field_index_(field_index) {
4976 DCHECK_EQ(field_type, value->GetType());
4977 SetRawInputAt(0, value);
4978 }
4979
4980 bool NeedsEnvironment() const OVERRIDE { return true; }
4981 bool CanThrow() const OVERRIDE { return true; }
4982
4983 Primitive::Type GetFieldType() const { return field_type_; }
4984 uint32_t GetFieldIndex() const { return field_index_; }
4985
4986 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
4987
4988 private:
4989 const Primitive::Type field_type_;
4990 const uint32_t field_index_;
4991
4992 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
4993};
4994
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004995// Implement the move-exception DEX instruction.
4996class HLoadException : public HExpression<0> {
4997 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004998 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4999 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005000
David Brazdilbbd733e2015-08-18 17:48:17 +01005001 bool CanBeNull() const OVERRIDE { return false; }
5002
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005003 DECLARE_INSTRUCTION(LoadException);
5004
5005 private:
5006 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5007};
5008
David Brazdilcb1c0552015-08-04 16:22:25 +01005009// Implicit part of move-exception which clears thread-local exception storage.
5010// Must not be removed because the runtime expects the TLS to get cleared.
5011class HClearException : public HTemplateInstruction<0> {
5012 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005013 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5014 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005015
5016 DECLARE_INSTRUCTION(ClearException);
5017
5018 private:
5019 DISALLOW_COPY_AND_ASSIGN(HClearException);
5020};
5021
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005022class HThrow : public HTemplateInstruction<1> {
5023 public:
5024 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005025 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005026 SetRawInputAt(0, exception);
5027 }
5028
5029 bool IsControlFlow() const OVERRIDE { return true; }
5030
5031 bool NeedsEnvironment() const OVERRIDE { return true; }
5032
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005033 bool CanThrow() const OVERRIDE { return true; }
5034
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005035
5036 DECLARE_INSTRUCTION(Throw);
5037
5038 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005039 DISALLOW_COPY_AND_ASSIGN(HThrow);
5040};
5041
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005042/**
5043 * Implementation strategies for the code generator of a HInstanceOf
5044 * or `HCheckCast`.
5045 */
5046enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005047 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005048 kExactCheck, // Can do a single class compare.
5049 kClassHierarchyCheck, // Can just walk the super class chain.
5050 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5051 kInterfaceCheck, // No optimization yet when checking against an interface.
5052 kArrayObjectCheck, // Can just check if the array is not primitive.
5053 kArrayCheck // No optimization yet when checking against a generic array.
5054};
5055
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005056class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005057 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005058 HInstanceOf(HInstruction* object,
5059 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005060 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005061 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005062 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005063 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005064 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005065 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005066 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005067 SetRawInputAt(0, object);
5068 SetRawInputAt(1, constant);
5069 }
5070
5071 bool CanBeMoved() const OVERRIDE { return true; }
5072
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005073 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005074 return true;
5075 }
5076
5077 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005078 return false;
5079 }
5080
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005081 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5082
5083 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005084
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005085 // Used only in code generation.
5086 bool MustDoNullCheck() const { return must_do_null_check_; }
5087 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5088
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005089 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5090 return (check_kind == TypeCheckKind::kExactCheck)
5091 ? SideEffects::None()
5092 // Mips currently does runtime calls for any other checks.
5093 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005094 }
5095
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005096 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005097
5098 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005099 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005100 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005101
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005102 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5103};
5104
Calin Juravleb1498f62015-02-16 13:13:29 +00005105class HBoundType : public HExpression<1> {
5106 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005107 // Constructs an HBoundType with the given upper_bound.
5108 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005109 HBoundType(HInstruction* input,
5110 ReferenceTypeInfo upper_bound,
5111 bool upper_can_be_null,
5112 uint32_t dex_pc = kNoDexPc)
5113 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005114 upper_bound_(upper_bound),
5115 upper_can_be_null_(upper_can_be_null),
5116 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005117 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005118 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005119 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005120 }
5121
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005122 // GetUpper* should only be used in reference type propagation.
5123 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5124 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005125
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005126 void SetCanBeNull(bool can_be_null) {
5127 DCHECK(upper_can_be_null_ || !can_be_null);
5128 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005129 }
5130
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005131 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5132
Calin Juravleb1498f62015-02-16 13:13:29 +00005133 DECLARE_INSTRUCTION(BoundType);
5134
5135 private:
5136 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005137 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5138 // It is used to bound the type in cases like:
5139 // if (x instanceof ClassX) {
5140 // // uper_bound_ will be ClassX
5141 // }
5142 const ReferenceTypeInfo upper_bound_;
5143 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5144 // is false then can_be_null_ cannot be true).
5145 const bool upper_can_be_null_;
5146 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005147
5148 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5149};
5150
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005151class HCheckCast : public HTemplateInstruction<2> {
5152 public:
5153 HCheckCast(HInstruction* object,
5154 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005155 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005156 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005157 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005158 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005159 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005160 SetRawInputAt(0, object);
5161 SetRawInputAt(1, constant);
5162 }
5163
5164 bool CanBeMoved() const OVERRIDE { return true; }
5165
5166 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5167 return true;
5168 }
5169
5170 bool NeedsEnvironment() const OVERRIDE {
5171 // Instruction may throw a CheckCastError.
5172 return true;
5173 }
5174
5175 bool CanThrow() const OVERRIDE { return true; }
5176
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005177 bool MustDoNullCheck() const { return must_do_null_check_; }
5178 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005179 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005180
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005181 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005182
5183 DECLARE_INSTRUCTION(CheckCast);
5184
5185 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005186 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005187 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005188
5189 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005190};
5191
Calin Juravle27df7582015-04-17 19:12:31 +01005192class HMemoryBarrier : public HTemplateInstruction<0> {
5193 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005194 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005195 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005196 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005197 barrier_kind_(barrier_kind) {}
5198
5199 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5200
5201 DECLARE_INSTRUCTION(MemoryBarrier);
5202
5203 private:
5204 const MemBarrierKind barrier_kind_;
5205
5206 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5207};
5208
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005209class HMonitorOperation : public HTemplateInstruction<1> {
5210 public:
5211 enum OperationKind {
5212 kEnter,
5213 kExit,
5214 };
5215
5216 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005217 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005218 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5219 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005220 SetRawInputAt(0, object);
5221 }
5222
5223 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005224 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5225
5226 bool CanThrow() const OVERRIDE {
5227 // Verifier guarantees that monitor-exit cannot throw.
5228 // This is important because it allows the HGraphBuilder to remove
5229 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5230 return IsEnter();
5231 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005232
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005233
5234 bool IsEnter() const { return kind_ == kEnter; }
5235
5236 DECLARE_INSTRUCTION(MonitorOperation);
5237
Calin Juravle52c48962014-12-16 17:02:57 +00005238 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005239 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005240
5241 private:
5242 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5243};
5244
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005245/**
5246 * A HInstruction used as a marker for the replacement of new + <init>
5247 * of a String to a call to a StringFactory. Only baseline will see
5248 * the node at code generation, where it will be be treated as null.
5249 * When compiling non-baseline, `HFakeString` instructions are being removed
5250 * in the instruction simplifier.
5251 */
5252class HFakeString : public HTemplateInstruction<0> {
5253 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005254 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5255 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005256
5257 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5258
5259 DECLARE_INSTRUCTION(FakeString);
5260
5261 private:
5262 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5263};
5264
Vladimir Markof9f64412015-09-02 14:05:49 +01005265class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005266 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005267 MoveOperands(Location source,
5268 Location destination,
5269 Primitive::Type type,
5270 HInstruction* instruction)
5271 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005272
5273 Location GetSource() const { return source_; }
5274 Location GetDestination() const { return destination_; }
5275
5276 void SetSource(Location value) { source_ = value; }
5277 void SetDestination(Location value) { destination_ = value; }
5278
5279 // The parallel move resolver marks moves as "in-progress" by clearing the
5280 // destination (but not the source).
5281 Location MarkPending() {
5282 DCHECK(!IsPending());
5283 Location dest = destination_;
5284 destination_ = Location::NoLocation();
5285 return dest;
5286 }
5287
5288 void ClearPending(Location dest) {
5289 DCHECK(IsPending());
5290 destination_ = dest;
5291 }
5292
5293 bool IsPending() const {
5294 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5295 return destination_.IsInvalid() && !source_.IsInvalid();
5296 }
5297
5298 // True if this blocks a move from the given location.
5299 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005300 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005301 }
5302
5303 // A move is redundant if it's been eliminated, if its source and
5304 // destination are the same, or if its destination is unneeded.
5305 bool IsRedundant() const {
5306 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5307 }
5308
5309 // We clear both operands to indicate move that's been eliminated.
5310 void Eliminate() {
5311 source_ = destination_ = Location::NoLocation();
5312 }
5313
5314 bool IsEliminated() const {
5315 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5316 return source_.IsInvalid();
5317 }
5318
Alexey Frunze4dda3372015-06-01 18:31:49 -07005319 Primitive::Type GetType() const { return type_; }
5320
Nicolas Geoffray90218252015-04-15 11:56:51 +01005321 bool Is64BitMove() const {
5322 return Primitive::Is64BitType(type_);
5323 }
5324
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005325 HInstruction* GetInstruction() const { return instruction_; }
5326
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005327 private:
5328 Location source_;
5329 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005330 // The type this move is for.
5331 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005332 // The instruction this move is assocatied with. Null when this move is
5333 // for moving an input in the expected locations of user (including a phi user).
5334 // This is only used in debug mode, to ensure we do not connect interval siblings
5335 // in the same parallel move.
5336 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005337};
5338
5339static constexpr size_t kDefaultNumberOfMoves = 4;
5340
5341class HParallelMove : public HTemplateInstruction<0> {
5342 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005343 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005344 : HTemplateInstruction(SideEffects::None(), dex_pc),
5345 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5346 moves_.reserve(kDefaultNumberOfMoves);
5347 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005348
Nicolas Geoffray90218252015-04-15 11:56:51 +01005349 void AddMove(Location source,
5350 Location destination,
5351 Primitive::Type type,
5352 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005353 DCHECK(source.IsValid());
5354 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005355 if (kIsDebugBuild) {
5356 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005357 for (const MoveOperands& move : moves_) {
5358 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005359 // Special case the situation where the move is for the spill slot
5360 // of the instruction.
5361 if ((GetPrevious() == instruction)
5362 || ((GetPrevious() == nullptr)
5363 && instruction->IsPhi()
5364 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005365 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005366 << "Doing parallel moves for the same instruction.";
5367 } else {
5368 DCHECK(false) << "Doing parallel moves for the same instruction.";
5369 }
5370 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005371 }
5372 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005373 for (const MoveOperands& move : moves_) {
5374 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005375 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005376 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005377 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005378 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005379 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005380 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005381 }
5382
Vladimir Marko225b6462015-09-28 12:17:40 +01005383 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005384 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005385 }
5386
Vladimir Marko225b6462015-09-28 12:17:40 +01005387 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005388
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005389 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005390
5391 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005392 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005393
5394 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5395};
5396
Mark Mendell0616ae02015-04-17 12:49:27 -04005397} // namespace art
5398
5399#ifdef ART_ENABLE_CODEGEN_x86
5400#include "nodes_x86.h"
5401#endif
5402
5403namespace art {
5404
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005405class HGraphVisitor : public ValueObject {
5406 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005407 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5408 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005409
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005410 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005411 virtual void VisitBasicBlock(HBasicBlock* block);
5412
Roland Levillain633021e2014-10-01 14:12:25 +01005413 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005414 void VisitInsertionOrder();
5415
Roland Levillain633021e2014-10-01 14:12:25 +01005416 // Visit the graph following dominator tree reverse post-order.
5417 void VisitReversePostOrder();
5418
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005419 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005420
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005421 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005422#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005423 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5424
5425 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5426
5427#undef DECLARE_VISIT_INSTRUCTION
5428
5429 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005430 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005431
5432 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5433};
5434
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005435class HGraphDelegateVisitor : public HGraphVisitor {
5436 public:
5437 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5438 virtual ~HGraphDelegateVisitor() {}
5439
5440 // Visit functions that delegate to to super class.
5441#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005442 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005443
5444 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5445
5446#undef DECLARE_VISIT_INSTRUCTION
5447
5448 private:
5449 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5450};
5451
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005452class HInsertionOrderIterator : public ValueObject {
5453 public:
5454 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5455
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005456 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005457 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005458 void Advance() { ++index_; }
5459
5460 private:
5461 const HGraph& graph_;
5462 size_t index_;
5463
5464 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5465};
5466
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005467class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005468 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005469 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5470 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005471 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005472 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005473
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005474 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5475 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005476 void Advance() { ++index_; }
5477
5478 private:
5479 const HGraph& graph_;
5480 size_t index_;
5481
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005482 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005483};
5484
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005485class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005486 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005487 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005488 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005489 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005490 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005491 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005492
5493 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005494 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005495 void Advance() { --index_; }
5496
5497 private:
5498 const HGraph& graph_;
5499 size_t index_;
5500
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005501 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005502};
5503
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005504class HLinearPostOrderIterator : public ValueObject {
5505 public:
5506 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005507 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005508
5509 bool Done() const { return index_ == 0; }
5510
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005511 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005512
5513 void Advance() {
5514 --index_;
5515 DCHECK_GE(index_, 0U);
5516 }
5517
5518 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005519 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005520 size_t index_;
5521
5522 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5523};
5524
5525class HLinearOrderIterator : public ValueObject {
5526 public:
5527 explicit HLinearOrderIterator(const HGraph& graph)
5528 : order_(graph.GetLinearOrder()), index_(0) {}
5529
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005530 bool Done() const { return index_ == order_.size(); }
5531 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005532 void Advance() { ++index_; }
5533
5534 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005535 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005536 size_t index_;
5537
5538 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5539};
5540
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005541// Iterator over the blocks that art part of the loop. Includes blocks part
5542// of an inner loop. The order in which the blocks are iterated is on their
5543// block id.
5544class HBlocksInLoopIterator : public ValueObject {
5545 public:
5546 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5547 : blocks_in_loop_(info.GetBlocks()),
5548 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5549 index_(0) {
5550 if (!blocks_in_loop_.IsBitSet(index_)) {
5551 Advance();
5552 }
5553 }
5554
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005555 bool Done() const { return index_ == blocks_.size(); }
5556 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005557 void Advance() {
5558 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005559 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005560 if (blocks_in_loop_.IsBitSet(index_)) {
5561 break;
5562 }
5563 }
5564 }
5565
5566 private:
5567 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005568 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005569 size_t index_;
5570
5571 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5572};
5573
Mingyao Yang3584bce2015-05-19 16:01:59 -07005574// Iterator over the blocks that art part of the loop. Includes blocks part
5575// of an inner loop. The order in which the blocks are iterated is reverse
5576// post order.
5577class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5578 public:
5579 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5580 : blocks_in_loop_(info.GetBlocks()),
5581 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5582 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005583 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005584 Advance();
5585 }
5586 }
5587
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005588 bool Done() const { return index_ == blocks_.size(); }
5589 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005590 void Advance() {
5591 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005592 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5593 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005594 break;
5595 }
5596 }
5597 }
5598
5599 private:
5600 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005601 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005602 size_t index_;
5603
5604 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5605};
5606
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005607inline int64_t Int64FromConstant(HConstant* constant) {
5608 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5609 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5610 : constant->AsLongConstant()->GetValue();
5611}
5612
Vladimir Marko58155012015-08-19 12:49:41 +00005613inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5614 // For the purposes of the compiler, the dex files must actually be the same object
5615 // if we want to safely treat them as the same. This is especially important for JIT
5616 // as custom class loaders can open the same underlying file (or memory) multiple
5617 // times and provide different class resolution but no two class loaders should ever
5618 // use the same DexFile object - doing so is an unsupported hack that can lead to
5619 // all sorts of weird failures.
5620 return &lhs == &rhs;
5621}
5622
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005623} // namespace art
5624
5625#endif // ART_COMPILER_OPTIMIZING_NODES_H_