blob: 4f4648d55d3a66dfc69b02d73c9e47dec0bbc33a [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
David Brazdil8d5b8b22015-03-24 10:51:52 +000024#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080025#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000026#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010027#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000028#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000029#include "handle.h"
30#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000031#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010032#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000033#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010035#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070036#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000037#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39namespace art {
40
David Brazdil1abb4192015-02-17 18:33:36 +000041class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010045class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010046class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang589dac72015-08-24 11:21:42 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
85 kCondEQ,
86 kCondNE,
87 kCondLT,
88 kCondLE,
89 kCondGT,
90 kCondGE,
91};
92
Vladimir Markof9f64412015-09-02 14:05:49 +010093class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010094 public:
95 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
96
97 void AddInstruction(HInstruction* instruction);
98 void RemoveInstruction(HInstruction* instruction);
99
David Brazdilc3d743f2015-04-22 13:40:50 +0100100 // Insert `instruction` before/after an existing instruction `cursor`.
101 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
102 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
103
Roland Levillain6b469232014-09-25 10:10:38 +0100104 // Return true if this list contains `instruction`.
105 bool Contains(HInstruction* instruction) const;
106
Roland Levillainccc07a92014-09-16 14:48:16 +0100107 // Return true if `instruction1` is found before `instruction2` in
108 // this instruction list and false otherwise. Abort if none
109 // of these instructions is found.
110 bool FoundBefore(const HInstruction* instruction1,
111 const HInstruction* instruction2) const;
112
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000113 bool IsEmpty() const { return first_instruction_ == nullptr; }
114 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
115
116 // Update the block of all instructions to be `block`.
117 void SetBlockOfInstructions(HBasicBlock* block) const;
118
119 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
120 void Add(const HInstructionList& instruction_list);
121
David Brazdil2d7352b2015-04-20 14:52:42 +0100122 // Return the number of instructions in the list. This is an expensive operation.
123 size_t CountSize() const;
124
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100125 private:
126 HInstruction* first_instruction_;
127 HInstruction* last_instruction_;
128
129 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000130 friend class HGraph;
131 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100132 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100133 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100134
135 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
136};
137
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000138// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100139class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000140 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100141 HGraph(ArenaAllocator* arena,
142 const DexFile& dex_file,
143 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100144 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700145 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100146 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100147 bool debuggable = false,
148 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000149 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100150 blocks_(arena->Adapter(kArenaAllocBlockList)),
151 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
152 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700153 entry_block_(nullptr),
154 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100155 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100156 number_of_vregs_(0),
157 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000158 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400159 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000160 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000161 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000162 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100163 dex_file_(dex_file),
164 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100165 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100166 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100167 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700168 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000169 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100170 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
171 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
172 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
173 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
174 cached_current_method_(nullptr) {
175 blocks_.reserve(kDefaultNumberOfBlocks);
176 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000177
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000178 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100179 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
180
David Brazdil69ba7b72015-06-23 18:27:30 +0100181 bool IsInSsaForm() const { return in_ssa_form_; }
182
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000183 HBasicBlock* GetEntryBlock() const { return entry_block_; }
184 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100185 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000186
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000187 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
188 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000189
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000190 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100191
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000192 // Try building the SSA form of this graph, with dominance computation and loop
193 // recognition. Returns whether it was successful in doing all these steps.
194 bool TryBuildingSsa() {
195 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000196 // The SSA builder requires loops to all be natural. Specifically, the dead phi
197 // elimination phase checks the consistency of the graph when doing a post-order
198 // visit for eliminating dead phis: a dead phi can only have loop header phi
199 // users remaining when being visited.
200 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100201 // Precompute per-block try membership before entering the SSA builder,
202 // which needs the information to build catch block phis from values of
203 // locals at throwing instructions inside try blocks.
204 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000205 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100206 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000207 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000208 }
209
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100210 void ComputeDominanceInformation();
211 void ClearDominanceInformation();
212
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000213 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000214 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100215 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100216 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000217
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000218 // Analyze all natural loops in this graph. Returns false if one
219 // loop is not natural, that is the header does not dominate the
220 // back edge.
221 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100222
David Brazdilffee3d32015-07-06 11:48:53 +0100223 // Iterate over blocks to compute try block membership. Needs reverse post
224 // order and loop information.
225 void ComputeTryBlockInformation();
226
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000227 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000228 // Returns the instruction used to replace the invoke expression or null if the
229 // invoke is for a void method.
230 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000231
Mingyao Yang3584bce2015-05-19 16:01:59 -0700232 // Need to add a couple of blocks to test if the loop body is entered and
233 // put deoptimization instructions, etc.
234 void TransformLoopHeaderForBCE(HBasicBlock* header);
235
David Brazdil2d7352b2015-04-20 14:52:42 +0100236 // Removes `block` from the graph.
237 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000238
David Brazdilfc6a86a2015-06-26 10:33:45 +0000239 // Splits the edge between `block` and `successor` while preserving the
240 // indices in the predecessor/successor lists. If there are multiple edges
241 // between the blocks, the lowest indices are used.
242 // Returns the new block which is empty and has the same dex pc as `successor`.
243 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
244
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100245 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
246 void SimplifyLoop(HBasicBlock* header);
247
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000248 int32_t GetNextInstructionId() {
249 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000250 return current_instruction_id_++;
251 }
252
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000253 int32_t GetCurrentInstructionId() const {
254 return current_instruction_id_;
255 }
256
257 void SetCurrentInstructionId(int32_t id) {
258 current_instruction_id_ = id;
259 }
260
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100261 uint16_t GetMaximumNumberOfOutVRegs() const {
262 return maximum_number_of_out_vregs_;
263 }
264
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000265 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
266 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100267 }
268
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100269 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
270 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
271 }
272
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000273 void UpdateTemporariesVRegSlots(size_t slots) {
274 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100275 }
276
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000277 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100278 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000279 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100280 }
281
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100282 void SetNumberOfVRegs(uint16_t number_of_vregs) {
283 number_of_vregs_ = number_of_vregs;
284 }
285
286 uint16_t GetNumberOfVRegs() const {
287 return number_of_vregs_;
288 }
289
290 void SetNumberOfInVRegs(uint16_t value) {
291 number_of_in_vregs_ = value;
292 }
293
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100294 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100295 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100296 return number_of_vregs_ - number_of_in_vregs_;
297 }
298
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100299 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100300 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100301 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100302
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100303 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100304 return linear_order_;
305 }
306
Mark Mendell1152c922015-04-24 17:06:35 -0400307 bool HasBoundsChecks() const {
308 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800309 }
310
Mark Mendell1152c922015-04-24 17:06:35 -0400311 void SetHasBoundsChecks(bool value) {
312 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800313 }
314
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100315 bool ShouldGenerateConstructorBarrier() const {
316 return should_generate_constructor_barrier_;
317 }
318
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000319 bool IsDebuggable() const { return debuggable_; }
320
David Brazdil8d5b8b22015-03-24 10:51:52 +0000321 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000322 // already, it is created and inserted into the graph. This method is only for
323 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600324 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000325
326 // TODO: This is problematic for the consistency of reference type propagation
327 // because it can be created anytime after the pass and thus it will be left
328 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600329 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000330
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600331 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
332 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000333 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600334 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
335 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000336 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600337 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
338 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000339 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600340 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
341 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000342 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000343
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100344 HCurrentMethod* GetCurrentMethod();
345
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000346 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100347
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100348 const DexFile& GetDexFile() const {
349 return dex_file_;
350 }
351
352 uint32_t GetMethodIdx() const {
353 return method_idx_;
354 }
355
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100356 InvokeType GetInvokeType() const {
357 return invoke_type_;
358 }
359
Mark Mendellc4701932015-04-10 13:18:51 -0400360 InstructionSet GetInstructionSet() const {
361 return instruction_set_;
362 }
363
David Brazdil77a48ae2015-09-15 12:34:04 +0000364 bool HasTryCatch() const { return has_try_catch_; }
365 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100366
David Brazdil2d7352b2015-04-20 14:52:42 +0100367 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100368 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000369 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100370 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000371
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000372 template <class InstructionType, typename ValueType>
373 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600374 ArenaSafeMap<ValueType, InstructionType*>* cache,
375 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000376 // Try to find an existing constant of the given value.
377 InstructionType* constant = nullptr;
378 auto cached_constant = cache->find(value);
379 if (cached_constant != cache->end()) {
380 constant = cached_constant->second;
381 }
382
383 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100384 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000385 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600386 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000387 cache->Overwrite(value, constant);
388 InsertConstant(constant);
389 }
390 return constant;
391 }
392
David Brazdil8d5b8b22015-03-24 10:51:52 +0000393 void InsertConstant(HConstant* instruction);
394
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000395 // Cache a float constant into the graph. This method should only be
396 // called by the SsaBuilder when creating "equivalent" instructions.
397 void CacheFloatConstant(HFloatConstant* constant);
398
399 // See CacheFloatConstant comment.
400 void CacheDoubleConstant(HDoubleConstant* constant);
401
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000402 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000403
404 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100405 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000406
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100407 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100408 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000409
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100410 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100411 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100412
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000413 HBasicBlock* entry_block_;
414 HBasicBlock* exit_block_;
415
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100416 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100417 uint16_t maximum_number_of_out_vregs_;
418
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100419 // The number of virtual registers in this method. Contains the parameters.
420 uint16_t number_of_vregs_;
421
422 // The number of virtual registers used by parameters of this method.
423 uint16_t number_of_in_vregs_;
424
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000425 // Number of vreg size slots that the temporaries use (used in baseline compiler).
426 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100427
Mark Mendell1152c922015-04-24 17:06:35 -0400428 // Has bounds checks. We can totally skip BCE if it's false.
429 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800430
David Brazdil77a48ae2015-09-15 12:34:04 +0000431 // Flag whether there are any try/catch blocks in the graph. We will skip
432 // try/catch-related passes if false.
433 bool has_try_catch_;
434
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000435 // Indicates whether the graph should be compiled in a way that
436 // ensures full debuggability. If false, we can apply more
437 // aggressive optimizations that may limit the level of debugging.
438 const bool debuggable_;
439
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000440 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000441 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000442
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100443 // The dex file from which the method is from.
444 const DexFile& dex_file_;
445
446 // The method index in the dex file.
447 const uint32_t method_idx_;
448
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100449 // If inlined, this encodes how the callee is being invoked.
450 const InvokeType invoke_type_;
451
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100452 // Whether the graph has been transformed to SSA form. Only used
453 // in debug mode to ensure we are not using properties only valid
454 // for non-SSA form (like the number of temporaries).
455 bool in_ssa_form_;
456
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100457 const bool should_generate_constructor_barrier_;
458
Mathieu Chartiere401d142015-04-22 13:56:20 -0700459 const InstructionSet instruction_set_;
460
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000461 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000462 HNullConstant* cached_null_constant_;
463 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000464 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000465 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000466 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000467
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100468 HCurrentMethod* cached_current_method_;
469
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000470 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100471 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000472 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000473 DISALLOW_COPY_AND_ASSIGN(HGraph);
474};
475
Vladimir Markof9f64412015-09-02 14:05:49 +0100476class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000477 public:
478 HLoopInformation(HBasicBlock* header, HGraph* graph)
479 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100480 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100481 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100482 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100483 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
484 back_edges_.reserve(kDefaultNumberOfBackEdges);
485 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100486
487 HBasicBlock* GetHeader() const {
488 return header_;
489 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000490
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000491 void SetHeader(HBasicBlock* block) {
492 header_ = block;
493 }
494
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100495 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
496 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
497 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
498
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000499 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100500 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000501 }
502
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100503 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100504 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100505 }
506
David Brazdil46e2a392015-03-16 17:31:52 +0000507 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100508 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 }
510
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000511 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100512 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000513 }
514
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100515 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100516
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100517 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100518 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100519 }
520
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100521 // Returns the lifetime position of the back edge that has the
522 // greatest lifetime position.
523 size_t GetLifetimeEnd() const;
524
525 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100526 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100527 }
528
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100529 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100530 // that is the header dominates the back edge.
531 bool Populate();
532
David Brazdila4b8c212015-05-07 09:59:30 +0100533 // Reanalyzes the loop by removing loop info from its blocks and re-running
534 // Populate(). If there are no back edges left, the loop info is completely
535 // removed as well as its SuspendCheck instruction. It must be run on nested
536 // inner loops first.
537 void Update();
538
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100539 // Returns whether this loop information contains `block`.
540 // Note that this loop information *must* be populated before entering this function.
541 bool Contains(const HBasicBlock& block) const;
542
543 // Returns whether this loop information is an inner loop of `other`.
544 // Note that `other` *must* be populated before entering this function.
545 bool IsIn(const HLoopInformation& other) const;
546
547 const ArenaBitVector& GetBlocks() const { return blocks_; }
548
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000549 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000550 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000551
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000552 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100553 // Internal recursive implementation of `Populate`.
554 void PopulateRecursive(HBasicBlock* block);
555
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000556 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100557 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100558 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000560
561 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
562};
563
David Brazdilec16f792015-08-19 15:04:01 +0100564// Stores try/catch information for basic blocks.
565// Note that HGraph is constructed so that catch blocks cannot simultaneously
566// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100567class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100568 public:
569 // Try block information constructor.
570 explicit TryCatchInformation(const HTryBoundary& try_entry)
571 : try_entry_(&try_entry),
572 catch_dex_file_(nullptr),
573 catch_type_index_(DexFile::kDexNoIndex16) {
574 DCHECK(try_entry_ != nullptr);
575 }
576
577 // Catch block information constructor.
578 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
579 : try_entry_(nullptr),
580 catch_dex_file_(&dex_file),
581 catch_type_index_(catch_type_index) {}
582
583 bool IsTryBlock() const { return try_entry_ != nullptr; }
584
585 const HTryBoundary& GetTryEntry() const {
586 DCHECK(IsTryBlock());
587 return *try_entry_;
588 }
589
590 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
591
592 bool IsCatchAllTypeIndex() const {
593 DCHECK(IsCatchBlock());
594 return catch_type_index_ == DexFile::kDexNoIndex16;
595 }
596
597 uint16_t GetCatchTypeIndex() const {
598 DCHECK(IsCatchBlock());
599 return catch_type_index_;
600 }
601
602 const DexFile& GetCatchDexFile() const {
603 DCHECK(IsCatchBlock());
604 return *catch_dex_file_;
605 }
606
607 private:
608 // One of possibly several TryBoundary instructions entering the block's try.
609 // Only set for try blocks.
610 const HTryBoundary* try_entry_;
611
612 // Exception type information. Only set for catch blocks.
613 const DexFile* catch_dex_file_;
614 const uint16_t catch_type_index_;
615};
616
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100617static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100618static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100619
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000620// A block in a method. Contains the list of instructions represented
621// as a double linked list. Each block knows its predecessors and
622// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100623
Vladimir Markof9f64412015-09-02 14:05:49 +0100624class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000625 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600626 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000627 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000628 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
629 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000630 loop_information_(nullptr),
631 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000632 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100633 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100634 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100635 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000636 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000637 try_catch_information_(nullptr) {
638 predecessors_.reserve(kDefaultNumberOfPredecessors);
639 successors_.reserve(kDefaultNumberOfSuccessors);
640 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
641 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000642
Vladimir Marko60584552015-09-03 13:35:12 +0000643 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100644 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000645 }
646
Vladimir Marko60584552015-09-03 13:35:12 +0000647 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100648 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000649 }
650
Vladimir Marko60584552015-09-03 13:35:12 +0000651 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
652 return ContainsElement(successors_, block, start_from);
653 }
654
655 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100656 return dominated_blocks_;
657 }
658
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100659 bool IsEntryBlock() const {
660 return graph_->GetEntryBlock() == this;
661 }
662
663 bool IsExitBlock() const {
664 return graph_->GetExitBlock() == this;
665 }
666
David Brazdil46e2a392015-03-16 17:31:52 +0000667 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000668 bool IsSingleTryBoundary() const;
669
670 // Returns true if this block emits nothing but a jump.
671 bool IsSingleJump() const {
672 HLoopInformation* loop_info = GetLoopInformation();
673 return (IsSingleGoto() || IsSingleTryBoundary())
674 // Back edges generate a suspend check.
675 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
676 }
David Brazdil46e2a392015-03-16 17:31:52 +0000677
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000678 void AddBackEdge(HBasicBlock* back_edge) {
679 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000680 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000681 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100682 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000683 loop_information_->AddBackEdge(back_edge);
684 }
685
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000686 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000687 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000688
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100689 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000690 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600691 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000692
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000693 HBasicBlock* GetDominator() const { return dominator_; }
694 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000695 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
696
697 void RemoveDominatedBlock(HBasicBlock* block) {
698 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100699 }
Vladimir Marko60584552015-09-03 13:35:12 +0000700
701 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
702 ReplaceElement(dominated_blocks_, existing, new_block);
703 }
704
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100705 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000706
707 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100708 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000709 }
710
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100711 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
712 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100713 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100714 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100715 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
716 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000717
718 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000719 successors_.push_back(block);
720 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000721 }
722
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100723 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
724 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100725 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000726 new_block->predecessors_.push_back(this);
727 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000728 }
729
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000730 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
731 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000732 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000733 new_block->successors_.push_back(this);
734 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000735 }
736
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100737 // Insert `this` between `predecessor` and `successor. This method
738 // preserves the indicies, and will update the first edge found between
739 // `predecessor` and `successor`.
740 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
741 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100742 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000743 successor->predecessors_[predecessor_index] = this;
744 predecessor->successors_[successor_index] = this;
745 successors_.push_back(successor);
746 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100747 }
748
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100749 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000750 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100751 }
752
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000753 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000754 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000755 }
756
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100757 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000758 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759 }
760
761 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000762 predecessors_.push_back(block);
763 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 }
765
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100766 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000767 DCHECK_EQ(predecessors_.size(), 2u);
768 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100769 }
770
David Brazdil769c9e52015-04-27 13:54:09 +0100771 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000772 DCHECK_EQ(successors_.size(), 2u);
773 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100774 }
775
David Brazdilfc6a86a2015-06-26 10:33:45 +0000776 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000777 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100778 }
779
David Brazdilfc6a86a2015-06-26 10:33:45 +0000780 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000781 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100782 }
783
David Brazdilfc6a86a2015-06-26 10:33:45 +0000784 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000785 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100786 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000787 }
788
789 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000790 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100791 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000792 }
793
794 // Returns whether the first occurrence of `predecessor` in the list of
795 // predecessors is at index `idx`.
796 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100797 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000798 return GetPredecessorIndexOf(predecessor) == idx;
799 }
800
David Brazdilffee3d32015-07-06 11:48:53 +0100801 // Returns the number of non-exceptional successors. SsaChecker ensures that
802 // these are stored at the beginning of the successor list.
803 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000804 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100805 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000806
David Brazdild7558da2015-09-22 13:04:14 +0100807 // Create a new block between this block and its predecessors. The new block
808 // is added to the graph, all predecessor edges are relinked to it and an edge
809 // is created to `this`. Returns the new empty block. Reverse post order or
810 // loop and try/catch information are not updated.
811 HBasicBlock* CreateImmediateDominator();
812
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100814 // created, latter block. Note that this method will add the block to the
815 // graph, create a Goto at the end of the former block and will create an edge
816 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100817 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000818 HBasicBlock* SplitBefore(HInstruction* cursor);
819
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000820 // Split the block into two blocks just after `cursor`. Returns the newly
821 // created block. Note that this method just updates raw block information,
822 // like predecessors, successors, dominators, and instruction list. It does not
823 // update the graph, reverse post order, loop information, nor make sure the
824 // blocks are consistent (for example ending with a control flow instruction).
825 HBasicBlock* SplitAfter(HInstruction* cursor);
826
827 // Merge `other` at the end of `this`. Successors and dominated blocks of
828 // `other` are changed to be successors and dominated blocks of `this`. Note
829 // that this method does not update the graph, reverse post order, loop
830 // information, nor make sure the blocks are consistent (for example ending
831 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100832 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000833
834 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
835 // of `this` are moved to `other`.
836 // Note that this method does not update the graph, reverse post order, loop
837 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000838 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000839 void ReplaceWith(HBasicBlock* other);
840
David Brazdil2d7352b2015-04-20 14:52:42 +0100841 // Merge `other` at the end of `this`. This method updates loops, reverse post
842 // order, links to predecessors, successors, dominators and deletes the block
843 // from the graph. The two blocks must be successive, i.e. `this` the only
844 // predecessor of `other` and vice versa.
845 void MergeWith(HBasicBlock* other);
846
847 // Disconnects `this` from all its predecessors, successors and dominator,
848 // removes it from all loops it is included in and eventually from the graph.
849 // The block must not dominate any other block. Predecessors and successors
850 // are safely updated.
851 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000852
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000853 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100854 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100855 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100856 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100857 // Replace instruction `initial` with `replacement` within this block.
858 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
859 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100860 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100861 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000862 // RemoveInstruction and RemovePhi delete a given instruction from the respective
863 // instruction list. With 'ensure_safety' set to true, it verifies that the
864 // instruction is not in use and removes it from the use lists of its inputs.
865 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
866 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100867 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100868
869 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100870 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100871 }
872
Roland Levillain6b879dd2014-09-22 17:13:44 +0100873 bool IsLoopPreHeaderFirstPredecessor() const {
874 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100875 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100876 }
877
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100878 HLoopInformation* GetLoopInformation() const {
879 return loop_information_;
880 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000881
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000882 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100883 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000884 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100885 void SetInLoop(HLoopInformation* info) {
886 if (IsLoopHeader()) {
887 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100888 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100889 loop_information_ = info;
890 } else if (loop_information_->Contains(*info->GetHeader())) {
891 // Block is currently part of an outer loop. Make it part of this inner loop.
892 // Note that a non loop header having a loop information means this loop information
893 // has already been populated
894 loop_information_ = info;
895 } else {
896 // Block is part of an inner loop. Do not update the loop information.
897 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
898 // at this point, because this method is being called while populating `info`.
899 }
900 }
901
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000902 // Raw update of the loop information.
903 void SetLoopInformation(HLoopInformation* info) {
904 loop_information_ = info;
905 }
906
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100907 bool IsInLoop() const { return loop_information_ != nullptr; }
908
David Brazdilec16f792015-08-19 15:04:01 +0100909 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
910
911 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
912 try_catch_information_ = try_catch_information;
913 }
914
915 bool IsTryBlock() const {
916 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
917 }
918
919 bool IsCatchBlock() const {
920 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
921 }
David Brazdilffee3d32015-07-06 11:48:53 +0100922
923 // Returns the try entry that this block's successors should have. They will
924 // be in the same try, unless the block ends in a try boundary. In that case,
925 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100926 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100927
David Brazdild7558da2015-09-22 13:04:14 +0100928 bool HasThrowingInstructions() const;
929
David Brazdila4b8c212015-05-07 09:59:30 +0100930 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100931 bool Dominates(HBasicBlock* block) const;
932
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100933 size_t GetLifetimeStart() const { return lifetime_start_; }
934 size_t GetLifetimeEnd() const { return lifetime_end_; }
935
936 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
937 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
938
David Brazdil8d5b8b22015-03-24 10:51:52 +0000939 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000940 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100941 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000942 bool HasSinglePhi() const;
943
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000944 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000945 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000946 ArenaVector<HBasicBlock*> predecessors_;
947 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100948 HInstructionList instructions_;
949 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000950 HLoopInformation* loop_information_;
951 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000952 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100953 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100954 // The dex program counter of the first instruction of this block.
955 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100956 size_t lifetime_start_;
957 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100958 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100959
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000960 friend class HGraph;
961 friend class HInstruction;
962
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
964};
965
David Brazdilb2bd1c52015-03-25 11:17:37 +0000966// Iterates over the LoopInformation of all loops which contain 'block'
967// from the innermost to the outermost.
968class HLoopInformationOutwardIterator : public ValueObject {
969 public:
970 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
971 : current_(block.GetLoopInformation()) {}
972
973 bool Done() const { return current_ == nullptr; }
974
975 void Advance() {
976 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100977 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000978 }
979
980 HLoopInformation* Current() const {
981 DCHECK(!Done());
982 return current_;
983 }
984
985 private:
986 HLoopInformation* current_;
987
988 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
989};
990
Alexandre Ramesef20f712015-06-09 10:29:30 +0100991#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100992 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000993 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000994 M(ArrayGet, Instruction) \
995 M(ArrayLength, Instruction) \
996 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100997 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000998 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000999 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001000 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001001 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001002 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001003 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001004 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001005 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001006 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001007 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001008 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001009 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001010 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001011 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001012 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001013 M(FloatConstant, Constant) \
1014 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001015 M(GreaterThan, Condition) \
1016 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001017 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001018 M(InstanceFieldGet, Instruction) \
1019 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001020 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001021 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001022 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001023 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001024 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001025 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001026 M(LessThan, Condition) \
1027 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001028 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001029 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001030 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001031 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(Local, Instruction) \
1033 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001034 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001035 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001036 M(Mul, BinaryOperation) \
1037 M(Neg, UnaryOperation) \
1038 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001039 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001040 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001041 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001042 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001043 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001044 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001045 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001046 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001047 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001048 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001049 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001050 M(Return, Instruction) \
1051 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001052 M(Shl, BinaryOperation) \
1053 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001054 M(StaticFieldGet, Instruction) \
1055 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001056 M(UnresolvedInstanceFieldGet, Instruction) \
1057 M(UnresolvedInstanceFieldSet, Instruction) \
1058 M(UnresolvedStaticFieldGet, Instruction) \
1059 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001060 M(StoreLocal, Instruction) \
1061 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001062 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001063 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001064 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001065 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001066 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001067 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001068 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069
Alexandre Ramesef20f712015-06-09 10:29:30 +01001070#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1071
1072#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1073
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001074#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1075
Mark Mendell0616ae02015-04-17 12:49:27 -04001076#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1077 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001078 M(X86LoadFromConstantTable, Instruction) \
1079 M(X86PackedSwitch, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001080
1081#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1082
1083#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1084 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1085 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1086 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001087 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001088 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1089 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1090
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001091#define FOR_EACH_INSTRUCTION(M) \
1092 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1093 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001094 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001095 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001096 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001097
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001098#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001099FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1100#undef FORWARD_DECLARATION
1101
Roland Levillainccc07a92014-09-16 14:48:16 +01001102#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001103 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1104 const char* DebugName() const OVERRIDE { return #type; } \
1105 const H##type* As##type() const OVERRIDE { return this; } \
1106 H##type* As##type() OVERRIDE { return this; } \
1107 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001108 return other->Is##type(); \
1109 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001110 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001111
David Brazdiled596192015-01-23 10:39:45 +00001112template <typename T> class HUseList;
1113
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001114template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001115class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001116 public:
David Brazdiled596192015-01-23 10:39:45 +00001117 HUseListNode* GetPrevious() const { return prev_; }
1118 HUseListNode* GetNext() const { return next_; }
1119 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001120 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001121 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001122
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001123 private:
David Brazdiled596192015-01-23 10:39:45 +00001124 HUseListNode(T user, size_t index)
1125 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1126
1127 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001128 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001129 HUseListNode<T>* prev_;
1130 HUseListNode<T>* next_;
1131
1132 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001133
1134 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1135};
1136
David Brazdiled596192015-01-23 10:39:45 +00001137template <typename T>
1138class HUseList : public ValueObject {
1139 public:
1140 HUseList() : first_(nullptr) {}
1141
1142 void Clear() {
1143 first_ = nullptr;
1144 }
1145
1146 // Adds a new entry at the beginning of the use list and returns
1147 // the newly created node.
1148 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001149 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001150 if (IsEmpty()) {
1151 first_ = new_node;
1152 } else {
1153 first_->prev_ = new_node;
1154 new_node->next_ = first_;
1155 first_ = new_node;
1156 }
1157 return new_node;
1158 }
1159
1160 HUseListNode<T>* GetFirst() const {
1161 return first_;
1162 }
1163
1164 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001165 DCHECK(node != nullptr);
1166 DCHECK(Contains(node));
1167
David Brazdiled596192015-01-23 10:39:45 +00001168 if (node->prev_ != nullptr) {
1169 node->prev_->next_ = node->next_;
1170 }
1171 if (node->next_ != nullptr) {
1172 node->next_->prev_ = node->prev_;
1173 }
1174 if (node == first_) {
1175 first_ = node->next_;
1176 }
1177 }
1178
David Brazdil1abb4192015-02-17 18:33:36 +00001179 bool Contains(const HUseListNode<T>* node) const {
1180 if (node == nullptr) {
1181 return false;
1182 }
1183 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1184 if (current == node) {
1185 return true;
1186 }
1187 }
1188 return false;
1189 }
1190
David Brazdiled596192015-01-23 10:39:45 +00001191 bool IsEmpty() const {
1192 return first_ == nullptr;
1193 }
1194
1195 bool HasOnlyOneUse() const {
1196 return first_ != nullptr && first_->next_ == nullptr;
1197 }
1198
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001199 size_t SizeSlow() const {
1200 size_t count = 0;
1201 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1202 ++count;
1203 }
1204 return count;
1205 }
1206
David Brazdiled596192015-01-23 10:39:45 +00001207 private:
1208 HUseListNode<T>* first_;
1209};
1210
1211template<typename T>
1212class HUseIterator : public ValueObject {
1213 public:
1214 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1215
1216 bool Done() const { return current_ == nullptr; }
1217
1218 void Advance() {
1219 DCHECK(!Done());
1220 current_ = current_->GetNext();
1221 }
1222
1223 HUseListNode<T>* Current() const {
1224 DCHECK(!Done());
1225 return current_;
1226 }
1227
1228 private:
1229 HUseListNode<T>* current_;
1230
1231 friend class HValue;
1232};
1233
David Brazdil1abb4192015-02-17 18:33:36 +00001234// This class is used by HEnvironment and HInstruction classes to record the
1235// instructions they use and pointers to the corresponding HUseListNodes kept
1236// by the used instructions.
1237template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001238class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001239 public:
1240 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1241 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1242
1243 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1244 : instruction_(old_record.instruction_), use_node_(use_node) {
1245 DCHECK(instruction_ != nullptr);
1246 DCHECK(use_node_ != nullptr);
1247 DCHECK(old_record.use_node_ == nullptr);
1248 }
1249
1250 HInstruction* GetInstruction() const { return instruction_; }
1251 HUseListNode<T>* GetUseNode() const { return use_node_; }
1252
1253 private:
1254 // Instruction used by the user.
1255 HInstruction* instruction_;
1256
1257 // Corresponding entry in the use list kept by 'instruction_'.
1258 HUseListNode<T>* use_node_;
1259};
1260
Aart Bik854a02b2015-07-14 16:07:00 -07001261/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001262 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001263 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001264 * For write/read dependences on fields/arrays, the dependence analysis uses
1265 * type disambiguation (e.g. a float field write cannot modify the value of an
1266 * integer field read) and the access type (e.g. a reference array write cannot
1267 * modify the value of a reference field read [although it may modify the
1268 * reference fetch prior to reading the field, which is represented by its own
1269 * write/read dependence]). The analysis makes conservative points-to
1270 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1271 * the same, and any reference read depends on any reference read without
1272 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001273 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001274 * The internal representation uses 38-bit and is described in the table below.
1275 * The first line indicates the side effect, and for field/array accesses the
1276 * second line indicates the type of the access (in the order of the
1277 * Primitive::Type enum).
1278 * The two numbered lines below indicate the bit position in the bitfield (read
1279 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001280 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001281 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1282 * +-------------+---------+---------+--------------+---------+---------+
1283 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1284 * | 3 |333333322|222222221| 1 |111111110|000000000|
1285 * | 7 |654321098|765432109| 8 |765432109|876543210|
1286 *
1287 * Note that, to ease the implementation, 'changes' bits are least significant
1288 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001289 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001290class SideEffects : public ValueObject {
1291 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001292 SideEffects() : flags_(0) {}
1293
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001294 static SideEffects None() {
1295 return SideEffects(0);
1296 }
1297
1298 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001299 return SideEffects(kAllChangeBits | kAllDependOnBits);
1300 }
1301
1302 static SideEffects AllChanges() {
1303 return SideEffects(kAllChangeBits);
1304 }
1305
1306 static SideEffects AllDependencies() {
1307 return SideEffects(kAllDependOnBits);
1308 }
1309
1310 static SideEffects AllExceptGCDependency() {
1311 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1312 }
1313
1314 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001315 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001316 }
1317
Aart Bik34c3ba92015-07-20 14:08:59 -07001318 static SideEffects AllWrites() {
1319 return SideEffects(kAllWrites);
1320 }
1321
1322 static SideEffects AllReads() {
1323 return SideEffects(kAllReads);
1324 }
1325
1326 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1327 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001328 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001329 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001330 }
1331
Aart Bik854a02b2015-07-14 16:07:00 -07001332 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1333 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001334 }
1335
Aart Bik34c3ba92015-07-20 14:08:59 -07001336 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1337 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001338 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001339 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001340 }
1341
1342 static SideEffects ArrayReadOfType(Primitive::Type type) {
1343 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1344 }
1345
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001346 static SideEffects CanTriggerGC() {
1347 return SideEffects(1ULL << kCanTriggerGCBit);
1348 }
1349
1350 static SideEffects DependsOnGC() {
1351 return SideEffects(1ULL << kDependsOnGCBit);
1352 }
1353
Aart Bik854a02b2015-07-14 16:07:00 -07001354 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001355 SideEffects Union(SideEffects other) const {
1356 return SideEffects(flags_ | other.flags_);
1357 }
1358
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001359 SideEffects Exclusion(SideEffects other) const {
1360 return SideEffects(flags_ & ~other.flags_);
1361 }
1362
1363 bool Includes(SideEffects other) const {
1364 return (other.flags_ & flags_) == other.flags_;
1365 }
1366
1367 bool HasSideEffects() const {
1368 return (flags_ & kAllChangeBits);
1369 }
1370
1371 bool HasDependencies() const {
1372 return (flags_ & kAllDependOnBits);
1373 }
1374
1375 // Returns true if there are no side effects or dependencies.
1376 bool DoesNothing() const {
1377 return flags_ == 0;
1378 }
1379
Aart Bik854a02b2015-07-14 16:07:00 -07001380 // Returns true if something is written.
1381 bool DoesAnyWrite() const {
1382 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001383 }
1384
Aart Bik854a02b2015-07-14 16:07:00 -07001385 // Returns true if something is read.
1386 bool DoesAnyRead() const {
1387 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001388 }
1389
Aart Bik854a02b2015-07-14 16:07:00 -07001390 // Returns true if potentially everything is written and read
1391 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001392 bool DoesAllReadWrite() const {
1393 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1394 }
1395
Aart Bik854a02b2015-07-14 16:07:00 -07001396 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001397 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001398 }
1399
1400 // Returns true if this may read something written by other.
1401 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001402 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1403 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001404 }
1405
1406 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001407 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001408 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001409 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001410 for (int s = kLastBit; s >= 0; s--) {
1411 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1412 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1413 // This is a bit for the GC side effect.
1414 if (current_bit_is_set) {
1415 flags += "GC";
1416 }
Aart Bik854a02b2015-07-14 16:07:00 -07001417 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001418 } else {
1419 // This is a bit for the array/field analysis.
1420 // The underscore character stands for the 'can trigger GC' bit.
1421 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1422 if (current_bit_is_set) {
1423 flags += kDebug[s];
1424 }
1425 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1426 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1427 flags += "|";
1428 }
1429 }
Aart Bik854a02b2015-07-14 16:07:00 -07001430 }
1431 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001432 }
1433
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001434 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001435
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001436 private:
1437 static constexpr int kFieldArrayAnalysisBits = 9;
1438
1439 static constexpr int kFieldWriteOffset = 0;
1440 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1441 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1442 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1443
1444 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1445
1446 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1447 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1448 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1449 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1450
1451 static constexpr int kLastBit = kDependsOnGCBit;
1452 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1453
1454 // Aliases.
1455
1456 static_assert(kChangeBits == kDependOnBits,
1457 "the 'change' bits should match the 'depend on' bits.");
1458
1459 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1460 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1461 static constexpr uint64_t kAllWrites =
1462 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1463 static constexpr uint64_t kAllReads =
1464 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001465
Aart Bik854a02b2015-07-14 16:07:00 -07001466 // Work around the fact that HIR aliases I/F and J/D.
1467 // TODO: remove this interceptor once HIR types are clean
1468 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1469 switch (type) {
1470 case Primitive::kPrimInt:
1471 case Primitive::kPrimFloat:
1472 return TypeFlag(Primitive::kPrimInt, offset) |
1473 TypeFlag(Primitive::kPrimFloat, offset);
1474 case Primitive::kPrimLong:
1475 case Primitive::kPrimDouble:
1476 return TypeFlag(Primitive::kPrimLong, offset) |
1477 TypeFlag(Primitive::kPrimDouble, offset);
1478 default:
1479 return TypeFlag(type, offset);
1480 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001481 }
1482
Aart Bik854a02b2015-07-14 16:07:00 -07001483 // Translates type to bit flag.
1484 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1485 CHECK_NE(type, Primitive::kPrimVoid);
1486 const uint64_t one = 1;
1487 const int shift = type; // 0-based consecutive enum
1488 DCHECK_LE(kFieldWriteOffset, shift);
1489 DCHECK_LT(shift, kArrayWriteOffset);
1490 return one << (type + offset);
1491 }
1492
1493 // Private constructor on direct flags value.
1494 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1495
1496 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001497};
1498
David Brazdiled596192015-01-23 10:39:45 +00001499// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001500class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001501 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001502 HEnvironment(ArenaAllocator* arena,
1503 size_t number_of_vregs,
1504 const DexFile& dex_file,
1505 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001506 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001507 InvokeType invoke_type,
1508 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001509 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1510 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001511 parent_(nullptr),
1512 dex_file_(dex_file),
1513 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001514 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001515 invoke_type_(invoke_type),
1516 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001517 }
1518
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001519 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001520 : HEnvironment(arena,
1521 to_copy.Size(),
1522 to_copy.GetDexFile(),
1523 to_copy.GetMethodIdx(),
1524 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001525 to_copy.GetInvokeType(),
1526 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001527
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001528 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001529 if (parent_ != nullptr) {
1530 parent_->SetAndCopyParentChain(allocator, parent);
1531 } else {
1532 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1533 parent_->CopyFrom(parent);
1534 if (parent->GetParent() != nullptr) {
1535 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1536 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001537 }
David Brazdiled596192015-01-23 10:39:45 +00001538 }
1539
Vladimir Marko71bf8092015-09-15 15:33:14 +01001540 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001541 void CopyFrom(HEnvironment* environment);
1542
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001543 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1544 // input to the loop phi instead. This is for inserting instructions that
1545 // require an environment (like HDeoptimization) in the loop pre-header.
1546 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001547
1548 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001549 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001550 }
1551
1552 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001553 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001554 }
1555
David Brazdil1abb4192015-02-17 18:33:36 +00001556 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001557
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001558 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001559
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001560 HEnvironment* GetParent() const { return parent_; }
1561
1562 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001563 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001564 }
1565
1566 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001567 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001568 }
1569
1570 uint32_t GetDexPc() const {
1571 return dex_pc_;
1572 }
1573
1574 uint32_t GetMethodIdx() const {
1575 return method_idx_;
1576 }
1577
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001578 InvokeType GetInvokeType() const {
1579 return invoke_type_;
1580 }
1581
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001582 const DexFile& GetDexFile() const {
1583 return dex_file_;
1584 }
1585
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001586 HInstruction* GetHolder() const {
1587 return holder_;
1588 }
1589
David Brazdiled596192015-01-23 10:39:45 +00001590 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001591 // Record instructions' use entries of this environment for constant-time removal.
1592 // It should only be called by HInstruction when a new environment use is added.
1593 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1594 DCHECK(env_use->GetUser() == this);
1595 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001596 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001597 }
David Brazdiled596192015-01-23 10:39:45 +00001598
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001599 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1600 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001601 HEnvironment* parent_;
1602 const DexFile& dex_file_;
1603 const uint32_t method_idx_;
1604 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001605 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001606
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001607 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001608 HInstruction* const holder_;
1609
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001610 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001611
1612 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1613};
1614
Calin Juravleacf735c2015-02-12 15:25:22 +00001615class ReferenceTypeInfo : ValueObject {
1616 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001617 typedef Handle<mirror::Class> TypeHandle;
1618
Calin Juravle2e768302015-07-28 14:41:11 +00001619 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1620 // The constructor will check that the type_handle is valid.
1621 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001622 }
1623
Calin Juravle2e768302015-07-28 14:41:11 +00001624 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1625
1626 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1627 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001628 }
1629
Calin Juravle2e768302015-07-28 14:41:11 +00001630 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1631 return IsValidHandle(type_handle_);
1632 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001633
Calin Juravleacf735c2015-02-12 15:25:22 +00001634 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001635
1636 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1637 DCHECK(IsValid());
1638 return GetTypeHandle()->IsObjectClass();
1639 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001640
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001641 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001642 DCHECK(IsValid());
1643 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001644 }
1645
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001646 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1647 DCHECK(IsValid());
1648 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1649 }
1650
Mathieu Chartier90443472015-07-16 20:32:27 -07001651 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001652 DCHECK(IsValid());
1653 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001654 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001655
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001656 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001657 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001658 return GetTypeHandle()->IsArrayClass();
1659 }
1660
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001661 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1662 DCHECK(IsValid());
1663 return GetTypeHandle()->IsPrimitiveArray();
1664 }
1665
1666 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1667 DCHECK(IsValid());
1668 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1669 }
1670
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001671 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001672 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001673 if (!IsExact()) return false;
1674 if (!IsArrayClass()) return false;
1675 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1676 }
1677
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001678 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1679 DCHECK(IsValid());
1680 if (!IsExact()) return false;
1681 if (!IsArrayClass()) return false;
1682 if (!rti.IsArrayClass()) return false;
1683 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1684 rti.GetTypeHandle()->GetComponentType());
1685 }
1686
Calin Juravleacf735c2015-02-12 15:25:22 +00001687 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1688
Mathieu Chartier90443472015-07-16 20:32:27 -07001689 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001690 DCHECK(IsValid());
1691 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001692 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1693 }
1694
1695 // Returns true if the type information provide the same amount of details.
1696 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001697 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001698 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001699 if (!IsValid() && !rti.IsValid()) {
1700 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001701 return true;
1702 }
Calin Juravle2e768302015-07-28 14:41:11 +00001703 if (!IsValid() || !rti.IsValid()) {
1704 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001705 return false;
1706 }
Calin Juravle2e768302015-07-28 14:41:11 +00001707 return IsExact() == rti.IsExact()
1708 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001709 }
1710
1711 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001712 ReferenceTypeInfo();
1713 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001714
Calin Juravleacf735c2015-02-12 15:25:22 +00001715 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001716 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001717 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001718 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001719 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001720};
1721
1722std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1723
Vladimir Markof9f64412015-09-02 14:05:49 +01001724class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001725 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001726 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001727 : previous_(nullptr),
1728 next_(nullptr),
1729 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001730 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001731 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001732 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001733 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001734 locations_(nullptr),
1735 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001736 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001737 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001738 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001739
Dave Allison20dfc792014-06-16 20:44:29 -07001740 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001741
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001742#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001743 enum InstructionKind {
1744 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1745 };
1746#undef DECLARE_KIND
1747
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001748 HInstruction* GetNext() const { return next_; }
1749 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001750
Calin Juravle77520bc2015-01-12 18:45:46 +00001751 HInstruction* GetNextDisregardingMoves() const;
1752 HInstruction* GetPreviousDisregardingMoves() const;
1753
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001754 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001755 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001756 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001757 bool IsInBlock() const { return block_ != nullptr; }
1758 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001759 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001760
Roland Levillain6b879dd2014-09-22 17:13:44 +01001761 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001762 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001763
1764 virtual void Accept(HGraphVisitor* visitor) = 0;
1765 virtual const char* DebugName() const = 0;
1766
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001767 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001768 void SetRawInputAt(size_t index, HInstruction* input) {
1769 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1770 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001771
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001772 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001773
1774 uint32_t GetDexPc() const { return dex_pc_; }
1775
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001776 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001777
Roland Levillaine161a2a2014-10-03 12:45:18 +01001778 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001779 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001780
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001781 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001782 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001783
Calin Juravle10e244f2015-01-26 18:54:32 +00001784 // Does not apply for all instructions, but having this at top level greatly
1785 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001786 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001787 virtual bool CanBeNull() const {
1788 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1789 return true;
1790 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001791
Calin Juravle641547a2015-04-21 22:08:51 +01001792 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1793 UNUSED(obj);
1794 return false;
1795 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001796
Calin Juravle2e768302015-07-28 14:41:11 +00001797 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001798
Calin Juravle61d544b2015-02-23 16:46:57 +00001799 ReferenceTypeInfo GetReferenceTypeInfo() const {
1800 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1801 return reference_type_info_;
1802 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001803
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001804 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001805 DCHECK(user != nullptr);
1806 HUseListNode<HInstruction*>* use =
1807 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1808 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001809 }
1810
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001811 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001812 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001813 HUseListNode<HEnvironment*>* env_use =
1814 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1815 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001816 }
1817
David Brazdil1abb4192015-02-17 18:33:36 +00001818 void RemoveAsUserOfInput(size_t input) {
1819 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1820 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1821 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001822
David Brazdil1abb4192015-02-17 18:33:36 +00001823 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1824 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001825
David Brazdiled596192015-01-23 10:39:45 +00001826 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1827 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001828 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001829 bool HasOnlyOneNonEnvironmentUse() const {
1830 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1831 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001832
Roland Levillain6c82d402014-10-13 16:10:27 +01001833 // Does this instruction strictly dominate `other_instruction`?
1834 // Returns false if this instruction and `other_instruction` are the same.
1835 // Aborts if this instruction and `other_instruction` are both phis.
1836 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001837
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001838 int GetId() const { return id_; }
1839 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001840
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001841 int GetSsaIndex() const { return ssa_index_; }
1842 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1843 bool HasSsaIndex() const { return ssa_index_ != -1; }
1844
1845 bool HasEnvironment() const { return environment_ != nullptr; }
1846 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001847 // Set the `environment_` field. Raw because this method does not
1848 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001849 void SetRawEnvironment(HEnvironment* environment) {
1850 DCHECK(environment_ == nullptr);
1851 DCHECK_EQ(environment->GetHolder(), this);
1852 environment_ = environment;
1853 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001854
1855 // Set the environment of this instruction, copying it from `environment`. While
1856 // copying, the uses lists are being updated.
1857 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001858 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001859 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001860 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001861 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001862 if (environment->GetParent() != nullptr) {
1863 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1864 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001865 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001866
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001867 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1868 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001869 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001870 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001871 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001872 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001873 if (environment->GetParent() != nullptr) {
1874 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1875 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001876 }
1877
Nicolas Geoffray39468442014-09-02 15:17:15 +01001878 // Returns the number of entries in the environment. Typically, that is the
1879 // number of dex registers in a method. It could be more in case of inlining.
1880 size_t EnvironmentSize() const;
1881
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001882 LocationSummary* GetLocations() const { return locations_; }
1883 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001884
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001885 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001886 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001887
Alexandre Rames188d4312015-04-09 18:30:21 +01001888 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1889 // uses of this instruction by `other` are *not* updated.
1890 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1891 ReplaceWith(other);
1892 other->ReplaceInput(this, use_index);
1893 }
1894
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001895 // Move `this` instruction before `cursor`.
1896 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001897
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001898#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001899 bool Is##type() const { return (As##type() != nullptr); } \
1900 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001901 virtual H##type* As##type() { return nullptr; }
1902
1903 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1904#undef INSTRUCTION_TYPE_CHECK
1905
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001906 // Returns whether the instruction can be moved within the graph.
1907 virtual bool CanBeMoved() const { return false; }
1908
1909 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001910 virtual bool InstructionTypeEquals(HInstruction* other) const {
1911 UNUSED(other);
1912 return false;
1913 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001914
1915 // Returns whether any data encoded in the two instructions is equal.
1916 // This method does not look at the inputs. Both instructions must be
1917 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001918 virtual bool InstructionDataEquals(HInstruction* other) const {
1919 UNUSED(other);
1920 return false;
1921 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001922
1923 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001924 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001925 // 2) Their inputs are identical.
1926 bool Equals(HInstruction* other) const;
1927
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001928 virtual InstructionKind GetKind() const = 0;
1929
1930 virtual size_t ComputeHashCode() const {
1931 size_t result = GetKind();
1932 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1933 result = (result * 31) + InputAt(i)->GetId();
1934 }
1935 return result;
1936 }
1937
1938 SideEffects GetSideEffects() const { return side_effects_; }
1939
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001940 size_t GetLifetimePosition() const { return lifetime_position_; }
1941 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1942 LiveInterval* GetLiveInterval() const { return live_interval_; }
1943 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1944 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1945
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001946 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1947
1948 // Returns whether the code generation of the instruction will require to have access
1949 // to the current method. Such instructions are:
1950 // (1): Instructions that require an environment, as calling the runtime requires
1951 // to walk the stack and have the current method stored at a specific stack address.
1952 // (2): Object literals like classes and strings, that are loaded from the dex cache
1953 // fields of the current method.
1954 bool NeedsCurrentMethod() const {
1955 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1956 }
1957
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001958 virtual bool NeedsDexCache() const { return false; }
1959
Mark Mendellc4701932015-04-10 13:18:51 -04001960 // Does this instruction have any use in an environment before
1961 // control flow hits 'other'?
1962 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1963
1964 // Remove all references to environment uses of this instruction.
1965 // The caller must ensure that this is safe to do.
1966 void RemoveEnvironmentUsers();
1967
David Brazdil1abb4192015-02-17 18:33:36 +00001968 protected:
1969 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1970 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1971
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001972 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001973 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1974
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001975 HInstruction* previous_;
1976 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001977 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001978 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001979
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001980 // An instruction gets an id when it is added to the graph.
1981 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001982 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001983 int id_;
1984
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001985 // When doing liveness analysis, instructions that have uses get an SSA index.
1986 int ssa_index_;
1987
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001988 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001989 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001990
1991 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001992 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001993
Nicolas Geoffray39468442014-09-02 15:17:15 +01001994 // The environment associated with this instruction. Not null if the instruction
1995 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001996 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001997
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001998 // Set by the code generator.
1999 LocationSummary* locations_;
2000
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002001 // Set by the liveness analysis.
2002 LiveInterval* live_interval_;
2003
2004 // Set by the liveness analysis, this is the position in a linear
2005 // order of blocks where this instruction's live interval start.
2006 size_t lifetime_position_;
2007
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002008 const SideEffects side_effects_;
2009
Calin Juravleacf735c2015-02-12 15:25:22 +00002010 // TODO: for primitive types this should be marked as invalid.
2011 ReferenceTypeInfo reference_type_info_;
2012
David Brazdil1abb4192015-02-17 18:33:36 +00002013 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002014 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002015 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002016 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002017 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018
2019 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2020};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002021std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002022
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002023class HInputIterator : public ValueObject {
2024 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002025 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002026
2027 bool Done() const { return index_ == instruction_->InputCount(); }
2028 HInstruction* Current() const { return instruction_->InputAt(index_); }
2029 void Advance() { index_++; }
2030
2031 private:
2032 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002033 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002034
2035 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2036};
2037
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002038class HInstructionIterator : public ValueObject {
2039 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002040 explicit HInstructionIterator(const HInstructionList& instructions)
2041 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002042 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002043 }
2044
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002045 bool Done() const { return instruction_ == nullptr; }
2046 HInstruction* Current() const { return instruction_; }
2047 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002048 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002049 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002050 }
2051
2052 private:
2053 HInstruction* instruction_;
2054 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002055
2056 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002057};
2058
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002059class HBackwardInstructionIterator : public ValueObject {
2060 public:
2061 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2062 : instruction_(instructions.last_instruction_) {
2063 next_ = Done() ? nullptr : instruction_->GetPrevious();
2064 }
2065
2066 bool Done() const { return instruction_ == nullptr; }
2067 HInstruction* Current() const { return instruction_; }
2068 void Advance() {
2069 instruction_ = next_;
2070 next_ = Done() ? nullptr : instruction_->GetPrevious();
2071 }
2072
2073 private:
2074 HInstruction* instruction_;
2075 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002076
2077 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002078};
2079
Vladimir Markof9f64412015-09-02 14:05:49 +01002080template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002081class HTemplateInstruction: public HInstruction {
2082 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002083 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002084 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002085 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002086
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002087 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002088
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002089 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002090 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2091 DCHECK_LT(i, N);
2092 return inputs_[i];
2093 }
David Brazdil1abb4192015-02-17 18:33:36 +00002094
2095 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002096 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002097 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002098 }
2099
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002100 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002101 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002102
2103 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002104};
2105
Vladimir Markof9f64412015-09-02 14:05:49 +01002106// HTemplateInstruction specialization for N=0.
2107template<>
2108class HTemplateInstruction<0>: public HInstruction {
2109 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002110 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002111 : HInstruction(side_effects, dex_pc) {}
2112
Vladimir Markof9f64412015-09-02 14:05:49 +01002113 virtual ~HTemplateInstruction() {}
2114
2115 size_t InputCount() const OVERRIDE { return 0; }
2116
2117 protected:
2118 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2119 LOG(FATAL) << "Unreachable";
2120 UNREACHABLE();
2121 }
2122
2123 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2124 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2125 LOG(FATAL) << "Unreachable";
2126 UNREACHABLE();
2127 }
2128
2129 private:
2130 friend class SsaBuilder;
2131};
2132
Dave Allison20dfc792014-06-16 20:44:29 -07002133template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002134class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002135 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002136 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002137 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002138 virtual ~HExpression() {}
2139
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002140 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002141
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002142 protected:
2143 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002144};
2145
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002146// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2147// instruction that branches to the exit block.
2148class HReturnVoid : public HTemplateInstruction<0> {
2149 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002150 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2151 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002152
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002153 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002154
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002155 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156
2157 private:
2158 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2159};
2160
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002161// Represents dex's RETURN opcodes. A HReturn is a control flow
2162// instruction that branches to the exit block.
2163class HReturn : public HTemplateInstruction<1> {
2164 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002165 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2166 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002167 SetRawInputAt(0, value);
2168 }
2169
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002170 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002171
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002172 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002173
2174 private:
2175 DISALLOW_COPY_AND_ASSIGN(HReturn);
2176};
2177
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002178// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002179// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002180// exit block.
2181class HExit : public HTemplateInstruction<0> {
2182 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002183 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002184
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002185 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002186
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002187 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002188
2189 private:
2190 DISALLOW_COPY_AND_ASSIGN(HExit);
2191};
2192
2193// Jumps from one block to another.
2194class HGoto : public HTemplateInstruction<0> {
2195 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002196 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002197
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002198 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002199
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002200 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002201 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002202 }
2203
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002204 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002205
2206 private:
2207 DISALLOW_COPY_AND_ASSIGN(HGoto);
2208};
2209
Roland Levillain9867bc72015-08-05 10:21:34 +01002210class HConstant : public HExpression<0> {
2211 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002212 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2213 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002214
2215 bool CanBeMoved() const OVERRIDE { return true; }
2216
2217 virtual bool IsMinusOne() const { return false; }
2218 virtual bool IsZero() const { return false; }
2219 virtual bool IsOne() const { return false; }
2220
David Brazdil77a48ae2015-09-15 12:34:04 +00002221 virtual uint64_t GetValueAsUint64() const = 0;
2222
Roland Levillain9867bc72015-08-05 10:21:34 +01002223 DECLARE_INSTRUCTION(Constant);
2224
2225 private:
2226 DISALLOW_COPY_AND_ASSIGN(HConstant);
2227};
2228
2229class HNullConstant : public HConstant {
2230 public:
2231 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2232 return true;
2233 }
2234
David Brazdil77a48ae2015-09-15 12:34:04 +00002235 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2236
Roland Levillain9867bc72015-08-05 10:21:34 +01002237 size_t ComputeHashCode() const OVERRIDE { return 0; }
2238
2239 DECLARE_INSTRUCTION(NullConstant);
2240
2241 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002242 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002243
2244 friend class HGraph;
2245 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2246};
2247
2248// Constants of the type int. Those can be from Dex instructions, or
2249// synthesized (for example with the if-eqz instruction).
2250class HIntConstant : public HConstant {
2251 public:
2252 int32_t GetValue() const { return value_; }
2253
David Brazdil9f389d42015-10-01 14:32:56 +01002254 uint64_t GetValueAsUint64() const OVERRIDE {
2255 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2256 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002257
Roland Levillain9867bc72015-08-05 10:21:34 +01002258 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2259 DCHECK(other->IsIntConstant());
2260 return other->AsIntConstant()->value_ == value_;
2261 }
2262
2263 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2264
2265 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2266 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2267 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2268
2269 DECLARE_INSTRUCTION(IntConstant);
2270
2271 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002272 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2273 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2274 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2275 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002276
2277 const int32_t value_;
2278
2279 friend class HGraph;
2280 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2281 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2282 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2283};
2284
2285class HLongConstant : public HConstant {
2286 public:
2287 int64_t GetValue() const { return value_; }
2288
David Brazdil77a48ae2015-09-15 12:34:04 +00002289 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2290
Roland Levillain9867bc72015-08-05 10:21:34 +01002291 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2292 DCHECK(other->IsLongConstant());
2293 return other->AsLongConstant()->value_ == value_;
2294 }
2295
2296 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2297
2298 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2299 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2300 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2301
2302 DECLARE_INSTRUCTION(LongConstant);
2303
2304 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002305 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2306 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002307
2308 const int64_t value_;
2309
2310 friend class HGraph;
2311 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2312};
Dave Allison20dfc792014-06-16 20:44:29 -07002313
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002314// Conditional branch. A block ending with an HIf instruction must have
2315// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002316class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002317 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002318 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2319 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002320 SetRawInputAt(0, input);
2321 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002322
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002323 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002324
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002325 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002326 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002327 }
2328
2329 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002330 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002331 }
2332
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002333 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002334
2335 private:
2336 DISALLOW_COPY_AND_ASSIGN(HIf);
2337};
2338
David Brazdilfc6a86a2015-06-26 10:33:45 +00002339
2340// Abstract instruction which marks the beginning and/or end of a try block and
2341// links it to the respective exception handlers. Behaves the same as a Goto in
2342// non-exceptional control flow.
2343// Normal-flow successor is stored at index zero, exception handlers under
2344// higher indices in no particular order.
2345class HTryBoundary : public HTemplateInstruction<0> {
2346 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002347 enum BoundaryKind {
2348 kEntry,
2349 kExit,
2350 };
2351
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002352 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2353 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002354
2355 bool IsControlFlow() const OVERRIDE { return true; }
2356
2357 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002358 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002359
2360 // Returns whether `handler` is among its exception handlers (non-zero index
2361 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002362 bool HasExceptionHandler(const HBasicBlock& handler) const {
2363 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002364 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002365 }
2366
2367 // If not present already, adds `handler` to its block's list of exception
2368 // handlers.
2369 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002370 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002371 GetBlock()->AddSuccessor(handler);
2372 }
2373 }
2374
David Brazdil56e1acc2015-06-30 15:41:36 +01002375 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002376
David Brazdilffee3d32015-07-06 11:48:53 +01002377 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2378
David Brazdilfc6a86a2015-06-26 10:33:45 +00002379 DECLARE_INSTRUCTION(TryBoundary);
2380
2381 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002382 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002383
2384 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2385};
2386
David Brazdilffee3d32015-07-06 11:48:53 +01002387// Iterator over exception handlers of a given HTryBoundary, i.e. over
2388// exceptional successors of its basic block.
2389class HExceptionHandlerIterator : public ValueObject {
2390 public:
2391 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2392 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2393
Vladimir Marko60584552015-09-03 13:35:12 +00002394 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002395 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002396 size_t CurrentSuccessorIndex() const { return index_; }
2397 void Advance() { ++index_; }
2398
2399 private:
2400 const HBasicBlock& block_;
2401 size_t index_;
2402
2403 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2404};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002405
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002406// Deoptimize to interpreter, upon checking a condition.
2407class HDeoptimize : public HTemplateInstruction<1> {
2408 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002409 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2410 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002411 SetRawInputAt(0, cond);
2412 }
2413
2414 bool NeedsEnvironment() const OVERRIDE { return true; }
2415 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002416
2417 DECLARE_INSTRUCTION(Deoptimize);
2418
2419 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002420 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2421};
2422
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002423// Represents the ArtMethod that was passed as a first argument to
2424// the method. It is used by instructions that depend on it, like
2425// instructions that work with the dex cache.
2426class HCurrentMethod : public HExpression<0> {
2427 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002428 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2429 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002430
2431 DECLARE_INSTRUCTION(CurrentMethod);
2432
2433 private:
2434 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2435};
2436
Mark Mendellfe57faa2015-09-18 09:26:15 -04002437// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2438// have one successor for each entry in the switch table, and the final successor
2439// will be the block containing the next Dex opcode.
2440class HPackedSwitch : public HTemplateInstruction<1> {
2441 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002442 HPackedSwitch(int32_t start_value,
2443 uint32_t num_entries,
2444 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002445 uint32_t dex_pc = kNoDexPc)
2446 : HTemplateInstruction(SideEffects::None(), dex_pc),
2447 start_value_(start_value),
2448 num_entries_(num_entries) {
2449 SetRawInputAt(0, input);
2450 }
2451
2452 bool IsControlFlow() const OVERRIDE { return true; }
2453
2454 int32_t GetStartValue() const { return start_value_; }
2455
Vladimir Marko211c2112015-09-24 16:52:33 +01002456 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002457
2458 HBasicBlock* GetDefaultBlock() const {
2459 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002460 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002461 }
2462 DECLARE_INSTRUCTION(PackedSwitch);
2463
2464 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002465 const int32_t start_value_;
2466 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002467
2468 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2469};
2470
Roland Levillain88cb1752014-10-20 16:36:47 +01002471class HUnaryOperation : public HExpression<1> {
2472 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002473 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2474 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002475 SetRawInputAt(0, input);
2476 }
2477
2478 HInstruction* GetInput() const { return InputAt(0); }
2479 Primitive::Type GetResultType() const { return GetType(); }
2480
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002481 bool CanBeMoved() const OVERRIDE { return true; }
2482 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002483 UNUSED(other);
2484 return true;
2485 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002486
Roland Levillain9240d6a2014-10-20 16:47:04 +01002487 // Try to statically evaluate `operation` and return a HConstant
2488 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002489 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002490 HConstant* TryStaticEvaluation() const;
2491
2492 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002493 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2494 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002495
Roland Levillain88cb1752014-10-20 16:36:47 +01002496 DECLARE_INSTRUCTION(UnaryOperation);
2497
2498 private:
2499 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2500};
2501
Dave Allison20dfc792014-06-16 20:44:29 -07002502class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002503 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002504 HBinaryOperation(Primitive::Type result_type,
2505 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002506 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002507 SideEffects side_effects = SideEffects::None(),
2508 uint32_t dex_pc = kNoDexPc)
2509 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002510 SetRawInputAt(0, left);
2511 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002512 }
2513
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002514 HInstruction* GetLeft() const { return InputAt(0); }
2515 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002516 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002517
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002518 virtual bool IsCommutative() const { return false; }
2519
2520 // Put constant on the right.
2521 // Returns whether order is changed.
2522 bool OrderInputsWithConstantOnTheRight() {
2523 HInstruction* left = InputAt(0);
2524 HInstruction* right = InputAt(1);
2525 if (left->IsConstant() && !right->IsConstant()) {
2526 ReplaceInput(right, 0);
2527 ReplaceInput(left, 1);
2528 return true;
2529 }
2530 return false;
2531 }
2532
2533 // Order inputs by instruction id, but favor constant on the right side.
2534 // This helps GVN for commutative ops.
2535 void OrderInputs() {
2536 DCHECK(IsCommutative());
2537 HInstruction* left = InputAt(0);
2538 HInstruction* right = InputAt(1);
2539 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2540 return;
2541 }
2542 if (OrderInputsWithConstantOnTheRight()) {
2543 return;
2544 }
2545 // Order according to instruction id.
2546 if (left->GetId() > right->GetId()) {
2547 ReplaceInput(right, 0);
2548 ReplaceInput(left, 1);
2549 }
2550 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002551
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002552 bool CanBeMoved() const OVERRIDE { return true; }
2553 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002554 UNUSED(other);
2555 return true;
2556 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002557
Roland Levillain9240d6a2014-10-20 16:47:04 +01002558 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002559 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002560 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002561 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002562
2563 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002564 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2565 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2566 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2567 HLongConstant* y ATTRIBUTE_UNUSED) const {
2568 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2569 return nullptr;
2570 }
2571 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2572 HIntConstant* y ATTRIBUTE_UNUSED) const {
2573 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2574 return nullptr;
2575 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002576
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002577 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002578 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002579 HConstant* GetConstantRight() const;
2580
2581 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002582 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002583 HInstruction* GetLeastConstantLeft() const;
2584
Roland Levillainccc07a92014-09-16 14:48:16 +01002585 DECLARE_INSTRUCTION(BinaryOperation);
2586
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002587 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002588 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2589};
2590
Mark Mendellc4701932015-04-10 13:18:51 -04002591// The comparison bias applies for floating point operations and indicates how NaN
2592// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002593enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002594 kNoBias, // bias is not applicable (i.e. for long operation)
2595 kGtBias, // return 1 for NaN comparisons
2596 kLtBias, // return -1 for NaN comparisons
2597};
2598
Dave Allison20dfc792014-06-16 20:44:29 -07002599class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002600 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002601 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2602 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002603 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002604 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002605
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002606 bool NeedsMaterialization() const { return needs_materialization_; }
2607 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002608
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002609 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002610 // `instruction`, and disregard moves in between.
2611 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002612
Dave Allison20dfc792014-06-16 20:44:29 -07002613 DECLARE_INSTRUCTION(Condition);
2614
2615 virtual IfCondition GetCondition() const = 0;
2616
Mark Mendellc4701932015-04-10 13:18:51 -04002617 virtual IfCondition GetOppositeCondition() const = 0;
2618
Roland Levillain4fa13f62015-07-06 18:11:54 +01002619 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002620
2621 void SetBias(ComparisonBias bias) { bias_ = bias; }
2622
2623 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2624 return bias_ == other->AsCondition()->bias_;
2625 }
2626
Roland Levillain4fa13f62015-07-06 18:11:54 +01002627 bool IsFPConditionTrueIfNaN() const {
2628 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2629 IfCondition if_cond = GetCondition();
2630 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2631 }
2632
2633 bool IsFPConditionFalseIfNaN() const {
2634 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2635 IfCondition if_cond = GetCondition();
2636 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2637 }
2638
Dave Allison20dfc792014-06-16 20:44:29 -07002639 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002640 // For register allocation purposes, returns whether this instruction needs to be
2641 // materialized (that is, not just be in the processor flags).
2642 bool needs_materialization_;
2643
Mark Mendellc4701932015-04-10 13:18:51 -04002644 // Needed if we merge a HCompare into a HCondition.
2645 ComparisonBias bias_;
2646
Dave Allison20dfc792014-06-16 20:44:29 -07002647 DISALLOW_COPY_AND_ASSIGN(HCondition);
2648};
2649
2650// Instruction to check if two inputs are equal to each other.
2651class HEqual : public HCondition {
2652 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002653 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2654 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002655
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002656 bool IsCommutative() const OVERRIDE { return true; }
2657
Roland Levillain9867bc72015-08-05 10:21:34 +01002658 template <typename T> bool Compute(T x, T y) const { return x == y; }
2659
2660 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002661 return GetBlock()->GetGraph()->GetIntConstant(
2662 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002663 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002664 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002665 return GetBlock()->GetGraph()->GetIntConstant(
2666 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002667 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002668
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002669 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002670
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002671 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002672 return kCondEQ;
2673 }
2674
Mark Mendellc4701932015-04-10 13:18:51 -04002675 IfCondition GetOppositeCondition() const OVERRIDE {
2676 return kCondNE;
2677 }
2678
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002679 private:
2680 DISALLOW_COPY_AND_ASSIGN(HEqual);
2681};
2682
Dave Allison20dfc792014-06-16 20:44:29 -07002683class HNotEqual : public HCondition {
2684 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002685 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2686 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002687
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002688 bool IsCommutative() const OVERRIDE { return true; }
2689
Roland Levillain9867bc72015-08-05 10:21:34 +01002690 template <typename T> bool Compute(T x, T y) const { return x != y; }
2691
2692 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002693 return GetBlock()->GetGraph()->GetIntConstant(
2694 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002695 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002696 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002697 return GetBlock()->GetGraph()->GetIntConstant(
2698 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002699 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002700
Dave Allison20dfc792014-06-16 20:44:29 -07002701 DECLARE_INSTRUCTION(NotEqual);
2702
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002703 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002704 return kCondNE;
2705 }
2706
Mark Mendellc4701932015-04-10 13:18:51 -04002707 IfCondition GetOppositeCondition() const OVERRIDE {
2708 return kCondEQ;
2709 }
2710
Dave Allison20dfc792014-06-16 20:44:29 -07002711 private:
2712 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2713};
2714
2715class HLessThan : public HCondition {
2716 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002717 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2718 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002719
Roland Levillain9867bc72015-08-05 10:21:34 +01002720 template <typename T> bool Compute(T x, T y) const { return x < y; }
2721
2722 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002723 return GetBlock()->GetGraph()->GetIntConstant(
2724 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002725 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002726 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002727 return GetBlock()->GetGraph()->GetIntConstant(
2728 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002729 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002730
Dave Allison20dfc792014-06-16 20:44:29 -07002731 DECLARE_INSTRUCTION(LessThan);
2732
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002733 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002734 return kCondLT;
2735 }
2736
Mark Mendellc4701932015-04-10 13:18:51 -04002737 IfCondition GetOppositeCondition() const OVERRIDE {
2738 return kCondGE;
2739 }
2740
Dave Allison20dfc792014-06-16 20:44:29 -07002741 private:
2742 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2743};
2744
2745class HLessThanOrEqual : public HCondition {
2746 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002747 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2748 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002749
Roland Levillain9867bc72015-08-05 10:21:34 +01002750 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2751
2752 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002753 return GetBlock()->GetGraph()->GetIntConstant(
2754 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002755 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002756 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002757 return GetBlock()->GetGraph()->GetIntConstant(
2758 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002759 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002760
Dave Allison20dfc792014-06-16 20:44:29 -07002761 DECLARE_INSTRUCTION(LessThanOrEqual);
2762
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002763 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002764 return kCondLE;
2765 }
2766
Mark Mendellc4701932015-04-10 13:18:51 -04002767 IfCondition GetOppositeCondition() const OVERRIDE {
2768 return kCondGT;
2769 }
2770
Dave Allison20dfc792014-06-16 20:44:29 -07002771 private:
2772 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2773};
2774
2775class HGreaterThan : public HCondition {
2776 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002777 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2778 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002779
Roland Levillain9867bc72015-08-05 10:21:34 +01002780 template <typename T> bool Compute(T x, T y) const { return x > y; }
2781
2782 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002783 return GetBlock()->GetGraph()->GetIntConstant(
2784 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002785 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002786 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002787 return GetBlock()->GetGraph()->GetIntConstant(
2788 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002789 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002790
Dave Allison20dfc792014-06-16 20:44:29 -07002791 DECLARE_INSTRUCTION(GreaterThan);
2792
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002793 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002794 return kCondGT;
2795 }
2796
Mark Mendellc4701932015-04-10 13:18:51 -04002797 IfCondition GetOppositeCondition() const OVERRIDE {
2798 return kCondLE;
2799 }
2800
Dave Allison20dfc792014-06-16 20:44:29 -07002801 private:
2802 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2803};
2804
2805class HGreaterThanOrEqual : public HCondition {
2806 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002807 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2808 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002809
Roland Levillain9867bc72015-08-05 10:21:34 +01002810 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2811
2812 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002813 return GetBlock()->GetGraph()->GetIntConstant(
2814 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002815 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002816 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002817 return GetBlock()->GetGraph()->GetIntConstant(
2818 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002819 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002820
Dave Allison20dfc792014-06-16 20:44:29 -07002821 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2822
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002823 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002824 return kCondGE;
2825 }
2826
Mark Mendellc4701932015-04-10 13:18:51 -04002827 IfCondition GetOppositeCondition() const OVERRIDE {
2828 return kCondLT;
2829 }
2830
Dave Allison20dfc792014-06-16 20:44:29 -07002831 private:
2832 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2833};
2834
2835
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002836// Instruction to check how two inputs compare to each other.
2837// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2838class HCompare : public HBinaryOperation {
2839 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002840 HCompare(Primitive::Type type,
2841 HInstruction* first,
2842 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002843 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002844 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002845 : HBinaryOperation(Primitive::kPrimInt,
2846 first,
2847 second,
2848 SideEffectsForArchRuntimeCalls(type),
2849 dex_pc),
2850 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002851 DCHECK_EQ(type, first->GetType());
2852 DCHECK_EQ(type, second->GetType());
2853 }
2854
Roland Levillain9867bc72015-08-05 10:21:34 +01002855 template <typename T>
2856 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002857
Roland Levillain9867bc72015-08-05 10:21:34 +01002858 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002859 return GetBlock()->GetGraph()->GetIntConstant(
2860 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002861 }
2862 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002863 return GetBlock()->GetGraph()->GetIntConstant(
2864 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002865 }
2866
Calin Juravleddb7df22014-11-25 20:56:51 +00002867 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2868 return bias_ == other->AsCompare()->bias_;
2869 }
2870
Mark Mendellc4701932015-04-10 13:18:51 -04002871 ComparisonBias GetBias() const { return bias_; }
2872
Roland Levillain4fa13f62015-07-06 18:11:54 +01002873 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002874
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002875
2876 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2877 // MIPS64 uses a runtime call for FP comparisons.
2878 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2879 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002880
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002881 DECLARE_INSTRUCTION(Compare);
2882
2883 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002884 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002885
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002886 DISALLOW_COPY_AND_ASSIGN(HCompare);
2887};
2888
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002889// A local in the graph. Corresponds to a Dex register.
2890class HLocal : public HTemplateInstruction<0> {
2891 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002892 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002893 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002894
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002895 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002896
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002897 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002898
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002899 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002900 // The Dex register number.
2901 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002902
2903 DISALLOW_COPY_AND_ASSIGN(HLocal);
2904};
2905
2906// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002907class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002908 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002909 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2910 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002911 SetRawInputAt(0, local);
2912 }
2913
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002914 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2915
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002916 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002917
2918 private:
2919 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2920};
2921
2922// Store a value in a given local. This instruction has two inputs: the value
2923// and the local.
2924class HStoreLocal : public HTemplateInstruction<2> {
2925 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002926 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2927 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002928 SetRawInputAt(0, local);
2929 SetRawInputAt(1, value);
2930 }
2931
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002932 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2933
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002934 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002935
2936 private:
2937 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2938};
2939
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002940class HFloatConstant : public HConstant {
2941 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002942 float GetValue() const { return value_; }
2943
David Brazdil77a48ae2015-09-15 12:34:04 +00002944 uint64_t GetValueAsUint64() const OVERRIDE {
2945 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2946 }
2947
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002948 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002949 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002950 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002951 }
2952
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002953 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002954
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002955 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002956 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002957 }
2958 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002959 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002960 }
2961 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002962 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2963 }
2964 bool IsNaN() const {
2965 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002966 }
2967
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002968 DECLARE_INSTRUCTION(FloatConstant);
2969
2970 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002971 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2972 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2973 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2974 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002975
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002976 const float value_;
2977
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002978 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002979 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002980 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002981 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2982};
2983
2984class HDoubleConstant : public HConstant {
2985 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002986 double GetValue() const { return value_; }
2987
David Brazdil77a48ae2015-09-15 12:34:04 +00002988 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2989
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002990 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002991 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002992 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002993 }
2994
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002995 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002996
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002997 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002998 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002999 }
3000 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003001 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003002 }
3003 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003004 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3005 }
3006 bool IsNaN() const {
3007 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003008 }
3009
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003010 DECLARE_INSTRUCTION(DoubleConstant);
3011
3012 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003013 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3014 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3015 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3016 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003017
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003018 const double value_;
3019
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003020 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003021 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003022 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003023 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3024};
3025
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003026enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003027#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003028#include "intrinsics_list.h"
3029 kNone,
3030 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3031#undef INTRINSICS_LIST
3032#undef OPTIMIZING_INTRINSICS
3033};
3034std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3035
Agi Csaki05f20562015-08-19 14:58:14 -07003036enum IntrinsicNeedsEnvironmentOrCache {
3037 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3038 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003039};
3040
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003041class HInvoke : public HInstruction {
3042 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003043 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003044
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003045 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003046
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003047 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003048 SetRawInputAt(index, argument);
3049 }
3050
Roland Levillain3e3d7332015-04-28 11:00:54 +01003051 // Return the number of arguments. This number can be lower than
3052 // the number of inputs returned by InputCount(), as some invoke
3053 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3054 // inputs at the end of their list of inputs.
3055 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3056
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003057 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003058
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003059
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003060 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003061 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003062
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003063 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3064
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003065 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003066 return intrinsic_;
3067 }
3068
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003069 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003070
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003071 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003072 return GetEnvironment()->GetParent() != nullptr;
3073 }
3074
3075 bool CanThrow() const OVERRIDE { return true; }
3076
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003077 uint32_t* GetIntrinsicOptimizations() {
3078 return &intrinsic_optimizations_;
3079 }
3080
3081 const uint32_t* GetIntrinsicOptimizations() const {
3082 return &intrinsic_optimizations_;
3083 }
3084
3085 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3086
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003087 DECLARE_INSTRUCTION(Invoke);
3088
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003089 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003090 HInvoke(ArenaAllocator* arena,
3091 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003092 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003093 Primitive::Type return_type,
3094 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003095 uint32_t dex_method_index,
3096 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003097 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003098 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003099 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003100 inputs_(number_of_arguments + number_of_other_inputs,
3101 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003102 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003103 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003104 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003105 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003106 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003107 }
3108
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003109 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003110 return inputs_[index];
3111 }
3112
David Brazdil1abb4192015-02-17 18:33:36 +00003113 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003114 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003115 }
3116
Roland Levillain3e3d7332015-04-28 11:00:54 +01003117 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003118 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003119 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003120 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003121 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003122 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003123
3124 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3125 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003126
3127 private:
3128 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3129};
3130
Calin Juravle175dc732015-08-25 15:42:32 +01003131class HInvokeUnresolved : public HInvoke {
3132 public:
3133 HInvokeUnresolved(ArenaAllocator* arena,
3134 uint32_t number_of_arguments,
3135 Primitive::Type return_type,
3136 uint32_t dex_pc,
3137 uint32_t dex_method_index,
3138 InvokeType invoke_type)
3139 : HInvoke(arena,
3140 number_of_arguments,
3141 0u /* number_of_other_inputs */,
3142 return_type,
3143 dex_pc,
3144 dex_method_index,
3145 invoke_type) {
3146 }
3147
3148 DECLARE_INSTRUCTION(InvokeUnresolved);
3149
3150 private:
3151 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3152};
3153
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003154class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003155 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003156 // Requirements of this method call regarding the class
3157 // initialization (clinit) check of its declaring class.
3158 enum class ClinitCheckRequirement {
3159 kNone, // Class already initialized.
3160 kExplicit, // Static call having explicit clinit check as last input.
3161 kImplicit, // Static call implicitly requiring a clinit check.
3162 };
3163
Vladimir Marko58155012015-08-19 12:49:41 +00003164 // Determines how to load the target ArtMethod*.
3165 enum class MethodLoadKind {
3166 // Use a String init ArtMethod* loaded from Thread entrypoints.
3167 kStringInit,
3168
3169 // Use the method's own ArtMethod* loaded by the register allocator.
3170 kRecursive,
3171
3172 // Use ArtMethod* at a known address, embed the direct address in the code.
3173 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3174 kDirectAddress,
3175
3176 // Use ArtMethod* at an address that will be known at link time, embed the direct
3177 // address in the code. If the image is relocatable, emit .patch_oat entry.
3178 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3179 // the image relocatable or not.
3180 kDirectAddressWithFixup,
3181
3182 // Load from resoved methods array in the dex cache using a PC-relative load.
3183 // Used when we need to use the dex cache, for example for invoke-static that
3184 // may cause class initialization (the entry may point to a resolution method),
3185 // and we know that we can access the dex cache arrays using a PC-relative load.
3186 kDexCachePcRelative,
3187
3188 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3189 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3190 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3191 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3192 kDexCacheViaMethod,
3193 };
3194
3195 // Determines the location of the code pointer.
3196 enum class CodePtrLocation {
3197 // Recursive call, use local PC-relative call instruction.
3198 kCallSelf,
3199
3200 // Use PC-relative call instruction patched at link time.
3201 // Used for calls within an oat file, boot->boot or app->app.
3202 kCallPCRelative,
3203
3204 // Call to a known target address, embed the direct address in code.
3205 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3206 kCallDirect,
3207
3208 // Call to a target address that will be known at link time, embed the direct
3209 // address in code. If the image is relocatable, emit .patch_oat entry.
3210 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3211 // the image relocatable or not.
3212 kCallDirectWithFixup,
3213
3214 // Use code pointer from the ArtMethod*.
3215 // Used when we don't know the target code. This is also the last-resort-kind used when
3216 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3217 kCallArtMethod,
3218 };
3219
3220 struct DispatchInfo {
3221 const MethodLoadKind method_load_kind;
3222 const CodePtrLocation code_ptr_location;
3223 // The method load data holds
3224 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3225 // Note that there are multiple string init methods, each having its own offset.
3226 // - the method address for kDirectAddress
3227 // - the dex cache arrays offset for kDexCachePcRel.
3228 const uint64_t method_load_data;
3229 const uint64_t direct_code_ptr;
3230 };
3231
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003232 HInvokeStaticOrDirect(ArenaAllocator* arena,
3233 uint32_t number_of_arguments,
3234 Primitive::Type return_type,
3235 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003236 uint32_t method_index,
3237 MethodReference target_method,
3238 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003239 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003240 InvokeType invoke_type,
3241 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003242 : HInvoke(arena,
3243 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003244 // There is one extra argument for the HCurrentMethod node, and
3245 // potentially one other if the clinit check is explicit, and one other
3246 // if the method is a string factory.
3247 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003248 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003249 return_type,
3250 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003251 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003252 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003253 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003254 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003255 target_method_(target_method),
3256 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003257
Calin Juravle641547a2015-04-21 22:08:51 +01003258 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3259 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003260 // We access the method via the dex cache so we can't do an implicit null check.
3261 // TODO: for intrinsics we can generate implicit null checks.
3262 return false;
3263 }
3264
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003265 bool CanBeNull() const OVERRIDE {
3266 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3267 }
3268
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003269 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003270 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3271 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3272 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003273 bool NeedsDexCache() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003274 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003275 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003276 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3277 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3278 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3279 MethodReference GetTargetMethod() const { return target_method_; }
3280
3281 int32_t GetStringInitOffset() const {
3282 DCHECK(IsStringInit());
3283 return dispatch_info_.method_load_data;
3284 }
3285
3286 uint64_t GetMethodAddress() const {
3287 DCHECK(HasMethodAddress());
3288 return dispatch_info_.method_load_data;
3289 }
3290
3291 uint32_t GetDexCacheArrayOffset() const {
3292 DCHECK(HasPcRelDexCache());
3293 return dispatch_info_.method_load_data;
3294 }
3295
3296 uint64_t GetDirectCodePtr() const {
3297 DCHECK(HasDirectCodePtr());
3298 return dispatch_info_.direct_code_ptr;
3299 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003300
Calin Juravle68ad6492015-08-18 17:08:12 +01003301 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3302
Roland Levillain4c0eb422015-04-24 16:43:49 +01003303 // Is this instruction a call to a static method?
3304 bool IsStatic() const {
3305 return GetInvokeType() == kStatic;
3306 }
3307
Roland Levillain3e3d7332015-04-28 11:00:54 +01003308 // Remove the art::HLoadClass instruction set as last input by
3309 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3310 // the initial art::HClinitCheck instruction (only relevant for
3311 // static calls with explicit clinit check).
3312 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003313 DCHECK(IsStaticWithExplicitClinitCheck());
3314 size_t last_input_index = InputCount() - 1;
3315 HInstruction* last_input = InputAt(last_input_index);
3316 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003317 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003318 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003319 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003320 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3321 DCHECK(IsStaticWithImplicitClinitCheck());
3322 }
3323
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003324 bool IsStringFactoryFor(HFakeString* str) const {
3325 if (!IsStringInit()) return false;
3326 // +1 for the current method.
3327 if (InputCount() == (number_of_arguments_ + 1)) return false;
3328 return InputAt(InputCount() - 1)->AsFakeString() == str;
3329 }
3330
3331 void RemoveFakeStringArgumentAsLastInput() {
3332 DCHECK(IsStringInit());
3333 size_t last_input_index = InputCount() - 1;
3334 HInstruction* last_input = InputAt(last_input_index);
3335 DCHECK(last_input != nullptr);
3336 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3337 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003338 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003339 }
3340
Roland Levillain4c0eb422015-04-24 16:43:49 +01003341 // Is this a call to a static method whose declaring class has an
3342 // explicit intialization check in the graph?
3343 bool IsStaticWithExplicitClinitCheck() const {
3344 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3345 }
3346
3347 // Is this a call to a static method whose declaring class has an
3348 // implicit intialization check requirement?
3349 bool IsStaticWithImplicitClinitCheck() const {
3350 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3351 }
3352
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003353 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003354
Roland Levillain4c0eb422015-04-24 16:43:49 +01003355 protected:
3356 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3357 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3358 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3359 HInstruction* input = input_record.GetInstruction();
3360 // `input` is the last input of a static invoke marked as having
3361 // an explicit clinit check. It must either be:
3362 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3363 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3364 DCHECK(input != nullptr);
3365 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3366 }
3367 return input_record;
3368 }
3369
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003370 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003371 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003372 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003373 // The target method may refer to different dex file or method index than the original
3374 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3375 // in derived class to increase visibility.
3376 MethodReference target_method_;
3377 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003378
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003379 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003380};
3381
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003382class HInvokeVirtual : public HInvoke {
3383 public:
3384 HInvokeVirtual(ArenaAllocator* arena,
3385 uint32_t number_of_arguments,
3386 Primitive::Type return_type,
3387 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003388 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003389 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003390 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003391 vtable_index_(vtable_index) {}
3392
Calin Juravle641547a2015-04-21 22:08:51 +01003393 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003394 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003395 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003396 }
3397
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003398 uint32_t GetVTableIndex() const { return vtable_index_; }
3399
3400 DECLARE_INSTRUCTION(InvokeVirtual);
3401
3402 private:
3403 const uint32_t vtable_index_;
3404
3405 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3406};
3407
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003408class HInvokeInterface : public HInvoke {
3409 public:
3410 HInvokeInterface(ArenaAllocator* arena,
3411 uint32_t number_of_arguments,
3412 Primitive::Type return_type,
3413 uint32_t dex_pc,
3414 uint32_t dex_method_index,
3415 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003416 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003417 imt_index_(imt_index) {}
3418
Calin Juravle641547a2015-04-21 22:08:51 +01003419 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003420 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003421 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003422 }
3423
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003424 uint32_t GetImtIndex() const { return imt_index_; }
3425 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3426
3427 DECLARE_INSTRUCTION(InvokeInterface);
3428
3429 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003430 const uint32_t imt_index_;
3431
3432 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3433};
3434
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003435class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003436 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003437 HNewInstance(HCurrentMethod* current_method,
3438 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003439 uint16_t type_index,
3440 const DexFile& dex_file,
3441 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003442 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003443 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003444 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003445 entrypoint_(entrypoint) {
3446 SetRawInputAt(0, current_method);
3447 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003448
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003449 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003450 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003451
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003452 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003453 bool NeedsEnvironment() const OVERRIDE { return true; }
3454 // It may throw when called on:
3455 // - interfaces
3456 // - abstract/innaccessible/unknown classes
3457 // TODO: optimize when possible.
3458 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003459
Calin Juravle10e244f2015-01-26 18:54:32 +00003460 bool CanBeNull() const OVERRIDE { return false; }
3461
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003462 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3463
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003464 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003465
3466 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003467 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003468 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003469 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003470
3471 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3472};
3473
Roland Levillain88cb1752014-10-20 16:36:47 +01003474class HNeg : public HUnaryOperation {
3475 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003476 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3477 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003478
Roland Levillain9867bc72015-08-05 10:21:34 +01003479 template <typename T> T Compute(T x) const { return -x; }
3480
3481 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003482 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003483 }
3484 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003485 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003486 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003487
Roland Levillain88cb1752014-10-20 16:36:47 +01003488 DECLARE_INSTRUCTION(Neg);
3489
3490 private:
3491 DISALLOW_COPY_AND_ASSIGN(HNeg);
3492};
3493
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003494class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003495 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003496 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003497 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003498 uint32_t dex_pc,
3499 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003500 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003501 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003502 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003503 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003504 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003505 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003506 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003507 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003508 }
3509
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003510 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003511 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003512
3513 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003514 bool NeedsEnvironment() const OVERRIDE { return true; }
3515
Mingyao Yang0c365e62015-03-31 15:09:29 -07003516 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3517 bool CanThrow() const OVERRIDE { return true; }
3518
Calin Juravle10e244f2015-01-26 18:54:32 +00003519 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003520
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003521 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3522
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003523 DECLARE_INSTRUCTION(NewArray);
3524
3525 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003526 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003527 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003528 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003529
3530 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3531};
3532
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003533class HAdd : public HBinaryOperation {
3534 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003535 HAdd(Primitive::Type result_type,
3536 HInstruction* left,
3537 HInstruction* right,
3538 uint32_t dex_pc = kNoDexPc)
3539 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003540
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003541 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003542
Roland Levillain9867bc72015-08-05 10:21:34 +01003543 template <typename T> T Compute(T x, T y) const { return x + y; }
3544
3545 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003546 return GetBlock()->GetGraph()->GetIntConstant(
3547 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003548 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003549 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003550 return GetBlock()->GetGraph()->GetLongConstant(
3551 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003552 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003553
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003554 DECLARE_INSTRUCTION(Add);
3555
3556 private:
3557 DISALLOW_COPY_AND_ASSIGN(HAdd);
3558};
3559
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003560class HSub : public HBinaryOperation {
3561 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003562 HSub(Primitive::Type result_type,
3563 HInstruction* left,
3564 HInstruction* right,
3565 uint32_t dex_pc = kNoDexPc)
3566 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003567
Roland Levillain9867bc72015-08-05 10:21:34 +01003568 template <typename T> T Compute(T x, T y) const { return x - y; }
3569
3570 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003571 return GetBlock()->GetGraph()->GetIntConstant(
3572 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003573 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003574 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003575 return GetBlock()->GetGraph()->GetLongConstant(
3576 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003577 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003578
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003579 DECLARE_INSTRUCTION(Sub);
3580
3581 private:
3582 DISALLOW_COPY_AND_ASSIGN(HSub);
3583};
3584
Calin Juravle34bacdf2014-10-07 20:23:36 +01003585class HMul : public HBinaryOperation {
3586 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003587 HMul(Primitive::Type result_type,
3588 HInstruction* left,
3589 HInstruction* right,
3590 uint32_t dex_pc = kNoDexPc)
3591 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003592
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003593 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003594
Roland Levillain9867bc72015-08-05 10:21:34 +01003595 template <typename T> T Compute(T x, T y) const { return x * y; }
3596
3597 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003598 return GetBlock()->GetGraph()->GetIntConstant(
3599 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003600 }
3601 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003602 return GetBlock()->GetGraph()->GetLongConstant(
3603 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003604 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003605
3606 DECLARE_INSTRUCTION(Mul);
3607
3608 private:
3609 DISALLOW_COPY_AND_ASSIGN(HMul);
3610};
3611
Calin Juravle7c4954d2014-10-28 16:57:40 +00003612class HDiv : public HBinaryOperation {
3613 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003614 HDiv(Primitive::Type result_type,
3615 HInstruction* left,
3616 HInstruction* right,
3617 uint32_t dex_pc)
3618 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003619
Roland Levillain9867bc72015-08-05 10:21:34 +01003620 template <typename T>
3621 T Compute(T x, T y) const {
3622 // Our graph structure ensures we never have 0 for `y` during
3623 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003624 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003625 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003626 return (y == -1) ? -x : x / y;
3627 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003628
Roland Levillain9867bc72015-08-05 10:21:34 +01003629 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003630 return GetBlock()->GetGraph()->GetIntConstant(
3631 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003632 }
3633 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003634 return GetBlock()->GetGraph()->GetLongConstant(
3635 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003636 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003637
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003638 static SideEffects SideEffectsForArchRuntimeCalls() {
3639 // The generated code can use a runtime call.
3640 return SideEffects::CanTriggerGC();
3641 }
3642
Calin Juravle7c4954d2014-10-28 16:57:40 +00003643 DECLARE_INSTRUCTION(Div);
3644
3645 private:
3646 DISALLOW_COPY_AND_ASSIGN(HDiv);
3647};
3648
Calin Juravlebacfec32014-11-14 15:54:36 +00003649class HRem : public HBinaryOperation {
3650 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003651 HRem(Primitive::Type result_type,
3652 HInstruction* left,
3653 HInstruction* right,
3654 uint32_t dex_pc)
3655 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003656
Roland Levillain9867bc72015-08-05 10:21:34 +01003657 template <typename T>
3658 T Compute(T x, T y) const {
3659 // Our graph structure ensures we never have 0 for `y` during
3660 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003661 DCHECK_NE(y, 0);
3662 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3663 return (y == -1) ? 0 : x % y;
3664 }
3665
Roland Levillain9867bc72015-08-05 10:21:34 +01003666 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003667 return GetBlock()->GetGraph()->GetIntConstant(
3668 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003669 }
3670 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003671 return GetBlock()->GetGraph()->GetLongConstant(
3672 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003673 }
3674
Calin Juravlebacfec32014-11-14 15:54:36 +00003675
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003676 static SideEffects SideEffectsForArchRuntimeCalls() {
3677 return SideEffects::CanTriggerGC();
3678 }
3679
Calin Juravlebacfec32014-11-14 15:54:36 +00003680 DECLARE_INSTRUCTION(Rem);
3681
3682 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003683 DISALLOW_COPY_AND_ASSIGN(HRem);
3684};
3685
Calin Juravled0d48522014-11-04 16:40:20 +00003686class HDivZeroCheck : public HExpression<1> {
3687 public:
3688 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003689 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003690 SetRawInputAt(0, value);
3691 }
3692
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003693 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3694
Calin Juravled0d48522014-11-04 16:40:20 +00003695 bool CanBeMoved() const OVERRIDE { return true; }
3696
3697 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3698 UNUSED(other);
3699 return true;
3700 }
3701
3702 bool NeedsEnvironment() const OVERRIDE { return true; }
3703 bool CanThrow() const OVERRIDE { return true; }
3704
Calin Juravled0d48522014-11-04 16:40:20 +00003705 DECLARE_INSTRUCTION(DivZeroCheck);
3706
3707 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003708 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3709};
3710
Calin Juravle9aec02f2014-11-18 23:06:35 +00003711class HShl : public HBinaryOperation {
3712 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003713 HShl(Primitive::Type result_type,
3714 HInstruction* left,
3715 HInstruction* right,
3716 uint32_t dex_pc = kNoDexPc)
3717 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003718
Roland Levillain9867bc72015-08-05 10:21:34 +01003719 template <typename T, typename U, typename V>
3720 T Compute(T x, U y, V max_shift_value) const {
3721 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3722 "V is not the unsigned integer type corresponding to T");
3723 return x << (y & max_shift_value);
3724 }
3725
3726 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3727 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003728 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003729 }
3730 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3731 // case is handled as `x << static_cast<int>(y)`.
3732 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3733 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003734 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003735 }
3736 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3737 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003738 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003739 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003740
3741 DECLARE_INSTRUCTION(Shl);
3742
3743 private:
3744 DISALLOW_COPY_AND_ASSIGN(HShl);
3745};
3746
3747class HShr : public HBinaryOperation {
3748 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003749 HShr(Primitive::Type result_type,
3750 HInstruction* left,
3751 HInstruction* right,
3752 uint32_t dex_pc = kNoDexPc)
3753 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003754
Roland Levillain9867bc72015-08-05 10:21:34 +01003755 template <typename T, typename U, typename V>
3756 T Compute(T x, U y, V max_shift_value) const {
3757 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3758 "V is not the unsigned integer type corresponding to T");
3759 return x >> (y & max_shift_value);
3760 }
3761
3762 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3763 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003764 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003765 }
3766 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3767 // case is handled as `x >> static_cast<int>(y)`.
3768 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3769 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003770 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003771 }
3772 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3773 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003774 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003775 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003776
3777 DECLARE_INSTRUCTION(Shr);
3778
3779 private:
3780 DISALLOW_COPY_AND_ASSIGN(HShr);
3781};
3782
3783class HUShr : public HBinaryOperation {
3784 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003785 HUShr(Primitive::Type result_type,
3786 HInstruction* left,
3787 HInstruction* right,
3788 uint32_t dex_pc = kNoDexPc)
3789 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003790
Roland Levillain9867bc72015-08-05 10:21:34 +01003791 template <typename T, typename U, typename V>
3792 T Compute(T x, U y, V max_shift_value) const {
3793 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3794 "V is not the unsigned integer type corresponding to T");
3795 V ux = static_cast<V>(x);
3796 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003797 }
3798
Roland Levillain9867bc72015-08-05 10:21:34 +01003799 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3800 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003801 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003802 }
3803 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3804 // case is handled as `x >>> static_cast<int>(y)`.
3805 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3806 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003807 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003808 }
3809 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3810 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003811 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003812 }
3813
3814 DECLARE_INSTRUCTION(UShr);
3815
3816 private:
3817 DISALLOW_COPY_AND_ASSIGN(HUShr);
3818};
3819
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003820class HAnd : public HBinaryOperation {
3821 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003822 HAnd(Primitive::Type result_type,
3823 HInstruction* left,
3824 HInstruction* right,
3825 uint32_t dex_pc = kNoDexPc)
3826 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003827
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003828 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003829
Roland Levillain9867bc72015-08-05 10:21:34 +01003830 template <typename T, typename U>
3831 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3832
3833 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003834 return GetBlock()->GetGraph()->GetIntConstant(
3835 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003836 }
3837 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003838 return GetBlock()->GetGraph()->GetLongConstant(
3839 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003840 }
3841 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003842 return GetBlock()->GetGraph()->GetLongConstant(
3843 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003844 }
3845 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003846 return GetBlock()->GetGraph()->GetLongConstant(
3847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003848 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003849
3850 DECLARE_INSTRUCTION(And);
3851
3852 private:
3853 DISALLOW_COPY_AND_ASSIGN(HAnd);
3854};
3855
3856class HOr : public HBinaryOperation {
3857 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003858 HOr(Primitive::Type result_type,
3859 HInstruction* left,
3860 HInstruction* right,
3861 uint32_t dex_pc = kNoDexPc)
3862 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003863
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003864 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003865
Roland Levillain9867bc72015-08-05 10:21:34 +01003866 template <typename T, typename U>
3867 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3868
3869 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003870 return GetBlock()->GetGraph()->GetIntConstant(
3871 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003872 }
3873 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003874 return GetBlock()->GetGraph()->GetLongConstant(
3875 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003876 }
3877 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003878 return GetBlock()->GetGraph()->GetLongConstant(
3879 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003880 }
3881 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003882 return GetBlock()->GetGraph()->GetLongConstant(
3883 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003884 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003885
3886 DECLARE_INSTRUCTION(Or);
3887
3888 private:
3889 DISALLOW_COPY_AND_ASSIGN(HOr);
3890};
3891
3892class HXor : public HBinaryOperation {
3893 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003894 HXor(Primitive::Type result_type,
3895 HInstruction* left,
3896 HInstruction* right,
3897 uint32_t dex_pc = kNoDexPc)
3898 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003899
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003900 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003901
Roland Levillain9867bc72015-08-05 10:21:34 +01003902 template <typename T, typename U>
3903 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3904
3905 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003906 return GetBlock()->GetGraph()->GetIntConstant(
3907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003908 }
3909 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003910 return GetBlock()->GetGraph()->GetLongConstant(
3911 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003912 }
3913 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003914 return GetBlock()->GetGraph()->GetLongConstant(
3915 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003916 }
3917 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003918 return GetBlock()->GetGraph()->GetLongConstant(
3919 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003920 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003921
3922 DECLARE_INSTRUCTION(Xor);
3923
3924 private:
3925 DISALLOW_COPY_AND_ASSIGN(HXor);
3926};
3927
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003928// The value of a parameter in this method. Its location depends on
3929// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003930class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003931 public:
Calin Juravlec05aca72015-10-13 13:10:33 +00003932 HParameterValue(uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003933 Primitive::Type parameter_type,
3934 bool is_this = false)
3935 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003936 index_(index),
3937 is_this_(is_this),
3938 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003939
3940 uint8_t GetIndex() const { return index_; }
3941
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003942 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3943 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003944
Calin Juravlec05aca72015-10-13 13:10:33 +00003945 bool IsThis() const { return is_this_; }
3946
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003947 DECLARE_INSTRUCTION(ParameterValue);
3948
3949 private:
3950 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003951 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003952 const uint8_t index_;
3953
Calin Juravle10e244f2015-01-26 18:54:32 +00003954 // Whether or not the parameter value corresponds to 'this' argument.
3955 const bool is_this_;
3956
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003957 bool can_be_null_;
3958
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003959 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3960};
3961
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003962class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003963 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003964 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3965 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003966
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003967 bool CanBeMoved() const OVERRIDE { return true; }
3968 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003969 UNUSED(other);
3970 return true;
3971 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003972
Roland Levillain9867bc72015-08-05 10:21:34 +01003973 template <typename T> T Compute(T x) const { return ~x; }
3974
3975 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003976 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003977 }
3978 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003979 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003980 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003981
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003982 DECLARE_INSTRUCTION(Not);
3983
3984 private:
3985 DISALLOW_COPY_AND_ASSIGN(HNot);
3986};
3987
David Brazdil66d126e2015-04-03 16:02:44 +01003988class HBooleanNot : public HUnaryOperation {
3989 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003990 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3991 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003992
3993 bool CanBeMoved() const OVERRIDE { return true; }
3994 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3995 UNUSED(other);
3996 return true;
3997 }
3998
Roland Levillain9867bc72015-08-05 10:21:34 +01003999 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004000 DCHECK(IsUint<1>(x));
4001 return !x;
4002 }
4003
Roland Levillain9867bc72015-08-05 10:21:34 +01004004 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004005 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004006 }
4007 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4008 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004009 UNREACHABLE();
4010 }
4011
4012 DECLARE_INSTRUCTION(BooleanNot);
4013
4014 private:
4015 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4016};
4017
Roland Levillaindff1f282014-11-05 14:15:05 +00004018class HTypeConversion : public HExpression<1> {
4019 public:
4020 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004021 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004022 : HExpression(result_type,
4023 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4024 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004025 SetRawInputAt(0, input);
4026 DCHECK_NE(input->GetType(), result_type);
4027 }
4028
4029 HInstruction* GetInput() const { return InputAt(0); }
4030 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4031 Primitive::Type GetResultType() const { return GetType(); }
4032
Roland Levillain624279f2014-12-04 11:54:28 +00004033 // Required by the x86 and ARM code generators when producing calls
4034 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004035
Roland Levillaindff1f282014-11-05 14:15:05 +00004036 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004037 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004038
Mark Mendelle82549b2015-05-06 10:55:34 -04004039 // Try to statically evaluate the conversion and return a HConstant
4040 // containing the result. If the input cannot be converted, return nullptr.
4041 HConstant* TryStaticEvaluation() const;
4042
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004043 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4044 Primitive::Type result_type) {
4045 // Some architectures may not require the 'GC' side effects, but at this point
4046 // in the compilation process we do not know what architecture we will
4047 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004048 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4049 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004050 return SideEffects::CanTriggerGC();
4051 }
4052 return SideEffects::None();
4053 }
4054
Roland Levillaindff1f282014-11-05 14:15:05 +00004055 DECLARE_INSTRUCTION(TypeConversion);
4056
4057 private:
4058 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4059};
4060
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004061static constexpr uint32_t kNoRegNumber = -1;
4062
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004063class HPhi : public HInstruction {
4064 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004065 HPhi(ArenaAllocator* arena,
4066 uint32_t reg_number,
4067 size_t number_of_inputs,
4068 Primitive::Type type,
4069 uint32_t dex_pc = kNoDexPc)
4070 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004071 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004072 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004073 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004074 is_live_(false),
4075 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004076 }
4077
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004078 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4079 static Primitive::Type ToPhiType(Primitive::Type type) {
4080 switch (type) {
4081 case Primitive::kPrimBoolean:
4082 case Primitive::kPrimByte:
4083 case Primitive::kPrimShort:
4084 case Primitive::kPrimChar:
4085 return Primitive::kPrimInt;
4086 default:
4087 return type;
4088 }
4089 }
4090
David Brazdilffee3d32015-07-06 11:48:53 +01004091 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4092
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004093 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004094
4095 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004096 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004097
Calin Juravle10e244f2015-01-26 18:54:32 +00004098 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004099 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004100
Calin Juravle10e244f2015-01-26 18:54:32 +00004101 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4102 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4103
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004104 uint32_t GetRegNumber() const { return reg_number_; }
4105
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004106 void SetDead() { is_live_ = false; }
4107 void SetLive() { is_live_ = true; }
4108 bool IsDead() const { return !is_live_; }
4109 bool IsLive() const { return is_live_; }
4110
David Brazdil77a48ae2015-09-15 12:34:04 +00004111 bool IsVRegEquivalentOf(HInstruction* other) const {
4112 return other != nullptr
4113 && other->IsPhi()
4114 && other->AsPhi()->GetBlock() == GetBlock()
4115 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4116 }
4117
Calin Juravlea4f88312015-04-16 12:57:19 +01004118 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4119 // An equivalent phi is a phi having the same dex register and type.
4120 // It assumes that phis with the same dex register are adjacent.
4121 HPhi* GetNextEquivalentPhiWithSameType() {
4122 HInstruction* next = GetNext();
4123 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4124 if (next->GetType() == GetType()) {
4125 return next->AsPhi();
4126 }
4127 next = next->GetNext();
4128 }
4129 return nullptr;
4130 }
4131
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004132 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004133
David Brazdil1abb4192015-02-17 18:33:36 +00004134 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004135 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004136 return inputs_[index];
4137 }
David Brazdil1abb4192015-02-17 18:33:36 +00004138
4139 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004140 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004141 }
4142
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004143 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004144 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004145 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004146 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004147 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004148 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004149
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004150 DISALLOW_COPY_AND_ASSIGN(HPhi);
4151};
4152
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004153class HNullCheck : public HExpression<1> {
4154 public:
4155 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004156 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004157 SetRawInputAt(0, value);
4158 }
4159
Calin Juravle10e244f2015-01-26 18:54:32 +00004160 bool CanBeMoved() const OVERRIDE { return true; }
4161 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004162 UNUSED(other);
4163 return true;
4164 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004165
Calin Juravle10e244f2015-01-26 18:54:32 +00004166 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004167
Calin Juravle10e244f2015-01-26 18:54:32 +00004168 bool CanThrow() const OVERRIDE { return true; }
4169
4170 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004171
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004172
4173 DECLARE_INSTRUCTION(NullCheck);
4174
4175 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004176 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4177};
4178
4179class FieldInfo : public ValueObject {
4180 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004181 FieldInfo(MemberOffset field_offset,
4182 Primitive::Type field_type,
4183 bool is_volatile,
4184 uint32_t index,
Mingyao Yang589dac72015-08-24 11:21:42 -07004185 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004186 const DexFile& dex_file,
4187 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004188 : field_offset_(field_offset),
4189 field_type_(field_type),
4190 is_volatile_(is_volatile),
4191 index_(index),
Mingyao Yang589dac72015-08-24 11:21:42 -07004192 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004193 dex_file_(dex_file),
4194 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004195
4196 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004197 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004198 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang589dac72015-08-24 11:21:42 -07004199 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004200 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004201 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004202 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004203
4204 private:
4205 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004206 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004207 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004208 const uint32_t index_;
Mingyao Yang589dac72015-08-24 11:21:42 -07004209 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004210 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004211 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004212};
4213
4214class HInstanceFieldGet : public HExpression<1> {
4215 public:
4216 HInstanceFieldGet(HInstruction* value,
4217 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004218 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004219 bool is_volatile,
4220 uint32_t field_idx,
Mingyao Yang589dac72015-08-24 11:21:42 -07004221 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004222 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004223 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004224 uint32_t dex_pc)
Mingyao Yang589dac72015-08-24 11:21:42 -07004225 : HExpression(field_type,
4226 SideEffects::FieldReadOfType(field_type, is_volatile),
4227 dex_pc),
4228 field_info_(field_offset,
4229 field_type,
4230 is_volatile,
4231 field_idx,
4232 declaring_class_def_index,
4233 dex_file,
4234 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004235 SetRawInputAt(0, value);
4236 }
4237
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004238 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004239
4240 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4241 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4242 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004243 }
4244
Calin Juravle641547a2015-04-21 22:08:51 +01004245 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4246 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004247 }
4248
4249 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004250 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4251 }
4252
Calin Juravle52c48962014-12-16 17:02:57 +00004253 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004254 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004255 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004256 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004257
4258 DECLARE_INSTRUCTION(InstanceFieldGet);
4259
4260 private:
4261 const FieldInfo field_info_;
4262
4263 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4264};
4265
4266class HInstanceFieldSet : public HTemplateInstruction<2> {
4267 public:
4268 HInstanceFieldSet(HInstruction* object,
4269 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004270 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004271 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004272 bool is_volatile,
4273 uint32_t field_idx,
Mingyao Yang589dac72015-08-24 11:21:42 -07004274 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004275 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004276 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004277 uint32_t dex_pc)
Mingyao Yang589dac72015-08-24 11:21:42 -07004278 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4279 dex_pc),
4280 field_info_(field_offset,
4281 field_type,
4282 is_volatile,
4283 field_idx,
4284 declaring_class_def_index,
4285 dex_file,
4286 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004287 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004288 SetRawInputAt(0, object);
4289 SetRawInputAt(1, value);
4290 }
4291
Calin Juravle641547a2015-04-21 22:08:51 +01004292 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4293 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004294 }
4295
Calin Juravle52c48962014-12-16 17:02:57 +00004296 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004297 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004298 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004299 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004300 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004301 bool GetValueCanBeNull() const { return value_can_be_null_; }
4302 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004303
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004304 DECLARE_INSTRUCTION(InstanceFieldSet);
4305
4306 private:
4307 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004308 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004309
4310 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4311};
4312
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004313class HArrayGet : public HExpression<2> {
4314 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004315 HArrayGet(HInstruction* array,
4316 HInstruction* index,
4317 Primitive::Type type,
Calin Juravle154746b2015-10-06 15:46:54 +01004318 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004319 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004320 SetRawInputAt(0, array);
4321 SetRawInputAt(1, index);
4322 }
4323
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004324 bool CanBeMoved() const OVERRIDE { return true; }
4325 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004326 UNUSED(other);
4327 return true;
4328 }
Calin Juravle641547a2015-04-21 22:08:51 +01004329 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4330 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004331 // TODO: We can be smarter here.
4332 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4333 // which generates the implicit null check. There are cases when these can be removed
4334 // to produce better code. If we ever add optimizations to do so we should allow an
4335 // implicit check here (as long as the address falls in the first page).
4336 return false;
4337 }
4338
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004339 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004340
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004341 HInstruction* GetArray() const { return InputAt(0); }
4342 HInstruction* GetIndex() const { return InputAt(1); }
4343
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004344 DECLARE_INSTRUCTION(ArrayGet);
4345
4346 private:
4347 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4348};
4349
4350class HArraySet : public HTemplateInstruction<3> {
4351 public:
4352 HArraySet(HInstruction* array,
4353 HInstruction* index,
4354 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004355 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004356 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004357 : HTemplateInstruction(
4358 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004359 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004360 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004361 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004362 value_can_be_null_(true),
4363 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004364 SetRawInputAt(0, array);
4365 SetRawInputAt(1, index);
4366 SetRawInputAt(2, value);
4367 }
4368
Calin Juravle77520bc2015-01-12 18:45:46 +00004369 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004370 // We currently always call a runtime method to catch array store
4371 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004372 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004373 }
4374
Mingyao Yang81014cb2015-06-02 03:16:27 -07004375 // Can throw ArrayStoreException.
4376 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4377
Calin Juravle641547a2015-04-21 22:08:51 +01004378 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4379 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004380 // TODO: Same as for ArrayGet.
4381 return false;
4382 }
4383
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004384 void ClearNeedsTypeCheck() {
4385 needs_type_check_ = false;
4386 }
4387
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004388 void ClearValueCanBeNull() {
4389 value_can_be_null_ = false;
4390 }
4391
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004392 void SetStaticTypeOfArrayIsObjectArray() {
4393 static_type_of_array_is_object_array_ = true;
4394 }
4395
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004396 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004397 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004398 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004399
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004400 HInstruction* GetArray() const { return InputAt(0); }
4401 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004402 HInstruction* GetValue() const { return InputAt(2); }
4403
4404 Primitive::Type GetComponentType() const {
4405 // The Dex format does not type floating point index operations. Since the
4406 // `expected_component_type_` is set during building and can therefore not
4407 // be correct, we also check what is the value type. If it is a floating
4408 // point type, we must use that type.
4409 Primitive::Type value_type = GetValue()->GetType();
4410 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4411 ? value_type
4412 : expected_component_type_;
4413 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004414
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004415 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4416 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4417 }
4418
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004419 DECLARE_INSTRUCTION(ArraySet);
4420
4421 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004422 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004423 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004424 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004425 // Cached information for the reference_type_info_ so that codegen
4426 // does not need to inspect the static type.
4427 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004428
4429 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4430};
4431
4432class HArrayLength : public HExpression<1> {
4433 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004434 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004435 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004436 // Note that arrays do not change length, so the instruction does not
4437 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004438 SetRawInputAt(0, array);
4439 }
4440
Calin Juravle77520bc2015-01-12 18:45:46 +00004441 bool CanBeMoved() const OVERRIDE { return true; }
4442 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004443 UNUSED(other);
4444 return true;
4445 }
Calin Juravle641547a2015-04-21 22:08:51 +01004446 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4447 return obj == InputAt(0);
4448 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004449
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004450 DECLARE_INSTRUCTION(ArrayLength);
4451
4452 private:
4453 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4454};
4455
4456class HBoundsCheck : public HExpression<2> {
4457 public:
4458 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004459 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004460 DCHECK(index->GetType() == Primitive::kPrimInt);
4461 SetRawInputAt(0, index);
4462 SetRawInputAt(1, length);
4463 }
4464
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004465 bool CanBeMoved() const OVERRIDE { return true; }
4466 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004467 UNUSED(other);
4468 return true;
4469 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004470
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004471 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004472
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004473 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004474
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004475
4476 DECLARE_INSTRUCTION(BoundsCheck);
4477
4478 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004479 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4480};
4481
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004482/**
4483 * Some DEX instructions are folded into multiple HInstructions that need
4484 * to stay live until the last HInstruction. This class
4485 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004486 * HInstruction stays live. `index` represents the stack location index of the
4487 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004488 */
4489class HTemporary : public HTemplateInstruction<0> {
4490 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004491 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4492 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004493
4494 size_t GetIndex() const { return index_; }
4495
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004496 Primitive::Type GetType() const OVERRIDE {
4497 // The previous instruction is the one that will be stored in the temporary location.
4498 DCHECK(GetPrevious() != nullptr);
4499 return GetPrevious()->GetType();
4500 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004501
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004502 DECLARE_INSTRUCTION(Temporary);
4503
4504 private:
4505 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004506 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4507};
4508
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004509class HSuspendCheck : public HTemplateInstruction<0> {
4510 public:
4511 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004512 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004513
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004514 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004515 return true;
4516 }
4517
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004518 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4519 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004520
4521 DECLARE_INSTRUCTION(SuspendCheck);
4522
4523 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004524 // Only used for code generation, in order to share the same slow path between back edges
4525 // of a same loop.
4526 SlowPathCode* slow_path_;
4527
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004528 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4529};
4530
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004531/**
4532 * Instruction to load a Class object.
4533 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004534class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004535 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004536 HLoadClass(HCurrentMethod* current_method,
4537 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004538 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004539 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004540 uint32_t dex_pc,
4541 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004542 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004543 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004544 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004545 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004546 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004547 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004548 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004549 // Referrers class should not need access check. We never inline unverified
4550 // methods so we can't possibly end up in this situation.
4551 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004552 SetRawInputAt(0, current_method);
4553 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004554
4555 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004556
4557 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004558 // Note that we don't need to test for generate_clinit_check_.
4559 // Whether or not we need to generate the clinit check is processed in
4560 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004561 return other->AsLoadClass()->type_index_ == type_index_ &&
4562 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004563 }
4564
4565 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4566
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004567 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004568 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004569 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004570
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004571 bool NeedsEnvironment() const OVERRIDE {
4572 // Will call runtime and load the class if the class is not loaded yet.
4573 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004574 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004575 }
4576
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004577 bool MustGenerateClinitCheck() const {
4578 return generate_clinit_check_;
4579 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004580 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4581 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004582 }
4583
4584 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004585 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4586 }
4587
4588 bool NeedsAccessCheck() const {
4589 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004590 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004591
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004592 bool CanThrow() const OVERRIDE {
4593 // May call runtime and and therefore can throw.
4594 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004595 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004596 }
4597
Calin Juravleacf735c2015-02-12 15:25:22 +00004598 ReferenceTypeInfo GetLoadedClassRTI() {
4599 return loaded_class_rti_;
4600 }
4601
4602 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4603 // Make sure we only set exact types (the loaded class should never be merged).
4604 DCHECK(rti.IsExact());
4605 loaded_class_rti_ = rti;
4606 }
4607
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004608 const DexFile& GetDexFile() { return dex_file_; }
4609
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004610 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4611
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004612 static SideEffects SideEffectsForArchRuntimeCalls() {
4613 return SideEffects::CanTriggerGC();
4614 }
4615
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004616 DECLARE_INSTRUCTION(LoadClass);
4617
4618 private:
4619 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004620 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004621 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004622 // Whether this instruction must generate the initialization check.
4623 // Used for code generation.
4624 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004625 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004626
Calin Juravleacf735c2015-02-12 15:25:22 +00004627 ReferenceTypeInfo loaded_class_rti_;
4628
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004629 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4630};
4631
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004632class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004633 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004634 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004635 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4636 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004637 SetRawInputAt(0, current_method);
4638 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004639
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004640 bool CanBeMoved() const OVERRIDE { return true; }
4641
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004642 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4643 return other->AsLoadString()->string_index_ == string_index_;
4644 }
4645
4646 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4647
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004648 uint32_t GetStringIndex() const { return string_index_; }
4649
4650 // TODO: Can we deopt or debug when we resolve a string?
4651 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004652 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004653 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004654
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004655 static SideEffects SideEffectsForArchRuntimeCalls() {
4656 return SideEffects::CanTriggerGC();
4657 }
4658
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004659 DECLARE_INSTRUCTION(LoadString);
4660
4661 private:
4662 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004663
4664 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4665};
4666
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004667/**
4668 * Performs an initialization check on its Class object input.
4669 */
4670class HClinitCheck : public HExpression<1> {
4671 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004672 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004673 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004674 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004675 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4676 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004677 SetRawInputAt(0, constant);
4678 }
4679
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004680 bool CanBeMoved() const OVERRIDE { return true; }
4681 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4682 UNUSED(other);
4683 return true;
4684 }
4685
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004686 bool NeedsEnvironment() const OVERRIDE {
4687 // May call runtime to initialize the class.
4688 return true;
4689 }
4690
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004691
4692 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4693
4694 DECLARE_INSTRUCTION(ClinitCheck);
4695
4696 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004697 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4698};
4699
4700class HStaticFieldGet : public HExpression<1> {
4701 public:
4702 HStaticFieldGet(HInstruction* cls,
4703 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004704 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004705 bool is_volatile,
4706 uint32_t field_idx,
Mingyao Yang589dac72015-08-24 11:21:42 -07004707 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004708 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004709 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004710 uint32_t dex_pc)
Mingyao Yang589dac72015-08-24 11:21:42 -07004711 : HExpression(field_type,
4712 SideEffects::FieldReadOfType(field_type, is_volatile),
4713 dex_pc),
4714 field_info_(field_offset,
4715 field_type,
4716 is_volatile,
4717 field_idx,
4718 declaring_class_def_index,
4719 dex_file,
4720 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004721 SetRawInputAt(0, cls);
4722 }
4723
Calin Juravle52c48962014-12-16 17:02:57 +00004724
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004725 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004726
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004727 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004728 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4729 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004730 }
4731
4732 size_t ComputeHashCode() const OVERRIDE {
4733 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4734 }
4735
Calin Juravle52c48962014-12-16 17:02:57 +00004736 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004737 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4738 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004739 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004740
4741 DECLARE_INSTRUCTION(StaticFieldGet);
4742
4743 private:
4744 const FieldInfo field_info_;
4745
4746 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4747};
4748
4749class HStaticFieldSet : public HTemplateInstruction<2> {
4750 public:
4751 HStaticFieldSet(HInstruction* cls,
4752 HInstruction* value,
4753 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004754 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004755 bool is_volatile,
4756 uint32_t field_idx,
Mingyao Yang589dac72015-08-24 11:21:42 -07004757 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004758 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004759 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004760 uint32_t dex_pc)
Mingyao Yang589dac72015-08-24 11:21:42 -07004761 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4762 dex_pc),
4763 field_info_(field_offset,
4764 field_type,
4765 is_volatile,
4766 field_idx,
4767 declaring_class_def_index,
4768 dex_file,
4769 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004770 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004771 SetRawInputAt(0, cls);
4772 SetRawInputAt(1, value);
4773 }
4774
Calin Juravle52c48962014-12-16 17:02:57 +00004775 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004776 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4777 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004778 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004779
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004780 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004781 bool GetValueCanBeNull() const { return value_can_be_null_; }
4782 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004783
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004784 DECLARE_INSTRUCTION(StaticFieldSet);
4785
4786 private:
4787 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004788 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004789
4790 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4791};
4792
Calin Juravlee460d1d2015-09-29 04:52:17 +01004793class HUnresolvedInstanceFieldGet : public HExpression<1> {
4794 public:
4795 HUnresolvedInstanceFieldGet(HInstruction* obj,
4796 Primitive::Type field_type,
4797 uint32_t field_index,
4798 uint32_t dex_pc)
4799 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4800 field_index_(field_index) {
4801 SetRawInputAt(0, obj);
4802 }
4803
4804 bool NeedsEnvironment() const OVERRIDE { return true; }
4805 bool CanThrow() const OVERRIDE { return true; }
4806
4807 Primitive::Type GetFieldType() const { return GetType(); }
4808 uint32_t GetFieldIndex() const { return field_index_; }
4809
4810 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4811
4812 private:
4813 const uint32_t field_index_;
4814
4815 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4816};
4817
4818class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4819 public:
4820 HUnresolvedInstanceFieldSet(HInstruction* obj,
4821 HInstruction* value,
4822 Primitive::Type field_type,
4823 uint32_t field_index,
4824 uint32_t dex_pc)
4825 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4826 field_type_(field_type),
4827 field_index_(field_index) {
4828 DCHECK_EQ(field_type, value->GetType());
4829 SetRawInputAt(0, obj);
4830 SetRawInputAt(1, value);
4831 }
4832
4833 bool NeedsEnvironment() const OVERRIDE { return true; }
4834 bool CanThrow() const OVERRIDE { return true; }
4835
4836 Primitive::Type GetFieldType() const { return field_type_; }
4837 uint32_t GetFieldIndex() const { return field_index_; }
4838
4839 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
4840
4841 private:
4842 const Primitive::Type field_type_;
4843 const uint32_t field_index_;
4844
4845 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
4846};
4847
4848class HUnresolvedStaticFieldGet : public HExpression<0> {
4849 public:
4850 HUnresolvedStaticFieldGet(Primitive::Type field_type,
4851 uint32_t field_index,
4852 uint32_t dex_pc)
4853 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4854 field_index_(field_index) {
4855 }
4856
4857 bool NeedsEnvironment() const OVERRIDE { return true; }
4858 bool CanThrow() const OVERRIDE { return true; }
4859
4860 Primitive::Type GetFieldType() const { return GetType(); }
4861 uint32_t GetFieldIndex() const { return field_index_; }
4862
4863 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
4864
4865 private:
4866 const uint32_t field_index_;
4867
4868 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
4869};
4870
4871class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
4872 public:
4873 HUnresolvedStaticFieldSet(HInstruction* value,
4874 Primitive::Type field_type,
4875 uint32_t field_index,
4876 uint32_t dex_pc)
4877 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4878 field_type_(field_type),
4879 field_index_(field_index) {
4880 DCHECK_EQ(field_type, value->GetType());
4881 SetRawInputAt(0, value);
4882 }
4883
4884 bool NeedsEnvironment() const OVERRIDE { return true; }
4885 bool CanThrow() const OVERRIDE { return true; }
4886
4887 Primitive::Type GetFieldType() const { return field_type_; }
4888 uint32_t GetFieldIndex() const { return field_index_; }
4889
4890 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
4891
4892 private:
4893 const Primitive::Type field_type_;
4894 const uint32_t field_index_;
4895
4896 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
4897};
4898
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004899// Implement the move-exception DEX instruction.
4900class HLoadException : public HExpression<0> {
4901 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004902 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4903 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004904
David Brazdilbbd733e2015-08-18 17:48:17 +01004905 bool CanBeNull() const OVERRIDE { return false; }
4906
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004907 DECLARE_INSTRUCTION(LoadException);
4908
4909 private:
4910 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4911};
4912
David Brazdilcb1c0552015-08-04 16:22:25 +01004913// Implicit part of move-exception which clears thread-local exception storage.
4914// Must not be removed because the runtime expects the TLS to get cleared.
4915class HClearException : public HTemplateInstruction<0> {
4916 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004917 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4918 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004919
4920 DECLARE_INSTRUCTION(ClearException);
4921
4922 private:
4923 DISALLOW_COPY_AND_ASSIGN(HClearException);
4924};
4925
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004926class HThrow : public HTemplateInstruction<1> {
4927 public:
4928 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004929 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004930 SetRawInputAt(0, exception);
4931 }
4932
4933 bool IsControlFlow() const OVERRIDE { return true; }
4934
4935 bool NeedsEnvironment() const OVERRIDE { return true; }
4936
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004937 bool CanThrow() const OVERRIDE { return true; }
4938
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004939
4940 DECLARE_INSTRUCTION(Throw);
4941
4942 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004943 DISALLOW_COPY_AND_ASSIGN(HThrow);
4944};
4945
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004946/**
4947 * Implementation strategies for the code generator of a HInstanceOf
4948 * or `HCheckCast`.
4949 */
4950enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01004951 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004952 kExactCheck, // Can do a single class compare.
4953 kClassHierarchyCheck, // Can just walk the super class chain.
4954 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
4955 kInterfaceCheck, // No optimization yet when checking against an interface.
4956 kArrayObjectCheck, // Can just check if the array is not primitive.
4957 kArrayCheck // No optimization yet when checking against a generic array.
4958};
4959
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004960class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004961 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004962 HInstanceOf(HInstruction* object,
4963 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004964 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004965 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004966 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004967 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004968 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004969 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004970 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004971 SetRawInputAt(0, object);
4972 SetRawInputAt(1, constant);
4973 }
4974
4975 bool CanBeMoved() const OVERRIDE { return true; }
4976
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004977 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004978 return true;
4979 }
4980
4981 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004982 return false;
4983 }
4984
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004985 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
4986
4987 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004988
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004989 // Used only in code generation.
4990 bool MustDoNullCheck() const { return must_do_null_check_; }
4991 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4992
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004993 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
4994 return (check_kind == TypeCheckKind::kExactCheck)
4995 ? SideEffects::None()
4996 // Mips currently does runtime calls for any other checks.
4997 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004998 }
4999
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005000 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005001
5002 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005003 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005004 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005005
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005006 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5007};
5008
Calin Juravleb1498f62015-02-16 13:13:29 +00005009class HBoundType : public HExpression<1> {
5010 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005011 // Constructs an HBoundType with the given upper_bound.
5012 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005013 HBoundType(HInstruction* input,
5014 ReferenceTypeInfo upper_bound,
5015 bool upper_can_be_null,
5016 uint32_t dex_pc = kNoDexPc)
5017 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005018 upper_bound_(upper_bound),
5019 upper_can_be_null_(upper_can_be_null),
5020 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005021 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005022 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005023 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005024 }
5025
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005026 // GetUpper* should only be used in reference type propagation.
5027 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5028 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005029
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005030 void SetCanBeNull(bool can_be_null) {
5031 DCHECK(upper_can_be_null_ || !can_be_null);
5032 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005033 }
5034
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005035 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5036
Calin Juravleb1498f62015-02-16 13:13:29 +00005037 DECLARE_INSTRUCTION(BoundType);
5038
5039 private:
5040 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005041 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5042 // It is used to bound the type in cases like:
5043 // if (x instanceof ClassX) {
5044 // // uper_bound_ will be ClassX
5045 // }
5046 const ReferenceTypeInfo upper_bound_;
5047 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5048 // is false then can_be_null_ cannot be true).
5049 const bool upper_can_be_null_;
5050 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005051
5052 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5053};
5054
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005055class HCheckCast : public HTemplateInstruction<2> {
5056 public:
5057 HCheckCast(HInstruction* object,
5058 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005059 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005060 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005061 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005062 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005063 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005064 SetRawInputAt(0, object);
5065 SetRawInputAt(1, constant);
5066 }
5067
5068 bool CanBeMoved() const OVERRIDE { return true; }
5069
5070 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5071 return true;
5072 }
5073
5074 bool NeedsEnvironment() const OVERRIDE {
5075 // Instruction may throw a CheckCastError.
5076 return true;
5077 }
5078
5079 bool CanThrow() const OVERRIDE { return true; }
5080
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005081 bool MustDoNullCheck() const { return must_do_null_check_; }
5082 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005083 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005084
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005085 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005086
5087 DECLARE_INSTRUCTION(CheckCast);
5088
5089 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005090 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005091 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005092
5093 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005094};
5095
Calin Juravle27df7582015-04-17 19:12:31 +01005096class HMemoryBarrier : public HTemplateInstruction<0> {
5097 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005098 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005099 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005100 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005101 barrier_kind_(barrier_kind) {}
5102
5103 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5104
5105 DECLARE_INSTRUCTION(MemoryBarrier);
5106
5107 private:
5108 const MemBarrierKind barrier_kind_;
5109
5110 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5111};
5112
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005113class HMonitorOperation : public HTemplateInstruction<1> {
5114 public:
5115 enum OperationKind {
5116 kEnter,
5117 kExit,
5118 };
5119
5120 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005121 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005122 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5123 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005124 SetRawInputAt(0, object);
5125 }
5126
5127 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005128 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5129
5130 bool CanThrow() const OVERRIDE {
5131 // Verifier guarantees that monitor-exit cannot throw.
5132 // This is important because it allows the HGraphBuilder to remove
5133 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5134 return IsEnter();
5135 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005136
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005137
5138 bool IsEnter() const { return kind_ == kEnter; }
5139
5140 DECLARE_INSTRUCTION(MonitorOperation);
5141
Calin Juravle52c48962014-12-16 17:02:57 +00005142 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005143 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005144
5145 private:
5146 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5147};
5148
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005149/**
5150 * A HInstruction used as a marker for the replacement of new + <init>
5151 * of a String to a call to a StringFactory. Only baseline will see
5152 * the node at code generation, where it will be be treated as null.
5153 * When compiling non-baseline, `HFakeString` instructions are being removed
5154 * in the instruction simplifier.
5155 */
5156class HFakeString : public HTemplateInstruction<0> {
5157 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005158 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5159 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005160
5161 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5162
5163 DECLARE_INSTRUCTION(FakeString);
5164
5165 private:
5166 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5167};
5168
Vladimir Markof9f64412015-09-02 14:05:49 +01005169class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005170 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005171 MoveOperands(Location source,
5172 Location destination,
5173 Primitive::Type type,
5174 HInstruction* instruction)
5175 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005176
5177 Location GetSource() const { return source_; }
5178 Location GetDestination() const { return destination_; }
5179
5180 void SetSource(Location value) { source_ = value; }
5181 void SetDestination(Location value) { destination_ = value; }
5182
5183 // The parallel move resolver marks moves as "in-progress" by clearing the
5184 // destination (but not the source).
5185 Location MarkPending() {
5186 DCHECK(!IsPending());
5187 Location dest = destination_;
5188 destination_ = Location::NoLocation();
5189 return dest;
5190 }
5191
5192 void ClearPending(Location dest) {
5193 DCHECK(IsPending());
5194 destination_ = dest;
5195 }
5196
5197 bool IsPending() const {
5198 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5199 return destination_.IsInvalid() && !source_.IsInvalid();
5200 }
5201
5202 // True if this blocks a move from the given location.
5203 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005204 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005205 }
5206
5207 // A move is redundant if it's been eliminated, if its source and
5208 // destination are the same, or if its destination is unneeded.
5209 bool IsRedundant() const {
5210 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5211 }
5212
5213 // We clear both operands to indicate move that's been eliminated.
5214 void Eliminate() {
5215 source_ = destination_ = Location::NoLocation();
5216 }
5217
5218 bool IsEliminated() const {
5219 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5220 return source_.IsInvalid();
5221 }
5222
Alexey Frunze4dda3372015-06-01 18:31:49 -07005223 Primitive::Type GetType() const { return type_; }
5224
Nicolas Geoffray90218252015-04-15 11:56:51 +01005225 bool Is64BitMove() const {
5226 return Primitive::Is64BitType(type_);
5227 }
5228
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005229 HInstruction* GetInstruction() const { return instruction_; }
5230
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005231 private:
5232 Location source_;
5233 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005234 // The type this move is for.
5235 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005236 // The instruction this move is assocatied with. Null when this move is
5237 // for moving an input in the expected locations of user (including a phi user).
5238 // This is only used in debug mode, to ensure we do not connect interval siblings
5239 // in the same parallel move.
5240 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005241};
5242
5243static constexpr size_t kDefaultNumberOfMoves = 4;
5244
5245class HParallelMove : public HTemplateInstruction<0> {
5246 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005247 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005248 : HTemplateInstruction(SideEffects::None(), dex_pc),
5249 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5250 moves_.reserve(kDefaultNumberOfMoves);
5251 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005252
Nicolas Geoffray90218252015-04-15 11:56:51 +01005253 void AddMove(Location source,
5254 Location destination,
5255 Primitive::Type type,
5256 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005257 DCHECK(source.IsValid());
5258 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005259 if (kIsDebugBuild) {
5260 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005261 for (const MoveOperands& move : moves_) {
5262 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005263 // Special case the situation where the move is for the spill slot
5264 // of the instruction.
5265 if ((GetPrevious() == instruction)
5266 || ((GetPrevious() == nullptr)
5267 && instruction->IsPhi()
5268 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005269 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005270 << "Doing parallel moves for the same instruction.";
5271 } else {
5272 DCHECK(false) << "Doing parallel moves for the same instruction.";
5273 }
5274 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005275 }
5276 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005277 for (const MoveOperands& move : moves_) {
5278 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005279 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005280 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005281 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005282 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005283 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005284 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005285 }
5286
Vladimir Marko225b6462015-09-28 12:17:40 +01005287 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005288 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005289 }
5290
Vladimir Marko225b6462015-09-28 12:17:40 +01005291 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005292
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005293 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005294
5295 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005296 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005297
5298 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5299};
5300
Mark Mendell0616ae02015-04-17 12:49:27 -04005301} // namespace art
5302
5303#ifdef ART_ENABLE_CODEGEN_x86
5304#include "nodes_x86.h"
5305#endif
5306
5307namespace art {
5308
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005309class HGraphVisitor : public ValueObject {
5310 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005311 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5312 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005313
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005314 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005315 virtual void VisitBasicBlock(HBasicBlock* block);
5316
Roland Levillain633021e2014-10-01 14:12:25 +01005317 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005318 void VisitInsertionOrder();
5319
Roland Levillain633021e2014-10-01 14:12:25 +01005320 // Visit the graph following dominator tree reverse post-order.
5321 void VisitReversePostOrder();
5322
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005323 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005324
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005325 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005326#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005327 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5328
5329 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5330
5331#undef DECLARE_VISIT_INSTRUCTION
5332
5333 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005334 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005335
5336 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5337};
5338
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005339class HGraphDelegateVisitor : public HGraphVisitor {
5340 public:
5341 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5342 virtual ~HGraphDelegateVisitor() {}
5343
5344 // Visit functions that delegate to to super class.
5345#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005346 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005347
5348 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5349
5350#undef DECLARE_VISIT_INSTRUCTION
5351
5352 private:
5353 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5354};
5355
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005356class HInsertionOrderIterator : public ValueObject {
5357 public:
5358 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5359
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005360 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005361 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005362 void Advance() { ++index_; }
5363
5364 private:
5365 const HGraph& graph_;
5366 size_t index_;
5367
5368 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5369};
5370
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005371class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005372 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005373 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5374 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005375 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005376 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005377
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005378 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5379 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005380 void Advance() { ++index_; }
5381
5382 private:
5383 const HGraph& graph_;
5384 size_t index_;
5385
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005386 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005387};
5388
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005389class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005390 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005391 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005392 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005393 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005394 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005395 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005396
5397 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005398 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005399 void Advance() { --index_; }
5400
5401 private:
5402 const HGraph& graph_;
5403 size_t index_;
5404
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005405 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005406};
5407
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005408class HLinearPostOrderIterator : public ValueObject {
5409 public:
5410 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005411 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005412
5413 bool Done() const { return index_ == 0; }
5414
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005415 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005416
5417 void Advance() {
5418 --index_;
5419 DCHECK_GE(index_, 0U);
5420 }
5421
5422 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005423 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005424 size_t index_;
5425
5426 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5427};
5428
5429class HLinearOrderIterator : public ValueObject {
5430 public:
5431 explicit HLinearOrderIterator(const HGraph& graph)
5432 : order_(graph.GetLinearOrder()), index_(0) {}
5433
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005434 bool Done() const { return index_ == order_.size(); }
5435 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005436 void Advance() { ++index_; }
5437
5438 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005439 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005440 size_t index_;
5441
5442 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5443};
5444
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005445// Iterator over the blocks that art part of the loop. Includes blocks part
5446// of an inner loop. The order in which the blocks are iterated is on their
5447// block id.
5448class HBlocksInLoopIterator : public ValueObject {
5449 public:
5450 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5451 : blocks_in_loop_(info.GetBlocks()),
5452 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5453 index_(0) {
5454 if (!blocks_in_loop_.IsBitSet(index_)) {
5455 Advance();
5456 }
5457 }
5458
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005459 bool Done() const { return index_ == blocks_.size(); }
5460 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005461 void Advance() {
5462 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005463 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005464 if (blocks_in_loop_.IsBitSet(index_)) {
5465 break;
5466 }
5467 }
5468 }
5469
5470 private:
5471 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005472 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005473 size_t index_;
5474
5475 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5476};
5477
Mingyao Yang3584bce2015-05-19 16:01:59 -07005478// Iterator over the blocks that art part of the loop. Includes blocks part
5479// of an inner loop. The order in which the blocks are iterated is reverse
5480// post order.
5481class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5482 public:
5483 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5484 : blocks_in_loop_(info.GetBlocks()),
5485 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5486 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005487 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005488 Advance();
5489 }
5490 }
5491
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005492 bool Done() const { return index_ == blocks_.size(); }
5493 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005494 void Advance() {
5495 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005496 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5497 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005498 break;
5499 }
5500 }
5501 }
5502
5503 private:
5504 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005505 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005506 size_t index_;
5507
5508 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5509};
5510
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005511inline int64_t Int64FromConstant(HConstant* constant) {
5512 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5513 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5514 : constant->AsLongConstant()->GetValue();
5515}
5516
Vladimir Marko58155012015-08-19 12:49:41 +00005517inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5518 // For the purposes of the compiler, the dex files must actually be the same object
5519 // if we want to safely treat them as the same. This is especially important for JIT
5520 // as custom class loaders can open the same underlying file (or memory) multiple
5521 // times and provide different class resolution but no two class loaders should ever
5522 // use the same DexFile object - doing so is an unsupported hack that can lead to
5523 // all sorts of weird failures.
5524 return &lhs == &rhs;
5525}
5526
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005527} // namespace art
5528
5529#endif // ART_COMPILER_OPTIMIZING_NODES_H_