blob: 490bfc8057c48cbd998c534dc909c829397b8c84 [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
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001082#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001083#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001084#else
1085#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1086 M(Arm64IntermediateAddress, Instruction)
1087#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001088
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001089#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1090
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001091#ifndef ART_ENABLE_CODEGEN_x86
1092#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1093#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001094#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1095 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001096 M(X86LoadFromConstantTable, Instruction) \
1097 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001098#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001099
1100#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1101
1102#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1103 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1104 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1105 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001106 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001107 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1108 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1109
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001110#define FOR_EACH_INSTRUCTION(M) \
1111 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1112 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001113 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001114 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001115 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001116
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001117#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001118FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1119#undef FORWARD_DECLARATION
1120
Roland Levillainccc07a92014-09-16 14:48:16 +01001121#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001122 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1123 const char* DebugName() const OVERRIDE { return #type; } \
1124 const H##type* As##type() const OVERRIDE { return this; } \
1125 H##type* As##type() OVERRIDE { return this; } \
1126 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001127 return other->Is##type(); \
1128 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001129 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001130
David Brazdiled596192015-01-23 10:39:45 +00001131template <typename T> class HUseList;
1132
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001133template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001134class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001135 public:
David Brazdiled596192015-01-23 10:39:45 +00001136 HUseListNode* GetPrevious() const { return prev_; }
1137 HUseListNode* GetNext() const { return next_; }
1138 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001139 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001140 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001141
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001142 private:
David Brazdiled596192015-01-23 10:39:45 +00001143 HUseListNode(T user, size_t index)
1144 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1145
1146 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001147 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001148 HUseListNode<T>* prev_;
1149 HUseListNode<T>* next_;
1150
1151 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001152
1153 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1154};
1155
David Brazdiled596192015-01-23 10:39:45 +00001156template <typename T>
1157class HUseList : public ValueObject {
1158 public:
1159 HUseList() : first_(nullptr) {}
1160
1161 void Clear() {
1162 first_ = nullptr;
1163 }
1164
1165 // Adds a new entry at the beginning of the use list and returns
1166 // the newly created node.
1167 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001168 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001169 if (IsEmpty()) {
1170 first_ = new_node;
1171 } else {
1172 first_->prev_ = new_node;
1173 new_node->next_ = first_;
1174 first_ = new_node;
1175 }
1176 return new_node;
1177 }
1178
1179 HUseListNode<T>* GetFirst() const {
1180 return first_;
1181 }
1182
1183 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001184 DCHECK(node != nullptr);
1185 DCHECK(Contains(node));
1186
David Brazdiled596192015-01-23 10:39:45 +00001187 if (node->prev_ != nullptr) {
1188 node->prev_->next_ = node->next_;
1189 }
1190 if (node->next_ != nullptr) {
1191 node->next_->prev_ = node->prev_;
1192 }
1193 if (node == first_) {
1194 first_ = node->next_;
1195 }
1196 }
1197
David Brazdil1abb4192015-02-17 18:33:36 +00001198 bool Contains(const HUseListNode<T>* node) const {
1199 if (node == nullptr) {
1200 return false;
1201 }
1202 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1203 if (current == node) {
1204 return true;
1205 }
1206 }
1207 return false;
1208 }
1209
David Brazdiled596192015-01-23 10:39:45 +00001210 bool IsEmpty() const {
1211 return first_ == nullptr;
1212 }
1213
1214 bool HasOnlyOneUse() const {
1215 return first_ != nullptr && first_->next_ == nullptr;
1216 }
1217
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001218 size_t SizeSlow() const {
1219 size_t count = 0;
1220 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1221 ++count;
1222 }
1223 return count;
1224 }
1225
David Brazdiled596192015-01-23 10:39:45 +00001226 private:
1227 HUseListNode<T>* first_;
1228};
1229
1230template<typename T>
1231class HUseIterator : public ValueObject {
1232 public:
1233 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1234
1235 bool Done() const { return current_ == nullptr; }
1236
1237 void Advance() {
1238 DCHECK(!Done());
1239 current_ = current_->GetNext();
1240 }
1241
1242 HUseListNode<T>* Current() const {
1243 DCHECK(!Done());
1244 return current_;
1245 }
1246
1247 private:
1248 HUseListNode<T>* current_;
1249
1250 friend class HValue;
1251};
1252
David Brazdil1abb4192015-02-17 18:33:36 +00001253// This class is used by HEnvironment and HInstruction classes to record the
1254// instructions they use and pointers to the corresponding HUseListNodes kept
1255// by the used instructions.
1256template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001257class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001258 public:
1259 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1260 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1261
1262 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1263 : instruction_(old_record.instruction_), use_node_(use_node) {
1264 DCHECK(instruction_ != nullptr);
1265 DCHECK(use_node_ != nullptr);
1266 DCHECK(old_record.use_node_ == nullptr);
1267 }
1268
1269 HInstruction* GetInstruction() const { return instruction_; }
1270 HUseListNode<T>* GetUseNode() const { return use_node_; }
1271
1272 private:
1273 // Instruction used by the user.
1274 HInstruction* instruction_;
1275
1276 // Corresponding entry in the use list kept by 'instruction_'.
1277 HUseListNode<T>* use_node_;
1278};
1279
Aart Bik854a02b2015-07-14 16:07:00 -07001280/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001281 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001282 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001283 * For write/read dependences on fields/arrays, the dependence analysis uses
1284 * type disambiguation (e.g. a float field write cannot modify the value of an
1285 * integer field read) and the access type (e.g. a reference array write cannot
1286 * modify the value of a reference field read [although it may modify the
1287 * reference fetch prior to reading the field, which is represented by its own
1288 * write/read dependence]). The analysis makes conservative points-to
1289 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1290 * the same, and any reference read depends on any reference read without
1291 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001292 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001293 * The internal representation uses 38-bit and is described in the table below.
1294 * The first line indicates the side effect, and for field/array accesses the
1295 * second line indicates the type of the access (in the order of the
1296 * Primitive::Type enum).
1297 * The two numbered lines below indicate the bit position in the bitfield (read
1298 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001299 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001300 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1301 * +-------------+---------+---------+--------------+---------+---------+
1302 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1303 * | 3 |333333322|222222221| 1 |111111110|000000000|
1304 * | 7 |654321098|765432109| 8 |765432109|876543210|
1305 *
1306 * Note that, to ease the implementation, 'changes' bits are least significant
1307 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001308 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001309class SideEffects : public ValueObject {
1310 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001311 SideEffects() : flags_(0) {}
1312
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001313 static SideEffects None() {
1314 return SideEffects(0);
1315 }
1316
1317 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001318 return SideEffects(kAllChangeBits | kAllDependOnBits);
1319 }
1320
1321 static SideEffects AllChanges() {
1322 return SideEffects(kAllChangeBits);
1323 }
1324
1325 static SideEffects AllDependencies() {
1326 return SideEffects(kAllDependOnBits);
1327 }
1328
1329 static SideEffects AllExceptGCDependency() {
1330 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1331 }
1332
1333 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001334 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001335 }
1336
Aart Bik34c3ba92015-07-20 14:08:59 -07001337 static SideEffects AllWrites() {
1338 return SideEffects(kAllWrites);
1339 }
1340
1341 static SideEffects AllReads() {
1342 return SideEffects(kAllReads);
1343 }
1344
1345 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1346 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001347 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001348 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001349 }
1350
Aart Bik854a02b2015-07-14 16:07:00 -07001351 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1352 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001353 }
1354
Aart Bik34c3ba92015-07-20 14:08:59 -07001355 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1356 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001357 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001358 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001359 }
1360
1361 static SideEffects ArrayReadOfType(Primitive::Type type) {
1362 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1363 }
1364
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001365 static SideEffects CanTriggerGC() {
1366 return SideEffects(1ULL << kCanTriggerGCBit);
1367 }
1368
1369 static SideEffects DependsOnGC() {
1370 return SideEffects(1ULL << kDependsOnGCBit);
1371 }
1372
Aart Bik854a02b2015-07-14 16:07:00 -07001373 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001374 SideEffects Union(SideEffects other) const {
1375 return SideEffects(flags_ | other.flags_);
1376 }
1377
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001378 SideEffects Exclusion(SideEffects other) const {
1379 return SideEffects(flags_ & ~other.flags_);
1380 }
1381
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001382 void Add(SideEffects other) {
1383 flags_ |= other.flags_;
1384 }
1385
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001386 bool Includes(SideEffects other) const {
1387 return (other.flags_ & flags_) == other.flags_;
1388 }
1389
1390 bool HasSideEffects() const {
1391 return (flags_ & kAllChangeBits);
1392 }
1393
1394 bool HasDependencies() const {
1395 return (flags_ & kAllDependOnBits);
1396 }
1397
1398 // Returns true if there are no side effects or dependencies.
1399 bool DoesNothing() const {
1400 return flags_ == 0;
1401 }
1402
Aart Bik854a02b2015-07-14 16:07:00 -07001403 // Returns true if something is written.
1404 bool DoesAnyWrite() const {
1405 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001406 }
1407
Aart Bik854a02b2015-07-14 16:07:00 -07001408 // Returns true if something is read.
1409 bool DoesAnyRead() const {
1410 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001411 }
1412
Aart Bik854a02b2015-07-14 16:07:00 -07001413 // Returns true if potentially everything is written and read
1414 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001415 bool DoesAllReadWrite() const {
1416 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1417 }
1418
Aart Bik854a02b2015-07-14 16:07:00 -07001419 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001420 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001421 }
1422
1423 // Returns true if this may read something written by other.
1424 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001425 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1426 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001427 }
1428
1429 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001430 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001431 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001432 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001433 for (int s = kLastBit; s >= 0; s--) {
1434 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1435 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1436 // This is a bit for the GC side effect.
1437 if (current_bit_is_set) {
1438 flags += "GC";
1439 }
Aart Bik854a02b2015-07-14 16:07:00 -07001440 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001441 } else {
1442 // This is a bit for the array/field analysis.
1443 // The underscore character stands for the 'can trigger GC' bit.
1444 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1445 if (current_bit_is_set) {
1446 flags += kDebug[s];
1447 }
1448 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1449 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1450 flags += "|";
1451 }
1452 }
Aart Bik854a02b2015-07-14 16:07:00 -07001453 }
1454 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001455 }
1456
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001457 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001458
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001459 private:
1460 static constexpr int kFieldArrayAnalysisBits = 9;
1461
1462 static constexpr int kFieldWriteOffset = 0;
1463 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1464 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1465 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1466
1467 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1468
1469 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1470 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1471 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1472 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1473
1474 static constexpr int kLastBit = kDependsOnGCBit;
1475 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1476
1477 // Aliases.
1478
1479 static_assert(kChangeBits == kDependOnBits,
1480 "the 'change' bits should match the 'depend on' bits.");
1481
1482 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1483 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1484 static constexpr uint64_t kAllWrites =
1485 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1486 static constexpr uint64_t kAllReads =
1487 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001488
Aart Bik854a02b2015-07-14 16:07:00 -07001489 // Work around the fact that HIR aliases I/F and J/D.
1490 // TODO: remove this interceptor once HIR types are clean
1491 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1492 switch (type) {
1493 case Primitive::kPrimInt:
1494 case Primitive::kPrimFloat:
1495 return TypeFlag(Primitive::kPrimInt, offset) |
1496 TypeFlag(Primitive::kPrimFloat, offset);
1497 case Primitive::kPrimLong:
1498 case Primitive::kPrimDouble:
1499 return TypeFlag(Primitive::kPrimLong, offset) |
1500 TypeFlag(Primitive::kPrimDouble, offset);
1501 default:
1502 return TypeFlag(type, offset);
1503 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001504 }
1505
Aart Bik854a02b2015-07-14 16:07:00 -07001506 // Translates type to bit flag.
1507 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1508 CHECK_NE(type, Primitive::kPrimVoid);
1509 const uint64_t one = 1;
1510 const int shift = type; // 0-based consecutive enum
1511 DCHECK_LE(kFieldWriteOffset, shift);
1512 DCHECK_LT(shift, kArrayWriteOffset);
1513 return one << (type + offset);
1514 }
1515
1516 // Private constructor on direct flags value.
1517 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1518
1519 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001520};
1521
David Brazdiled596192015-01-23 10:39:45 +00001522// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001523class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001524 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001525 HEnvironment(ArenaAllocator* arena,
1526 size_t number_of_vregs,
1527 const DexFile& dex_file,
1528 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001529 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001530 InvokeType invoke_type,
1531 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001532 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1533 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001534 parent_(nullptr),
1535 dex_file_(dex_file),
1536 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001537 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001538 invoke_type_(invoke_type),
1539 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001540 }
1541
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001542 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001543 : HEnvironment(arena,
1544 to_copy.Size(),
1545 to_copy.GetDexFile(),
1546 to_copy.GetMethodIdx(),
1547 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001548 to_copy.GetInvokeType(),
1549 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001550
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001551 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001552 if (parent_ != nullptr) {
1553 parent_->SetAndCopyParentChain(allocator, parent);
1554 } else {
1555 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1556 parent_->CopyFrom(parent);
1557 if (parent->GetParent() != nullptr) {
1558 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1559 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001560 }
David Brazdiled596192015-01-23 10:39:45 +00001561 }
1562
Vladimir Marko71bf8092015-09-15 15:33:14 +01001563 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001564 void CopyFrom(HEnvironment* environment);
1565
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001566 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1567 // input to the loop phi instead. This is for inserting instructions that
1568 // require an environment (like HDeoptimization) in the loop pre-header.
1569 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001570
1571 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001572 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001573 }
1574
1575 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001576 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001577 }
1578
David Brazdil1abb4192015-02-17 18:33:36 +00001579 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001580
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001581 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001582
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001583 HEnvironment* GetParent() const { return parent_; }
1584
1585 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001586 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001587 }
1588
1589 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001590 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001591 }
1592
1593 uint32_t GetDexPc() const {
1594 return dex_pc_;
1595 }
1596
1597 uint32_t GetMethodIdx() const {
1598 return method_idx_;
1599 }
1600
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001601 InvokeType GetInvokeType() const {
1602 return invoke_type_;
1603 }
1604
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001605 const DexFile& GetDexFile() const {
1606 return dex_file_;
1607 }
1608
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001609 HInstruction* GetHolder() const {
1610 return holder_;
1611 }
1612
David Brazdiled596192015-01-23 10:39:45 +00001613 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001614 // Record instructions' use entries of this environment for constant-time removal.
1615 // It should only be called by HInstruction when a new environment use is added.
1616 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1617 DCHECK(env_use->GetUser() == this);
1618 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001619 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001620 }
David Brazdiled596192015-01-23 10:39:45 +00001621
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001622 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1623 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001624 HEnvironment* parent_;
1625 const DexFile& dex_file_;
1626 const uint32_t method_idx_;
1627 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001628 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001629
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001630 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001631 HInstruction* const holder_;
1632
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001633 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001634
1635 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1636};
1637
Calin Juravleacf735c2015-02-12 15:25:22 +00001638class ReferenceTypeInfo : ValueObject {
1639 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001640 typedef Handle<mirror::Class> TypeHandle;
1641
Calin Juravle2e768302015-07-28 14:41:11 +00001642 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1643 // The constructor will check that the type_handle is valid.
1644 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001645 }
1646
Calin Juravle2e768302015-07-28 14:41:11 +00001647 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1648
1649 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1650 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001651 }
1652
Calin Juravle2e768302015-07-28 14:41:11 +00001653 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1654 return IsValidHandle(type_handle_);
1655 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001656
Calin Juravleacf735c2015-02-12 15:25:22 +00001657 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001658
1659 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1660 DCHECK(IsValid());
1661 return GetTypeHandle()->IsObjectClass();
1662 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001663
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001664 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001665 DCHECK(IsValid());
1666 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001667 }
1668
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001669 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1670 DCHECK(IsValid());
1671 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1672 }
1673
Mathieu Chartier90443472015-07-16 20:32:27 -07001674 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001675 DCHECK(IsValid());
1676 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001677 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001678
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001679 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001680 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001681 return GetTypeHandle()->IsArrayClass();
1682 }
1683
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001684 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1685 DCHECK(IsValid());
1686 return GetTypeHandle()->IsPrimitiveArray();
1687 }
1688
1689 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1690 DCHECK(IsValid());
1691 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1692 }
1693
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001694 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001695 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001696 if (!IsExact()) return false;
1697 if (!IsArrayClass()) return false;
1698 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1699 }
1700
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001701 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1702 DCHECK(IsValid());
1703 if (!IsExact()) return false;
1704 if (!IsArrayClass()) return false;
1705 if (!rti.IsArrayClass()) return false;
1706 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1707 rti.GetTypeHandle()->GetComponentType());
1708 }
1709
Calin Juravleacf735c2015-02-12 15:25:22 +00001710 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1711
Mathieu Chartier90443472015-07-16 20:32:27 -07001712 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001713 DCHECK(IsValid());
1714 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001715 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1716 }
1717
1718 // Returns true if the type information provide the same amount of details.
1719 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001720 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001721 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001722 if (!IsValid() && !rti.IsValid()) {
1723 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001724 return true;
1725 }
Calin Juravle2e768302015-07-28 14:41:11 +00001726 if (!IsValid() || !rti.IsValid()) {
1727 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001728 return false;
1729 }
Calin Juravle2e768302015-07-28 14:41:11 +00001730 return IsExact() == rti.IsExact()
1731 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001732 }
1733
1734 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001735 ReferenceTypeInfo();
1736 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001737
Calin Juravleacf735c2015-02-12 15:25:22 +00001738 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001739 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001740 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001741 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001742 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001743};
1744
1745std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1746
Vladimir Markof9f64412015-09-02 14:05:49 +01001747class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001748 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001749 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001750 : previous_(nullptr),
1751 next_(nullptr),
1752 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001753 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001754 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001755 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001756 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001757 locations_(nullptr),
1758 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001759 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001760 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001761 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001762
Dave Allison20dfc792014-06-16 20:44:29 -07001763 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001764
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001765#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001766 enum InstructionKind {
1767 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1768 };
1769#undef DECLARE_KIND
1770
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001771 HInstruction* GetNext() const { return next_; }
1772 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001773
Calin Juravle77520bc2015-01-12 18:45:46 +00001774 HInstruction* GetNextDisregardingMoves() const;
1775 HInstruction* GetPreviousDisregardingMoves() const;
1776
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001777 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001778 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001779 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001780 bool IsInBlock() const { return block_ != nullptr; }
1781 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001782 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001783
Roland Levillain6b879dd2014-09-22 17:13:44 +01001784 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001785 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001786
1787 virtual void Accept(HGraphVisitor* visitor) = 0;
1788 virtual const char* DebugName() const = 0;
1789
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001790 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001791 void SetRawInputAt(size_t index, HInstruction* input) {
1792 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1793 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001794
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001795 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001796
1797 uint32_t GetDexPc() const { return dex_pc_; }
1798
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001799 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001800
Roland Levillaine161a2a2014-10-03 12:45:18 +01001801 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001802 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001803
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001804 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001805 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001806
Calin Juravle10e244f2015-01-26 18:54:32 +00001807 // Does not apply for all instructions, but having this at top level greatly
1808 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001809 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001810 virtual bool CanBeNull() const {
1811 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1812 return true;
1813 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001814
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001815 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001816 return false;
1817 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001818
Calin Juravle2e768302015-07-28 14:41:11 +00001819 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001820
Calin Juravle61d544b2015-02-23 16:46:57 +00001821 ReferenceTypeInfo GetReferenceTypeInfo() const {
1822 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1823 return reference_type_info_;
1824 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001825
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001826 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001827 DCHECK(user != nullptr);
1828 HUseListNode<HInstruction*>* use =
1829 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1830 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001831 }
1832
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001833 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001834 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001835 HUseListNode<HEnvironment*>* env_use =
1836 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1837 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001838 }
1839
David Brazdil1abb4192015-02-17 18:33:36 +00001840 void RemoveAsUserOfInput(size_t input) {
1841 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1842 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1843 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001844
David Brazdil1abb4192015-02-17 18:33:36 +00001845 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1846 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001847
David Brazdiled596192015-01-23 10:39:45 +00001848 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1849 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001850 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001851 bool HasOnlyOneNonEnvironmentUse() const {
1852 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1853 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001854
Roland Levillain6c82d402014-10-13 16:10:27 +01001855 // Does this instruction strictly dominate `other_instruction`?
1856 // Returns false if this instruction and `other_instruction` are the same.
1857 // Aborts if this instruction and `other_instruction` are both phis.
1858 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001859
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001860 int GetId() const { return id_; }
1861 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001862
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001863 int GetSsaIndex() const { return ssa_index_; }
1864 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1865 bool HasSsaIndex() const { return ssa_index_ != -1; }
1866
1867 bool HasEnvironment() const { return environment_ != nullptr; }
1868 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001869 // Set the `environment_` field. Raw because this method does not
1870 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001871 void SetRawEnvironment(HEnvironment* environment) {
1872 DCHECK(environment_ == nullptr);
1873 DCHECK_EQ(environment->GetHolder(), this);
1874 environment_ = environment;
1875 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001876
1877 // Set the environment of this instruction, copying it from `environment`. While
1878 // copying, the uses lists are being updated.
1879 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001880 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001881 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001882 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001883 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001884 if (environment->GetParent() != nullptr) {
1885 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1886 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001887 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001888
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001889 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1890 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001891 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001892 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001893 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001894 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001895 if (environment->GetParent() != nullptr) {
1896 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1897 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001898 }
1899
Nicolas Geoffray39468442014-09-02 15:17:15 +01001900 // Returns the number of entries in the environment. Typically, that is the
1901 // number of dex registers in a method. It could be more in case of inlining.
1902 size_t EnvironmentSize() const;
1903
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001904 LocationSummary* GetLocations() const { return locations_; }
1905 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001906
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001907 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001908 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001909
Alexandre Rames188d4312015-04-09 18:30:21 +01001910 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1911 // uses of this instruction by `other` are *not* updated.
1912 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1913 ReplaceWith(other);
1914 other->ReplaceInput(this, use_index);
1915 }
1916
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001917 // Move `this` instruction before `cursor`.
1918 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001919
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001920#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001921 bool Is##type() const { return (As##type() != nullptr); } \
1922 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001923 virtual H##type* As##type() { return nullptr; }
1924
1925 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1926#undef INSTRUCTION_TYPE_CHECK
1927
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001928 // Returns whether the instruction can be moved within the graph.
1929 virtual bool CanBeMoved() const { return false; }
1930
1931 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001932 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001933 return false;
1934 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001935
1936 // Returns whether any data encoded in the two instructions is equal.
1937 // This method does not look at the inputs. Both instructions must be
1938 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001939 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001940 return false;
1941 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001942
1943 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001944 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001945 // 2) Their inputs are identical.
1946 bool Equals(HInstruction* other) const;
1947
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001948 virtual InstructionKind GetKind() const = 0;
1949
1950 virtual size_t ComputeHashCode() const {
1951 size_t result = GetKind();
1952 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1953 result = (result * 31) + InputAt(i)->GetId();
1954 }
1955 return result;
1956 }
1957
1958 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001959 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001960
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001961 size_t GetLifetimePosition() const { return lifetime_position_; }
1962 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1963 LiveInterval* GetLiveInterval() const { return live_interval_; }
1964 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1965 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1966
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001967 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1968
1969 // Returns whether the code generation of the instruction will require to have access
1970 // to the current method. Such instructions are:
1971 // (1): Instructions that require an environment, as calling the runtime requires
1972 // to walk the stack and have the current method stored at a specific stack address.
1973 // (2): Object literals like classes and strings, that are loaded from the dex cache
1974 // fields of the current method.
1975 bool NeedsCurrentMethod() const {
1976 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1977 }
1978
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001979 virtual bool NeedsDexCache() const { return false; }
1980
Mark Mendellc4701932015-04-10 13:18:51 -04001981 // Does this instruction have any use in an environment before
1982 // control flow hits 'other'?
1983 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1984
1985 // Remove all references to environment uses of this instruction.
1986 // The caller must ensure that this is safe to do.
1987 void RemoveEnvironmentUsers();
1988
David Brazdil1abb4192015-02-17 18:33:36 +00001989 protected:
1990 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1991 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1992
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001993 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001994 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1995
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001996 HInstruction* previous_;
1997 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001998 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001999 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002000
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002001 // An instruction gets an id when it is added to the graph.
2002 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002003 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002004 int id_;
2005
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002006 // When doing liveness analysis, instructions that have uses get an SSA index.
2007 int ssa_index_;
2008
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002009 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002010 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002011
2012 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002013 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002014
Nicolas Geoffray39468442014-09-02 15:17:15 +01002015 // The environment associated with this instruction. Not null if the instruction
2016 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002017 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002018
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002019 // Set by the code generator.
2020 LocationSummary* locations_;
2021
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002022 // Set by the liveness analysis.
2023 LiveInterval* live_interval_;
2024
2025 // Set by the liveness analysis, this is the position in a linear
2026 // order of blocks where this instruction's live interval start.
2027 size_t lifetime_position_;
2028
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002029 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002030
Calin Juravleacf735c2015-02-12 15:25:22 +00002031 // TODO: for primitive types this should be marked as invalid.
2032 ReferenceTypeInfo reference_type_info_;
2033
David Brazdil1abb4192015-02-17 18:33:36 +00002034 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002035 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002036 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002037 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002038 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002039
2040 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2041};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002042std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002043
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002044class HInputIterator : public ValueObject {
2045 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002046 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002047
2048 bool Done() const { return index_ == instruction_->InputCount(); }
2049 HInstruction* Current() const { return instruction_->InputAt(index_); }
2050 void Advance() { index_++; }
2051
2052 private:
2053 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002054 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002055
2056 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2057};
2058
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002059class HInstructionIterator : public ValueObject {
2060 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002061 explicit HInstructionIterator(const HInstructionList& instructions)
2062 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002063 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002064 }
2065
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002066 bool Done() const { return instruction_ == nullptr; }
2067 HInstruction* Current() const { return instruction_; }
2068 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002069 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002070 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002071 }
2072
2073 private:
2074 HInstruction* instruction_;
2075 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002076
2077 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002078};
2079
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002080class HBackwardInstructionIterator : public ValueObject {
2081 public:
2082 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2083 : instruction_(instructions.last_instruction_) {
2084 next_ = Done() ? nullptr : instruction_->GetPrevious();
2085 }
2086
2087 bool Done() const { return instruction_ == nullptr; }
2088 HInstruction* Current() const { return instruction_; }
2089 void Advance() {
2090 instruction_ = next_;
2091 next_ = Done() ? nullptr : instruction_->GetPrevious();
2092 }
2093
2094 private:
2095 HInstruction* instruction_;
2096 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002097
2098 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002099};
2100
Vladimir Markof9f64412015-09-02 14:05:49 +01002101template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002102class HTemplateInstruction: public HInstruction {
2103 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002104 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002105 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002106 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002107
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002108 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002109
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002110 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002111 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2112 DCHECK_LT(i, N);
2113 return inputs_[i];
2114 }
David Brazdil1abb4192015-02-17 18:33:36 +00002115
2116 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002117 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002118 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002119 }
2120
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002121 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002122 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002123
2124 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002125};
2126
Vladimir Markof9f64412015-09-02 14:05:49 +01002127// HTemplateInstruction specialization for N=0.
2128template<>
2129class HTemplateInstruction<0>: public HInstruction {
2130 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002131 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002132 : HInstruction(side_effects, dex_pc) {}
2133
Vladimir Markof9f64412015-09-02 14:05:49 +01002134 virtual ~HTemplateInstruction() {}
2135
2136 size_t InputCount() const OVERRIDE { return 0; }
2137
2138 protected:
2139 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2140 LOG(FATAL) << "Unreachable";
2141 UNREACHABLE();
2142 }
2143
2144 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2145 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2146 LOG(FATAL) << "Unreachable";
2147 UNREACHABLE();
2148 }
2149
2150 private:
2151 friend class SsaBuilder;
2152};
2153
Dave Allison20dfc792014-06-16 20:44:29 -07002154template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002155class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002156 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002157 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002158 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002159 virtual ~HExpression() {}
2160
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002161 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002162
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002163 protected:
2164 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002165};
2166
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002167// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2168// instruction that branches to the exit block.
2169class HReturnVoid : public HTemplateInstruction<0> {
2170 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002171 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2172 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002173
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002174 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002176 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002177
2178 private:
2179 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2180};
2181
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002182// Represents dex's RETURN opcodes. A HReturn is a control flow
2183// instruction that branches to the exit block.
2184class HReturn : public HTemplateInstruction<1> {
2185 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002186 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2187 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002188 SetRawInputAt(0, value);
2189 }
2190
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002191 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002192
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002193 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002194
2195 private:
2196 DISALLOW_COPY_AND_ASSIGN(HReturn);
2197};
2198
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002199// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002200// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002201// exit block.
2202class HExit : public HTemplateInstruction<0> {
2203 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002204 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002205
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002206 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002207
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002208 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002209
2210 private:
2211 DISALLOW_COPY_AND_ASSIGN(HExit);
2212};
2213
2214// Jumps from one block to another.
2215class HGoto : public HTemplateInstruction<0> {
2216 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002217 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002218
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002219 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002220
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002221 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002222 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002223 }
2224
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002225 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226
2227 private:
2228 DISALLOW_COPY_AND_ASSIGN(HGoto);
2229};
2230
Roland Levillain9867bc72015-08-05 10:21:34 +01002231class HConstant : public HExpression<0> {
2232 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002233 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2234 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002235
2236 bool CanBeMoved() const OVERRIDE { return true; }
2237
2238 virtual bool IsMinusOne() const { return false; }
2239 virtual bool IsZero() const { return false; }
2240 virtual bool IsOne() const { return false; }
2241
David Brazdil77a48ae2015-09-15 12:34:04 +00002242 virtual uint64_t GetValueAsUint64() const = 0;
2243
Roland Levillain9867bc72015-08-05 10:21:34 +01002244 DECLARE_INSTRUCTION(Constant);
2245
2246 private:
2247 DISALLOW_COPY_AND_ASSIGN(HConstant);
2248};
2249
2250class HNullConstant : public HConstant {
2251 public:
2252 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2253 return true;
2254 }
2255
David Brazdil77a48ae2015-09-15 12:34:04 +00002256 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2257
Roland Levillain9867bc72015-08-05 10:21:34 +01002258 size_t ComputeHashCode() const OVERRIDE { return 0; }
2259
2260 DECLARE_INSTRUCTION(NullConstant);
2261
2262 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002263 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002264
2265 friend class HGraph;
2266 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2267};
2268
2269// Constants of the type int. Those can be from Dex instructions, or
2270// synthesized (for example with the if-eqz instruction).
2271class HIntConstant : public HConstant {
2272 public:
2273 int32_t GetValue() const { return value_; }
2274
David Brazdil9f389d42015-10-01 14:32:56 +01002275 uint64_t GetValueAsUint64() const OVERRIDE {
2276 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2277 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002278
Roland Levillain9867bc72015-08-05 10:21:34 +01002279 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2280 DCHECK(other->IsIntConstant());
2281 return other->AsIntConstant()->value_ == value_;
2282 }
2283
2284 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2285
2286 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2287 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2288 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2289
2290 DECLARE_INSTRUCTION(IntConstant);
2291
2292 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002293 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2294 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2295 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2296 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002297
2298 const int32_t value_;
2299
2300 friend class HGraph;
2301 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2302 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2303 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2304};
2305
2306class HLongConstant : public HConstant {
2307 public:
2308 int64_t GetValue() const { return value_; }
2309
David Brazdil77a48ae2015-09-15 12:34:04 +00002310 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2311
Roland Levillain9867bc72015-08-05 10:21:34 +01002312 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2313 DCHECK(other->IsLongConstant());
2314 return other->AsLongConstant()->value_ == value_;
2315 }
2316
2317 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2318
2319 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2320 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2321 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2322
2323 DECLARE_INSTRUCTION(LongConstant);
2324
2325 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002326 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2327 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002328
2329 const int64_t value_;
2330
2331 friend class HGraph;
2332 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2333};
Dave Allison20dfc792014-06-16 20:44:29 -07002334
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002335// Conditional branch. A block ending with an HIf instruction must have
2336// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002337class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002338 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002339 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2340 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002341 SetRawInputAt(0, input);
2342 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002343
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002344 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002345
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002346 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002347 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002348 }
2349
2350 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002351 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002352 }
2353
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002354 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002355
2356 private:
2357 DISALLOW_COPY_AND_ASSIGN(HIf);
2358};
2359
David Brazdilfc6a86a2015-06-26 10:33:45 +00002360
2361// Abstract instruction which marks the beginning and/or end of a try block and
2362// links it to the respective exception handlers. Behaves the same as a Goto in
2363// non-exceptional control flow.
2364// Normal-flow successor is stored at index zero, exception handlers under
2365// higher indices in no particular order.
2366class HTryBoundary : public HTemplateInstruction<0> {
2367 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002368 enum BoundaryKind {
2369 kEntry,
2370 kExit,
2371 };
2372
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002373 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2374 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002375
2376 bool IsControlFlow() const OVERRIDE { return true; }
2377
2378 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002379 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002380
2381 // Returns whether `handler` is among its exception handlers (non-zero index
2382 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002383 bool HasExceptionHandler(const HBasicBlock& handler) const {
2384 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002385 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002386 }
2387
2388 // If not present already, adds `handler` to its block's list of exception
2389 // handlers.
2390 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002391 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002392 GetBlock()->AddSuccessor(handler);
2393 }
2394 }
2395
David Brazdil56e1acc2015-06-30 15:41:36 +01002396 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002397
David Brazdilffee3d32015-07-06 11:48:53 +01002398 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2399
David Brazdilfc6a86a2015-06-26 10:33:45 +00002400 DECLARE_INSTRUCTION(TryBoundary);
2401
2402 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002403 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002404
2405 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2406};
2407
David Brazdilffee3d32015-07-06 11:48:53 +01002408// Iterator over exception handlers of a given HTryBoundary, i.e. over
2409// exceptional successors of its basic block.
2410class HExceptionHandlerIterator : public ValueObject {
2411 public:
2412 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2413 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2414
Vladimir Marko60584552015-09-03 13:35:12 +00002415 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002416 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002417 size_t CurrentSuccessorIndex() const { return index_; }
2418 void Advance() { ++index_; }
2419
2420 private:
2421 const HBasicBlock& block_;
2422 size_t index_;
2423
2424 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2425};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002426
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002427// Deoptimize to interpreter, upon checking a condition.
2428class HDeoptimize : public HTemplateInstruction<1> {
2429 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002430 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2431 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002432 SetRawInputAt(0, cond);
2433 }
2434
2435 bool NeedsEnvironment() const OVERRIDE { return true; }
2436 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002437
2438 DECLARE_INSTRUCTION(Deoptimize);
2439
2440 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002441 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2442};
2443
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002444// Represents the ArtMethod that was passed as a first argument to
2445// the method. It is used by instructions that depend on it, like
2446// instructions that work with the dex cache.
2447class HCurrentMethod : public HExpression<0> {
2448 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002449 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2450 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002451
2452 DECLARE_INSTRUCTION(CurrentMethod);
2453
2454 private:
2455 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2456};
2457
Mark Mendellfe57faa2015-09-18 09:26:15 -04002458// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2459// have one successor for each entry in the switch table, and the final successor
2460// will be the block containing the next Dex opcode.
2461class HPackedSwitch : public HTemplateInstruction<1> {
2462 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002463 HPackedSwitch(int32_t start_value,
2464 uint32_t num_entries,
2465 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002466 uint32_t dex_pc = kNoDexPc)
2467 : HTemplateInstruction(SideEffects::None(), dex_pc),
2468 start_value_(start_value),
2469 num_entries_(num_entries) {
2470 SetRawInputAt(0, input);
2471 }
2472
2473 bool IsControlFlow() const OVERRIDE { return true; }
2474
2475 int32_t GetStartValue() const { return start_value_; }
2476
Vladimir Marko211c2112015-09-24 16:52:33 +01002477 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002478
2479 HBasicBlock* GetDefaultBlock() const {
2480 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002481 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002482 }
2483 DECLARE_INSTRUCTION(PackedSwitch);
2484
2485 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002486 const int32_t start_value_;
2487 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002488
2489 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2490};
2491
Roland Levillain88cb1752014-10-20 16:36:47 +01002492class HUnaryOperation : public HExpression<1> {
2493 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002494 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2495 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002496 SetRawInputAt(0, input);
2497 }
2498
2499 HInstruction* GetInput() const { return InputAt(0); }
2500 Primitive::Type GetResultType() const { return GetType(); }
2501
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002502 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002503 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002504 return true;
2505 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002506
Roland Levillain9240d6a2014-10-20 16:47:04 +01002507 // Try to statically evaluate `operation` and return a HConstant
2508 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002509 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002510 HConstant* TryStaticEvaluation() const;
2511
2512 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002513 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2514 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002515
Roland Levillain88cb1752014-10-20 16:36:47 +01002516 DECLARE_INSTRUCTION(UnaryOperation);
2517
2518 private:
2519 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2520};
2521
Dave Allison20dfc792014-06-16 20:44:29 -07002522class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002523 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002524 HBinaryOperation(Primitive::Type result_type,
2525 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002526 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002527 SideEffects side_effects = SideEffects::None(),
2528 uint32_t dex_pc = kNoDexPc)
2529 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002530 SetRawInputAt(0, left);
2531 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002532 }
2533
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002534 HInstruction* GetLeft() const { return InputAt(0); }
2535 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002536 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002537
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002538 virtual bool IsCommutative() const { return false; }
2539
2540 // Put constant on the right.
2541 // Returns whether order is changed.
2542 bool OrderInputsWithConstantOnTheRight() {
2543 HInstruction* left = InputAt(0);
2544 HInstruction* right = InputAt(1);
2545 if (left->IsConstant() && !right->IsConstant()) {
2546 ReplaceInput(right, 0);
2547 ReplaceInput(left, 1);
2548 return true;
2549 }
2550 return false;
2551 }
2552
2553 // Order inputs by instruction id, but favor constant on the right side.
2554 // This helps GVN for commutative ops.
2555 void OrderInputs() {
2556 DCHECK(IsCommutative());
2557 HInstruction* left = InputAt(0);
2558 HInstruction* right = InputAt(1);
2559 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2560 return;
2561 }
2562 if (OrderInputsWithConstantOnTheRight()) {
2563 return;
2564 }
2565 // Order according to instruction id.
2566 if (left->GetId() > right->GetId()) {
2567 ReplaceInput(right, 0);
2568 ReplaceInput(left, 1);
2569 }
2570 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002571
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002572 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002573 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002574 return true;
2575 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002576
Roland Levillain9240d6a2014-10-20 16:47:04 +01002577 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002578 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002579 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002580 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002581
2582 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002583 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2584 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2585 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2586 HLongConstant* y ATTRIBUTE_UNUSED) const {
2587 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2588 return nullptr;
2589 }
2590 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2591 HIntConstant* y ATTRIBUTE_UNUSED) const {
2592 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2593 return nullptr;
2594 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002595
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002596 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002597 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002598 HConstant* GetConstantRight() const;
2599
2600 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002601 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002602 HInstruction* GetLeastConstantLeft() const;
2603
Roland Levillainccc07a92014-09-16 14:48:16 +01002604 DECLARE_INSTRUCTION(BinaryOperation);
2605
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002606 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002607 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2608};
2609
Mark Mendellc4701932015-04-10 13:18:51 -04002610// The comparison bias applies for floating point operations and indicates how NaN
2611// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002612enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002613 kNoBias, // bias is not applicable (i.e. for long operation)
2614 kGtBias, // return 1 for NaN comparisons
2615 kLtBias, // return -1 for NaN comparisons
2616};
2617
Dave Allison20dfc792014-06-16 20:44:29 -07002618class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002619 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002620 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2621 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002622 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002623 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002624
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002625 bool NeedsMaterialization() const { return needs_materialization_; }
2626 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002627
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002628 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002629 // `instruction`, and disregard moves in between.
2630 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002631
Dave Allison20dfc792014-06-16 20:44:29 -07002632 DECLARE_INSTRUCTION(Condition);
2633
2634 virtual IfCondition GetCondition() const = 0;
2635
Mark Mendellc4701932015-04-10 13:18:51 -04002636 virtual IfCondition GetOppositeCondition() const = 0;
2637
Roland Levillain4fa13f62015-07-06 18:11:54 +01002638 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002639
2640 void SetBias(ComparisonBias bias) { bias_ = bias; }
2641
2642 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2643 return bias_ == other->AsCondition()->bias_;
2644 }
2645
Roland Levillain4fa13f62015-07-06 18:11:54 +01002646 bool IsFPConditionTrueIfNaN() const {
2647 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2648 IfCondition if_cond = GetCondition();
2649 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2650 }
2651
2652 bool IsFPConditionFalseIfNaN() const {
2653 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2654 IfCondition if_cond = GetCondition();
2655 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2656 }
2657
Dave Allison20dfc792014-06-16 20:44:29 -07002658 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002659 // For register allocation purposes, returns whether this instruction needs to be
2660 // materialized (that is, not just be in the processor flags).
2661 bool needs_materialization_;
2662
Mark Mendellc4701932015-04-10 13:18:51 -04002663 // Needed if we merge a HCompare into a HCondition.
2664 ComparisonBias bias_;
2665
Dave Allison20dfc792014-06-16 20:44:29 -07002666 DISALLOW_COPY_AND_ASSIGN(HCondition);
2667};
2668
2669// Instruction to check if two inputs are equal to each other.
2670class HEqual : public HCondition {
2671 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002672 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2673 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002674
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002675 bool IsCommutative() const OVERRIDE { return true; }
2676
Roland Levillain9867bc72015-08-05 10:21:34 +01002677 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002678 return GetBlock()->GetGraph()->GetIntConstant(
2679 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002680 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002681 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002682 return GetBlock()->GetGraph()->GetIntConstant(
2683 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002684 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002685
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002686 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002687
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002688 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002689 return kCondEQ;
2690 }
2691
Mark Mendellc4701932015-04-10 13:18:51 -04002692 IfCondition GetOppositeCondition() const OVERRIDE {
2693 return kCondNE;
2694 }
2695
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002696 private:
Aart Bike9f37602015-10-09 11:15:55 -07002697 template <typename T> bool Compute(T x, T y) const { return x == y; }
2698
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002699 DISALLOW_COPY_AND_ASSIGN(HEqual);
2700};
2701
Dave Allison20dfc792014-06-16 20:44:29 -07002702class HNotEqual : public HCondition {
2703 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002704 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2705 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002706
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002707 bool IsCommutative() const OVERRIDE { return true; }
2708
Roland Levillain9867bc72015-08-05 10:21:34 +01002709 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002710 return GetBlock()->GetGraph()->GetIntConstant(
2711 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002712 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002713 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002714 return GetBlock()->GetGraph()->GetIntConstant(
2715 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002716 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002717
Dave Allison20dfc792014-06-16 20:44:29 -07002718 DECLARE_INSTRUCTION(NotEqual);
2719
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002720 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002721 return kCondNE;
2722 }
2723
Mark Mendellc4701932015-04-10 13:18:51 -04002724 IfCondition GetOppositeCondition() const OVERRIDE {
2725 return kCondEQ;
2726 }
2727
Dave Allison20dfc792014-06-16 20:44:29 -07002728 private:
Aart Bike9f37602015-10-09 11:15:55 -07002729 template <typename T> bool Compute(T x, T y) const { return x != y; }
2730
Dave Allison20dfc792014-06-16 20:44:29 -07002731 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2732};
2733
2734class HLessThan : public HCondition {
2735 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002736 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2737 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002738
Roland Levillain9867bc72015-08-05 10:21:34 +01002739 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002740 return GetBlock()->GetGraph()->GetIntConstant(
2741 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002742 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002743 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002744 return GetBlock()->GetGraph()->GetIntConstant(
2745 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002746 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002747
Dave Allison20dfc792014-06-16 20:44:29 -07002748 DECLARE_INSTRUCTION(LessThan);
2749
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002750 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002751 return kCondLT;
2752 }
2753
Mark Mendellc4701932015-04-10 13:18:51 -04002754 IfCondition GetOppositeCondition() const OVERRIDE {
2755 return kCondGE;
2756 }
2757
Dave Allison20dfc792014-06-16 20:44:29 -07002758 private:
Aart Bike9f37602015-10-09 11:15:55 -07002759 template <typename T> bool Compute(T x, T y) const { return x < y; }
2760
Dave Allison20dfc792014-06-16 20:44:29 -07002761 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2762};
2763
2764class HLessThanOrEqual : public HCondition {
2765 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002766 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2767 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002768
Roland Levillain9867bc72015-08-05 10:21:34 +01002769 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002770 return GetBlock()->GetGraph()->GetIntConstant(
2771 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002772 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002773 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002774 return GetBlock()->GetGraph()->GetIntConstant(
2775 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002776 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002777
Dave Allison20dfc792014-06-16 20:44:29 -07002778 DECLARE_INSTRUCTION(LessThanOrEqual);
2779
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002780 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002781 return kCondLE;
2782 }
2783
Mark Mendellc4701932015-04-10 13:18:51 -04002784 IfCondition GetOppositeCondition() const OVERRIDE {
2785 return kCondGT;
2786 }
2787
Dave Allison20dfc792014-06-16 20:44:29 -07002788 private:
Aart Bike9f37602015-10-09 11:15:55 -07002789 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2790
Dave Allison20dfc792014-06-16 20:44:29 -07002791 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2792};
2793
2794class HGreaterThan : public HCondition {
2795 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002796 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2797 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002798
Roland Levillain9867bc72015-08-05 10:21:34 +01002799 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002800 return GetBlock()->GetGraph()->GetIntConstant(
2801 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002802 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002803 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002804 return GetBlock()->GetGraph()->GetIntConstant(
2805 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002806 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002807
Dave Allison20dfc792014-06-16 20:44:29 -07002808 DECLARE_INSTRUCTION(GreaterThan);
2809
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002810 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002811 return kCondGT;
2812 }
2813
Mark Mendellc4701932015-04-10 13:18:51 -04002814 IfCondition GetOppositeCondition() const OVERRIDE {
2815 return kCondLE;
2816 }
2817
Dave Allison20dfc792014-06-16 20:44:29 -07002818 private:
Aart Bike9f37602015-10-09 11:15:55 -07002819 template <typename T> bool Compute(T x, T y) const { return x > y; }
2820
Dave Allison20dfc792014-06-16 20:44:29 -07002821 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2822};
2823
2824class HGreaterThanOrEqual : public HCondition {
2825 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002826 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2827 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002828
Roland Levillain9867bc72015-08-05 10:21:34 +01002829 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002830 return GetBlock()->GetGraph()->GetIntConstant(
2831 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002832 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002833 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002834 return GetBlock()->GetGraph()->GetIntConstant(
2835 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002836 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002837
Dave Allison20dfc792014-06-16 20:44:29 -07002838 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002840 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002841 return kCondGE;
2842 }
2843
Mark Mendellc4701932015-04-10 13:18:51 -04002844 IfCondition GetOppositeCondition() const OVERRIDE {
2845 return kCondLT;
2846 }
2847
Dave Allison20dfc792014-06-16 20:44:29 -07002848 private:
Aart Bike9f37602015-10-09 11:15:55 -07002849 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2850
Dave Allison20dfc792014-06-16 20:44:29 -07002851 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2852};
2853
Aart Bike9f37602015-10-09 11:15:55 -07002854class HBelow : public HCondition {
2855 public:
2856 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2857 : HCondition(first, second, dex_pc) {}
2858
2859 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2860 return GetBlock()->GetGraph()->GetIntConstant(
2861 Compute(static_cast<uint32_t>(x->GetValue()),
2862 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2863 }
2864 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2865 return GetBlock()->GetGraph()->GetIntConstant(
2866 Compute(static_cast<uint64_t>(x->GetValue()),
2867 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2868 }
2869
2870 DECLARE_INSTRUCTION(Below);
2871
2872 IfCondition GetCondition() const OVERRIDE {
2873 return kCondB;
2874 }
2875
2876 IfCondition GetOppositeCondition() const OVERRIDE {
2877 return kCondAE;
2878 }
2879
2880 private:
2881 template <typename T> bool Compute(T x, T y) const { return x < y; }
2882
2883 DISALLOW_COPY_AND_ASSIGN(HBelow);
2884};
2885
2886class HBelowOrEqual : public HCondition {
2887 public:
2888 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2889 : HCondition(first, second, dex_pc) {}
2890
2891 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2892 return GetBlock()->GetGraph()->GetIntConstant(
2893 Compute(static_cast<uint32_t>(x->GetValue()),
2894 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2895 }
2896 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2897 return GetBlock()->GetGraph()->GetIntConstant(
2898 Compute(static_cast<uint64_t>(x->GetValue()),
2899 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2900 }
2901
2902 DECLARE_INSTRUCTION(BelowOrEqual);
2903
2904 IfCondition GetCondition() const OVERRIDE {
2905 return kCondBE;
2906 }
2907
2908 IfCondition GetOppositeCondition() const OVERRIDE {
2909 return kCondA;
2910 }
2911
2912 private:
2913 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2914
2915 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2916};
2917
2918class HAbove : public HCondition {
2919 public:
2920 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2921 : HCondition(first, second, dex_pc) {}
2922
2923 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2924 return GetBlock()->GetGraph()->GetIntConstant(
2925 Compute(static_cast<uint32_t>(x->GetValue()),
2926 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2927 }
2928 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2929 return GetBlock()->GetGraph()->GetIntConstant(
2930 Compute(static_cast<uint64_t>(x->GetValue()),
2931 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2932 }
2933
2934 DECLARE_INSTRUCTION(Above);
2935
2936 IfCondition GetCondition() const OVERRIDE {
2937 return kCondA;
2938 }
2939
2940 IfCondition GetOppositeCondition() const OVERRIDE {
2941 return kCondBE;
2942 }
2943
2944 private:
2945 template <typename T> bool Compute(T x, T y) const { return x > y; }
2946
2947 DISALLOW_COPY_AND_ASSIGN(HAbove);
2948};
2949
2950class HAboveOrEqual : public HCondition {
2951 public:
2952 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2953 : HCondition(first, second, dex_pc) {}
2954
2955 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2956 return GetBlock()->GetGraph()->GetIntConstant(
2957 Compute(static_cast<uint32_t>(x->GetValue()),
2958 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2959 }
2960 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2961 return GetBlock()->GetGraph()->GetIntConstant(
2962 Compute(static_cast<uint64_t>(x->GetValue()),
2963 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2964 }
2965
2966 DECLARE_INSTRUCTION(AboveOrEqual);
2967
2968 IfCondition GetCondition() const OVERRIDE {
2969 return kCondAE;
2970 }
2971
2972 IfCondition GetOppositeCondition() const OVERRIDE {
2973 return kCondB;
2974 }
2975
2976 private:
2977 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2978
2979 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
2980};
Dave Allison20dfc792014-06-16 20:44:29 -07002981
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002982// Instruction to check how two inputs compare to each other.
2983// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2984class HCompare : public HBinaryOperation {
2985 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002986 HCompare(Primitive::Type type,
2987 HInstruction* first,
2988 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002989 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002990 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002991 : HBinaryOperation(Primitive::kPrimInt,
2992 first,
2993 second,
2994 SideEffectsForArchRuntimeCalls(type),
2995 dex_pc),
2996 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002997 DCHECK_EQ(type, first->GetType());
2998 DCHECK_EQ(type, second->GetType());
2999 }
3000
Roland Levillain9867bc72015-08-05 10:21:34 +01003001 template <typename T>
3002 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003003
Roland Levillain9867bc72015-08-05 10:21:34 +01003004 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003005 return GetBlock()->GetGraph()->GetIntConstant(
3006 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003007 }
3008 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003009 return GetBlock()->GetGraph()->GetIntConstant(
3010 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003011 }
3012
Calin Juravleddb7df22014-11-25 20:56:51 +00003013 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3014 return bias_ == other->AsCompare()->bias_;
3015 }
3016
Mark Mendellc4701932015-04-10 13:18:51 -04003017 ComparisonBias GetBias() const { return bias_; }
3018
Roland Levillain4fa13f62015-07-06 18:11:54 +01003019 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003020
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003021
3022 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3023 // MIPS64 uses a runtime call for FP comparisons.
3024 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3025 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003026
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003027 DECLARE_INSTRUCTION(Compare);
3028
3029 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003030 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003031
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003032 DISALLOW_COPY_AND_ASSIGN(HCompare);
3033};
3034
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003035// A local in the graph. Corresponds to a Dex register.
3036class HLocal : public HTemplateInstruction<0> {
3037 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003038 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003039 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003040
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003041 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003042
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003043 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003044
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003045 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003046 // The Dex register number.
3047 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003048
3049 DISALLOW_COPY_AND_ASSIGN(HLocal);
3050};
3051
3052// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003053class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003054 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003055 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3056 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003057 SetRawInputAt(0, local);
3058 }
3059
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003060 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3061
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003062 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003063
3064 private:
3065 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3066};
3067
3068// Store a value in a given local. This instruction has two inputs: the value
3069// and the local.
3070class HStoreLocal : public HTemplateInstruction<2> {
3071 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003072 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3073 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003074 SetRawInputAt(0, local);
3075 SetRawInputAt(1, value);
3076 }
3077
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003078 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3079
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003080 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003081
3082 private:
3083 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3084};
3085
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003086class HFloatConstant : public HConstant {
3087 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003088 float GetValue() const { return value_; }
3089
David Brazdil77a48ae2015-09-15 12:34:04 +00003090 uint64_t GetValueAsUint64() const OVERRIDE {
3091 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3092 }
3093
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003094 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003095 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003096 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003097 }
3098
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003099 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003100
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003101 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003102 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003103 }
3104 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003105 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003106 }
3107 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003108 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3109 }
3110 bool IsNaN() const {
3111 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003112 }
3113
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003114 DECLARE_INSTRUCTION(FloatConstant);
3115
3116 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003117 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3118 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3119 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3120 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003121
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003122 const float value_;
3123
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003124 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003125 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003126 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003127 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3128};
3129
3130class HDoubleConstant : public HConstant {
3131 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003132 double GetValue() const { return value_; }
3133
David Brazdil77a48ae2015-09-15 12:34:04 +00003134 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3135
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003136 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003137 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003138 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003139 }
3140
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003141 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003142
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003143 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003144 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003145 }
3146 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003147 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003148 }
3149 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003150 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3151 }
3152 bool IsNaN() const {
3153 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003154 }
3155
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003156 DECLARE_INSTRUCTION(DoubleConstant);
3157
3158 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003159 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3160 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3161 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3162 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003163
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003164 const double value_;
3165
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003166 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003167 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003168 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003169 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3170};
3171
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003172enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003173#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003174#include "intrinsics_list.h"
3175 kNone,
3176 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3177#undef INTRINSICS_LIST
3178#undef OPTIMIZING_INTRINSICS
3179};
3180std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3181
Agi Csaki05f20562015-08-19 14:58:14 -07003182enum IntrinsicNeedsEnvironmentOrCache {
3183 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3184 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003185};
3186
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003187class HInvoke : public HInstruction {
3188 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003189 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003190
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003191 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003192
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003193 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003194 SetRawInputAt(index, argument);
3195 }
3196
Roland Levillain3e3d7332015-04-28 11:00:54 +01003197 // Return the number of arguments. This number can be lower than
3198 // the number of inputs returned by InputCount(), as some invoke
3199 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3200 // inputs at the end of their list of inputs.
3201 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3202
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003203 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003204
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003205
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003206 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003207 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003208
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003209 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3210
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003211 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003212 return intrinsic_;
3213 }
3214
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003215 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003216
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003217 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003218 return GetEnvironment()->GetParent() != nullptr;
3219 }
3220
3221 bool CanThrow() const OVERRIDE { return true; }
3222
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003223 uint32_t* GetIntrinsicOptimizations() {
3224 return &intrinsic_optimizations_;
3225 }
3226
3227 const uint32_t* GetIntrinsicOptimizations() const {
3228 return &intrinsic_optimizations_;
3229 }
3230
3231 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3232
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003233 DECLARE_INSTRUCTION(Invoke);
3234
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003235 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003236 HInvoke(ArenaAllocator* arena,
3237 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003238 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003239 Primitive::Type return_type,
3240 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003241 uint32_t dex_method_index,
3242 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003243 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003244 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003245 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003246 inputs_(number_of_arguments + number_of_other_inputs,
3247 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003248 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003249 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003250 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003251 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003252 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003253 }
3254
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003255 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003256 return inputs_[index];
3257 }
3258
David Brazdil1abb4192015-02-17 18:33:36 +00003259 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003260 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003261 }
3262
Roland Levillain3e3d7332015-04-28 11:00:54 +01003263 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003264 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003265 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003266 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003267 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003268 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003269
3270 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3271 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003272
3273 private:
3274 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3275};
3276
Calin Juravle175dc732015-08-25 15:42:32 +01003277class HInvokeUnresolved : public HInvoke {
3278 public:
3279 HInvokeUnresolved(ArenaAllocator* arena,
3280 uint32_t number_of_arguments,
3281 Primitive::Type return_type,
3282 uint32_t dex_pc,
3283 uint32_t dex_method_index,
3284 InvokeType invoke_type)
3285 : HInvoke(arena,
3286 number_of_arguments,
3287 0u /* number_of_other_inputs */,
3288 return_type,
3289 dex_pc,
3290 dex_method_index,
3291 invoke_type) {
3292 }
3293
3294 DECLARE_INSTRUCTION(InvokeUnresolved);
3295
3296 private:
3297 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3298};
3299
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003300class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003301 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003302 // Requirements of this method call regarding the class
3303 // initialization (clinit) check of its declaring class.
3304 enum class ClinitCheckRequirement {
3305 kNone, // Class already initialized.
3306 kExplicit, // Static call having explicit clinit check as last input.
3307 kImplicit, // Static call implicitly requiring a clinit check.
3308 };
3309
Vladimir Marko58155012015-08-19 12:49:41 +00003310 // Determines how to load the target ArtMethod*.
3311 enum class MethodLoadKind {
3312 // Use a String init ArtMethod* loaded from Thread entrypoints.
3313 kStringInit,
3314
3315 // Use the method's own ArtMethod* loaded by the register allocator.
3316 kRecursive,
3317
3318 // Use ArtMethod* at a known address, embed the direct address in the code.
3319 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3320 kDirectAddress,
3321
3322 // Use ArtMethod* at an address that will be known at link time, embed the direct
3323 // address in the code. If the image is relocatable, emit .patch_oat entry.
3324 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3325 // the image relocatable or not.
3326 kDirectAddressWithFixup,
3327
3328 // Load from resoved methods array in the dex cache using a PC-relative load.
3329 // Used when we need to use the dex cache, for example for invoke-static that
3330 // may cause class initialization (the entry may point to a resolution method),
3331 // and we know that we can access the dex cache arrays using a PC-relative load.
3332 kDexCachePcRelative,
3333
3334 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3335 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3336 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3337 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3338 kDexCacheViaMethod,
3339 };
3340
3341 // Determines the location of the code pointer.
3342 enum class CodePtrLocation {
3343 // Recursive call, use local PC-relative call instruction.
3344 kCallSelf,
3345
3346 // Use PC-relative call instruction patched at link time.
3347 // Used for calls within an oat file, boot->boot or app->app.
3348 kCallPCRelative,
3349
3350 // Call to a known target address, embed the direct address in code.
3351 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3352 kCallDirect,
3353
3354 // Call to a target address that will be known at link time, embed the direct
3355 // address in code. If the image is relocatable, emit .patch_oat entry.
3356 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3357 // the image relocatable or not.
3358 kCallDirectWithFixup,
3359
3360 // Use code pointer from the ArtMethod*.
3361 // Used when we don't know the target code. This is also the last-resort-kind used when
3362 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3363 kCallArtMethod,
3364 };
3365
3366 struct DispatchInfo {
3367 const MethodLoadKind method_load_kind;
3368 const CodePtrLocation code_ptr_location;
3369 // The method load data holds
3370 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3371 // Note that there are multiple string init methods, each having its own offset.
3372 // - the method address for kDirectAddress
3373 // - the dex cache arrays offset for kDexCachePcRel.
3374 const uint64_t method_load_data;
3375 const uint64_t direct_code_ptr;
3376 };
3377
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003378 HInvokeStaticOrDirect(ArenaAllocator* arena,
3379 uint32_t number_of_arguments,
3380 Primitive::Type return_type,
3381 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003382 uint32_t method_index,
3383 MethodReference target_method,
3384 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003385 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003386 InvokeType invoke_type,
3387 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003388 : HInvoke(arena,
3389 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003390 // There is one extra argument for the HCurrentMethod node, and
3391 // potentially one other if the clinit check is explicit, and one other
3392 // if the method is a string factory.
3393 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003394 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003395 return_type,
3396 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003397 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003398 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003399 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003400 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003401 target_method_(target_method),
3402 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003403
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003404 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003405 // We access the method via the dex cache so we can't do an implicit null check.
3406 // TODO: for intrinsics we can generate implicit null checks.
3407 return false;
3408 }
3409
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003410 bool CanBeNull() const OVERRIDE {
3411 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3412 }
3413
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003414 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003415 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3416 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3417 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003418 bool NeedsDexCache() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003419 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003420 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003421 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3422 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3423 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3424 MethodReference GetTargetMethod() const { return target_method_; }
3425
3426 int32_t GetStringInitOffset() const {
3427 DCHECK(IsStringInit());
3428 return dispatch_info_.method_load_data;
3429 }
3430
3431 uint64_t GetMethodAddress() const {
3432 DCHECK(HasMethodAddress());
3433 return dispatch_info_.method_load_data;
3434 }
3435
3436 uint32_t GetDexCacheArrayOffset() const {
3437 DCHECK(HasPcRelDexCache());
3438 return dispatch_info_.method_load_data;
3439 }
3440
3441 uint64_t GetDirectCodePtr() const {
3442 DCHECK(HasDirectCodePtr());
3443 return dispatch_info_.direct_code_ptr;
3444 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003445
Calin Juravle68ad6492015-08-18 17:08:12 +01003446 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3447
Roland Levillain4c0eb422015-04-24 16:43:49 +01003448 // Is this instruction a call to a static method?
3449 bool IsStatic() const {
3450 return GetInvokeType() == kStatic;
3451 }
3452
Roland Levillain3e3d7332015-04-28 11:00:54 +01003453 // Remove the art::HLoadClass instruction set as last input by
3454 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3455 // the initial art::HClinitCheck instruction (only relevant for
3456 // static calls with explicit clinit check).
3457 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003458 DCHECK(IsStaticWithExplicitClinitCheck());
3459 size_t last_input_index = InputCount() - 1;
3460 HInstruction* last_input = InputAt(last_input_index);
3461 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003462 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003463 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003464 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003465 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3466 DCHECK(IsStaticWithImplicitClinitCheck());
3467 }
3468
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003469 bool IsStringFactoryFor(HFakeString* str) const {
3470 if (!IsStringInit()) return false;
3471 // +1 for the current method.
3472 if (InputCount() == (number_of_arguments_ + 1)) return false;
3473 return InputAt(InputCount() - 1)->AsFakeString() == str;
3474 }
3475
3476 void RemoveFakeStringArgumentAsLastInput() {
3477 DCHECK(IsStringInit());
3478 size_t last_input_index = InputCount() - 1;
3479 HInstruction* last_input = InputAt(last_input_index);
3480 DCHECK(last_input != nullptr);
3481 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3482 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003483 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003484 }
3485
Roland Levillain4c0eb422015-04-24 16:43:49 +01003486 // Is this a call to a static method whose declaring class has an
3487 // explicit intialization check in the graph?
3488 bool IsStaticWithExplicitClinitCheck() const {
3489 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3490 }
3491
3492 // Is this a call to a static method whose declaring class has an
3493 // implicit intialization check requirement?
3494 bool IsStaticWithImplicitClinitCheck() const {
3495 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3496 }
3497
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003498 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003499
Roland Levillain4c0eb422015-04-24 16:43:49 +01003500 protected:
3501 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3502 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3503 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3504 HInstruction* input = input_record.GetInstruction();
3505 // `input` is the last input of a static invoke marked as having
3506 // an explicit clinit check. It must either be:
3507 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3508 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3509 DCHECK(input != nullptr);
3510 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3511 }
3512 return input_record;
3513 }
3514
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003515 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003516 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003517 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003518 // The target method may refer to different dex file or method index than the original
3519 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3520 // in derived class to increase visibility.
3521 MethodReference target_method_;
3522 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003523
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003524 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003525};
3526
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003527class HInvokeVirtual : public HInvoke {
3528 public:
3529 HInvokeVirtual(ArenaAllocator* arena,
3530 uint32_t number_of_arguments,
3531 Primitive::Type return_type,
3532 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003533 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003534 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003535 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003536 vtable_index_(vtable_index) {}
3537
Calin Juravle641547a2015-04-21 22:08:51 +01003538 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003539 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003540 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003541 }
3542
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003543 uint32_t GetVTableIndex() const { return vtable_index_; }
3544
3545 DECLARE_INSTRUCTION(InvokeVirtual);
3546
3547 private:
3548 const uint32_t vtable_index_;
3549
3550 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3551};
3552
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003553class HInvokeInterface : public HInvoke {
3554 public:
3555 HInvokeInterface(ArenaAllocator* arena,
3556 uint32_t number_of_arguments,
3557 Primitive::Type return_type,
3558 uint32_t dex_pc,
3559 uint32_t dex_method_index,
3560 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003561 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003562 imt_index_(imt_index) {}
3563
Calin Juravle641547a2015-04-21 22:08:51 +01003564 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003565 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003566 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003567 }
3568
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003569 uint32_t GetImtIndex() const { return imt_index_; }
3570 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3571
3572 DECLARE_INSTRUCTION(InvokeInterface);
3573
3574 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003575 const uint32_t imt_index_;
3576
3577 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3578};
3579
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003580class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003581 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003582 HNewInstance(HCurrentMethod* current_method,
3583 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003584 uint16_t type_index,
3585 const DexFile& dex_file,
3586 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003587 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003588 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003589 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003590 entrypoint_(entrypoint) {
3591 SetRawInputAt(0, current_method);
3592 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003593
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003594 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003595 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003596
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003597 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003598 bool NeedsEnvironment() const OVERRIDE { return true; }
3599 // It may throw when called on:
3600 // - interfaces
3601 // - abstract/innaccessible/unknown classes
3602 // TODO: optimize when possible.
3603 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003604
Calin Juravle10e244f2015-01-26 18:54:32 +00003605 bool CanBeNull() const OVERRIDE { return false; }
3606
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003607 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3608
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003609 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003610
3611 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003612 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003613 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003614 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003615
3616 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3617};
3618
Roland Levillain88cb1752014-10-20 16:36:47 +01003619class HNeg : public HUnaryOperation {
3620 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003621 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3622 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003623
Roland Levillain9867bc72015-08-05 10:21:34 +01003624 template <typename T> T Compute(T x) const { return -x; }
3625
3626 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003627 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003628 }
3629 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003630 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003631 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003632
Roland Levillain88cb1752014-10-20 16:36:47 +01003633 DECLARE_INSTRUCTION(Neg);
3634
3635 private:
3636 DISALLOW_COPY_AND_ASSIGN(HNeg);
3637};
3638
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003639class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003640 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003641 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003642 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003643 uint32_t dex_pc,
3644 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003645 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003646 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003647 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003648 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003649 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003650 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003651 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003652 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003653 }
3654
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003655 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003656 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003657
3658 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003659 bool NeedsEnvironment() const OVERRIDE { return true; }
3660
Mingyao Yang0c365e62015-03-31 15:09:29 -07003661 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3662 bool CanThrow() const OVERRIDE { return true; }
3663
Calin Juravle10e244f2015-01-26 18:54:32 +00003664 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003665
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003666 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3667
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003668 DECLARE_INSTRUCTION(NewArray);
3669
3670 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003671 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003672 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003673 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003674
3675 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3676};
3677
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003678class HAdd : public HBinaryOperation {
3679 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003680 HAdd(Primitive::Type result_type,
3681 HInstruction* left,
3682 HInstruction* right,
3683 uint32_t dex_pc = kNoDexPc)
3684 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003685
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003686 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003687
Roland Levillain9867bc72015-08-05 10:21:34 +01003688 template <typename T> T Compute(T x, T y) const { return x + y; }
3689
3690 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003691 return GetBlock()->GetGraph()->GetIntConstant(
3692 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003693 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003694 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003695 return GetBlock()->GetGraph()->GetLongConstant(
3696 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003697 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003698
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003699 DECLARE_INSTRUCTION(Add);
3700
3701 private:
3702 DISALLOW_COPY_AND_ASSIGN(HAdd);
3703};
3704
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003705class HSub : public HBinaryOperation {
3706 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003707 HSub(Primitive::Type result_type,
3708 HInstruction* left,
3709 HInstruction* right,
3710 uint32_t dex_pc = kNoDexPc)
3711 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003712
Roland Levillain9867bc72015-08-05 10:21:34 +01003713 template <typename T> T Compute(T x, T y) const { return x - y; }
3714
3715 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003716 return GetBlock()->GetGraph()->GetIntConstant(
3717 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003718 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003720 return GetBlock()->GetGraph()->GetLongConstant(
3721 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003722 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003723
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003724 DECLARE_INSTRUCTION(Sub);
3725
3726 private:
3727 DISALLOW_COPY_AND_ASSIGN(HSub);
3728};
3729
Calin Juravle34bacdf2014-10-07 20:23:36 +01003730class HMul : public HBinaryOperation {
3731 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003732 HMul(Primitive::Type result_type,
3733 HInstruction* left,
3734 HInstruction* right,
3735 uint32_t dex_pc = kNoDexPc)
3736 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003737
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003738 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003739
Roland Levillain9867bc72015-08-05 10:21:34 +01003740 template <typename T> T Compute(T x, T y) const { return x * y; }
3741
3742 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003743 return GetBlock()->GetGraph()->GetIntConstant(
3744 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003745 }
3746 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003747 return GetBlock()->GetGraph()->GetLongConstant(
3748 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003749 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003750
3751 DECLARE_INSTRUCTION(Mul);
3752
3753 private:
3754 DISALLOW_COPY_AND_ASSIGN(HMul);
3755};
3756
Calin Juravle7c4954d2014-10-28 16:57:40 +00003757class HDiv : public HBinaryOperation {
3758 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003759 HDiv(Primitive::Type result_type,
3760 HInstruction* left,
3761 HInstruction* right,
3762 uint32_t dex_pc)
3763 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003764
Roland Levillain9867bc72015-08-05 10:21:34 +01003765 template <typename T>
3766 T Compute(T x, T y) const {
3767 // Our graph structure ensures we never have 0 for `y` during
3768 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003769 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003770 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003771 return (y == -1) ? -x : x / y;
3772 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003773
Roland Levillain9867bc72015-08-05 10:21:34 +01003774 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003775 return GetBlock()->GetGraph()->GetIntConstant(
3776 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003777 }
3778 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003779 return GetBlock()->GetGraph()->GetLongConstant(
3780 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003781 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003782
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003783 static SideEffects SideEffectsForArchRuntimeCalls() {
3784 // The generated code can use a runtime call.
3785 return SideEffects::CanTriggerGC();
3786 }
3787
Calin Juravle7c4954d2014-10-28 16:57:40 +00003788 DECLARE_INSTRUCTION(Div);
3789
3790 private:
3791 DISALLOW_COPY_AND_ASSIGN(HDiv);
3792};
3793
Calin Juravlebacfec32014-11-14 15:54:36 +00003794class HRem : public HBinaryOperation {
3795 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003796 HRem(Primitive::Type result_type,
3797 HInstruction* left,
3798 HInstruction* right,
3799 uint32_t dex_pc)
3800 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003801
Roland Levillain9867bc72015-08-05 10:21:34 +01003802 template <typename T>
3803 T Compute(T x, T y) const {
3804 // Our graph structure ensures we never have 0 for `y` during
3805 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003806 DCHECK_NE(y, 0);
3807 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3808 return (y == -1) ? 0 : x % y;
3809 }
3810
Roland Levillain9867bc72015-08-05 10:21:34 +01003811 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003812 return GetBlock()->GetGraph()->GetIntConstant(
3813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003814 }
3815 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003816 return GetBlock()->GetGraph()->GetLongConstant(
3817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003818 }
3819
Calin Juravlebacfec32014-11-14 15:54:36 +00003820
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003821 static SideEffects SideEffectsForArchRuntimeCalls() {
3822 return SideEffects::CanTriggerGC();
3823 }
3824
Calin Juravlebacfec32014-11-14 15:54:36 +00003825 DECLARE_INSTRUCTION(Rem);
3826
3827 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003828 DISALLOW_COPY_AND_ASSIGN(HRem);
3829};
3830
Calin Juravled0d48522014-11-04 16:40:20 +00003831class HDivZeroCheck : public HExpression<1> {
3832 public:
3833 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003834 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003835 SetRawInputAt(0, value);
3836 }
3837
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003838 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3839
Calin Juravled0d48522014-11-04 16:40:20 +00003840 bool CanBeMoved() const OVERRIDE { return true; }
3841
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003842 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003843 return true;
3844 }
3845
3846 bool NeedsEnvironment() const OVERRIDE { return true; }
3847 bool CanThrow() const OVERRIDE { return true; }
3848
Calin Juravled0d48522014-11-04 16:40:20 +00003849 DECLARE_INSTRUCTION(DivZeroCheck);
3850
3851 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003852 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3853};
3854
Calin Juravle9aec02f2014-11-18 23:06:35 +00003855class HShl : public HBinaryOperation {
3856 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003857 HShl(Primitive::Type result_type,
3858 HInstruction* left,
3859 HInstruction* right,
3860 uint32_t dex_pc = kNoDexPc)
3861 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003862
Roland Levillain9867bc72015-08-05 10:21:34 +01003863 template <typename T, typename U, typename V>
3864 T Compute(T x, U y, V max_shift_value) const {
3865 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3866 "V is not the unsigned integer type corresponding to T");
3867 return x << (y & max_shift_value);
3868 }
3869
3870 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3871 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003872 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003873 }
3874 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3875 // case is handled as `x << static_cast<int>(y)`.
3876 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3877 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003878 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003879 }
3880 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3881 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003882 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003883 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003884
3885 DECLARE_INSTRUCTION(Shl);
3886
3887 private:
3888 DISALLOW_COPY_AND_ASSIGN(HShl);
3889};
3890
3891class HShr : public HBinaryOperation {
3892 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003893 HShr(Primitive::Type result_type,
3894 HInstruction* left,
3895 HInstruction* right,
3896 uint32_t dex_pc = kNoDexPc)
3897 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003898
Roland Levillain9867bc72015-08-05 10:21:34 +01003899 template <typename T, typename U, typename V>
3900 T Compute(T x, U y, V max_shift_value) const {
3901 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3902 "V is not the unsigned integer type corresponding to T");
3903 return x >> (y & max_shift_value);
3904 }
3905
3906 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3907 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003908 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003909 }
3910 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3911 // case is handled as `x >> static_cast<int>(y)`.
3912 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3913 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003914 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003915 }
3916 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3917 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003918 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003919 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003920
3921 DECLARE_INSTRUCTION(Shr);
3922
3923 private:
3924 DISALLOW_COPY_AND_ASSIGN(HShr);
3925};
3926
3927class HUShr : public HBinaryOperation {
3928 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003929 HUShr(Primitive::Type result_type,
3930 HInstruction* left,
3931 HInstruction* right,
3932 uint32_t dex_pc = kNoDexPc)
3933 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003934
Roland Levillain9867bc72015-08-05 10:21:34 +01003935 template <typename T, typename U, typename V>
3936 T Compute(T x, U y, V max_shift_value) const {
3937 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3938 "V is not the unsigned integer type corresponding to T");
3939 V ux = static_cast<V>(x);
3940 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003941 }
3942
Roland Levillain9867bc72015-08-05 10:21:34 +01003943 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3944 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003945 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003946 }
3947 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3948 // case is handled as `x >>> static_cast<int>(y)`.
3949 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3950 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003951 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003952 }
3953 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3954 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003955 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003956 }
3957
3958 DECLARE_INSTRUCTION(UShr);
3959
3960 private:
3961 DISALLOW_COPY_AND_ASSIGN(HUShr);
3962};
3963
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003964class HAnd : public HBinaryOperation {
3965 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003966 HAnd(Primitive::Type result_type,
3967 HInstruction* left,
3968 HInstruction* right,
3969 uint32_t dex_pc = kNoDexPc)
3970 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003971
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003972 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003973
Roland Levillain9867bc72015-08-05 10:21:34 +01003974 template <typename T, typename U>
3975 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3976
3977 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003978 return GetBlock()->GetGraph()->GetIntConstant(
3979 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003980 }
3981 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003982 return GetBlock()->GetGraph()->GetLongConstant(
3983 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003984 }
3985 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003986 return GetBlock()->GetGraph()->GetLongConstant(
3987 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003988 }
3989 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003990 return GetBlock()->GetGraph()->GetLongConstant(
3991 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003993
3994 DECLARE_INSTRUCTION(And);
3995
3996 private:
3997 DISALLOW_COPY_AND_ASSIGN(HAnd);
3998};
3999
4000class HOr : public HBinaryOperation {
4001 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004002 HOr(Primitive::Type result_type,
4003 HInstruction* left,
4004 HInstruction* right,
4005 uint32_t dex_pc = kNoDexPc)
4006 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004007
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004008 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004009
Roland Levillain9867bc72015-08-05 10:21:34 +01004010 template <typename T, typename U>
4011 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4012
4013 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004014 return GetBlock()->GetGraph()->GetIntConstant(
4015 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004016 }
4017 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004018 return GetBlock()->GetGraph()->GetLongConstant(
4019 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004020 }
4021 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004022 return GetBlock()->GetGraph()->GetLongConstant(
4023 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004024 }
4025 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004026 return GetBlock()->GetGraph()->GetLongConstant(
4027 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004028 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004029
4030 DECLARE_INSTRUCTION(Or);
4031
4032 private:
4033 DISALLOW_COPY_AND_ASSIGN(HOr);
4034};
4035
4036class HXor : public HBinaryOperation {
4037 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004038 HXor(Primitive::Type result_type,
4039 HInstruction* left,
4040 HInstruction* right,
4041 uint32_t dex_pc = kNoDexPc)
4042 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004043
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004044 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004045
Roland Levillain9867bc72015-08-05 10:21:34 +01004046 template <typename T, typename U>
4047 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4048
4049 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004050 return GetBlock()->GetGraph()->GetIntConstant(
4051 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004052 }
4053 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004054 return GetBlock()->GetGraph()->GetLongConstant(
4055 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004056 }
4057 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004058 return GetBlock()->GetGraph()->GetLongConstant(
4059 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004060 }
4061 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004062 return GetBlock()->GetGraph()->GetLongConstant(
4063 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004064 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004065
4066 DECLARE_INSTRUCTION(Xor);
4067
4068 private:
4069 DISALLOW_COPY_AND_ASSIGN(HXor);
4070};
4071
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004072// The value of a parameter in this method. Its location depends on
4073// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004074class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004075 public:
Calin Juravlec05aca72015-10-13 13:10:33 +00004076 HParameterValue(uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004077 Primitive::Type parameter_type,
4078 bool is_this = false)
4079 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004080 index_(index),
4081 is_this_(is_this),
4082 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004083
4084 uint8_t GetIndex() const { return index_; }
4085
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004086 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4087 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004088
Calin Juravlec05aca72015-10-13 13:10:33 +00004089 bool IsThis() const { return is_this_; }
4090
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004091 DECLARE_INSTRUCTION(ParameterValue);
4092
4093 private:
4094 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004095 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004096 const uint8_t index_;
4097
Calin Juravle10e244f2015-01-26 18:54:32 +00004098 // Whether or not the parameter value corresponds to 'this' argument.
4099 const bool is_this_;
4100
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004101 bool can_be_null_;
4102
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004103 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4104};
4105
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004106class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004107 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004108 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4109 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004110
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004111 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004112 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004113 return true;
4114 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004115
Roland Levillain9867bc72015-08-05 10:21:34 +01004116 template <typename T> T Compute(T x) const { return ~x; }
4117
4118 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004119 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004120 }
4121 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004122 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004123 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004124
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004125 DECLARE_INSTRUCTION(Not);
4126
4127 private:
4128 DISALLOW_COPY_AND_ASSIGN(HNot);
4129};
4130
David Brazdil66d126e2015-04-03 16:02:44 +01004131class HBooleanNot : public HUnaryOperation {
4132 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004133 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4134 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004135
4136 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004137 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004138 return true;
4139 }
4140
Roland Levillain9867bc72015-08-05 10:21:34 +01004141 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004142 DCHECK(IsUint<1>(x));
4143 return !x;
4144 }
4145
Roland Levillain9867bc72015-08-05 10:21:34 +01004146 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004147 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004148 }
4149 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4150 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004151 UNREACHABLE();
4152 }
4153
4154 DECLARE_INSTRUCTION(BooleanNot);
4155
4156 private:
4157 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4158};
4159
Roland Levillaindff1f282014-11-05 14:15:05 +00004160class HTypeConversion : public HExpression<1> {
4161 public:
4162 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004163 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004164 : HExpression(result_type,
4165 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4166 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004167 SetRawInputAt(0, input);
4168 DCHECK_NE(input->GetType(), result_type);
4169 }
4170
4171 HInstruction* GetInput() const { return InputAt(0); }
4172 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4173 Primitive::Type GetResultType() const { return GetType(); }
4174
Roland Levillain624279f2014-12-04 11:54:28 +00004175 // Required by the x86 and ARM code generators when producing calls
4176 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004177
Roland Levillaindff1f282014-11-05 14:15:05 +00004178 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004179 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004180
Mark Mendelle82549b2015-05-06 10:55:34 -04004181 // Try to statically evaluate the conversion and return a HConstant
4182 // containing the result. If the input cannot be converted, return nullptr.
4183 HConstant* TryStaticEvaluation() const;
4184
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004185 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4186 Primitive::Type result_type) {
4187 // Some architectures may not require the 'GC' side effects, but at this point
4188 // in the compilation process we do not know what architecture we will
4189 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004190 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4191 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004192 return SideEffects::CanTriggerGC();
4193 }
4194 return SideEffects::None();
4195 }
4196
Roland Levillaindff1f282014-11-05 14:15:05 +00004197 DECLARE_INSTRUCTION(TypeConversion);
4198
4199 private:
4200 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4201};
4202
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004203static constexpr uint32_t kNoRegNumber = -1;
4204
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004205class HPhi : public HInstruction {
4206 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004207 HPhi(ArenaAllocator* arena,
4208 uint32_t reg_number,
4209 size_t number_of_inputs,
4210 Primitive::Type type,
4211 uint32_t dex_pc = kNoDexPc)
4212 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004213 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004214 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004215 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004216 is_live_(false),
4217 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004218 }
4219
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004220 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4221 static Primitive::Type ToPhiType(Primitive::Type type) {
4222 switch (type) {
4223 case Primitive::kPrimBoolean:
4224 case Primitive::kPrimByte:
4225 case Primitive::kPrimShort:
4226 case Primitive::kPrimChar:
4227 return Primitive::kPrimInt;
4228 default:
4229 return type;
4230 }
4231 }
4232
David Brazdilffee3d32015-07-06 11:48:53 +01004233 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4234
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004235 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004236
4237 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004238 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004239
Calin Juravle10e244f2015-01-26 18:54:32 +00004240 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004241 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004242
Calin Juravle10e244f2015-01-26 18:54:32 +00004243 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4244 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4245
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004246 uint32_t GetRegNumber() const { return reg_number_; }
4247
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004248 void SetDead() { is_live_ = false; }
4249 void SetLive() { is_live_ = true; }
4250 bool IsDead() const { return !is_live_; }
4251 bool IsLive() const { return is_live_; }
4252
David Brazdil77a48ae2015-09-15 12:34:04 +00004253 bool IsVRegEquivalentOf(HInstruction* other) const {
4254 return other != nullptr
4255 && other->IsPhi()
4256 && other->AsPhi()->GetBlock() == GetBlock()
4257 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4258 }
4259
Calin Juravlea4f88312015-04-16 12:57:19 +01004260 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4261 // An equivalent phi is a phi having the same dex register and type.
4262 // It assumes that phis with the same dex register are adjacent.
4263 HPhi* GetNextEquivalentPhiWithSameType() {
4264 HInstruction* next = GetNext();
4265 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4266 if (next->GetType() == GetType()) {
4267 return next->AsPhi();
4268 }
4269 next = next->GetNext();
4270 }
4271 return nullptr;
4272 }
4273
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004274 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004275
David Brazdil1abb4192015-02-17 18:33:36 +00004276 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004277 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004278 return inputs_[index];
4279 }
David Brazdil1abb4192015-02-17 18:33:36 +00004280
4281 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004282 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004283 }
4284
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004285 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004286 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004287 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004288 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004289 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004290 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004291
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004292 DISALLOW_COPY_AND_ASSIGN(HPhi);
4293};
4294
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004295class HNullCheck : public HExpression<1> {
4296 public:
4297 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004298 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004299 SetRawInputAt(0, value);
4300 }
4301
Calin Juravle10e244f2015-01-26 18:54:32 +00004302 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004303 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004304 return true;
4305 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004306
Calin Juravle10e244f2015-01-26 18:54:32 +00004307 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004308
Calin Juravle10e244f2015-01-26 18:54:32 +00004309 bool CanThrow() const OVERRIDE { return true; }
4310
4311 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004312
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004313
4314 DECLARE_INSTRUCTION(NullCheck);
4315
4316 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004317 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4318};
4319
4320class FieldInfo : public ValueObject {
4321 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004322 FieldInfo(MemberOffset field_offset,
4323 Primitive::Type field_type,
4324 bool is_volatile,
4325 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004326 const DexFile& dex_file,
4327 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004328 : field_offset_(field_offset),
4329 field_type_(field_type),
4330 is_volatile_(is_volatile),
4331 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004332 dex_file_(dex_file),
4333 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004334
4335 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004336 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004337 uint32_t GetFieldIndex() const { return index_; }
4338 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004339 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004340 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004341
4342 private:
4343 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004344 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004345 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004346 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004347 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004348 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004349};
4350
4351class HInstanceFieldGet : public HExpression<1> {
4352 public:
4353 HInstanceFieldGet(HInstruction* value,
4354 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004355 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004356 bool is_volatile,
4357 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004358 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004359 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004360 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004361 : HExpression(
4362 field_type,
4363 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
4364 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004365 SetRawInputAt(0, value);
4366 }
4367
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004368 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004369
4370 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4371 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4372 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004373 }
4374
Calin Juravle641547a2015-04-21 22:08:51 +01004375 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4376 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004377 }
4378
4379 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004380 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4381 }
4382
Calin Juravle52c48962014-12-16 17:02:57 +00004383 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004384 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004385 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004386 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004387
4388 DECLARE_INSTRUCTION(InstanceFieldGet);
4389
4390 private:
4391 const FieldInfo field_info_;
4392
4393 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4394};
4395
4396class HInstanceFieldSet : public HTemplateInstruction<2> {
4397 public:
4398 HInstanceFieldSet(HInstruction* object,
4399 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004400 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004401 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004402 bool is_volatile,
4403 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004404 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004405 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004406 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004407 : HTemplateInstruction(
4408 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
4409 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004410 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004411 SetRawInputAt(0, object);
4412 SetRawInputAt(1, value);
4413 }
4414
Calin Juravle641547a2015-04-21 22:08:51 +01004415 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4416 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004417 }
4418
Calin Juravle52c48962014-12-16 17:02:57 +00004419 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004420 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004421 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004422 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004423 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004424 bool GetValueCanBeNull() const { return value_can_be_null_; }
4425 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004426
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004427 DECLARE_INSTRUCTION(InstanceFieldSet);
4428
4429 private:
4430 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004431 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004432
4433 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4434};
4435
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004436class HArrayGet : public HExpression<2> {
4437 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004438 HArrayGet(HInstruction* array,
4439 HInstruction* index,
4440 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004441 uint32_t dex_pc,
4442 SideEffects additional_side_effects = SideEffects::None())
4443 : HExpression(type,
4444 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
4445 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004446 SetRawInputAt(0, array);
4447 SetRawInputAt(1, index);
4448 }
4449
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004450 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004451 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004452 return true;
4453 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004454 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004455 // TODO: We can be smarter here.
4456 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4457 // which generates the implicit null check. There are cases when these can be removed
4458 // to produce better code. If we ever add optimizations to do so we should allow an
4459 // implicit check here (as long as the address falls in the first page).
4460 return false;
4461 }
4462
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004463 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004464
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004465 HInstruction* GetArray() const { return InputAt(0); }
4466 HInstruction* GetIndex() const { return InputAt(1); }
4467
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004468 DECLARE_INSTRUCTION(ArrayGet);
4469
4470 private:
4471 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4472};
4473
4474class HArraySet : public HTemplateInstruction<3> {
4475 public:
4476 HArraySet(HInstruction* array,
4477 HInstruction* index,
4478 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004479 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004480 uint32_t dex_pc,
4481 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004482 : HTemplateInstruction(
4483 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004484 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4485 additional_side_effects),
4486 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004487 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004488 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004489 value_can_be_null_(true),
4490 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004491 SetRawInputAt(0, array);
4492 SetRawInputAt(1, index);
4493 SetRawInputAt(2, value);
4494 }
4495
Calin Juravle77520bc2015-01-12 18:45:46 +00004496 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004497 // We currently always call a runtime method to catch array store
4498 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004499 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004500 }
4501
Mingyao Yang81014cb2015-06-02 03:16:27 -07004502 // Can throw ArrayStoreException.
4503 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4504
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004505 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004506 // TODO: Same as for ArrayGet.
4507 return false;
4508 }
4509
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004510 void ClearNeedsTypeCheck() {
4511 needs_type_check_ = false;
4512 }
4513
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004514 void ClearValueCanBeNull() {
4515 value_can_be_null_ = false;
4516 }
4517
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004518 void SetStaticTypeOfArrayIsObjectArray() {
4519 static_type_of_array_is_object_array_ = true;
4520 }
4521
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004522 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004523 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004524 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004525
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004526 HInstruction* GetArray() const { return InputAt(0); }
4527 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004528 HInstruction* GetValue() const { return InputAt(2); }
4529
4530 Primitive::Type GetComponentType() const {
4531 // The Dex format does not type floating point index operations. Since the
4532 // `expected_component_type_` is set during building and can therefore not
4533 // be correct, we also check what is the value type. If it is a floating
4534 // point type, we must use that type.
4535 Primitive::Type value_type = GetValue()->GetType();
4536 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4537 ? value_type
4538 : expected_component_type_;
4539 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004540
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004541 Primitive::Type GetRawExpectedComponentType() const {
4542 return expected_component_type_;
4543 }
4544
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004545 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4546 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4547 }
4548
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004549 DECLARE_INSTRUCTION(ArraySet);
4550
4551 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004552 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004553 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004554 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004555 // Cached information for the reference_type_info_ so that codegen
4556 // does not need to inspect the static type.
4557 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004558
4559 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4560};
4561
4562class HArrayLength : public HExpression<1> {
4563 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004564 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004565 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004566 // Note that arrays do not change length, so the instruction does not
4567 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004568 SetRawInputAt(0, array);
4569 }
4570
Calin Juravle77520bc2015-01-12 18:45:46 +00004571 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004572 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004573 return true;
4574 }
Calin Juravle641547a2015-04-21 22:08:51 +01004575 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4576 return obj == InputAt(0);
4577 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004578
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004579 DECLARE_INSTRUCTION(ArrayLength);
4580
4581 private:
4582 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4583};
4584
4585class HBoundsCheck : public HExpression<2> {
4586 public:
4587 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004588 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589 DCHECK(index->GetType() == Primitive::kPrimInt);
4590 SetRawInputAt(0, index);
4591 SetRawInputAt(1, length);
4592 }
4593
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004594 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004595 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004596 return true;
4597 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004598
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004599 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004601 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004602
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004603 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004604
4605 DECLARE_INSTRUCTION(BoundsCheck);
4606
4607 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004608 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4609};
4610
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004611/**
4612 * Some DEX instructions are folded into multiple HInstructions that need
4613 * to stay live until the last HInstruction. This class
4614 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004615 * HInstruction stays live. `index` represents the stack location index of the
4616 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004617 */
4618class HTemporary : public HTemplateInstruction<0> {
4619 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004620 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4621 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004622
4623 size_t GetIndex() const { return index_; }
4624
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004625 Primitive::Type GetType() const OVERRIDE {
4626 // The previous instruction is the one that will be stored in the temporary location.
4627 DCHECK(GetPrevious() != nullptr);
4628 return GetPrevious()->GetType();
4629 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004630
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004631 DECLARE_INSTRUCTION(Temporary);
4632
4633 private:
4634 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004635 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4636};
4637
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004638class HSuspendCheck : public HTemplateInstruction<0> {
4639 public:
4640 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004641 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004642
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004643 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004644 return true;
4645 }
4646
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004647 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4648 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004649
4650 DECLARE_INSTRUCTION(SuspendCheck);
4651
4652 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004653 // Only used for code generation, in order to share the same slow path between back edges
4654 // of a same loop.
4655 SlowPathCode* slow_path_;
4656
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004657 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4658};
4659
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004660/**
4661 * Instruction to load a Class object.
4662 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004663class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004664 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004665 HLoadClass(HCurrentMethod* current_method,
4666 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004667 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004668 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004669 uint32_t dex_pc,
4670 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004672 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004673 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004674 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004675 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004676 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004677 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004678 // Referrers class should not need access check. We never inline unverified
4679 // methods so we can't possibly end up in this situation.
4680 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004681 SetRawInputAt(0, current_method);
4682 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004683
4684 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004685
4686 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004687 // Note that we don't need to test for generate_clinit_check_.
4688 // Whether or not we need to generate the clinit check is processed in
4689 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004690 return other->AsLoadClass()->type_index_ == type_index_ &&
4691 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004692 }
4693
4694 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4695
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004696 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004697 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004698 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004699
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004700 bool NeedsEnvironment() const OVERRIDE {
4701 // Will call runtime and load the class if the class is not loaded yet.
4702 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004703 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004704 }
4705
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004706 bool MustGenerateClinitCheck() const {
4707 return generate_clinit_check_;
4708 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004709 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4710 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004711 }
4712
4713 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004714 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4715 }
4716
4717 bool NeedsAccessCheck() const {
4718 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004719 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004720
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004721 bool CanThrow() const OVERRIDE {
4722 // May call runtime and and therefore can throw.
4723 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004724 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004725 }
4726
Calin Juravleacf735c2015-02-12 15:25:22 +00004727 ReferenceTypeInfo GetLoadedClassRTI() {
4728 return loaded_class_rti_;
4729 }
4730
4731 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4732 // Make sure we only set exact types (the loaded class should never be merged).
4733 DCHECK(rti.IsExact());
4734 loaded_class_rti_ = rti;
4735 }
4736
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004737 const DexFile& GetDexFile() { return dex_file_; }
4738
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004739 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4740
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004741 static SideEffects SideEffectsForArchRuntimeCalls() {
4742 return SideEffects::CanTriggerGC();
4743 }
4744
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004745 DECLARE_INSTRUCTION(LoadClass);
4746
4747 private:
4748 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004749 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004750 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004751 // Whether this instruction must generate the initialization check.
4752 // Used for code generation.
4753 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004754 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004755
Calin Juravleacf735c2015-02-12 15:25:22 +00004756 ReferenceTypeInfo loaded_class_rti_;
4757
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004758 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4759};
4760
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004761class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004762 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004763 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004764 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4765 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004766 SetRawInputAt(0, current_method);
4767 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004768
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004769 bool CanBeMoved() const OVERRIDE { return true; }
4770
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004771 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4772 return other->AsLoadString()->string_index_ == string_index_;
4773 }
4774
4775 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4776
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004777 uint32_t GetStringIndex() const { return string_index_; }
4778
4779 // TODO: Can we deopt or debug when we resolve a string?
4780 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004781 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004782 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004783
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004784 static SideEffects SideEffectsForArchRuntimeCalls() {
4785 return SideEffects::CanTriggerGC();
4786 }
4787
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004788 DECLARE_INSTRUCTION(LoadString);
4789
4790 private:
4791 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004792
4793 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4794};
4795
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004796/**
4797 * Performs an initialization check on its Class object input.
4798 */
4799class HClinitCheck : public HExpression<1> {
4800 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004801 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004802 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004803 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004804 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4805 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004806 SetRawInputAt(0, constant);
4807 }
4808
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004809 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004810 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004811 return true;
4812 }
4813
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004814 bool NeedsEnvironment() const OVERRIDE {
4815 // May call runtime to initialize the class.
4816 return true;
4817 }
4818
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004819
4820 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4821
4822 DECLARE_INSTRUCTION(ClinitCheck);
4823
4824 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004825 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4826};
4827
4828class HStaticFieldGet : public HExpression<1> {
4829 public:
4830 HStaticFieldGet(HInstruction* cls,
4831 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004832 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004833 bool is_volatile,
4834 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004835 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004836 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004837 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004838 : HExpression(
4839 field_type,
4840 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
4841 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004842 SetRawInputAt(0, cls);
4843 }
4844
Calin Juravle52c48962014-12-16 17:02:57 +00004845
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004846 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004847
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004848 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004849 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4850 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004851 }
4852
4853 size_t ComputeHashCode() const OVERRIDE {
4854 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4855 }
4856
Calin Juravle52c48962014-12-16 17:02:57 +00004857 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004858 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4859 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004860 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004861
4862 DECLARE_INSTRUCTION(StaticFieldGet);
4863
4864 private:
4865 const FieldInfo field_info_;
4866
4867 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4868};
4869
4870class HStaticFieldSet : public HTemplateInstruction<2> {
4871 public:
4872 HStaticFieldSet(HInstruction* cls,
4873 HInstruction* value,
4874 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004875 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004876 bool is_volatile,
4877 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004878 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004879 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004880 uint32_t dex_pc)
Nicolas Geoffray8030c412015-10-15 10:30:18 +00004881 : HTemplateInstruction(
4882 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
4883 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004884 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004885 SetRawInputAt(0, cls);
4886 SetRawInputAt(1, value);
4887 }
4888
Calin Juravle52c48962014-12-16 17:02:57 +00004889 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004890 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4891 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004892 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004893
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004894 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004895 bool GetValueCanBeNull() const { return value_can_be_null_; }
4896 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004897
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004898 DECLARE_INSTRUCTION(StaticFieldSet);
4899
4900 private:
4901 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004902 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004903
4904 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4905};
4906
Calin Juravlee460d1d2015-09-29 04:52:17 +01004907class HUnresolvedInstanceFieldGet : public HExpression<1> {
4908 public:
4909 HUnresolvedInstanceFieldGet(HInstruction* obj,
4910 Primitive::Type field_type,
4911 uint32_t field_index,
4912 uint32_t dex_pc)
4913 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4914 field_index_(field_index) {
4915 SetRawInputAt(0, obj);
4916 }
4917
4918 bool NeedsEnvironment() const OVERRIDE { return true; }
4919 bool CanThrow() const OVERRIDE { return true; }
4920
4921 Primitive::Type GetFieldType() const { return GetType(); }
4922 uint32_t GetFieldIndex() const { return field_index_; }
4923
4924 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4925
4926 private:
4927 const uint32_t field_index_;
4928
4929 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4930};
4931
4932class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4933 public:
4934 HUnresolvedInstanceFieldSet(HInstruction* obj,
4935 HInstruction* value,
4936 Primitive::Type field_type,
4937 uint32_t field_index,
4938 uint32_t dex_pc)
4939 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4940 field_type_(field_type),
4941 field_index_(field_index) {
4942 DCHECK_EQ(field_type, value->GetType());
4943 SetRawInputAt(0, obj);
4944 SetRawInputAt(1, value);
4945 }
4946
4947 bool NeedsEnvironment() const OVERRIDE { return true; }
4948 bool CanThrow() const OVERRIDE { return true; }
4949
4950 Primitive::Type GetFieldType() const { return field_type_; }
4951 uint32_t GetFieldIndex() const { return field_index_; }
4952
4953 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
4954
4955 private:
4956 const Primitive::Type field_type_;
4957 const uint32_t field_index_;
4958
4959 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
4960};
4961
4962class HUnresolvedStaticFieldGet : public HExpression<0> {
4963 public:
4964 HUnresolvedStaticFieldGet(Primitive::Type field_type,
4965 uint32_t field_index,
4966 uint32_t dex_pc)
4967 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4968 field_index_(field_index) {
4969 }
4970
4971 bool NeedsEnvironment() const OVERRIDE { return true; }
4972 bool CanThrow() const OVERRIDE { return true; }
4973
4974 Primitive::Type GetFieldType() const { return GetType(); }
4975 uint32_t GetFieldIndex() const { return field_index_; }
4976
4977 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
4978
4979 private:
4980 const uint32_t field_index_;
4981
4982 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
4983};
4984
4985class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
4986 public:
4987 HUnresolvedStaticFieldSet(HInstruction* value,
4988 Primitive::Type field_type,
4989 uint32_t field_index,
4990 uint32_t dex_pc)
4991 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4992 field_type_(field_type),
4993 field_index_(field_index) {
4994 DCHECK_EQ(field_type, value->GetType());
4995 SetRawInputAt(0, value);
4996 }
4997
4998 bool NeedsEnvironment() const OVERRIDE { return true; }
4999 bool CanThrow() const OVERRIDE { return true; }
5000
5001 Primitive::Type GetFieldType() const { return field_type_; }
5002 uint32_t GetFieldIndex() const { return field_index_; }
5003
5004 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5005
5006 private:
5007 const Primitive::Type field_type_;
5008 const uint32_t field_index_;
5009
5010 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5011};
5012
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005013// Implement the move-exception DEX instruction.
5014class HLoadException : public HExpression<0> {
5015 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005016 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5017 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005018
David Brazdilbbd733e2015-08-18 17:48:17 +01005019 bool CanBeNull() const OVERRIDE { return false; }
5020
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005021 DECLARE_INSTRUCTION(LoadException);
5022
5023 private:
5024 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5025};
5026
David Brazdilcb1c0552015-08-04 16:22:25 +01005027// Implicit part of move-exception which clears thread-local exception storage.
5028// Must not be removed because the runtime expects the TLS to get cleared.
5029class HClearException : public HTemplateInstruction<0> {
5030 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005031 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5032 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005033
5034 DECLARE_INSTRUCTION(ClearException);
5035
5036 private:
5037 DISALLOW_COPY_AND_ASSIGN(HClearException);
5038};
5039
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005040class HThrow : public HTemplateInstruction<1> {
5041 public:
5042 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005043 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005044 SetRawInputAt(0, exception);
5045 }
5046
5047 bool IsControlFlow() const OVERRIDE { return true; }
5048
5049 bool NeedsEnvironment() const OVERRIDE { return true; }
5050
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005051 bool CanThrow() const OVERRIDE { return true; }
5052
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005053
5054 DECLARE_INSTRUCTION(Throw);
5055
5056 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005057 DISALLOW_COPY_AND_ASSIGN(HThrow);
5058};
5059
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005060/**
5061 * Implementation strategies for the code generator of a HInstanceOf
5062 * or `HCheckCast`.
5063 */
5064enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005065 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005066 kExactCheck, // Can do a single class compare.
5067 kClassHierarchyCheck, // Can just walk the super class chain.
5068 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5069 kInterfaceCheck, // No optimization yet when checking against an interface.
5070 kArrayObjectCheck, // Can just check if the array is not primitive.
5071 kArrayCheck // No optimization yet when checking against a generic array.
5072};
5073
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005074class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005075 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005076 HInstanceOf(HInstruction* object,
5077 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005078 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005079 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005080 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005081 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005082 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005083 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005084 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005085 SetRawInputAt(0, object);
5086 SetRawInputAt(1, constant);
5087 }
5088
5089 bool CanBeMoved() const OVERRIDE { return true; }
5090
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005091 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005092 return true;
5093 }
5094
5095 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005096 return false;
5097 }
5098
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005099 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5100
5101 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005102
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005103 // Used only in code generation.
5104 bool MustDoNullCheck() const { return must_do_null_check_; }
5105 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5106
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005107 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5108 return (check_kind == TypeCheckKind::kExactCheck)
5109 ? SideEffects::None()
5110 // Mips currently does runtime calls for any other checks.
5111 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005112 }
5113
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005114 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005115
5116 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005117 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005118 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005119
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005120 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5121};
5122
Calin Juravleb1498f62015-02-16 13:13:29 +00005123class HBoundType : public HExpression<1> {
5124 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005125 // Constructs an HBoundType with the given upper_bound.
5126 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005127 HBoundType(HInstruction* input,
5128 ReferenceTypeInfo upper_bound,
5129 bool upper_can_be_null,
5130 uint32_t dex_pc = kNoDexPc)
5131 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005132 upper_bound_(upper_bound),
5133 upper_can_be_null_(upper_can_be_null),
5134 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005135 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005136 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005137 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005138 }
5139
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005140 // GetUpper* should only be used in reference type propagation.
5141 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5142 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005143
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005144 void SetCanBeNull(bool can_be_null) {
5145 DCHECK(upper_can_be_null_ || !can_be_null);
5146 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005147 }
5148
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005149 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5150
Calin Juravleb1498f62015-02-16 13:13:29 +00005151 DECLARE_INSTRUCTION(BoundType);
5152
5153 private:
5154 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005155 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5156 // It is used to bound the type in cases like:
5157 // if (x instanceof ClassX) {
5158 // // uper_bound_ will be ClassX
5159 // }
5160 const ReferenceTypeInfo upper_bound_;
5161 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5162 // is false then can_be_null_ cannot be true).
5163 const bool upper_can_be_null_;
5164 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005165
5166 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5167};
5168
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005169class HCheckCast : public HTemplateInstruction<2> {
5170 public:
5171 HCheckCast(HInstruction* object,
5172 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005173 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005174 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005175 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005176 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005177 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005178 SetRawInputAt(0, object);
5179 SetRawInputAt(1, constant);
5180 }
5181
5182 bool CanBeMoved() const OVERRIDE { return true; }
5183
5184 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5185 return true;
5186 }
5187
5188 bool NeedsEnvironment() const OVERRIDE {
5189 // Instruction may throw a CheckCastError.
5190 return true;
5191 }
5192
5193 bool CanThrow() const OVERRIDE { return true; }
5194
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005195 bool MustDoNullCheck() const { return must_do_null_check_; }
5196 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005197 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005198
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005199 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005200
5201 DECLARE_INSTRUCTION(CheckCast);
5202
5203 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005204 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005205 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005206
5207 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005208};
5209
Calin Juravle27df7582015-04-17 19:12:31 +01005210class HMemoryBarrier : public HTemplateInstruction<0> {
5211 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005212 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005213 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005214 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005215 barrier_kind_(barrier_kind) {}
5216
5217 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5218
5219 DECLARE_INSTRUCTION(MemoryBarrier);
5220
5221 private:
5222 const MemBarrierKind barrier_kind_;
5223
5224 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5225};
5226
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005227class HMonitorOperation : public HTemplateInstruction<1> {
5228 public:
5229 enum OperationKind {
5230 kEnter,
5231 kExit,
5232 };
5233
5234 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005235 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005236 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5237 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005238 SetRawInputAt(0, object);
5239 }
5240
5241 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005242 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5243
5244 bool CanThrow() const OVERRIDE {
5245 // Verifier guarantees that monitor-exit cannot throw.
5246 // This is important because it allows the HGraphBuilder to remove
5247 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5248 return IsEnter();
5249 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005250
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005251
5252 bool IsEnter() const { return kind_ == kEnter; }
5253
5254 DECLARE_INSTRUCTION(MonitorOperation);
5255
Calin Juravle52c48962014-12-16 17:02:57 +00005256 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005257 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005258
5259 private:
5260 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5261};
5262
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005263/**
5264 * A HInstruction used as a marker for the replacement of new + <init>
5265 * of a String to a call to a StringFactory. Only baseline will see
5266 * the node at code generation, where it will be be treated as null.
5267 * When compiling non-baseline, `HFakeString` instructions are being removed
5268 * in the instruction simplifier.
5269 */
5270class HFakeString : public HTemplateInstruction<0> {
5271 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005272 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5273 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005274
5275 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5276
5277 DECLARE_INSTRUCTION(FakeString);
5278
5279 private:
5280 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5281};
5282
Vladimir Markof9f64412015-09-02 14:05:49 +01005283class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005284 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005285 MoveOperands(Location source,
5286 Location destination,
5287 Primitive::Type type,
5288 HInstruction* instruction)
5289 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005290
5291 Location GetSource() const { return source_; }
5292 Location GetDestination() const { return destination_; }
5293
5294 void SetSource(Location value) { source_ = value; }
5295 void SetDestination(Location value) { destination_ = value; }
5296
5297 // The parallel move resolver marks moves as "in-progress" by clearing the
5298 // destination (but not the source).
5299 Location MarkPending() {
5300 DCHECK(!IsPending());
5301 Location dest = destination_;
5302 destination_ = Location::NoLocation();
5303 return dest;
5304 }
5305
5306 void ClearPending(Location dest) {
5307 DCHECK(IsPending());
5308 destination_ = dest;
5309 }
5310
5311 bool IsPending() const {
5312 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5313 return destination_.IsInvalid() && !source_.IsInvalid();
5314 }
5315
5316 // True if this blocks a move from the given location.
5317 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005318 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005319 }
5320
5321 // A move is redundant if it's been eliminated, if its source and
5322 // destination are the same, or if its destination is unneeded.
5323 bool IsRedundant() const {
5324 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5325 }
5326
5327 // We clear both operands to indicate move that's been eliminated.
5328 void Eliminate() {
5329 source_ = destination_ = Location::NoLocation();
5330 }
5331
5332 bool IsEliminated() const {
5333 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5334 return source_.IsInvalid();
5335 }
5336
Alexey Frunze4dda3372015-06-01 18:31:49 -07005337 Primitive::Type GetType() const { return type_; }
5338
Nicolas Geoffray90218252015-04-15 11:56:51 +01005339 bool Is64BitMove() const {
5340 return Primitive::Is64BitType(type_);
5341 }
5342
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005343 HInstruction* GetInstruction() const { return instruction_; }
5344
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005345 private:
5346 Location source_;
5347 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005348 // The type this move is for.
5349 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005350 // The instruction this move is assocatied with. Null when this move is
5351 // for moving an input in the expected locations of user (including a phi user).
5352 // This is only used in debug mode, to ensure we do not connect interval siblings
5353 // in the same parallel move.
5354 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005355};
5356
5357static constexpr size_t kDefaultNumberOfMoves = 4;
5358
5359class HParallelMove : public HTemplateInstruction<0> {
5360 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005361 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005362 : HTemplateInstruction(SideEffects::None(), dex_pc),
5363 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5364 moves_.reserve(kDefaultNumberOfMoves);
5365 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005366
Nicolas Geoffray90218252015-04-15 11:56:51 +01005367 void AddMove(Location source,
5368 Location destination,
5369 Primitive::Type type,
5370 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005371 DCHECK(source.IsValid());
5372 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005373 if (kIsDebugBuild) {
5374 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005375 for (const MoveOperands& move : moves_) {
5376 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005377 // Special case the situation where the move is for the spill slot
5378 // of the instruction.
5379 if ((GetPrevious() == instruction)
5380 || ((GetPrevious() == nullptr)
5381 && instruction->IsPhi()
5382 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005383 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005384 << "Doing parallel moves for the same instruction.";
5385 } else {
5386 DCHECK(false) << "Doing parallel moves for the same instruction.";
5387 }
5388 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005389 }
5390 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005391 for (const MoveOperands& move : moves_) {
5392 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005393 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005394 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005395 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005396 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005397 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005398 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005399 }
5400
Vladimir Marko225b6462015-09-28 12:17:40 +01005401 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005402 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005403 }
5404
Vladimir Marko225b6462015-09-28 12:17:40 +01005405 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005406
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005407 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005408
5409 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005410 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005411
5412 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5413};
5414
Mark Mendell0616ae02015-04-17 12:49:27 -04005415} // namespace art
5416
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005417#ifdef ART_ENABLE_CODEGEN_arm64
5418#include "nodes_arm64.h"
5419#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005420#ifdef ART_ENABLE_CODEGEN_x86
5421#include "nodes_x86.h"
5422#endif
5423
5424namespace art {
5425
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005426class HGraphVisitor : public ValueObject {
5427 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005428 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5429 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005430
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005431 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005432 virtual void VisitBasicBlock(HBasicBlock* block);
5433
Roland Levillain633021e2014-10-01 14:12:25 +01005434 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005435 void VisitInsertionOrder();
5436
Roland Levillain633021e2014-10-01 14:12:25 +01005437 // Visit the graph following dominator tree reverse post-order.
5438 void VisitReversePostOrder();
5439
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005440 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005441
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005442 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005443#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005444 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5445
5446 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5447
5448#undef DECLARE_VISIT_INSTRUCTION
5449
5450 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005451 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005452
5453 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5454};
5455
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005456class HGraphDelegateVisitor : public HGraphVisitor {
5457 public:
5458 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5459 virtual ~HGraphDelegateVisitor() {}
5460
5461 // Visit functions that delegate to to super class.
5462#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005463 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005464
5465 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5466
5467#undef DECLARE_VISIT_INSTRUCTION
5468
5469 private:
5470 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5471};
5472
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005473class HInsertionOrderIterator : public ValueObject {
5474 public:
5475 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5476
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005477 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005478 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005479 void Advance() { ++index_; }
5480
5481 private:
5482 const HGraph& graph_;
5483 size_t index_;
5484
5485 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5486};
5487
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005488class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005489 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005490 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5491 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005492 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005493 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005494
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005495 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5496 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005497 void Advance() { ++index_; }
5498
5499 private:
5500 const HGraph& graph_;
5501 size_t index_;
5502
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005503 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005504};
5505
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005506class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005507 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005508 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005509 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005510 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005511 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005512 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005513
5514 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005515 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005516 void Advance() { --index_; }
5517
5518 private:
5519 const HGraph& graph_;
5520 size_t index_;
5521
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005522 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005523};
5524
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005525class HLinearPostOrderIterator : public ValueObject {
5526 public:
5527 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005528 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005529
5530 bool Done() const { return index_ == 0; }
5531
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005532 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005533
5534 void Advance() {
5535 --index_;
5536 DCHECK_GE(index_, 0U);
5537 }
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(HLinearPostOrderIterator);
5544};
5545
5546class HLinearOrderIterator : public ValueObject {
5547 public:
5548 explicit HLinearOrderIterator(const HGraph& graph)
5549 : order_(graph.GetLinearOrder()), index_(0) {}
5550
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005551 bool Done() const { return index_ == order_.size(); }
5552 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005553 void Advance() { ++index_; }
5554
5555 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005556 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005557 size_t index_;
5558
5559 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5560};
5561
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005562// Iterator over the blocks that art part of the loop. Includes blocks part
5563// of an inner loop. The order in which the blocks are iterated is on their
5564// block id.
5565class HBlocksInLoopIterator : public ValueObject {
5566 public:
5567 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5568 : blocks_in_loop_(info.GetBlocks()),
5569 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5570 index_(0) {
5571 if (!blocks_in_loop_.IsBitSet(index_)) {
5572 Advance();
5573 }
5574 }
5575
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005576 bool Done() const { return index_ == blocks_.size(); }
5577 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005578 void Advance() {
5579 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005580 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005581 if (blocks_in_loop_.IsBitSet(index_)) {
5582 break;
5583 }
5584 }
5585 }
5586
5587 private:
5588 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005589 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005590 size_t index_;
5591
5592 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5593};
5594
Mingyao Yang3584bce2015-05-19 16:01:59 -07005595// Iterator over the blocks that art part of the loop. Includes blocks part
5596// of an inner loop. The order in which the blocks are iterated is reverse
5597// post order.
5598class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5599 public:
5600 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5601 : blocks_in_loop_(info.GetBlocks()),
5602 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5603 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005604 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005605 Advance();
5606 }
5607 }
5608
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005609 bool Done() const { return index_ == blocks_.size(); }
5610 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005611 void Advance() {
5612 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005613 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5614 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005615 break;
5616 }
5617 }
5618 }
5619
5620 private:
5621 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005622 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005623 size_t index_;
5624
5625 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5626};
5627
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005628inline int64_t Int64FromConstant(HConstant* constant) {
5629 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5630 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5631 : constant->AsLongConstant()->GetValue();
5632}
5633
Vladimir Marko58155012015-08-19 12:49:41 +00005634inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5635 // For the purposes of the compiler, the dex files must actually be the same object
5636 // if we want to safely treat them as the same. This is especially important for JIT
5637 // as custom class loaders can open the same underlying file (or memory) multiple
5638 // times and provide different class resolution but no two class loaders should ever
5639 // use the same DexFile object - doing so is an unsupported hack that can lead to
5640 // all sorts of weird failures.
5641 return &lhs == &rhs;
5642}
5643
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005644} // namespace art
5645
5646#endif // ART_COMPILER_OPTIMIZING_NODES_H_