blob: 7cf6339b6e54bbe5ee6f38a893f95040e5e312ff [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
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001084#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1085
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001086#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1087
Mark Mendell0616ae02015-04-17 12:49:27 -04001088#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1089 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001090 M(X86LoadFromConstantTable, Instruction) \
1091 M(X86PackedSwitch, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001092
1093#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1094
1095#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1096 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1097 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1098 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001099 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001100 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001101 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1102 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1103
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001104#define FOR_EACH_INSTRUCTION(M) \
1105 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1106 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001107 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001108 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001109 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001110
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001111#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001112FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1113#undef FORWARD_DECLARATION
1114
Roland Levillainccc07a92014-09-16 14:48:16 +01001115#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001116 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1117 const char* DebugName() const OVERRIDE { return #type; } \
1118 const H##type* As##type() const OVERRIDE { return this; } \
1119 H##type* As##type() OVERRIDE { return this; } \
1120 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001121 return other->Is##type(); \
1122 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001123 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001124
David Brazdiled596192015-01-23 10:39:45 +00001125template <typename T> class HUseList;
1126
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001127template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001128class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001129 public:
David Brazdiled596192015-01-23 10:39:45 +00001130 HUseListNode* GetPrevious() const { return prev_; }
1131 HUseListNode* GetNext() const { return next_; }
1132 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001133 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001134 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001135
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001136 private:
David Brazdiled596192015-01-23 10:39:45 +00001137 HUseListNode(T user, size_t index)
1138 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1139
1140 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001141 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001142 HUseListNode<T>* prev_;
1143 HUseListNode<T>* next_;
1144
1145 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001146
1147 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1148};
1149
David Brazdiled596192015-01-23 10:39:45 +00001150template <typename T>
1151class HUseList : public ValueObject {
1152 public:
1153 HUseList() : first_(nullptr) {}
1154
1155 void Clear() {
1156 first_ = nullptr;
1157 }
1158
1159 // Adds a new entry at the beginning of the use list and returns
1160 // the newly created node.
1161 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001162 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001163 if (IsEmpty()) {
1164 first_ = new_node;
1165 } else {
1166 first_->prev_ = new_node;
1167 new_node->next_ = first_;
1168 first_ = new_node;
1169 }
1170 return new_node;
1171 }
1172
1173 HUseListNode<T>* GetFirst() const {
1174 return first_;
1175 }
1176
1177 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001178 DCHECK(node != nullptr);
1179 DCHECK(Contains(node));
1180
David Brazdiled596192015-01-23 10:39:45 +00001181 if (node->prev_ != nullptr) {
1182 node->prev_->next_ = node->next_;
1183 }
1184 if (node->next_ != nullptr) {
1185 node->next_->prev_ = node->prev_;
1186 }
1187 if (node == first_) {
1188 first_ = node->next_;
1189 }
1190 }
1191
David Brazdil1abb4192015-02-17 18:33:36 +00001192 bool Contains(const HUseListNode<T>* node) const {
1193 if (node == nullptr) {
1194 return false;
1195 }
1196 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1197 if (current == node) {
1198 return true;
1199 }
1200 }
1201 return false;
1202 }
1203
David Brazdiled596192015-01-23 10:39:45 +00001204 bool IsEmpty() const {
1205 return first_ == nullptr;
1206 }
1207
1208 bool HasOnlyOneUse() const {
1209 return first_ != nullptr && first_->next_ == nullptr;
1210 }
1211
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001212 size_t SizeSlow() const {
1213 size_t count = 0;
1214 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1215 ++count;
1216 }
1217 return count;
1218 }
1219
David Brazdiled596192015-01-23 10:39:45 +00001220 private:
1221 HUseListNode<T>* first_;
1222};
1223
1224template<typename T>
1225class HUseIterator : public ValueObject {
1226 public:
1227 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1228
1229 bool Done() const { return current_ == nullptr; }
1230
1231 void Advance() {
1232 DCHECK(!Done());
1233 current_ = current_->GetNext();
1234 }
1235
1236 HUseListNode<T>* Current() const {
1237 DCHECK(!Done());
1238 return current_;
1239 }
1240
1241 private:
1242 HUseListNode<T>* current_;
1243
1244 friend class HValue;
1245};
1246
David Brazdil1abb4192015-02-17 18:33:36 +00001247// This class is used by HEnvironment and HInstruction classes to record the
1248// instructions they use and pointers to the corresponding HUseListNodes kept
1249// by the used instructions.
1250template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001251class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001252 public:
1253 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1254 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1255
1256 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1257 : instruction_(old_record.instruction_), use_node_(use_node) {
1258 DCHECK(instruction_ != nullptr);
1259 DCHECK(use_node_ != nullptr);
1260 DCHECK(old_record.use_node_ == nullptr);
1261 }
1262
1263 HInstruction* GetInstruction() const { return instruction_; }
1264 HUseListNode<T>* GetUseNode() const { return use_node_; }
1265
1266 private:
1267 // Instruction used by the user.
1268 HInstruction* instruction_;
1269
1270 // Corresponding entry in the use list kept by 'instruction_'.
1271 HUseListNode<T>* use_node_;
1272};
1273
Aart Bik854a02b2015-07-14 16:07:00 -07001274/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001275 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001276 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001277 * For write/read dependences on fields/arrays, the dependence analysis uses
1278 * type disambiguation (e.g. a float field write cannot modify the value of an
1279 * integer field read) and the access type (e.g. a reference array write cannot
1280 * modify the value of a reference field read [although it may modify the
1281 * reference fetch prior to reading the field, which is represented by its own
1282 * write/read dependence]). The analysis makes conservative points-to
1283 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1284 * the same, and any reference read depends on any reference read without
1285 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001286 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001287 * The internal representation uses 38-bit and is described in the table below.
1288 * The first line indicates the side effect, and for field/array accesses the
1289 * second line indicates the type of the access (in the order of the
1290 * Primitive::Type enum).
1291 * The two numbered lines below indicate the bit position in the bitfield (read
1292 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001293 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001294 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1295 * +-------------+---------+---------+--------------+---------+---------+
1296 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1297 * | 3 |333333322|222222221| 1 |111111110|000000000|
1298 * | 7 |654321098|765432109| 8 |765432109|876543210|
1299 *
1300 * Note that, to ease the implementation, 'changes' bits are least significant
1301 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001302 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001303class SideEffects : public ValueObject {
1304 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001305 SideEffects() : flags_(0) {}
1306
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001307 static SideEffects None() {
1308 return SideEffects(0);
1309 }
1310
1311 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001312 return SideEffects(kAllChangeBits | kAllDependOnBits);
1313 }
1314
1315 static SideEffects AllChanges() {
1316 return SideEffects(kAllChangeBits);
1317 }
1318
1319 static SideEffects AllDependencies() {
1320 return SideEffects(kAllDependOnBits);
1321 }
1322
1323 static SideEffects AllExceptGCDependency() {
1324 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1325 }
1326
1327 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001328 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001329 }
1330
Aart Bik34c3ba92015-07-20 14:08:59 -07001331 static SideEffects AllWrites() {
1332 return SideEffects(kAllWrites);
1333 }
1334
1335 static SideEffects AllReads() {
1336 return SideEffects(kAllReads);
1337 }
1338
1339 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1340 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001341 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001342 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001343 }
1344
Aart Bik854a02b2015-07-14 16:07:00 -07001345 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1346 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001347 }
1348
Aart Bik34c3ba92015-07-20 14:08:59 -07001349 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1350 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001351 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001352 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001353 }
1354
1355 static SideEffects ArrayReadOfType(Primitive::Type type) {
1356 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1357 }
1358
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001359 static SideEffects CanTriggerGC() {
1360 return SideEffects(1ULL << kCanTriggerGCBit);
1361 }
1362
1363 static SideEffects DependsOnGC() {
1364 return SideEffects(1ULL << kDependsOnGCBit);
1365 }
1366
Aart Bik854a02b2015-07-14 16:07:00 -07001367 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001368 SideEffects Union(SideEffects other) const {
1369 return SideEffects(flags_ | other.flags_);
1370 }
1371
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001372 SideEffects Exclusion(SideEffects other) const {
1373 return SideEffects(flags_ & ~other.flags_);
1374 }
1375
1376 bool Includes(SideEffects other) const {
1377 return (other.flags_ & flags_) == other.flags_;
1378 }
1379
1380 bool HasSideEffects() const {
1381 return (flags_ & kAllChangeBits);
1382 }
1383
1384 bool HasDependencies() const {
1385 return (flags_ & kAllDependOnBits);
1386 }
1387
1388 // Returns true if there are no side effects or dependencies.
1389 bool DoesNothing() const {
1390 return flags_ == 0;
1391 }
1392
Aart Bik854a02b2015-07-14 16:07:00 -07001393 // Returns true if something is written.
1394 bool DoesAnyWrite() const {
1395 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001396 }
1397
Aart Bik854a02b2015-07-14 16:07:00 -07001398 // Returns true if something is read.
1399 bool DoesAnyRead() const {
1400 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001401 }
1402
Aart Bik854a02b2015-07-14 16:07:00 -07001403 // Returns true if potentially everything is written and read
1404 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 bool DoesAllReadWrite() const {
1406 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1407 }
1408
Aart Bik854a02b2015-07-14 16:07:00 -07001409 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001410 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001411 }
1412
1413 // Returns true if this may read something written by other.
1414 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001415 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1416 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001417 }
1418
1419 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001420 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001421 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001422 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001423 for (int s = kLastBit; s >= 0; s--) {
1424 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1425 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1426 // This is a bit for the GC side effect.
1427 if (current_bit_is_set) {
1428 flags += "GC";
1429 }
Aart Bik854a02b2015-07-14 16:07:00 -07001430 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001431 } else {
1432 // This is a bit for the array/field analysis.
1433 // The underscore character stands for the 'can trigger GC' bit.
1434 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1435 if (current_bit_is_set) {
1436 flags += kDebug[s];
1437 }
1438 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1439 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1440 flags += "|";
1441 }
1442 }
Aart Bik854a02b2015-07-14 16:07:00 -07001443 }
1444 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001445 }
1446
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001448
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001449 private:
1450 static constexpr int kFieldArrayAnalysisBits = 9;
1451
1452 static constexpr int kFieldWriteOffset = 0;
1453 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1454 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1455 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1456
1457 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1458
1459 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1460 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1461 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1462 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1463
1464 static constexpr int kLastBit = kDependsOnGCBit;
1465 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1466
1467 // Aliases.
1468
1469 static_assert(kChangeBits == kDependOnBits,
1470 "the 'change' bits should match the 'depend on' bits.");
1471
1472 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1473 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1474 static constexpr uint64_t kAllWrites =
1475 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1476 static constexpr uint64_t kAllReads =
1477 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001478
Aart Bik854a02b2015-07-14 16:07:00 -07001479 // Work around the fact that HIR aliases I/F and J/D.
1480 // TODO: remove this interceptor once HIR types are clean
1481 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1482 switch (type) {
1483 case Primitive::kPrimInt:
1484 case Primitive::kPrimFloat:
1485 return TypeFlag(Primitive::kPrimInt, offset) |
1486 TypeFlag(Primitive::kPrimFloat, offset);
1487 case Primitive::kPrimLong:
1488 case Primitive::kPrimDouble:
1489 return TypeFlag(Primitive::kPrimLong, offset) |
1490 TypeFlag(Primitive::kPrimDouble, offset);
1491 default:
1492 return TypeFlag(type, offset);
1493 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001494 }
1495
Aart Bik854a02b2015-07-14 16:07:00 -07001496 // Translates type to bit flag.
1497 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1498 CHECK_NE(type, Primitive::kPrimVoid);
1499 const uint64_t one = 1;
1500 const int shift = type; // 0-based consecutive enum
1501 DCHECK_LE(kFieldWriteOffset, shift);
1502 DCHECK_LT(shift, kArrayWriteOffset);
1503 return one << (type + offset);
1504 }
1505
1506 // Private constructor on direct flags value.
1507 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1508
1509 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001510};
1511
David Brazdiled596192015-01-23 10:39:45 +00001512// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001513class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001514 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001515 HEnvironment(ArenaAllocator* arena,
1516 size_t number_of_vregs,
1517 const DexFile& dex_file,
1518 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001519 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001520 InvokeType invoke_type,
1521 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001522 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1523 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001524 parent_(nullptr),
1525 dex_file_(dex_file),
1526 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001527 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001528 invoke_type_(invoke_type),
1529 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001530 }
1531
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001532 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001533 : HEnvironment(arena,
1534 to_copy.Size(),
1535 to_copy.GetDexFile(),
1536 to_copy.GetMethodIdx(),
1537 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001538 to_copy.GetInvokeType(),
1539 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001540
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001541 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001542 if (parent_ != nullptr) {
1543 parent_->SetAndCopyParentChain(allocator, parent);
1544 } else {
1545 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1546 parent_->CopyFrom(parent);
1547 if (parent->GetParent() != nullptr) {
1548 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1549 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001550 }
David Brazdiled596192015-01-23 10:39:45 +00001551 }
1552
Vladimir Marko71bf8092015-09-15 15:33:14 +01001553 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001554 void CopyFrom(HEnvironment* environment);
1555
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001556 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1557 // input to the loop phi instead. This is for inserting instructions that
1558 // require an environment (like HDeoptimization) in the loop pre-header.
1559 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001560
1561 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001562 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001563 }
1564
1565 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001566 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001567 }
1568
David Brazdil1abb4192015-02-17 18:33:36 +00001569 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001570
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001571 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001572
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001573 HEnvironment* GetParent() const { return parent_; }
1574
1575 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001576 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001577 }
1578
1579 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001580 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001581 }
1582
1583 uint32_t GetDexPc() const {
1584 return dex_pc_;
1585 }
1586
1587 uint32_t GetMethodIdx() const {
1588 return method_idx_;
1589 }
1590
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001591 InvokeType GetInvokeType() const {
1592 return invoke_type_;
1593 }
1594
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001595 const DexFile& GetDexFile() const {
1596 return dex_file_;
1597 }
1598
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001599 HInstruction* GetHolder() const {
1600 return holder_;
1601 }
1602
David Brazdiled596192015-01-23 10:39:45 +00001603 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001604 // Record instructions' use entries of this environment for constant-time removal.
1605 // It should only be called by HInstruction when a new environment use is added.
1606 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1607 DCHECK(env_use->GetUser() == this);
1608 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001609 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001610 }
David Brazdiled596192015-01-23 10:39:45 +00001611
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001612 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1613 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001614 HEnvironment* parent_;
1615 const DexFile& dex_file_;
1616 const uint32_t method_idx_;
1617 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001618 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001619
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001620 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001621 HInstruction* const holder_;
1622
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001623 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001624
1625 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1626};
1627
Calin Juravleacf735c2015-02-12 15:25:22 +00001628class ReferenceTypeInfo : ValueObject {
1629 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001630 typedef Handle<mirror::Class> TypeHandle;
1631
Calin Juravle2e768302015-07-28 14:41:11 +00001632 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1633 // The constructor will check that the type_handle is valid.
1634 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001635 }
1636
Calin Juravle2e768302015-07-28 14:41:11 +00001637 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1638
1639 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1640 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001641 }
1642
Calin Juravle2e768302015-07-28 14:41:11 +00001643 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1644 return IsValidHandle(type_handle_);
1645 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001646
Calin Juravleacf735c2015-02-12 15:25:22 +00001647 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001648
1649 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1650 DCHECK(IsValid());
1651 return GetTypeHandle()->IsObjectClass();
1652 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001653
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001654 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001655 DCHECK(IsValid());
1656 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001657 }
1658
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001659 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1660 DCHECK(IsValid());
1661 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1662 }
1663
Mathieu Chartier90443472015-07-16 20:32:27 -07001664 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001665 DCHECK(IsValid());
1666 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001667 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001668
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001669 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001670 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001671 return GetTypeHandle()->IsArrayClass();
1672 }
1673
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001674 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1675 DCHECK(IsValid());
1676 return GetTypeHandle()->IsPrimitiveArray();
1677 }
1678
1679 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1680 DCHECK(IsValid());
1681 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1682 }
1683
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001684 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001685 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001686 if (!IsExact()) return false;
1687 if (!IsArrayClass()) return false;
1688 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1689 }
1690
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001691 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1692 DCHECK(IsValid());
1693 if (!IsExact()) return false;
1694 if (!IsArrayClass()) return false;
1695 if (!rti.IsArrayClass()) return false;
1696 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1697 rti.GetTypeHandle()->GetComponentType());
1698 }
1699
Calin Juravleacf735c2015-02-12 15:25:22 +00001700 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1701
Mathieu Chartier90443472015-07-16 20:32:27 -07001702 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001703 DCHECK(IsValid());
1704 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001705 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1706 }
1707
1708 // Returns true if the type information provide the same amount of details.
1709 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001710 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001711 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001712 if (!IsValid() && !rti.IsValid()) {
1713 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001714 return true;
1715 }
Calin Juravle2e768302015-07-28 14:41:11 +00001716 if (!IsValid() || !rti.IsValid()) {
1717 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001718 return false;
1719 }
Calin Juravle2e768302015-07-28 14:41:11 +00001720 return IsExact() == rti.IsExact()
1721 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001722 }
1723
1724 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001725 ReferenceTypeInfo();
1726 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001727
Calin Juravleacf735c2015-02-12 15:25:22 +00001728 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001729 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001730 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001731 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001732 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001733};
1734
1735std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1736
Vladimir Markof9f64412015-09-02 14:05:49 +01001737class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001738 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001739 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001740 : previous_(nullptr),
1741 next_(nullptr),
1742 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001743 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001744 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001745 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001746 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001747 locations_(nullptr),
1748 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001749 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001750 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001751 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001752
Dave Allison20dfc792014-06-16 20:44:29 -07001753 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001754
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001755#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001756 enum InstructionKind {
1757 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1758 };
1759#undef DECLARE_KIND
1760
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001761 HInstruction* GetNext() const { return next_; }
1762 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001763
Calin Juravle77520bc2015-01-12 18:45:46 +00001764 HInstruction* GetNextDisregardingMoves() const;
1765 HInstruction* GetPreviousDisregardingMoves() const;
1766
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001767 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001768 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001769 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001770 bool IsInBlock() const { return block_ != nullptr; }
1771 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001772 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001773
Roland Levillain6b879dd2014-09-22 17:13:44 +01001774 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001775 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001776
1777 virtual void Accept(HGraphVisitor* visitor) = 0;
1778 virtual const char* DebugName() const = 0;
1779
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001780 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001781 void SetRawInputAt(size_t index, HInstruction* input) {
1782 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1783 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001784
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001785 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001786
1787 uint32_t GetDexPc() const { return dex_pc_; }
1788
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001789 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001790
Roland Levillaine161a2a2014-10-03 12:45:18 +01001791 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001792 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001793
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001794 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001795 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001796
Calin Juravle10e244f2015-01-26 18:54:32 +00001797 // Does not apply for all instructions, but having this at top level greatly
1798 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001799 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001800 virtual bool CanBeNull() const {
1801 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1802 return true;
1803 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001804
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001805 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001806 return false;
1807 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001808
Calin Juravle2e768302015-07-28 14:41:11 +00001809 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001810
Calin Juravle61d544b2015-02-23 16:46:57 +00001811 ReferenceTypeInfo GetReferenceTypeInfo() const {
1812 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1813 return reference_type_info_;
1814 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001815
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001816 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001817 DCHECK(user != nullptr);
1818 HUseListNode<HInstruction*>* use =
1819 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1820 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001821 }
1822
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001823 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001824 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001825 HUseListNode<HEnvironment*>* env_use =
1826 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1827 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001828 }
1829
David Brazdil1abb4192015-02-17 18:33:36 +00001830 void RemoveAsUserOfInput(size_t input) {
1831 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1832 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1833 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001834
David Brazdil1abb4192015-02-17 18:33:36 +00001835 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1836 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001837
David Brazdiled596192015-01-23 10:39:45 +00001838 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1839 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001840 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001841 bool HasOnlyOneNonEnvironmentUse() const {
1842 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1843 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001844
Roland Levillain6c82d402014-10-13 16:10:27 +01001845 // Does this instruction strictly dominate `other_instruction`?
1846 // Returns false if this instruction and `other_instruction` are the same.
1847 // Aborts if this instruction and `other_instruction` are both phis.
1848 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001849
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001850 int GetId() const { return id_; }
1851 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001852
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001853 int GetSsaIndex() const { return ssa_index_; }
1854 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1855 bool HasSsaIndex() const { return ssa_index_ != -1; }
1856
1857 bool HasEnvironment() const { return environment_ != nullptr; }
1858 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001859 // Set the `environment_` field. Raw because this method does not
1860 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001861 void SetRawEnvironment(HEnvironment* environment) {
1862 DCHECK(environment_ == nullptr);
1863 DCHECK_EQ(environment->GetHolder(), this);
1864 environment_ = environment;
1865 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001866
1867 // Set the environment of this instruction, copying it from `environment`. While
1868 // copying, the uses lists are being updated.
1869 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001870 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001871 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001872 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001873 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001874 if (environment->GetParent() != nullptr) {
1875 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1876 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001877 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001879 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1880 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001881 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001882 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001883 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001884 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001885 if (environment->GetParent() != nullptr) {
1886 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1887 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001888 }
1889
Nicolas Geoffray39468442014-09-02 15:17:15 +01001890 // Returns the number of entries in the environment. Typically, that is the
1891 // number of dex registers in a method. It could be more in case of inlining.
1892 size_t EnvironmentSize() const;
1893
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001894 LocationSummary* GetLocations() const { return locations_; }
1895 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001896
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001897 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001898 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001899
Alexandre Rames188d4312015-04-09 18:30:21 +01001900 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1901 // uses of this instruction by `other` are *not* updated.
1902 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1903 ReplaceWith(other);
1904 other->ReplaceInput(this, use_index);
1905 }
1906
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001907 // Move `this` instruction before `cursor`.
1908 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001909
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001910#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001911 bool Is##type() const { return (As##type() != nullptr); } \
1912 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001913 virtual H##type* As##type() { return nullptr; }
1914
1915 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1916#undef INSTRUCTION_TYPE_CHECK
1917
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001918 // Returns whether the instruction can be moved within the graph.
1919 virtual bool CanBeMoved() const { return false; }
1920
1921 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001922 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001923 return false;
1924 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001925
1926 // Returns whether any data encoded in the two instructions is equal.
1927 // This method does not look at the inputs. Both instructions must be
1928 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001929 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001930 return false;
1931 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001932
1933 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001934 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001935 // 2) Their inputs are identical.
1936 bool Equals(HInstruction* other) const;
1937
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001938 virtual InstructionKind GetKind() const = 0;
1939
1940 virtual size_t ComputeHashCode() const {
1941 size_t result = GetKind();
1942 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1943 result = (result * 31) + InputAt(i)->GetId();
1944 }
1945 return result;
1946 }
1947
1948 SideEffects GetSideEffects() const { return side_effects_; }
1949
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001950 size_t GetLifetimePosition() const { return lifetime_position_; }
1951 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1952 LiveInterval* GetLiveInterval() const { return live_interval_; }
1953 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1954 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1955
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001956 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1957
1958 // Returns whether the code generation of the instruction will require to have access
1959 // to the current method. Such instructions are:
1960 // (1): Instructions that require an environment, as calling the runtime requires
1961 // to walk the stack and have the current method stored at a specific stack address.
1962 // (2): Object literals like classes and strings, that are loaded from the dex cache
1963 // fields of the current method.
1964 bool NeedsCurrentMethod() const {
1965 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1966 }
1967
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001968 virtual bool NeedsDexCache() const { return false; }
1969
Mark Mendellc4701932015-04-10 13:18:51 -04001970 // Does this instruction have any use in an environment before
1971 // control flow hits 'other'?
1972 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1973
1974 // Remove all references to environment uses of this instruction.
1975 // The caller must ensure that this is safe to do.
1976 void RemoveEnvironmentUsers();
1977
David Brazdil1abb4192015-02-17 18:33:36 +00001978 protected:
1979 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1980 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1981
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001982 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001983 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1984
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001985 HInstruction* previous_;
1986 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001987 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001988 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001989
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001990 // An instruction gets an id when it is added to the graph.
1991 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001992 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001993 int id_;
1994
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001995 // When doing liveness analysis, instructions that have uses get an SSA index.
1996 int ssa_index_;
1997
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001998 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001999 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002000
2001 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002002 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002003
Nicolas Geoffray39468442014-09-02 15:17:15 +01002004 // The environment associated with this instruction. Not null if the instruction
2005 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002006 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002007
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008 // Set by the code generator.
2009 LocationSummary* locations_;
2010
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002011 // Set by the liveness analysis.
2012 LiveInterval* live_interval_;
2013
2014 // Set by the liveness analysis, this is the position in a linear
2015 // order of blocks where this instruction's live interval start.
2016 size_t lifetime_position_;
2017
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002018 const SideEffects side_effects_;
2019
Calin Juravleacf735c2015-02-12 15:25:22 +00002020 // TODO: for primitive types this should be marked as invalid.
2021 ReferenceTypeInfo reference_type_info_;
2022
David Brazdil1abb4192015-02-17 18:33:36 +00002023 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002024 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002025 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002026 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002027 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002028
2029 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2030};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002031std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002032
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002033class HInputIterator : public ValueObject {
2034 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002035 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002036
2037 bool Done() const { return index_ == instruction_->InputCount(); }
2038 HInstruction* Current() const { return instruction_->InputAt(index_); }
2039 void Advance() { index_++; }
2040
2041 private:
2042 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002043 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002044
2045 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2046};
2047
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002048class HInstructionIterator : public ValueObject {
2049 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002050 explicit HInstructionIterator(const HInstructionList& instructions)
2051 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002052 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002053 }
2054
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002055 bool Done() const { return instruction_ == nullptr; }
2056 HInstruction* Current() const { return instruction_; }
2057 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002058 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002059 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002060 }
2061
2062 private:
2063 HInstruction* instruction_;
2064 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002065
2066 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002067};
2068
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002069class HBackwardInstructionIterator : public ValueObject {
2070 public:
2071 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2072 : instruction_(instructions.last_instruction_) {
2073 next_ = Done() ? nullptr : instruction_->GetPrevious();
2074 }
2075
2076 bool Done() const { return instruction_ == nullptr; }
2077 HInstruction* Current() const { return instruction_; }
2078 void Advance() {
2079 instruction_ = next_;
2080 next_ = Done() ? nullptr : instruction_->GetPrevious();
2081 }
2082
2083 private:
2084 HInstruction* instruction_;
2085 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002086
2087 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002088};
2089
Vladimir Markof9f64412015-09-02 14:05:49 +01002090template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002091class HTemplateInstruction: public HInstruction {
2092 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002093 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002094 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002095 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002096
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002097 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002098
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002099 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002100 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2101 DCHECK_LT(i, N);
2102 return inputs_[i];
2103 }
David Brazdil1abb4192015-02-17 18:33:36 +00002104
2105 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002106 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002107 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002108 }
2109
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002110 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002111 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002112
2113 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114};
2115
Vladimir Markof9f64412015-09-02 14:05:49 +01002116// HTemplateInstruction specialization for N=0.
2117template<>
2118class HTemplateInstruction<0>: public HInstruction {
2119 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002120 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002121 : HInstruction(side_effects, dex_pc) {}
2122
Vladimir Markof9f64412015-09-02 14:05:49 +01002123 virtual ~HTemplateInstruction() {}
2124
2125 size_t InputCount() const OVERRIDE { return 0; }
2126
2127 protected:
2128 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2129 LOG(FATAL) << "Unreachable";
2130 UNREACHABLE();
2131 }
2132
2133 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2134 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2135 LOG(FATAL) << "Unreachable";
2136 UNREACHABLE();
2137 }
2138
2139 private:
2140 friend class SsaBuilder;
2141};
2142
Dave Allison20dfc792014-06-16 20:44:29 -07002143template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002144class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002145 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002146 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002147 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002148 virtual ~HExpression() {}
2149
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002150 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002151
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002152 protected:
2153 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002154};
2155
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2157// instruction that branches to the exit block.
2158class HReturnVoid : public HTemplateInstruction<0> {
2159 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002160 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2161 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002162
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002163 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002164
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002165 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002166
2167 private:
2168 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2169};
2170
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002171// Represents dex's RETURN opcodes. A HReturn is a control flow
2172// instruction that branches to the exit block.
2173class HReturn : public HTemplateInstruction<1> {
2174 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002175 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2176 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002177 SetRawInputAt(0, value);
2178 }
2179
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002180 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002181
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002182 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002183
2184 private:
2185 DISALLOW_COPY_AND_ASSIGN(HReturn);
2186};
2187
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002188// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002189// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002190// exit block.
2191class HExit : public HTemplateInstruction<0> {
2192 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002193 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002194
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002195 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002196
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002197 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002198
2199 private:
2200 DISALLOW_COPY_AND_ASSIGN(HExit);
2201};
2202
2203// Jumps from one block to another.
2204class HGoto : public HTemplateInstruction<0> {
2205 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002206 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002207
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002208 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002209
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002210 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002211 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002212 }
2213
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002214 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002215
2216 private:
2217 DISALLOW_COPY_AND_ASSIGN(HGoto);
2218};
2219
Roland Levillain9867bc72015-08-05 10:21:34 +01002220class HConstant : public HExpression<0> {
2221 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002222 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2223 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002224
2225 bool CanBeMoved() const OVERRIDE { return true; }
2226
2227 virtual bool IsMinusOne() const { return false; }
2228 virtual bool IsZero() const { return false; }
2229 virtual bool IsOne() const { return false; }
2230
David Brazdil77a48ae2015-09-15 12:34:04 +00002231 virtual uint64_t GetValueAsUint64() const = 0;
2232
Roland Levillain9867bc72015-08-05 10:21:34 +01002233 DECLARE_INSTRUCTION(Constant);
2234
2235 private:
2236 DISALLOW_COPY_AND_ASSIGN(HConstant);
2237};
2238
2239class HNullConstant : public HConstant {
2240 public:
2241 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2242 return true;
2243 }
2244
David Brazdil77a48ae2015-09-15 12:34:04 +00002245 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2246
Roland Levillain9867bc72015-08-05 10:21:34 +01002247 size_t ComputeHashCode() const OVERRIDE { return 0; }
2248
2249 DECLARE_INSTRUCTION(NullConstant);
2250
2251 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002252 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002253
2254 friend class HGraph;
2255 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2256};
2257
2258// Constants of the type int. Those can be from Dex instructions, or
2259// synthesized (for example with the if-eqz instruction).
2260class HIntConstant : public HConstant {
2261 public:
2262 int32_t GetValue() const { return value_; }
2263
David Brazdil9f389d42015-10-01 14:32:56 +01002264 uint64_t GetValueAsUint64() const OVERRIDE {
2265 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2266 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002267
Roland Levillain9867bc72015-08-05 10:21:34 +01002268 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2269 DCHECK(other->IsIntConstant());
2270 return other->AsIntConstant()->value_ == value_;
2271 }
2272
2273 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2274
2275 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2276 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2277 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2278
2279 DECLARE_INSTRUCTION(IntConstant);
2280
2281 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002282 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2283 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2284 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2285 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002286
2287 const int32_t value_;
2288
2289 friend class HGraph;
2290 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2291 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2292 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2293};
2294
2295class HLongConstant : public HConstant {
2296 public:
2297 int64_t GetValue() const { return value_; }
2298
David Brazdil77a48ae2015-09-15 12:34:04 +00002299 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2300
Roland Levillain9867bc72015-08-05 10:21:34 +01002301 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2302 DCHECK(other->IsLongConstant());
2303 return other->AsLongConstant()->value_ == value_;
2304 }
2305
2306 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2307
2308 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2309 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2310 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2311
2312 DECLARE_INSTRUCTION(LongConstant);
2313
2314 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002315 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2316 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002317
2318 const int64_t value_;
2319
2320 friend class HGraph;
2321 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2322};
Dave Allison20dfc792014-06-16 20:44:29 -07002323
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002324// Conditional branch. A block ending with an HIf instruction must have
2325// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002326class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002327 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002328 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2329 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002330 SetRawInputAt(0, input);
2331 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002332
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002333 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002334
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002335 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002336 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002337 }
2338
2339 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002340 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002341 }
2342
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002343 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002344
2345 private:
2346 DISALLOW_COPY_AND_ASSIGN(HIf);
2347};
2348
David Brazdilfc6a86a2015-06-26 10:33:45 +00002349
2350// Abstract instruction which marks the beginning and/or end of a try block and
2351// links it to the respective exception handlers. Behaves the same as a Goto in
2352// non-exceptional control flow.
2353// Normal-flow successor is stored at index zero, exception handlers under
2354// higher indices in no particular order.
2355class HTryBoundary : public HTemplateInstruction<0> {
2356 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002357 enum BoundaryKind {
2358 kEntry,
2359 kExit,
2360 };
2361
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002362 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2363 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002364
2365 bool IsControlFlow() const OVERRIDE { return true; }
2366
2367 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002368 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002369
2370 // Returns whether `handler` is among its exception handlers (non-zero index
2371 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002372 bool HasExceptionHandler(const HBasicBlock& handler) const {
2373 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002374 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002375 }
2376
2377 // If not present already, adds `handler` to its block's list of exception
2378 // handlers.
2379 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002380 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002381 GetBlock()->AddSuccessor(handler);
2382 }
2383 }
2384
David Brazdil56e1acc2015-06-30 15:41:36 +01002385 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002386
David Brazdilffee3d32015-07-06 11:48:53 +01002387 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2388
David Brazdilfc6a86a2015-06-26 10:33:45 +00002389 DECLARE_INSTRUCTION(TryBoundary);
2390
2391 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002392 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002393
2394 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2395};
2396
David Brazdilffee3d32015-07-06 11:48:53 +01002397// Iterator over exception handlers of a given HTryBoundary, i.e. over
2398// exceptional successors of its basic block.
2399class HExceptionHandlerIterator : public ValueObject {
2400 public:
2401 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2402 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2403
Vladimir Marko60584552015-09-03 13:35:12 +00002404 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002405 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002406 size_t CurrentSuccessorIndex() const { return index_; }
2407 void Advance() { ++index_; }
2408
2409 private:
2410 const HBasicBlock& block_;
2411 size_t index_;
2412
2413 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2414};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002415
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002416// Deoptimize to interpreter, upon checking a condition.
2417class HDeoptimize : public HTemplateInstruction<1> {
2418 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002419 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2420 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002421 SetRawInputAt(0, cond);
2422 }
2423
2424 bool NeedsEnvironment() const OVERRIDE { return true; }
2425 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002426
2427 DECLARE_INSTRUCTION(Deoptimize);
2428
2429 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002430 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2431};
2432
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002433// Represents the ArtMethod that was passed as a first argument to
2434// the method. It is used by instructions that depend on it, like
2435// instructions that work with the dex cache.
2436class HCurrentMethod : public HExpression<0> {
2437 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002438 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2439 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002440
2441 DECLARE_INSTRUCTION(CurrentMethod);
2442
2443 private:
2444 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2445};
2446
Mark Mendellfe57faa2015-09-18 09:26:15 -04002447// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2448// have one successor for each entry in the switch table, and the final successor
2449// will be the block containing the next Dex opcode.
2450class HPackedSwitch : public HTemplateInstruction<1> {
2451 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002452 HPackedSwitch(int32_t start_value,
2453 uint32_t num_entries,
2454 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002455 uint32_t dex_pc = kNoDexPc)
2456 : HTemplateInstruction(SideEffects::None(), dex_pc),
2457 start_value_(start_value),
2458 num_entries_(num_entries) {
2459 SetRawInputAt(0, input);
2460 }
2461
2462 bool IsControlFlow() const OVERRIDE { return true; }
2463
2464 int32_t GetStartValue() const { return start_value_; }
2465
Vladimir Marko211c2112015-09-24 16:52:33 +01002466 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002467
2468 HBasicBlock* GetDefaultBlock() const {
2469 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002470 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002471 }
2472 DECLARE_INSTRUCTION(PackedSwitch);
2473
2474 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002475 const int32_t start_value_;
2476 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002477
2478 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2479};
2480
Roland Levillain88cb1752014-10-20 16:36:47 +01002481class HUnaryOperation : public HExpression<1> {
2482 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002483 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2484 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002485 SetRawInputAt(0, input);
2486 }
2487
2488 HInstruction* GetInput() const { return InputAt(0); }
2489 Primitive::Type GetResultType() const { return GetType(); }
2490
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002491 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002492 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002493 return true;
2494 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002495
Roland Levillain9240d6a2014-10-20 16:47:04 +01002496 // Try to statically evaluate `operation` and return a HConstant
2497 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002498 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002499 HConstant* TryStaticEvaluation() const;
2500
2501 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002502 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2503 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002504
Roland Levillain88cb1752014-10-20 16:36:47 +01002505 DECLARE_INSTRUCTION(UnaryOperation);
2506
2507 private:
2508 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2509};
2510
Dave Allison20dfc792014-06-16 20:44:29 -07002511class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002512 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002513 HBinaryOperation(Primitive::Type result_type,
2514 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002515 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002516 SideEffects side_effects = SideEffects::None(),
2517 uint32_t dex_pc = kNoDexPc)
2518 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002519 SetRawInputAt(0, left);
2520 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002521 }
2522
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002523 HInstruction* GetLeft() const { return InputAt(0); }
2524 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002525 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002526
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002527 virtual bool IsCommutative() const { return false; }
2528
2529 // Put constant on the right.
2530 // Returns whether order is changed.
2531 bool OrderInputsWithConstantOnTheRight() {
2532 HInstruction* left = InputAt(0);
2533 HInstruction* right = InputAt(1);
2534 if (left->IsConstant() && !right->IsConstant()) {
2535 ReplaceInput(right, 0);
2536 ReplaceInput(left, 1);
2537 return true;
2538 }
2539 return false;
2540 }
2541
2542 // Order inputs by instruction id, but favor constant on the right side.
2543 // This helps GVN for commutative ops.
2544 void OrderInputs() {
2545 DCHECK(IsCommutative());
2546 HInstruction* left = InputAt(0);
2547 HInstruction* right = InputAt(1);
2548 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2549 return;
2550 }
2551 if (OrderInputsWithConstantOnTheRight()) {
2552 return;
2553 }
2554 // Order according to instruction id.
2555 if (left->GetId() > right->GetId()) {
2556 ReplaceInput(right, 0);
2557 ReplaceInput(left, 1);
2558 }
2559 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002560
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002561 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002562 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002563 return true;
2564 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002565
Roland Levillain9240d6a2014-10-20 16:47:04 +01002566 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002567 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002568 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002569 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002570
2571 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002572 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2573 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2574 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2575 HLongConstant* y ATTRIBUTE_UNUSED) const {
2576 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2577 return nullptr;
2578 }
2579 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2580 HIntConstant* y ATTRIBUTE_UNUSED) const {
2581 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2582 return nullptr;
2583 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002584
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002585 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002586 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002587 HConstant* GetConstantRight() const;
2588
2589 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002590 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002591 HInstruction* GetLeastConstantLeft() const;
2592
Roland Levillainccc07a92014-09-16 14:48:16 +01002593 DECLARE_INSTRUCTION(BinaryOperation);
2594
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002595 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002596 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2597};
2598
Mark Mendellc4701932015-04-10 13:18:51 -04002599// The comparison bias applies for floating point operations and indicates how NaN
2600// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002601enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002602 kNoBias, // bias is not applicable (i.e. for long operation)
2603 kGtBias, // return 1 for NaN comparisons
2604 kLtBias, // return -1 for NaN comparisons
2605};
2606
Dave Allison20dfc792014-06-16 20:44:29 -07002607class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002608 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002609 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2610 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002611 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002612 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002613
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002614 bool NeedsMaterialization() const { return needs_materialization_; }
2615 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002616
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002617 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002618 // `instruction`, and disregard moves in between.
2619 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002620
Dave Allison20dfc792014-06-16 20:44:29 -07002621 DECLARE_INSTRUCTION(Condition);
2622
2623 virtual IfCondition GetCondition() const = 0;
2624
Mark Mendellc4701932015-04-10 13:18:51 -04002625 virtual IfCondition GetOppositeCondition() const = 0;
2626
Roland Levillain4fa13f62015-07-06 18:11:54 +01002627 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002628
2629 void SetBias(ComparisonBias bias) { bias_ = bias; }
2630
2631 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2632 return bias_ == other->AsCondition()->bias_;
2633 }
2634
Roland Levillain4fa13f62015-07-06 18:11:54 +01002635 bool IsFPConditionTrueIfNaN() const {
2636 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2637 IfCondition if_cond = GetCondition();
2638 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2639 }
2640
2641 bool IsFPConditionFalseIfNaN() const {
2642 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2643 IfCondition if_cond = GetCondition();
2644 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2645 }
2646
Dave Allison20dfc792014-06-16 20:44:29 -07002647 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002648 // For register allocation purposes, returns whether this instruction needs to be
2649 // materialized (that is, not just be in the processor flags).
2650 bool needs_materialization_;
2651
Mark Mendellc4701932015-04-10 13:18:51 -04002652 // Needed if we merge a HCompare into a HCondition.
2653 ComparisonBias bias_;
2654
Dave Allison20dfc792014-06-16 20:44:29 -07002655 DISALLOW_COPY_AND_ASSIGN(HCondition);
2656};
2657
2658// Instruction to check if two inputs are equal to each other.
2659class HEqual : public HCondition {
2660 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002661 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2662 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002663
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002664 bool IsCommutative() const OVERRIDE { return true; }
2665
Roland Levillain9867bc72015-08-05 10:21:34 +01002666 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002667 return GetBlock()->GetGraph()->GetIntConstant(
2668 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002669 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002670 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002671 return GetBlock()->GetGraph()->GetIntConstant(
2672 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002673 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002674
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002675 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002676
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002677 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002678 return kCondEQ;
2679 }
2680
Mark Mendellc4701932015-04-10 13:18:51 -04002681 IfCondition GetOppositeCondition() const OVERRIDE {
2682 return kCondNE;
2683 }
2684
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002685 private:
Aart Bike9f37602015-10-09 11:15:55 -07002686 template <typename T> bool Compute(T x, T y) const { return x == y; }
2687
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002688 DISALLOW_COPY_AND_ASSIGN(HEqual);
2689};
2690
Dave Allison20dfc792014-06-16 20:44:29 -07002691class HNotEqual : public HCondition {
2692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002693 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2694 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002695
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002696 bool IsCommutative() const OVERRIDE { return true; }
2697
Roland Levillain9867bc72015-08-05 10:21:34 +01002698 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002699 return GetBlock()->GetGraph()->GetIntConstant(
2700 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002701 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002702 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002703 return GetBlock()->GetGraph()->GetIntConstant(
2704 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002705 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002706
Dave Allison20dfc792014-06-16 20:44:29 -07002707 DECLARE_INSTRUCTION(NotEqual);
2708
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002709 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002710 return kCondNE;
2711 }
2712
Mark Mendellc4701932015-04-10 13:18:51 -04002713 IfCondition GetOppositeCondition() const OVERRIDE {
2714 return kCondEQ;
2715 }
2716
Dave Allison20dfc792014-06-16 20:44:29 -07002717 private:
Aart Bike9f37602015-10-09 11:15:55 -07002718 template <typename T> bool Compute(T x, T y) const { return x != y; }
2719
Dave Allison20dfc792014-06-16 20:44:29 -07002720 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2721};
2722
2723class HLessThan : public HCondition {
2724 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002725 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2726 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002727
Roland Levillain9867bc72015-08-05 10:21:34 +01002728 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002729 return GetBlock()->GetGraph()->GetIntConstant(
2730 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002731 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002732 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002733 return GetBlock()->GetGraph()->GetIntConstant(
2734 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002735 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002736
Dave Allison20dfc792014-06-16 20:44:29 -07002737 DECLARE_INSTRUCTION(LessThan);
2738
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002739 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002740 return kCondLT;
2741 }
2742
Mark Mendellc4701932015-04-10 13:18:51 -04002743 IfCondition GetOppositeCondition() const OVERRIDE {
2744 return kCondGE;
2745 }
2746
Dave Allison20dfc792014-06-16 20:44:29 -07002747 private:
Aart Bike9f37602015-10-09 11:15:55 -07002748 template <typename T> bool Compute(T x, T y) const { return x < y; }
2749
Dave Allison20dfc792014-06-16 20:44:29 -07002750 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2751};
2752
2753class HLessThanOrEqual : public HCondition {
2754 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002755 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2756 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002757
Roland Levillain9867bc72015-08-05 10:21:34 +01002758 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002759 return GetBlock()->GetGraph()->GetIntConstant(
2760 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002761 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002762 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002763 return GetBlock()->GetGraph()->GetIntConstant(
2764 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002765 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002766
Dave Allison20dfc792014-06-16 20:44:29 -07002767 DECLARE_INSTRUCTION(LessThanOrEqual);
2768
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002769 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002770 return kCondLE;
2771 }
2772
Mark Mendellc4701932015-04-10 13:18:51 -04002773 IfCondition GetOppositeCondition() const OVERRIDE {
2774 return kCondGT;
2775 }
2776
Dave Allison20dfc792014-06-16 20:44:29 -07002777 private:
Aart Bike9f37602015-10-09 11:15:55 -07002778 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2779
Dave Allison20dfc792014-06-16 20:44:29 -07002780 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2781};
2782
2783class HGreaterThan : public HCondition {
2784 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002785 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2786 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002787
Roland Levillain9867bc72015-08-05 10:21:34 +01002788 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002789 return GetBlock()->GetGraph()->GetIntConstant(
2790 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002791 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002792 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002793 return GetBlock()->GetGraph()->GetIntConstant(
2794 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002795 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002796
Dave Allison20dfc792014-06-16 20:44:29 -07002797 DECLARE_INSTRUCTION(GreaterThan);
2798
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002799 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002800 return kCondGT;
2801 }
2802
Mark Mendellc4701932015-04-10 13:18:51 -04002803 IfCondition GetOppositeCondition() const OVERRIDE {
2804 return kCondLE;
2805 }
2806
Dave Allison20dfc792014-06-16 20:44:29 -07002807 private:
Aart Bike9f37602015-10-09 11:15:55 -07002808 template <typename T> bool Compute(T x, T y) const { return x > y; }
2809
Dave Allison20dfc792014-06-16 20:44:29 -07002810 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2811};
2812
2813class HGreaterThanOrEqual : public HCondition {
2814 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002815 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2816 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002817
Roland Levillain9867bc72015-08-05 10:21:34 +01002818 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002819 return GetBlock()->GetGraph()->GetIntConstant(
2820 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002821 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002822 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002823 return GetBlock()->GetGraph()->GetIntConstant(
2824 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002825 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002826
Dave Allison20dfc792014-06-16 20:44:29 -07002827 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2828
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002829 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002830 return kCondGE;
2831 }
2832
Mark Mendellc4701932015-04-10 13:18:51 -04002833 IfCondition GetOppositeCondition() const OVERRIDE {
2834 return kCondLT;
2835 }
2836
Dave Allison20dfc792014-06-16 20:44:29 -07002837 private:
Aart Bike9f37602015-10-09 11:15:55 -07002838 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2839
Dave Allison20dfc792014-06-16 20:44:29 -07002840 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2841};
2842
Aart Bike9f37602015-10-09 11:15:55 -07002843class HBelow : public HCondition {
2844 public:
2845 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2846 : HCondition(first, second, dex_pc) {}
2847
2848 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2849 return GetBlock()->GetGraph()->GetIntConstant(
2850 Compute(static_cast<uint32_t>(x->GetValue()),
2851 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2852 }
2853 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2854 return GetBlock()->GetGraph()->GetIntConstant(
2855 Compute(static_cast<uint64_t>(x->GetValue()),
2856 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2857 }
2858
2859 DECLARE_INSTRUCTION(Below);
2860
2861 IfCondition GetCondition() const OVERRIDE {
2862 return kCondB;
2863 }
2864
2865 IfCondition GetOppositeCondition() const OVERRIDE {
2866 return kCondAE;
2867 }
2868
2869 private:
2870 template <typename T> bool Compute(T x, T y) const { return x < y; }
2871
2872 DISALLOW_COPY_AND_ASSIGN(HBelow);
2873};
2874
2875class HBelowOrEqual : public HCondition {
2876 public:
2877 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2878 : HCondition(first, second, dex_pc) {}
2879
2880 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2881 return GetBlock()->GetGraph()->GetIntConstant(
2882 Compute(static_cast<uint32_t>(x->GetValue()),
2883 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2884 }
2885 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2886 return GetBlock()->GetGraph()->GetIntConstant(
2887 Compute(static_cast<uint64_t>(x->GetValue()),
2888 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2889 }
2890
2891 DECLARE_INSTRUCTION(BelowOrEqual);
2892
2893 IfCondition GetCondition() const OVERRIDE {
2894 return kCondBE;
2895 }
2896
2897 IfCondition GetOppositeCondition() const OVERRIDE {
2898 return kCondA;
2899 }
2900
2901 private:
2902 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2903
2904 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2905};
2906
2907class HAbove : public HCondition {
2908 public:
2909 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2910 : HCondition(first, second, dex_pc) {}
2911
2912 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2913 return GetBlock()->GetGraph()->GetIntConstant(
2914 Compute(static_cast<uint32_t>(x->GetValue()),
2915 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2916 }
2917 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2918 return GetBlock()->GetGraph()->GetIntConstant(
2919 Compute(static_cast<uint64_t>(x->GetValue()),
2920 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2921 }
2922
2923 DECLARE_INSTRUCTION(Above);
2924
2925 IfCondition GetCondition() const OVERRIDE {
2926 return kCondA;
2927 }
2928
2929 IfCondition GetOppositeCondition() const OVERRIDE {
2930 return kCondBE;
2931 }
2932
2933 private:
2934 template <typename T> bool Compute(T x, T y) const { return x > y; }
2935
2936 DISALLOW_COPY_AND_ASSIGN(HAbove);
2937};
2938
2939class HAboveOrEqual : public HCondition {
2940 public:
2941 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2942 : HCondition(first, second, dex_pc) {}
2943
2944 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2945 return GetBlock()->GetGraph()->GetIntConstant(
2946 Compute(static_cast<uint32_t>(x->GetValue()),
2947 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2948 }
2949 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2950 return GetBlock()->GetGraph()->GetIntConstant(
2951 Compute(static_cast<uint64_t>(x->GetValue()),
2952 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2953 }
2954
2955 DECLARE_INSTRUCTION(AboveOrEqual);
2956
2957 IfCondition GetCondition() const OVERRIDE {
2958 return kCondAE;
2959 }
2960
2961 IfCondition GetOppositeCondition() const OVERRIDE {
2962 return kCondB;
2963 }
2964
2965 private:
2966 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2967
2968 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
2969};
Dave Allison20dfc792014-06-16 20:44:29 -07002970
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002971// Instruction to check how two inputs compare to each other.
2972// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2973class HCompare : public HBinaryOperation {
2974 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002975 HCompare(Primitive::Type type,
2976 HInstruction* first,
2977 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002978 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002979 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002980 : HBinaryOperation(Primitive::kPrimInt,
2981 first,
2982 second,
2983 SideEffectsForArchRuntimeCalls(type),
2984 dex_pc),
2985 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002986 DCHECK_EQ(type, first->GetType());
2987 DCHECK_EQ(type, second->GetType());
2988 }
2989
Roland Levillain9867bc72015-08-05 10:21:34 +01002990 template <typename T>
2991 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002992
Roland Levillain9867bc72015-08-05 10:21:34 +01002993 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002994 return GetBlock()->GetGraph()->GetIntConstant(
2995 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002996 }
2997 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002998 return GetBlock()->GetGraph()->GetIntConstant(
2999 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003000 }
3001
Calin Juravleddb7df22014-11-25 20:56:51 +00003002 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3003 return bias_ == other->AsCompare()->bias_;
3004 }
3005
Mark Mendellc4701932015-04-10 13:18:51 -04003006 ComparisonBias GetBias() const { return bias_; }
3007
Roland Levillain4fa13f62015-07-06 18:11:54 +01003008 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003009
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003010
3011 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3012 // MIPS64 uses a runtime call for FP comparisons.
3013 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3014 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003015
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003016 DECLARE_INSTRUCTION(Compare);
3017
3018 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003019 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003020
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003021 DISALLOW_COPY_AND_ASSIGN(HCompare);
3022};
3023
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003024// A local in the graph. Corresponds to a Dex register.
3025class HLocal : public HTemplateInstruction<0> {
3026 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003027 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003028 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003029
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003030 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003031
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003032 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003033
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003034 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003035 // The Dex register number.
3036 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003037
3038 DISALLOW_COPY_AND_ASSIGN(HLocal);
3039};
3040
3041// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003042class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003043 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003044 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3045 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003046 SetRawInputAt(0, local);
3047 }
3048
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003049 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3050
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003051 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003052
3053 private:
3054 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3055};
3056
3057// Store a value in a given local. This instruction has two inputs: the value
3058// and the local.
3059class HStoreLocal : public HTemplateInstruction<2> {
3060 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003061 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3062 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003063 SetRawInputAt(0, local);
3064 SetRawInputAt(1, value);
3065 }
3066
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003067 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3068
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003069 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003070
3071 private:
3072 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3073};
3074
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003075class HFloatConstant : public HConstant {
3076 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003077 float GetValue() const { return value_; }
3078
David Brazdil77a48ae2015-09-15 12:34:04 +00003079 uint64_t GetValueAsUint64() const OVERRIDE {
3080 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3081 }
3082
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003083 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003084 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003085 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003086 }
3087
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003088 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003089
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003090 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003091 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003092 }
3093 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003094 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003095 }
3096 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003097 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3098 }
3099 bool IsNaN() const {
3100 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003101 }
3102
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003103 DECLARE_INSTRUCTION(FloatConstant);
3104
3105 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003106 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3107 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3108 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3109 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003110
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003111 const float value_;
3112
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003113 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003114 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003115 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003116 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3117};
3118
3119class HDoubleConstant : public HConstant {
3120 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003121 double GetValue() const { return value_; }
3122
David Brazdil77a48ae2015-09-15 12:34:04 +00003123 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3124
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003125 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003126 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003127 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003128 }
3129
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003130 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003131
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003132 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003133 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003134 }
3135 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003136 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003137 }
3138 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003139 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3140 }
3141 bool IsNaN() const {
3142 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003143 }
3144
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003145 DECLARE_INSTRUCTION(DoubleConstant);
3146
3147 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003148 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3149 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3150 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3151 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003152
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003153 const double value_;
3154
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003155 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003156 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003157 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003158 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3159};
3160
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003161enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003162#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003163#include "intrinsics_list.h"
3164 kNone,
3165 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3166#undef INTRINSICS_LIST
3167#undef OPTIMIZING_INTRINSICS
3168};
3169std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3170
Agi Csaki05f20562015-08-19 14:58:14 -07003171enum IntrinsicNeedsEnvironmentOrCache {
3172 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3173 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003174};
3175
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003176class HInvoke : public HInstruction {
3177 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003178 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003179
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003180 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003181
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003182 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003183 SetRawInputAt(index, argument);
3184 }
3185
Roland Levillain3e3d7332015-04-28 11:00:54 +01003186 // Return the number of arguments. This number can be lower than
3187 // the number of inputs returned by InputCount(), as some invoke
3188 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3189 // inputs at the end of their list of inputs.
3190 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3191
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003192 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003193
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003194
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003195 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003196 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003197
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003198 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3199
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003200 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003201 return intrinsic_;
3202 }
3203
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003204 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003205
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003206 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003207 return GetEnvironment()->GetParent() != nullptr;
3208 }
3209
3210 bool CanThrow() const OVERRIDE { return true; }
3211
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003212 uint32_t* GetIntrinsicOptimizations() {
3213 return &intrinsic_optimizations_;
3214 }
3215
3216 const uint32_t* GetIntrinsicOptimizations() const {
3217 return &intrinsic_optimizations_;
3218 }
3219
3220 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3221
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003222 DECLARE_INSTRUCTION(Invoke);
3223
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003224 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003225 HInvoke(ArenaAllocator* arena,
3226 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003227 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003228 Primitive::Type return_type,
3229 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003230 uint32_t dex_method_index,
3231 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003232 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003233 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003234 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003235 inputs_(number_of_arguments + number_of_other_inputs,
3236 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003237 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003238 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003239 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003240 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003241 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003242 }
3243
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003244 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003245 return inputs_[index];
3246 }
3247
David Brazdil1abb4192015-02-17 18:33:36 +00003248 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003249 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003250 }
3251
Roland Levillain3e3d7332015-04-28 11:00:54 +01003252 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003253 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003254 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003255 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003256 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003257 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003258
3259 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3260 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003261
3262 private:
3263 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3264};
3265
Calin Juravle175dc732015-08-25 15:42:32 +01003266class HInvokeUnresolved : public HInvoke {
3267 public:
3268 HInvokeUnresolved(ArenaAllocator* arena,
3269 uint32_t number_of_arguments,
3270 Primitive::Type return_type,
3271 uint32_t dex_pc,
3272 uint32_t dex_method_index,
3273 InvokeType invoke_type)
3274 : HInvoke(arena,
3275 number_of_arguments,
3276 0u /* number_of_other_inputs */,
3277 return_type,
3278 dex_pc,
3279 dex_method_index,
3280 invoke_type) {
3281 }
3282
3283 DECLARE_INSTRUCTION(InvokeUnresolved);
3284
3285 private:
3286 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3287};
3288
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003289class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003290 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003291 // Requirements of this method call regarding the class
3292 // initialization (clinit) check of its declaring class.
3293 enum class ClinitCheckRequirement {
3294 kNone, // Class already initialized.
3295 kExplicit, // Static call having explicit clinit check as last input.
3296 kImplicit, // Static call implicitly requiring a clinit check.
3297 };
3298
Vladimir Marko58155012015-08-19 12:49:41 +00003299 // Determines how to load the target ArtMethod*.
3300 enum class MethodLoadKind {
3301 // Use a String init ArtMethod* loaded from Thread entrypoints.
3302 kStringInit,
3303
3304 // Use the method's own ArtMethod* loaded by the register allocator.
3305 kRecursive,
3306
3307 // Use ArtMethod* at a known address, embed the direct address in the code.
3308 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3309 kDirectAddress,
3310
3311 // Use ArtMethod* at an address that will be known at link time, embed the direct
3312 // address in the code. If the image is relocatable, emit .patch_oat entry.
3313 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3314 // the image relocatable or not.
3315 kDirectAddressWithFixup,
3316
3317 // Load from resoved methods array in the dex cache using a PC-relative load.
3318 // Used when we need to use the dex cache, for example for invoke-static that
3319 // may cause class initialization (the entry may point to a resolution method),
3320 // and we know that we can access the dex cache arrays using a PC-relative load.
3321 kDexCachePcRelative,
3322
3323 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3324 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3325 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3326 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3327 kDexCacheViaMethod,
3328 };
3329
3330 // Determines the location of the code pointer.
3331 enum class CodePtrLocation {
3332 // Recursive call, use local PC-relative call instruction.
3333 kCallSelf,
3334
3335 // Use PC-relative call instruction patched at link time.
3336 // Used for calls within an oat file, boot->boot or app->app.
3337 kCallPCRelative,
3338
3339 // Call to a known target address, embed the direct address in code.
3340 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3341 kCallDirect,
3342
3343 // Call to a target address that will be known at link time, embed the direct
3344 // address in code. If the image is relocatable, emit .patch_oat entry.
3345 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3346 // the image relocatable or not.
3347 kCallDirectWithFixup,
3348
3349 // Use code pointer from the ArtMethod*.
3350 // Used when we don't know the target code. This is also the last-resort-kind used when
3351 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3352 kCallArtMethod,
3353 };
3354
3355 struct DispatchInfo {
3356 const MethodLoadKind method_load_kind;
3357 const CodePtrLocation code_ptr_location;
3358 // The method load data holds
3359 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3360 // Note that there are multiple string init methods, each having its own offset.
3361 // - the method address for kDirectAddress
3362 // - the dex cache arrays offset for kDexCachePcRel.
3363 const uint64_t method_load_data;
3364 const uint64_t direct_code_ptr;
3365 };
3366
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003367 HInvokeStaticOrDirect(ArenaAllocator* arena,
3368 uint32_t number_of_arguments,
3369 Primitive::Type return_type,
3370 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003371 uint32_t method_index,
3372 MethodReference target_method,
3373 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003374 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003375 InvokeType invoke_type,
3376 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003377 : HInvoke(arena,
3378 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003379 // There is one extra argument for the HCurrentMethod node, and
3380 // potentially one other if the clinit check is explicit, and one other
3381 // if the method is a string factory.
3382 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003383 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003384 return_type,
3385 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003386 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003387 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003388 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003389 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003390 target_method_(target_method),
3391 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003392
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003393 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003394 // We access the method via the dex cache so we can't do an implicit null check.
3395 // TODO: for intrinsics we can generate implicit null checks.
3396 return false;
3397 }
3398
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003399 bool CanBeNull() const OVERRIDE {
3400 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3401 }
3402
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003403 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003404 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3405 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3406 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003407 bool NeedsDexCache() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003408 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003409 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003410 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3411 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3412 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3413 MethodReference GetTargetMethod() const { return target_method_; }
3414
3415 int32_t GetStringInitOffset() const {
3416 DCHECK(IsStringInit());
3417 return dispatch_info_.method_load_data;
3418 }
3419
3420 uint64_t GetMethodAddress() const {
3421 DCHECK(HasMethodAddress());
3422 return dispatch_info_.method_load_data;
3423 }
3424
3425 uint32_t GetDexCacheArrayOffset() const {
3426 DCHECK(HasPcRelDexCache());
3427 return dispatch_info_.method_load_data;
3428 }
3429
3430 uint64_t GetDirectCodePtr() const {
3431 DCHECK(HasDirectCodePtr());
3432 return dispatch_info_.direct_code_ptr;
3433 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003434
Calin Juravle68ad6492015-08-18 17:08:12 +01003435 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3436
Roland Levillain4c0eb422015-04-24 16:43:49 +01003437 // Is this instruction a call to a static method?
3438 bool IsStatic() const {
3439 return GetInvokeType() == kStatic;
3440 }
3441
Roland Levillain3e3d7332015-04-28 11:00:54 +01003442 // Remove the art::HLoadClass instruction set as last input by
3443 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3444 // the initial art::HClinitCheck instruction (only relevant for
3445 // static calls with explicit clinit check).
3446 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003447 DCHECK(IsStaticWithExplicitClinitCheck());
3448 size_t last_input_index = InputCount() - 1;
3449 HInstruction* last_input = InputAt(last_input_index);
3450 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003451 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003452 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003453 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003454 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3455 DCHECK(IsStaticWithImplicitClinitCheck());
3456 }
3457
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003458 bool IsStringFactoryFor(HFakeString* str) const {
3459 if (!IsStringInit()) return false;
3460 // +1 for the current method.
3461 if (InputCount() == (number_of_arguments_ + 1)) return false;
3462 return InputAt(InputCount() - 1)->AsFakeString() == str;
3463 }
3464
3465 void RemoveFakeStringArgumentAsLastInput() {
3466 DCHECK(IsStringInit());
3467 size_t last_input_index = InputCount() - 1;
3468 HInstruction* last_input = InputAt(last_input_index);
3469 DCHECK(last_input != nullptr);
3470 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3471 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003472 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003473 }
3474
Roland Levillain4c0eb422015-04-24 16:43:49 +01003475 // Is this a call to a static method whose declaring class has an
3476 // explicit intialization check in the graph?
3477 bool IsStaticWithExplicitClinitCheck() const {
3478 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3479 }
3480
3481 // Is this a call to a static method whose declaring class has an
3482 // implicit intialization check requirement?
3483 bool IsStaticWithImplicitClinitCheck() const {
3484 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3485 }
3486
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003487 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003488
Roland Levillain4c0eb422015-04-24 16:43:49 +01003489 protected:
3490 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3491 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3492 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3493 HInstruction* input = input_record.GetInstruction();
3494 // `input` is the last input of a static invoke marked as having
3495 // an explicit clinit check. It must either be:
3496 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3497 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3498 DCHECK(input != nullptr);
3499 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3500 }
3501 return input_record;
3502 }
3503
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003504 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003505 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003506 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003507 // The target method may refer to different dex file or method index than the original
3508 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3509 // in derived class to increase visibility.
3510 MethodReference target_method_;
3511 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003512
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003513 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003514};
3515
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003516class HInvokeVirtual : public HInvoke {
3517 public:
3518 HInvokeVirtual(ArenaAllocator* arena,
3519 uint32_t number_of_arguments,
3520 Primitive::Type return_type,
3521 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003522 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003523 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003524 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003525 vtable_index_(vtable_index) {}
3526
Calin Juravle641547a2015-04-21 22:08:51 +01003527 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003528 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003529 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003530 }
3531
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003532 uint32_t GetVTableIndex() const { return vtable_index_; }
3533
3534 DECLARE_INSTRUCTION(InvokeVirtual);
3535
3536 private:
3537 const uint32_t vtable_index_;
3538
3539 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3540};
3541
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003542class HInvokeInterface : public HInvoke {
3543 public:
3544 HInvokeInterface(ArenaAllocator* arena,
3545 uint32_t number_of_arguments,
3546 Primitive::Type return_type,
3547 uint32_t dex_pc,
3548 uint32_t dex_method_index,
3549 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003550 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003551 imt_index_(imt_index) {}
3552
Calin Juravle641547a2015-04-21 22:08:51 +01003553 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003554 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003555 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003556 }
3557
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003558 uint32_t GetImtIndex() const { return imt_index_; }
3559 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3560
3561 DECLARE_INSTRUCTION(InvokeInterface);
3562
3563 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003564 const uint32_t imt_index_;
3565
3566 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3567};
3568
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003569class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003570 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003571 HNewInstance(HCurrentMethod* current_method,
3572 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003573 uint16_t type_index,
3574 const DexFile& dex_file,
3575 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003576 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003577 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003578 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003579 entrypoint_(entrypoint) {
3580 SetRawInputAt(0, current_method);
3581 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003582
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003583 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003584 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003585
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003586 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003587 bool NeedsEnvironment() const OVERRIDE { return true; }
3588 // It may throw when called on:
3589 // - interfaces
3590 // - abstract/innaccessible/unknown classes
3591 // TODO: optimize when possible.
3592 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003593
Calin Juravle10e244f2015-01-26 18:54:32 +00003594 bool CanBeNull() const OVERRIDE { return false; }
3595
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003596 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3597
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003598 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003599
3600 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003601 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003602 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003603 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003604
3605 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3606};
3607
Roland Levillain88cb1752014-10-20 16:36:47 +01003608class HNeg : public HUnaryOperation {
3609 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003610 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3611 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003612
Roland Levillain9867bc72015-08-05 10:21:34 +01003613 template <typename T> T Compute(T x) const { return -x; }
3614
3615 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003616 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003617 }
3618 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003619 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003620 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003621
Roland Levillain88cb1752014-10-20 16:36:47 +01003622 DECLARE_INSTRUCTION(Neg);
3623
3624 private:
3625 DISALLOW_COPY_AND_ASSIGN(HNeg);
3626};
3627
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003628class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003629 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003630 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003631 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003632 uint32_t dex_pc,
3633 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003634 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003635 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003636 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003637 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003638 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003639 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003640 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003641 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003642 }
3643
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003644 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003645 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003646
3647 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003648 bool NeedsEnvironment() const OVERRIDE { return true; }
3649
Mingyao Yang0c365e62015-03-31 15:09:29 -07003650 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3651 bool CanThrow() const OVERRIDE { return true; }
3652
Calin Juravle10e244f2015-01-26 18:54:32 +00003653 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003654
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003655 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3656
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003657 DECLARE_INSTRUCTION(NewArray);
3658
3659 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003660 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003661 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003662 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003663
3664 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3665};
3666
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003667class HAdd : public HBinaryOperation {
3668 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003669 HAdd(Primitive::Type result_type,
3670 HInstruction* left,
3671 HInstruction* right,
3672 uint32_t dex_pc = kNoDexPc)
3673 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003674
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003675 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003676
Roland Levillain9867bc72015-08-05 10:21:34 +01003677 template <typename T> T Compute(T x, T y) const { return x + y; }
3678
3679 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003680 return GetBlock()->GetGraph()->GetIntConstant(
3681 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003682 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003683 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003684 return GetBlock()->GetGraph()->GetLongConstant(
3685 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003686 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003687
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003688 DECLARE_INSTRUCTION(Add);
3689
3690 private:
3691 DISALLOW_COPY_AND_ASSIGN(HAdd);
3692};
3693
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003694class HSub : public HBinaryOperation {
3695 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003696 HSub(Primitive::Type result_type,
3697 HInstruction* left,
3698 HInstruction* right,
3699 uint32_t dex_pc = kNoDexPc)
3700 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003701
Roland Levillain9867bc72015-08-05 10:21:34 +01003702 template <typename T> T Compute(T x, T y) const { return x - y; }
3703
3704 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003705 return GetBlock()->GetGraph()->GetIntConstant(
3706 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003707 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003708 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003709 return GetBlock()->GetGraph()->GetLongConstant(
3710 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003711 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003712
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003713 DECLARE_INSTRUCTION(Sub);
3714
3715 private:
3716 DISALLOW_COPY_AND_ASSIGN(HSub);
3717};
3718
Calin Juravle34bacdf2014-10-07 20:23:36 +01003719class HMul : public HBinaryOperation {
3720 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003721 HMul(Primitive::Type result_type,
3722 HInstruction* left,
3723 HInstruction* right,
3724 uint32_t dex_pc = kNoDexPc)
3725 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003726
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003727 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003728
Roland Levillain9867bc72015-08-05 10:21:34 +01003729 template <typename T> T Compute(T x, T y) const { return x * y; }
3730
3731 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003732 return GetBlock()->GetGraph()->GetIntConstant(
3733 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003734 }
3735 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003736 return GetBlock()->GetGraph()->GetLongConstant(
3737 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003738 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003739
3740 DECLARE_INSTRUCTION(Mul);
3741
3742 private:
3743 DISALLOW_COPY_AND_ASSIGN(HMul);
3744};
3745
Calin Juravle7c4954d2014-10-28 16:57:40 +00003746class HDiv : public HBinaryOperation {
3747 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003748 HDiv(Primitive::Type result_type,
3749 HInstruction* left,
3750 HInstruction* right,
3751 uint32_t dex_pc)
3752 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003753
Roland Levillain9867bc72015-08-05 10:21:34 +01003754 template <typename T>
3755 T Compute(T x, T y) const {
3756 // Our graph structure ensures we never have 0 for `y` during
3757 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003758 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003759 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003760 return (y == -1) ? -x : x / y;
3761 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003762
Roland Levillain9867bc72015-08-05 10:21:34 +01003763 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003764 return GetBlock()->GetGraph()->GetIntConstant(
3765 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003766 }
3767 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003768 return GetBlock()->GetGraph()->GetLongConstant(
3769 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003770 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003771
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003772 static SideEffects SideEffectsForArchRuntimeCalls() {
3773 // The generated code can use a runtime call.
3774 return SideEffects::CanTriggerGC();
3775 }
3776
Calin Juravle7c4954d2014-10-28 16:57:40 +00003777 DECLARE_INSTRUCTION(Div);
3778
3779 private:
3780 DISALLOW_COPY_AND_ASSIGN(HDiv);
3781};
3782
Calin Juravlebacfec32014-11-14 15:54:36 +00003783class HRem : public HBinaryOperation {
3784 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003785 HRem(Primitive::Type result_type,
3786 HInstruction* left,
3787 HInstruction* right,
3788 uint32_t dex_pc)
3789 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003790
Roland Levillain9867bc72015-08-05 10:21:34 +01003791 template <typename T>
3792 T Compute(T x, T y) const {
3793 // Our graph structure ensures we never have 0 for `y` during
3794 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003795 DCHECK_NE(y, 0);
3796 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3797 return (y == -1) ? 0 : x % y;
3798 }
3799
Roland Levillain9867bc72015-08-05 10:21:34 +01003800 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003801 return GetBlock()->GetGraph()->GetIntConstant(
3802 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003803 }
3804 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003805 return GetBlock()->GetGraph()->GetLongConstant(
3806 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003807 }
3808
Calin Juravlebacfec32014-11-14 15:54:36 +00003809
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003810 static SideEffects SideEffectsForArchRuntimeCalls() {
3811 return SideEffects::CanTriggerGC();
3812 }
3813
Calin Juravlebacfec32014-11-14 15:54:36 +00003814 DECLARE_INSTRUCTION(Rem);
3815
3816 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003817 DISALLOW_COPY_AND_ASSIGN(HRem);
3818};
3819
Calin Juravled0d48522014-11-04 16:40:20 +00003820class HDivZeroCheck : public HExpression<1> {
3821 public:
3822 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003823 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003824 SetRawInputAt(0, value);
3825 }
3826
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003827 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3828
Calin Juravled0d48522014-11-04 16:40:20 +00003829 bool CanBeMoved() const OVERRIDE { return true; }
3830
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003831 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003832 return true;
3833 }
3834
3835 bool NeedsEnvironment() const OVERRIDE { return true; }
3836 bool CanThrow() const OVERRIDE { return true; }
3837
Calin Juravled0d48522014-11-04 16:40:20 +00003838 DECLARE_INSTRUCTION(DivZeroCheck);
3839
3840 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003841 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3842};
3843
Calin Juravle9aec02f2014-11-18 23:06:35 +00003844class HShl : public HBinaryOperation {
3845 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003846 HShl(Primitive::Type result_type,
3847 HInstruction* left,
3848 HInstruction* right,
3849 uint32_t dex_pc = kNoDexPc)
3850 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003851
Roland Levillain9867bc72015-08-05 10:21:34 +01003852 template <typename T, typename U, typename V>
3853 T Compute(T x, U y, V max_shift_value) const {
3854 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3855 "V is not the unsigned integer type corresponding to T");
3856 return x << (y & max_shift_value);
3857 }
3858
3859 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3860 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003861 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003862 }
3863 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3864 // case is handled as `x << static_cast<int>(y)`.
3865 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3866 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003867 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003868 }
3869 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3870 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003871 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003872 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003873
3874 DECLARE_INSTRUCTION(Shl);
3875
3876 private:
3877 DISALLOW_COPY_AND_ASSIGN(HShl);
3878};
3879
3880class HShr : public HBinaryOperation {
3881 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003882 HShr(Primitive::Type result_type,
3883 HInstruction* left,
3884 HInstruction* right,
3885 uint32_t dex_pc = kNoDexPc)
3886 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003887
Roland Levillain9867bc72015-08-05 10:21:34 +01003888 template <typename T, typename U, typename V>
3889 T Compute(T x, U y, V max_shift_value) const {
3890 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3891 "V is not the unsigned integer type corresponding to T");
3892 return x >> (y & max_shift_value);
3893 }
3894
3895 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3896 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003897 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003898 }
3899 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3900 // case is handled as `x >> static_cast<int>(y)`.
3901 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3902 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003903 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003904 }
3905 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3906 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003907 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003908 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003909
3910 DECLARE_INSTRUCTION(Shr);
3911
3912 private:
3913 DISALLOW_COPY_AND_ASSIGN(HShr);
3914};
3915
3916class HUShr : public HBinaryOperation {
3917 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003918 HUShr(Primitive::Type result_type,
3919 HInstruction* left,
3920 HInstruction* right,
3921 uint32_t dex_pc = kNoDexPc)
3922 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003923
Roland Levillain9867bc72015-08-05 10:21:34 +01003924 template <typename T, typename U, typename V>
3925 T Compute(T x, U y, V max_shift_value) const {
3926 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3927 "V is not the unsigned integer type corresponding to T");
3928 V ux = static_cast<V>(x);
3929 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003930 }
3931
Roland Levillain9867bc72015-08-05 10:21:34 +01003932 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3933 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003934 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003935 }
3936 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3937 // case is handled as `x >>> static_cast<int>(y)`.
3938 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3939 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003940 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003941 }
3942 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3943 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003944 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003945 }
3946
3947 DECLARE_INSTRUCTION(UShr);
3948
3949 private:
3950 DISALLOW_COPY_AND_ASSIGN(HUShr);
3951};
3952
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003953class HAnd : public HBinaryOperation {
3954 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003955 HAnd(Primitive::Type result_type,
3956 HInstruction* left,
3957 HInstruction* right,
3958 uint32_t dex_pc = kNoDexPc)
3959 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003960
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003961 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003962
Roland Levillain9867bc72015-08-05 10:21:34 +01003963 template <typename T, typename U>
3964 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3965
3966 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003967 return GetBlock()->GetGraph()->GetIntConstant(
3968 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003969 }
3970 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003971 return GetBlock()->GetGraph()->GetLongConstant(
3972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003973 }
3974 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003975 return GetBlock()->GetGraph()->GetLongConstant(
3976 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003977 }
3978 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003979 return GetBlock()->GetGraph()->GetLongConstant(
3980 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003981 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003982
3983 DECLARE_INSTRUCTION(And);
3984
3985 private:
3986 DISALLOW_COPY_AND_ASSIGN(HAnd);
3987};
3988
3989class HOr : public HBinaryOperation {
3990 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003991 HOr(Primitive::Type result_type,
3992 HInstruction* left,
3993 HInstruction* right,
3994 uint32_t dex_pc = kNoDexPc)
3995 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003996
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003997 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003998
Roland Levillain9867bc72015-08-05 10:21:34 +01003999 template <typename T, typename U>
4000 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4001
4002 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004003 return GetBlock()->GetGraph()->GetIntConstant(
4004 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004005 }
4006 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004007 return GetBlock()->GetGraph()->GetLongConstant(
4008 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004009 }
4010 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004011 return GetBlock()->GetGraph()->GetLongConstant(
4012 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004013 }
4014 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004015 return GetBlock()->GetGraph()->GetLongConstant(
4016 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004017 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004018
4019 DECLARE_INSTRUCTION(Or);
4020
4021 private:
4022 DISALLOW_COPY_AND_ASSIGN(HOr);
4023};
4024
4025class HXor : public HBinaryOperation {
4026 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004027 HXor(Primitive::Type result_type,
4028 HInstruction* left,
4029 HInstruction* right,
4030 uint32_t dex_pc = kNoDexPc)
4031 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004032
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004033 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004034
Roland Levillain9867bc72015-08-05 10:21:34 +01004035 template <typename T, typename U>
4036 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4037
4038 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004039 return GetBlock()->GetGraph()->GetIntConstant(
4040 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004041 }
4042 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004043 return GetBlock()->GetGraph()->GetLongConstant(
4044 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004045 }
4046 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004047 return GetBlock()->GetGraph()->GetLongConstant(
4048 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004049 }
4050 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004051 return GetBlock()->GetGraph()->GetLongConstant(
4052 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004053 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004054
4055 DECLARE_INSTRUCTION(Xor);
4056
4057 private:
4058 DISALLOW_COPY_AND_ASSIGN(HXor);
4059};
4060
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004061// The value of a parameter in this method. Its location depends on
4062// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004063class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004064 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004065 HParameterValue(const DexFile& dex_file,
4066 uint16_t type_index,
4067 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004068 Primitive::Type parameter_type,
4069 bool is_this = false)
4070 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004071 dex_file_(dex_file),
4072 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004073 index_(index),
4074 is_this_(is_this),
4075 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004076
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004077 const DexFile& GetDexFile() const { return dex_file_; }
4078 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004079 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004080 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004081
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004082 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4083 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004084
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004085 DECLARE_INSTRUCTION(ParameterValue);
4086
4087 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004088 const DexFile& dex_file_;
4089 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004090 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004091 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004092 const uint8_t index_;
4093
Calin Juravle10e244f2015-01-26 18:54:32 +00004094 // Whether or not the parameter value corresponds to 'this' argument.
4095 const bool is_this_;
4096
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004097 bool can_be_null_;
4098
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004099 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4100};
4101
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004102class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004103 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004104 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4105 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004106
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004107 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004108 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004109 return true;
4110 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004111
Roland Levillain9867bc72015-08-05 10:21:34 +01004112 template <typename T> T Compute(T x) const { return ~x; }
4113
4114 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004115 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004116 }
4117 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004118 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004119 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004120
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004121 DECLARE_INSTRUCTION(Not);
4122
4123 private:
4124 DISALLOW_COPY_AND_ASSIGN(HNot);
4125};
4126
David Brazdil66d126e2015-04-03 16:02:44 +01004127class HBooleanNot : public HUnaryOperation {
4128 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004129 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4130 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004131
4132 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004133 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004134 return true;
4135 }
4136
Roland Levillain9867bc72015-08-05 10:21:34 +01004137 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004138 DCHECK(IsUint<1>(x));
4139 return !x;
4140 }
4141
Roland Levillain9867bc72015-08-05 10:21:34 +01004142 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004143 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004144 }
4145 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4146 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004147 UNREACHABLE();
4148 }
4149
4150 DECLARE_INSTRUCTION(BooleanNot);
4151
4152 private:
4153 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4154};
4155
Roland Levillaindff1f282014-11-05 14:15:05 +00004156class HTypeConversion : public HExpression<1> {
4157 public:
4158 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004159 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004160 : HExpression(result_type,
4161 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4162 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004163 SetRawInputAt(0, input);
4164 DCHECK_NE(input->GetType(), result_type);
4165 }
4166
4167 HInstruction* GetInput() const { return InputAt(0); }
4168 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4169 Primitive::Type GetResultType() const { return GetType(); }
4170
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004171 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004172 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004173
Roland Levillaindff1f282014-11-05 14:15:05 +00004174 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004175 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004176
Mark Mendelle82549b2015-05-06 10:55:34 -04004177 // Try to statically evaluate the conversion and return a HConstant
4178 // containing the result. If the input cannot be converted, return nullptr.
4179 HConstant* TryStaticEvaluation() const;
4180
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004181 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4182 Primitive::Type result_type) {
4183 // Some architectures may not require the 'GC' side effects, but at this point
4184 // in the compilation process we do not know what architecture we will
4185 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004186 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4187 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004188 return SideEffects::CanTriggerGC();
4189 }
4190 return SideEffects::None();
4191 }
4192
Roland Levillaindff1f282014-11-05 14:15:05 +00004193 DECLARE_INSTRUCTION(TypeConversion);
4194
4195 private:
4196 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4197};
4198
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004199static constexpr uint32_t kNoRegNumber = -1;
4200
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004201class HPhi : public HInstruction {
4202 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004203 HPhi(ArenaAllocator* arena,
4204 uint32_t reg_number,
4205 size_t number_of_inputs,
4206 Primitive::Type type,
4207 uint32_t dex_pc = kNoDexPc)
4208 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004209 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004210 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004211 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004212 is_live_(false),
4213 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004214 }
4215
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004216 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4217 static Primitive::Type ToPhiType(Primitive::Type type) {
4218 switch (type) {
4219 case Primitive::kPrimBoolean:
4220 case Primitive::kPrimByte:
4221 case Primitive::kPrimShort:
4222 case Primitive::kPrimChar:
4223 return Primitive::kPrimInt;
4224 default:
4225 return type;
4226 }
4227 }
4228
David Brazdilffee3d32015-07-06 11:48:53 +01004229 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4230
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004231 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004232
4233 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004234 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004235
Calin Juravle10e244f2015-01-26 18:54:32 +00004236 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004237 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004238
Calin Juravle10e244f2015-01-26 18:54:32 +00004239 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4240 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4241
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004242 uint32_t GetRegNumber() const { return reg_number_; }
4243
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004244 void SetDead() { is_live_ = false; }
4245 void SetLive() { is_live_ = true; }
4246 bool IsDead() const { return !is_live_; }
4247 bool IsLive() const { return is_live_; }
4248
David Brazdil77a48ae2015-09-15 12:34:04 +00004249 bool IsVRegEquivalentOf(HInstruction* other) const {
4250 return other != nullptr
4251 && other->IsPhi()
4252 && other->AsPhi()->GetBlock() == GetBlock()
4253 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4254 }
4255
Calin Juravlea4f88312015-04-16 12:57:19 +01004256 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4257 // An equivalent phi is a phi having the same dex register and type.
4258 // It assumes that phis with the same dex register are adjacent.
4259 HPhi* GetNextEquivalentPhiWithSameType() {
4260 HInstruction* next = GetNext();
4261 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4262 if (next->GetType() == GetType()) {
4263 return next->AsPhi();
4264 }
4265 next = next->GetNext();
4266 }
4267 return nullptr;
4268 }
4269
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004270 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004271
David Brazdil1abb4192015-02-17 18:33:36 +00004272 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004273 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004274 return inputs_[index];
4275 }
David Brazdil1abb4192015-02-17 18:33:36 +00004276
4277 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004278 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004279 }
4280
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004281 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004282 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004283 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004284 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004285 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004286 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004287
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004288 DISALLOW_COPY_AND_ASSIGN(HPhi);
4289};
4290
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004291class HNullCheck : public HExpression<1> {
4292 public:
4293 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004294 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004295 SetRawInputAt(0, value);
4296 }
4297
Calin Juravle10e244f2015-01-26 18:54:32 +00004298 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004299 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004300 return true;
4301 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004302
Calin Juravle10e244f2015-01-26 18:54:32 +00004303 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004304
Calin Juravle10e244f2015-01-26 18:54:32 +00004305 bool CanThrow() const OVERRIDE { return true; }
4306
4307 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004308
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004309
4310 DECLARE_INSTRUCTION(NullCheck);
4311
4312 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004313 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4314};
4315
4316class FieldInfo : public ValueObject {
4317 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004318 FieldInfo(MemberOffset field_offset,
4319 Primitive::Type field_type,
4320 bool is_volatile,
4321 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004322 const DexFile& dex_file,
4323 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004324 : field_offset_(field_offset),
4325 field_type_(field_type),
4326 is_volatile_(is_volatile),
4327 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004328 dex_file_(dex_file),
4329 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004330
4331 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004332 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004333 uint32_t GetFieldIndex() const { return index_; }
4334 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004335 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004336 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004337
4338 private:
4339 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004340 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004341 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004342 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004343 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004344 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004345};
4346
4347class HInstanceFieldGet : public HExpression<1> {
4348 public:
4349 HInstanceFieldGet(HInstruction* value,
4350 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004351 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004352 bool is_volatile,
4353 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004354 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004355 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004356 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004357 : HExpression(
4358 field_type,
4359 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
4360 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004361 SetRawInputAt(0, value);
4362 }
4363
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004364 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004365
4366 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4367 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4368 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004369 }
4370
Calin Juravle641547a2015-04-21 22:08:51 +01004371 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4372 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004373 }
4374
4375 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004376 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4377 }
4378
Calin Juravle52c48962014-12-16 17:02:57 +00004379 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004380 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004381 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004382 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004383
4384 DECLARE_INSTRUCTION(InstanceFieldGet);
4385
4386 private:
4387 const FieldInfo field_info_;
4388
4389 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4390};
4391
4392class HInstanceFieldSet : public HTemplateInstruction<2> {
4393 public:
4394 HInstanceFieldSet(HInstruction* object,
4395 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004396 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004397 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004398 bool is_volatile,
4399 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004400 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004401 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004402 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004403 : HTemplateInstruction(
4404 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
4405 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004406 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004407 SetRawInputAt(0, object);
4408 SetRawInputAt(1, value);
4409 }
4410
Calin Juravle641547a2015-04-21 22:08:51 +01004411 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4412 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004413 }
4414
Calin Juravle52c48962014-12-16 17:02:57 +00004415 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004416 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004417 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004418 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004419 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004420 bool GetValueCanBeNull() const { return value_can_be_null_; }
4421 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004422
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004423 DECLARE_INSTRUCTION(InstanceFieldSet);
4424
4425 private:
4426 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004427 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004428
4429 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4430};
4431
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004432class HArrayGet : public HExpression<2> {
4433 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004434 HArrayGet(HInstruction* array,
4435 HInstruction* index,
4436 Primitive::Type type,
Calin Juravle154746b2015-10-06 15:46:54 +01004437 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004438 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004439 SetRawInputAt(0, array);
4440 SetRawInputAt(1, index);
4441 }
4442
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004443 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004444 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004445 return true;
4446 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004447 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004448 // TODO: We can be smarter here.
4449 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4450 // which generates the implicit null check. There are cases when these can be removed
4451 // to produce better code. If we ever add optimizations to do so we should allow an
4452 // implicit check here (as long as the address falls in the first page).
4453 return false;
4454 }
4455
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004456 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004457
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004458 HInstruction* GetArray() const { return InputAt(0); }
4459 HInstruction* GetIndex() const { return InputAt(1); }
4460
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004461 DECLARE_INSTRUCTION(ArrayGet);
4462
4463 private:
4464 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4465};
4466
4467class HArraySet : public HTemplateInstruction<3> {
4468 public:
4469 HArraySet(HInstruction* array,
4470 HInstruction* index,
4471 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004472 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004473 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004474 : HTemplateInstruction(
4475 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004476 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004477 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004478 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004479 value_can_be_null_(true),
4480 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004481 SetRawInputAt(0, array);
4482 SetRawInputAt(1, index);
4483 SetRawInputAt(2, value);
4484 }
4485
Calin Juravle77520bc2015-01-12 18:45:46 +00004486 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004487 // We currently always call a runtime method to catch array store
4488 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004489 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004490 }
4491
Mingyao Yang81014cb2015-06-02 03:16:27 -07004492 // Can throw ArrayStoreException.
4493 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4494
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004495 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004496 // TODO: Same as for ArrayGet.
4497 return false;
4498 }
4499
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004500 void ClearNeedsTypeCheck() {
4501 needs_type_check_ = false;
4502 }
4503
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004504 void ClearValueCanBeNull() {
4505 value_can_be_null_ = false;
4506 }
4507
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004508 void SetStaticTypeOfArrayIsObjectArray() {
4509 static_type_of_array_is_object_array_ = true;
4510 }
4511
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004512 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004513 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004514 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004515
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004516 HInstruction* GetArray() const { return InputAt(0); }
4517 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004518 HInstruction* GetValue() const { return InputAt(2); }
4519
4520 Primitive::Type GetComponentType() const {
4521 // The Dex format does not type floating point index operations. Since the
4522 // `expected_component_type_` is set during building and can therefore not
4523 // be correct, we also check what is the value type. If it is a floating
4524 // point type, we must use that type.
4525 Primitive::Type value_type = GetValue()->GetType();
4526 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4527 ? value_type
4528 : expected_component_type_;
4529 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004530
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004531 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4532 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4533 }
4534
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004535 DECLARE_INSTRUCTION(ArraySet);
4536
4537 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004538 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004539 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004540 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004541 // Cached information for the reference_type_info_ so that codegen
4542 // does not need to inspect the static type.
4543 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004544
4545 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4546};
4547
4548class HArrayLength : public HExpression<1> {
4549 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004550 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004551 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004552 // Note that arrays do not change length, so the instruction does not
4553 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004554 SetRawInputAt(0, array);
4555 }
4556
Calin Juravle77520bc2015-01-12 18:45:46 +00004557 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004558 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004559 return true;
4560 }
Calin Juravle641547a2015-04-21 22:08:51 +01004561 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4562 return obj == InputAt(0);
4563 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004564
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004565 DECLARE_INSTRUCTION(ArrayLength);
4566
4567 private:
4568 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4569};
4570
4571class HBoundsCheck : public HExpression<2> {
4572 public:
4573 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004574 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004575 DCHECK(index->GetType() == Primitive::kPrimInt);
4576 SetRawInputAt(0, index);
4577 SetRawInputAt(1, length);
4578 }
4579
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004580 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004581 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004582 return true;
4583 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004584
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004585 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004586
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004587 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004588
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589
4590 DECLARE_INSTRUCTION(BoundsCheck);
4591
4592 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004593 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4594};
4595
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004596/**
4597 * Some DEX instructions are folded into multiple HInstructions that need
4598 * to stay live until the last HInstruction. This class
4599 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004600 * HInstruction stays live. `index` represents the stack location index of the
4601 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004602 */
4603class HTemporary : public HTemplateInstruction<0> {
4604 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004605 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4606 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607
4608 size_t GetIndex() const { return index_; }
4609
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004610 Primitive::Type GetType() const OVERRIDE {
4611 // The previous instruction is the one that will be stored in the temporary location.
4612 DCHECK(GetPrevious() != nullptr);
4613 return GetPrevious()->GetType();
4614 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004615
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004616 DECLARE_INSTRUCTION(Temporary);
4617
4618 private:
4619 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004620 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4621};
4622
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004623class HSuspendCheck : public HTemplateInstruction<0> {
4624 public:
4625 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004626 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004627
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004628 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004629 return true;
4630 }
4631
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004632 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4633 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004634
4635 DECLARE_INSTRUCTION(SuspendCheck);
4636
4637 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004638 // Only used for code generation, in order to share the same slow path between back edges
4639 // of a same loop.
4640 SlowPathCode* slow_path_;
4641
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004642 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4643};
4644
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004645/**
4646 * Instruction to load a Class object.
4647 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004648class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004649 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004650 HLoadClass(HCurrentMethod* current_method,
4651 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004652 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004653 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004654 uint32_t dex_pc,
4655 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004656 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004657 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004658 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004659 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004660 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004661 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004662 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004663 // Referrers class should not need access check. We never inline unverified
4664 // methods so we can't possibly end up in this situation.
4665 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004666 SetRawInputAt(0, current_method);
4667 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004668
4669 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004670
4671 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004672 // Note that we don't need to test for generate_clinit_check_.
4673 // Whether or not we need to generate the clinit check is processed in
4674 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004675 return other->AsLoadClass()->type_index_ == type_index_ &&
4676 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004677 }
4678
4679 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4680
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004681 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004682 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004683 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004684
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004685 bool NeedsEnvironment() const OVERRIDE {
4686 // Will call runtime and load the class if the class is not loaded yet.
4687 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004688 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004689 }
4690
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004691 bool MustGenerateClinitCheck() const {
4692 return generate_clinit_check_;
4693 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004694 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4695 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004696 }
4697
4698 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004699 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4700 }
4701
4702 bool NeedsAccessCheck() const {
4703 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004704 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004705
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004706 bool CanThrow() const OVERRIDE {
4707 // May call runtime and and therefore can throw.
4708 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004709 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004710 }
4711
Calin Juravleacf735c2015-02-12 15:25:22 +00004712 ReferenceTypeInfo GetLoadedClassRTI() {
4713 return loaded_class_rti_;
4714 }
4715
4716 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4717 // Make sure we only set exact types (the loaded class should never be merged).
4718 DCHECK(rti.IsExact());
4719 loaded_class_rti_ = rti;
4720 }
4721
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004722 const DexFile& GetDexFile() { return dex_file_; }
4723
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004724 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4725
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004726 static SideEffects SideEffectsForArchRuntimeCalls() {
4727 return SideEffects::CanTriggerGC();
4728 }
4729
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004730 DECLARE_INSTRUCTION(LoadClass);
4731
4732 private:
4733 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004734 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004735 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004736 // Whether this instruction must generate the initialization check.
4737 // Used for code generation.
4738 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004739 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004740
Calin Juravleacf735c2015-02-12 15:25:22 +00004741 ReferenceTypeInfo loaded_class_rti_;
4742
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004743 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4744};
4745
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004746class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004747 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004748 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004749 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4750 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004751 SetRawInputAt(0, current_method);
4752 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004753
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004754 bool CanBeMoved() const OVERRIDE { return true; }
4755
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004756 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4757 return other->AsLoadString()->string_index_ == string_index_;
4758 }
4759
4760 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4761
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004762 uint32_t GetStringIndex() const { return string_index_; }
4763
4764 // TODO: Can we deopt or debug when we resolve a string?
4765 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004766 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004767 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004768
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004769 static SideEffects SideEffectsForArchRuntimeCalls() {
4770 return SideEffects::CanTriggerGC();
4771 }
4772
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004773 DECLARE_INSTRUCTION(LoadString);
4774
4775 private:
4776 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004777
4778 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4779};
4780
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004781/**
4782 * Performs an initialization check on its Class object input.
4783 */
4784class HClinitCheck : public HExpression<1> {
4785 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004786 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004787 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004788 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004789 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4790 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004791 SetRawInputAt(0, constant);
4792 }
4793
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004794 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004795 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004796 return true;
4797 }
4798
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004799 bool NeedsEnvironment() const OVERRIDE {
4800 // May call runtime to initialize the class.
4801 return true;
4802 }
4803
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004804
4805 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4806
4807 DECLARE_INSTRUCTION(ClinitCheck);
4808
4809 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004810 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4811};
4812
4813class HStaticFieldGet : public HExpression<1> {
4814 public:
4815 HStaticFieldGet(HInstruction* cls,
4816 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004817 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004818 bool is_volatile,
4819 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004820 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004821 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004822 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004823 : HExpression(
4824 field_type,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004825 SideEffects::FieldReadOfType(field_type, is_volatile),
4826 dex_pc),
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004827 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004828 SetRawInputAt(0, cls);
4829 }
4830
Calin Juravle52c48962014-12-16 17:02:57 +00004831
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004832 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004833
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004834 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004835 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4836 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004837 }
4838
4839 size_t ComputeHashCode() const OVERRIDE {
4840 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4841 }
4842
Calin Juravle52c48962014-12-16 17:02:57 +00004843 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004844 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4845 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004846 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004847
4848 DECLARE_INSTRUCTION(StaticFieldGet);
4849
4850 private:
4851 const FieldInfo field_info_;
4852
4853 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4854};
4855
4856class HStaticFieldSet : public HTemplateInstruction<2> {
4857 public:
4858 HStaticFieldSet(HInstruction* cls,
4859 HInstruction* value,
4860 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004861 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004862 bool is_volatile,
4863 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004864 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004865 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004866 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004867 : HTemplateInstruction(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004868 SideEffects::FieldWriteOfType(field_type, is_volatile),
4869 dex_pc),
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004870 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004871 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004872 SetRawInputAt(0, cls);
4873 SetRawInputAt(1, value);
4874 }
4875
Calin Juravle52c48962014-12-16 17:02:57 +00004876 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004877 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4878 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004879 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004880
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004881 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004882 bool GetValueCanBeNull() const { return value_can_be_null_; }
4883 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004884
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004885 DECLARE_INSTRUCTION(StaticFieldSet);
4886
4887 private:
4888 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004889 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004890
4891 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4892};
4893
Calin Juravlee460d1d2015-09-29 04:52:17 +01004894class HUnresolvedInstanceFieldGet : public HExpression<1> {
4895 public:
4896 HUnresolvedInstanceFieldGet(HInstruction* obj,
4897 Primitive::Type field_type,
4898 uint32_t field_index,
4899 uint32_t dex_pc)
4900 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4901 field_index_(field_index) {
4902 SetRawInputAt(0, obj);
4903 }
4904
4905 bool NeedsEnvironment() const OVERRIDE { return true; }
4906 bool CanThrow() const OVERRIDE { return true; }
4907
4908 Primitive::Type GetFieldType() const { return GetType(); }
4909 uint32_t GetFieldIndex() const { return field_index_; }
4910
4911 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4912
4913 private:
4914 const uint32_t field_index_;
4915
4916 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4917};
4918
4919class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4920 public:
4921 HUnresolvedInstanceFieldSet(HInstruction* obj,
4922 HInstruction* value,
4923 Primitive::Type field_type,
4924 uint32_t field_index,
4925 uint32_t dex_pc)
4926 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4927 field_type_(field_type),
4928 field_index_(field_index) {
4929 DCHECK_EQ(field_type, value->GetType());
4930 SetRawInputAt(0, obj);
4931 SetRawInputAt(1, value);
4932 }
4933
4934 bool NeedsEnvironment() const OVERRIDE { return true; }
4935 bool CanThrow() const OVERRIDE { return true; }
4936
4937 Primitive::Type GetFieldType() const { return field_type_; }
4938 uint32_t GetFieldIndex() const { return field_index_; }
4939
4940 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
4941
4942 private:
4943 const Primitive::Type field_type_;
4944 const uint32_t field_index_;
4945
4946 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
4947};
4948
4949class HUnresolvedStaticFieldGet : public HExpression<0> {
4950 public:
4951 HUnresolvedStaticFieldGet(Primitive::Type field_type,
4952 uint32_t field_index,
4953 uint32_t dex_pc)
4954 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4955 field_index_(field_index) {
4956 }
4957
4958 bool NeedsEnvironment() const OVERRIDE { return true; }
4959 bool CanThrow() const OVERRIDE { return true; }
4960
4961 Primitive::Type GetFieldType() const { return GetType(); }
4962 uint32_t GetFieldIndex() const { return field_index_; }
4963
4964 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
4965
4966 private:
4967 const uint32_t field_index_;
4968
4969 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
4970};
4971
4972class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
4973 public:
4974 HUnresolvedStaticFieldSet(HInstruction* value,
4975 Primitive::Type field_type,
4976 uint32_t field_index,
4977 uint32_t dex_pc)
4978 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4979 field_type_(field_type),
4980 field_index_(field_index) {
4981 DCHECK_EQ(field_type, value->GetType());
4982 SetRawInputAt(0, value);
4983 }
4984
4985 bool NeedsEnvironment() const OVERRIDE { return true; }
4986 bool CanThrow() const OVERRIDE { return true; }
4987
4988 Primitive::Type GetFieldType() const { return field_type_; }
4989 uint32_t GetFieldIndex() const { return field_index_; }
4990
4991 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
4992
4993 private:
4994 const Primitive::Type field_type_;
4995 const uint32_t field_index_;
4996
4997 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
4998};
4999
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005000// Implement the move-exception DEX instruction.
5001class HLoadException : public HExpression<0> {
5002 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005003 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5004 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005005
David Brazdilbbd733e2015-08-18 17:48:17 +01005006 bool CanBeNull() const OVERRIDE { return false; }
5007
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005008 DECLARE_INSTRUCTION(LoadException);
5009
5010 private:
5011 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5012};
5013
David Brazdilcb1c0552015-08-04 16:22:25 +01005014// Implicit part of move-exception which clears thread-local exception storage.
5015// Must not be removed because the runtime expects the TLS to get cleared.
5016class HClearException : public HTemplateInstruction<0> {
5017 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005018 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5019 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005020
5021 DECLARE_INSTRUCTION(ClearException);
5022
5023 private:
5024 DISALLOW_COPY_AND_ASSIGN(HClearException);
5025};
5026
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005027class HThrow : public HTemplateInstruction<1> {
5028 public:
5029 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005030 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005031 SetRawInputAt(0, exception);
5032 }
5033
5034 bool IsControlFlow() const OVERRIDE { return true; }
5035
5036 bool NeedsEnvironment() const OVERRIDE { return true; }
5037
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005038 bool CanThrow() const OVERRIDE { return true; }
5039
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005040
5041 DECLARE_INSTRUCTION(Throw);
5042
5043 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005044 DISALLOW_COPY_AND_ASSIGN(HThrow);
5045};
5046
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005047/**
5048 * Implementation strategies for the code generator of a HInstanceOf
5049 * or `HCheckCast`.
5050 */
5051enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005052 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005053 kExactCheck, // Can do a single class compare.
5054 kClassHierarchyCheck, // Can just walk the super class chain.
5055 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5056 kInterfaceCheck, // No optimization yet when checking against an interface.
5057 kArrayObjectCheck, // Can just check if the array is not primitive.
5058 kArrayCheck // No optimization yet when checking against a generic array.
5059};
5060
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005061class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005062 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005063 HInstanceOf(HInstruction* object,
5064 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005065 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005066 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005067 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005068 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005069 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005070 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005071 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005072 SetRawInputAt(0, object);
5073 SetRawInputAt(1, constant);
5074 }
5075
5076 bool CanBeMoved() const OVERRIDE { return true; }
5077
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005078 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005079 return true;
5080 }
5081
5082 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005083 return false;
5084 }
5085
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005086 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5087
5088 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005089
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005090 // Used only in code generation.
5091 bool MustDoNullCheck() const { return must_do_null_check_; }
5092 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5093
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005094 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5095 return (check_kind == TypeCheckKind::kExactCheck)
5096 ? SideEffects::None()
5097 // Mips currently does runtime calls for any other checks.
5098 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005099 }
5100
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005101 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005102
5103 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005104 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005105 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005106
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005107 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5108};
5109
Calin Juravleb1498f62015-02-16 13:13:29 +00005110class HBoundType : public HExpression<1> {
5111 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005112 // Constructs an HBoundType with the given upper_bound.
5113 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005114 HBoundType(HInstruction* input,
5115 ReferenceTypeInfo upper_bound,
5116 bool upper_can_be_null,
5117 uint32_t dex_pc = kNoDexPc)
5118 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005119 upper_bound_(upper_bound),
5120 upper_can_be_null_(upper_can_be_null),
5121 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005122 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005123 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005124 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005125 }
5126
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005127 // GetUpper* should only be used in reference type propagation.
5128 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5129 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005130
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005131 void SetCanBeNull(bool can_be_null) {
5132 DCHECK(upper_can_be_null_ || !can_be_null);
5133 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005134 }
5135
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005136 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5137
Calin Juravleb1498f62015-02-16 13:13:29 +00005138 DECLARE_INSTRUCTION(BoundType);
5139
5140 private:
5141 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005142 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5143 // It is used to bound the type in cases like:
5144 // if (x instanceof ClassX) {
5145 // // uper_bound_ will be ClassX
5146 // }
5147 const ReferenceTypeInfo upper_bound_;
5148 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5149 // is false then can_be_null_ cannot be true).
5150 const bool upper_can_be_null_;
5151 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005152
5153 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5154};
5155
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005156class HCheckCast : public HTemplateInstruction<2> {
5157 public:
5158 HCheckCast(HInstruction* object,
5159 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005160 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005161 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005162 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005163 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005164 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005165 SetRawInputAt(0, object);
5166 SetRawInputAt(1, constant);
5167 }
5168
5169 bool CanBeMoved() const OVERRIDE { return true; }
5170
5171 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5172 return true;
5173 }
5174
5175 bool NeedsEnvironment() const OVERRIDE {
5176 // Instruction may throw a CheckCastError.
5177 return true;
5178 }
5179
5180 bool CanThrow() const OVERRIDE { return true; }
5181
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005182 bool MustDoNullCheck() const { return must_do_null_check_; }
5183 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005184 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005185
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005186 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005187
5188 DECLARE_INSTRUCTION(CheckCast);
5189
5190 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005191 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005192 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005193
5194 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005195};
5196
Calin Juravle27df7582015-04-17 19:12:31 +01005197class HMemoryBarrier : public HTemplateInstruction<0> {
5198 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005199 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005200 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005201 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005202 barrier_kind_(barrier_kind) {}
5203
5204 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5205
5206 DECLARE_INSTRUCTION(MemoryBarrier);
5207
5208 private:
5209 const MemBarrierKind barrier_kind_;
5210
5211 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5212};
5213
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005214class HMonitorOperation : public HTemplateInstruction<1> {
5215 public:
5216 enum OperationKind {
5217 kEnter,
5218 kExit,
5219 };
5220
5221 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005222 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005223 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5224 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005225 SetRawInputAt(0, object);
5226 }
5227
5228 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005229 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5230
5231 bool CanThrow() const OVERRIDE {
5232 // Verifier guarantees that monitor-exit cannot throw.
5233 // This is important because it allows the HGraphBuilder to remove
5234 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5235 return IsEnter();
5236 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005237
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005238
5239 bool IsEnter() const { return kind_ == kEnter; }
5240
5241 DECLARE_INSTRUCTION(MonitorOperation);
5242
Calin Juravle52c48962014-12-16 17:02:57 +00005243 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005244 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005245
5246 private:
5247 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5248};
5249
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005250/**
5251 * A HInstruction used as a marker for the replacement of new + <init>
5252 * of a String to a call to a StringFactory. Only baseline will see
5253 * the node at code generation, where it will be be treated as null.
5254 * When compiling non-baseline, `HFakeString` instructions are being removed
5255 * in the instruction simplifier.
5256 */
5257class HFakeString : public HTemplateInstruction<0> {
5258 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005259 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5260 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005261
5262 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5263
5264 DECLARE_INSTRUCTION(FakeString);
5265
5266 private:
5267 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5268};
5269
Vladimir Markof9f64412015-09-02 14:05:49 +01005270class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005271 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005272 MoveOperands(Location source,
5273 Location destination,
5274 Primitive::Type type,
5275 HInstruction* instruction)
5276 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005277
5278 Location GetSource() const { return source_; }
5279 Location GetDestination() const { return destination_; }
5280
5281 void SetSource(Location value) { source_ = value; }
5282 void SetDestination(Location value) { destination_ = value; }
5283
5284 // The parallel move resolver marks moves as "in-progress" by clearing the
5285 // destination (but not the source).
5286 Location MarkPending() {
5287 DCHECK(!IsPending());
5288 Location dest = destination_;
5289 destination_ = Location::NoLocation();
5290 return dest;
5291 }
5292
5293 void ClearPending(Location dest) {
5294 DCHECK(IsPending());
5295 destination_ = dest;
5296 }
5297
5298 bool IsPending() const {
5299 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5300 return destination_.IsInvalid() && !source_.IsInvalid();
5301 }
5302
5303 // True if this blocks a move from the given location.
5304 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005305 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005306 }
5307
5308 // A move is redundant if it's been eliminated, if its source and
5309 // destination are the same, or if its destination is unneeded.
5310 bool IsRedundant() const {
5311 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5312 }
5313
5314 // We clear both operands to indicate move that's been eliminated.
5315 void Eliminate() {
5316 source_ = destination_ = Location::NoLocation();
5317 }
5318
5319 bool IsEliminated() const {
5320 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5321 return source_.IsInvalid();
5322 }
5323
Alexey Frunze4dda3372015-06-01 18:31:49 -07005324 Primitive::Type GetType() const { return type_; }
5325
Nicolas Geoffray90218252015-04-15 11:56:51 +01005326 bool Is64BitMove() const {
5327 return Primitive::Is64BitType(type_);
5328 }
5329
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005330 HInstruction* GetInstruction() const { return instruction_; }
5331
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005332 private:
5333 Location source_;
5334 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005335 // The type this move is for.
5336 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005337 // The instruction this move is assocatied with. Null when this move is
5338 // for moving an input in the expected locations of user (including a phi user).
5339 // This is only used in debug mode, to ensure we do not connect interval siblings
5340 // in the same parallel move.
5341 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005342};
5343
5344static constexpr size_t kDefaultNumberOfMoves = 4;
5345
5346class HParallelMove : public HTemplateInstruction<0> {
5347 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005348 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005349 : HTemplateInstruction(SideEffects::None(), dex_pc),
5350 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5351 moves_.reserve(kDefaultNumberOfMoves);
5352 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005353
Nicolas Geoffray90218252015-04-15 11:56:51 +01005354 void AddMove(Location source,
5355 Location destination,
5356 Primitive::Type type,
5357 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005358 DCHECK(source.IsValid());
5359 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005360 if (kIsDebugBuild) {
5361 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005362 for (const MoveOperands& move : moves_) {
5363 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005364 // Special case the situation where the move is for the spill slot
5365 // of the instruction.
5366 if ((GetPrevious() == instruction)
5367 || ((GetPrevious() == nullptr)
5368 && instruction->IsPhi()
5369 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005370 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005371 << "Doing parallel moves for the same instruction.";
5372 } else {
5373 DCHECK(false) << "Doing parallel moves for the same instruction.";
5374 }
5375 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005376 }
5377 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005378 for (const MoveOperands& move : moves_) {
5379 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005380 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005381 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005382 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005383 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005384 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005385 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005386 }
5387
Vladimir Marko225b6462015-09-28 12:17:40 +01005388 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005389 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005390 }
5391
Vladimir Marko225b6462015-09-28 12:17:40 +01005392 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005393
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005394 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005395
5396 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005397 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005398
5399 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5400};
5401
Mark Mendell0616ae02015-04-17 12:49:27 -04005402} // namespace art
5403
5404#ifdef ART_ENABLE_CODEGEN_x86
5405#include "nodes_x86.h"
5406#endif
5407
5408namespace art {
5409
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005410class HGraphVisitor : public ValueObject {
5411 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005412 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5413 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005414
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005415 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005416 virtual void VisitBasicBlock(HBasicBlock* block);
5417
Roland Levillain633021e2014-10-01 14:12:25 +01005418 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005419 void VisitInsertionOrder();
5420
Roland Levillain633021e2014-10-01 14:12:25 +01005421 // Visit the graph following dominator tree reverse post-order.
5422 void VisitReversePostOrder();
5423
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005424 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005425
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005426 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005427#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005428 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5429
5430 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5431
5432#undef DECLARE_VISIT_INSTRUCTION
5433
5434 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005435 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005436
5437 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5438};
5439
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005440class HGraphDelegateVisitor : public HGraphVisitor {
5441 public:
5442 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5443 virtual ~HGraphDelegateVisitor() {}
5444
5445 // Visit functions that delegate to to super class.
5446#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005447 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005448
5449 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5450
5451#undef DECLARE_VISIT_INSTRUCTION
5452
5453 private:
5454 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5455};
5456
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005457class HInsertionOrderIterator : public ValueObject {
5458 public:
5459 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5460
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005461 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005462 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005463 void Advance() { ++index_; }
5464
5465 private:
5466 const HGraph& graph_;
5467 size_t index_;
5468
5469 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5470};
5471
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005472class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005473 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005474 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5475 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005476 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005477 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005478
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005479 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5480 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005481 void Advance() { ++index_; }
5482
5483 private:
5484 const HGraph& graph_;
5485 size_t index_;
5486
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005487 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005488};
5489
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005490class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005491 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005492 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005493 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005494 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005495 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005496 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005497
5498 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005499 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005500 void Advance() { --index_; }
5501
5502 private:
5503 const HGraph& graph_;
5504 size_t index_;
5505
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005506 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005507};
5508
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005509class HLinearPostOrderIterator : public ValueObject {
5510 public:
5511 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005512 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005513
5514 bool Done() const { return index_ == 0; }
5515
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005516 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005517
5518 void Advance() {
5519 --index_;
5520 DCHECK_GE(index_, 0U);
5521 }
5522
5523 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005524 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005525 size_t index_;
5526
5527 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5528};
5529
5530class HLinearOrderIterator : public ValueObject {
5531 public:
5532 explicit HLinearOrderIterator(const HGraph& graph)
5533 : order_(graph.GetLinearOrder()), index_(0) {}
5534
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005535 bool Done() const { return index_ == order_.size(); }
5536 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005537 void Advance() { ++index_; }
5538
5539 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005540 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005541 size_t index_;
5542
5543 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5544};
5545
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005546// Iterator over the blocks that art part of the loop. Includes blocks part
5547// of an inner loop. The order in which the blocks are iterated is on their
5548// block id.
5549class HBlocksInLoopIterator : public ValueObject {
5550 public:
5551 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5552 : blocks_in_loop_(info.GetBlocks()),
5553 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5554 index_(0) {
5555 if (!blocks_in_loop_.IsBitSet(index_)) {
5556 Advance();
5557 }
5558 }
5559
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005560 bool Done() const { return index_ == blocks_.size(); }
5561 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005562 void Advance() {
5563 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005564 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005565 if (blocks_in_loop_.IsBitSet(index_)) {
5566 break;
5567 }
5568 }
5569 }
5570
5571 private:
5572 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005573 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005574 size_t index_;
5575
5576 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5577};
5578
Mingyao Yang3584bce2015-05-19 16:01:59 -07005579// Iterator over the blocks that art part of the loop. Includes blocks part
5580// of an inner loop. The order in which the blocks are iterated is reverse
5581// post order.
5582class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5583 public:
5584 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5585 : blocks_in_loop_(info.GetBlocks()),
5586 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5587 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005588 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005589 Advance();
5590 }
5591 }
5592
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005593 bool Done() const { return index_ == blocks_.size(); }
5594 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005595 void Advance() {
5596 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005597 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5598 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005599 break;
5600 }
5601 }
5602 }
5603
5604 private:
5605 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005606 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005607 size_t index_;
5608
5609 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5610};
5611
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005612inline int64_t Int64FromConstant(HConstant* constant) {
5613 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5614 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5615 : constant->AsLongConstant()->GetValue();
5616}
5617
Vladimir Marko58155012015-08-19 12:49:41 +00005618inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5619 // For the purposes of the compiler, the dex files must actually be the same object
5620 // if we want to safely treat them as the same. This is especially important for JIT
5621 // as custom class loaders can open the same underlying file (or memory) multiple
5622 // times and provide different class resolution but no two class loaders should ever
5623 // use the same DexFile object - doing so is an unsupported hack that can lead to
5624 // all sorts of weird failures.
5625 return &lhs == &rhs;
5626}
5627
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005628} // namespace art
5629
5630#endif // ART_COMPILER_OPTIMIZING_NODES_H_