blob: 5ec3f22e8125e15055e3c111dc670dd6d6a6f0d5 [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#include "utils/growable_array.h"
39
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010047class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Calin Juravle9aec02f2014-11-18 23:06:35 +000075static constexpr uint32_t kMaxIntShiftValue = 0x1f;
76static constexpr uint64_t kMaxLongShiftValue = 0x3f;
77
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010078static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
79
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),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000150 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100151 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100152 linear_order_(arena, kDefaultNumberOfBlocks),
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),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000160 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000161 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100162 dex_file_(dex_file),
163 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100164 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100165 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100166 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000168 cached_null_constant_(nullptr),
169 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000170 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
171 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100172 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
173 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000174
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000175 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100176 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100177 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000178
David Brazdil69ba7b72015-06-23 18:27:30 +0100179 bool IsInSsaForm() const { return in_ssa_form_; }
180
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000181 HBasicBlock* GetEntryBlock() const { return entry_block_; }
182 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100183 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000184
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000185 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
186 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000187
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000188 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100189
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000190 // Try building the SSA form of this graph, with dominance computation and loop
191 // recognition. Returns whether it was successful in doing all these steps.
192 bool TryBuildingSsa() {
193 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000194 // The SSA builder requires loops to all be natural. Specifically, the dead phi
195 // elimination phase checks the consistency of the graph when doing a post-order
196 // visit for eliminating dead phis: a dead phi can only have loop header phi
197 // users remaining when being visited.
198 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100199 // Precompute per-block try membership before entering the SSA builder,
200 // which needs the information to build catch block phis from values of
201 // locals at throwing instructions inside try blocks.
202 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000203 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100204 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000205 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000206 }
207
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100208 void ComputeDominanceInformation();
209 void ClearDominanceInformation();
210
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000211 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100213 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100214 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000215
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000216 // Analyze all natural loops in this graph. Returns false if one
217 // loop is not natural, that is the header does not dominate the
218 // back edge.
219 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100220
David Brazdilffee3d32015-07-06 11:48:53 +0100221 // Iterate over blocks to compute try block membership. Needs reverse post
222 // order and loop information.
223 void ComputeTryBlockInformation();
224
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000225 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000226 // Returns the instruction used to replace the invoke expression or null if the
227 // invoke is for a void method.
228 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000229
Mingyao Yang3584bce2015-05-19 16:01:59 -0700230 // Need to add a couple of blocks to test if the loop body is entered and
231 // put deoptimization instructions, etc.
232 void TransformLoopHeaderForBCE(HBasicBlock* header);
233
David Brazdil2d7352b2015-04-20 14:52:42 +0100234 // Removes `block` from the graph.
235 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000236
David Brazdilfc6a86a2015-06-26 10:33:45 +0000237 // Splits the edge between `block` and `successor` while preserving the
238 // indices in the predecessor/successor lists. If there are multiple edges
239 // between the blocks, the lowest indices are used.
240 // Returns the new block which is empty and has the same dex pc as `successor`.
241 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
242
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100243 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
244 void SimplifyLoop(HBasicBlock* header);
245
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000246 int32_t GetNextInstructionId() {
247 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000248 return current_instruction_id_++;
249 }
250
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000251 int32_t GetCurrentInstructionId() const {
252 return current_instruction_id_;
253 }
254
255 void SetCurrentInstructionId(int32_t id) {
256 current_instruction_id_ = id;
257 }
258
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100259 uint16_t GetMaximumNumberOfOutVRegs() const {
260 return maximum_number_of_out_vregs_;
261 }
262
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000263 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
264 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100265 }
266
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100267 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
268 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
269 }
270
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000271 void UpdateTemporariesVRegSlots(size_t slots) {
272 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100273 }
274
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000275 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100276 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000277 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100278 }
279
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100280 void SetNumberOfVRegs(uint16_t number_of_vregs) {
281 number_of_vregs_ = number_of_vregs;
282 }
283
284 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100285 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100286 return number_of_vregs_;
287 }
288
289 void SetNumberOfInVRegs(uint16_t value) {
290 number_of_in_vregs_ = value;
291 }
292
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100293 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100294 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100295 return number_of_vregs_ - number_of_in_vregs_;
296 }
297
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100298 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
299 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100300 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100301
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100302 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
303 return linear_order_;
304 }
305
Mark Mendell1152c922015-04-24 17:06:35 -0400306 bool HasBoundsChecks() const {
307 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800308 }
309
Mark Mendell1152c922015-04-24 17:06:35 -0400310 void SetHasBoundsChecks(bool value) {
311 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800312 }
313
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100314 bool ShouldGenerateConstructorBarrier() const {
315 return should_generate_constructor_barrier_;
316 }
317
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000318 bool IsDebuggable() const { return debuggable_; }
319
David Brazdil8d5b8b22015-03-24 10:51:52 +0000320 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000321 // already, it is created and inserted into the graph. This method is only for
322 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600323 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000324
325 // TODO: This is problematic for the consistency of reference type propagation
326 // because it can be created anytime after the pass and thus it will be left
327 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600328 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000329
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600330 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
331 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000332 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600333 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
334 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000335 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600336 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
337 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000338 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600339 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
340 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000341 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000342
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100343 HCurrentMethod* GetCurrentMethod();
344
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000345 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100346
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100347 const DexFile& GetDexFile() const {
348 return dex_file_;
349 }
350
351 uint32_t GetMethodIdx() const {
352 return method_idx_;
353 }
354
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100355 InvokeType GetInvokeType() const {
356 return invoke_type_;
357 }
358
Mark Mendellc4701932015-04-10 13:18:51 -0400359 InstructionSet GetInstructionSet() const {
360 return instruction_set_;
361 }
362
David Brazdilbbd733e2015-08-18 17:48:17 +0100363 // TODO: Remove once the full compilation pipeline is enabled for try/catch.
364 bool HasTryCatch() const;
365
David Brazdil2d7352b2015-04-20 14:52:42 +0100366 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000367 void VisitBlockForDominatorTree(HBasicBlock* block,
368 HBasicBlock* predecessor,
369 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100370 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000371 void VisitBlockForBackEdges(HBasicBlock* block,
372 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100373 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000374 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100375 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000376
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000377 template <class InstructionType, typename ValueType>
378 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600379 ArenaSafeMap<ValueType, InstructionType*>* cache,
380 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000381 // Try to find an existing constant of the given value.
382 InstructionType* constant = nullptr;
383 auto cached_constant = cache->find(value);
384 if (cached_constant != cache->end()) {
385 constant = cached_constant->second;
386 }
387
388 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100389 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000390 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600391 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000392 cache->Overwrite(value, constant);
393 InsertConstant(constant);
394 }
395 return constant;
396 }
397
David Brazdil8d5b8b22015-03-24 10:51:52 +0000398 void InsertConstant(HConstant* instruction);
399
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000400 // Cache a float constant into the graph. This method should only be
401 // called by the SsaBuilder when creating "equivalent" instructions.
402 void CacheFloatConstant(HFloatConstant* constant);
403
404 // See CacheFloatConstant comment.
405 void CacheDoubleConstant(HDoubleConstant* constant);
406
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000407 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000408
409 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000410 GrowableArray<HBasicBlock*> blocks_;
411
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100412 // List of blocks to perform a reverse post order tree traversal.
413 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000414
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100415 // List of blocks to perform a linear order tree traversal.
416 GrowableArray<HBasicBlock*> linear_order_;
417
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000418 HBasicBlock* entry_block_;
419 HBasicBlock* exit_block_;
420
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100421 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100422 uint16_t maximum_number_of_out_vregs_;
423
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100424 // The number of virtual registers in this method. Contains the parameters.
425 uint16_t number_of_vregs_;
426
427 // The number of virtual registers used by parameters of this method.
428 uint16_t number_of_in_vregs_;
429
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000430 // Number of vreg size slots that the temporaries use (used in baseline compiler).
431 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100432
Mark Mendell1152c922015-04-24 17:06:35 -0400433 // Has bounds checks. We can totally skip BCE if it's false.
434 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800435
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000436 // Indicates whether the graph should be compiled in a way that
437 // ensures full debuggability. If false, we can apply more
438 // aggressive optimizations that may limit the level of debugging.
439 const bool debuggable_;
440
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000441 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000442 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000443
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100444 // The dex file from which the method is from.
445 const DexFile& dex_file_;
446
447 // The method index in the dex file.
448 const uint32_t method_idx_;
449
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100450 // If inlined, this encodes how the callee is being invoked.
451 const InvokeType invoke_type_;
452
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100453 // Whether the graph has been transformed to SSA form. Only used
454 // in debug mode to ensure we are not using properties only valid
455 // for non-SSA form (like the number of temporaries).
456 bool in_ssa_form_;
457
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100458 const bool should_generate_constructor_barrier_;
459
Mathieu Chartiere401d142015-04-22 13:56:20 -0700460 const InstructionSet instruction_set_;
461
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000462 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000463 HNullConstant* cached_null_constant_;
464 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000465 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000466 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000467 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000468
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100469 HCurrentMethod* cached_current_method_;
470
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000471 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100472 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000473 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000474 DISALLOW_COPY_AND_ASSIGN(HGraph);
475};
476
Vladimir Markof9f64412015-09-02 14:05:49 +0100477class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000478 public:
479 HLoopInformation(HBasicBlock* header, HGraph* graph)
480 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100481 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100482 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100483 // Make bit vector growable, as the number of blocks may change.
484 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485
486 HBasicBlock* GetHeader() const {
487 return header_;
488 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000490 void SetHeader(HBasicBlock* block) {
491 header_ = block;
492 }
493
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100494 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
495 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
496 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
497
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000498 void AddBackEdge(HBasicBlock* back_edge) {
499 back_edges_.Add(back_edge);
500 }
501
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100502 void RemoveBackEdge(HBasicBlock* back_edge) {
503 back_edges_.Delete(back_edge);
504 }
505
David Brazdil46e2a392015-03-16 17:31:52 +0000506 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100507 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000508 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 }
510 return false;
511 }
512
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000513 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000514 return back_edges_.Size();
515 }
516
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100517 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100518
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100519 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
520 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100521 }
522
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100523 // Returns the lifetime position of the back edge that has the
524 // greatest lifetime position.
525 size_t GetLifetimeEnd() const;
526
527 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
528 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
529 if (back_edges_.Get(i) == existing) {
530 back_edges_.Put(i, new_back_edge);
531 return;
532 }
533 }
534 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100535 }
536
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100537 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100538 // that is the header dominates the back edge.
539 bool Populate();
540
David Brazdila4b8c212015-05-07 09:59:30 +0100541 // Reanalyzes the loop by removing loop info from its blocks and re-running
542 // Populate(). If there are no back edges left, the loop info is completely
543 // removed as well as its SuspendCheck instruction. It must be run on nested
544 // inner loops first.
545 void Update();
546
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100547 // Returns whether this loop information contains `block`.
548 // Note that this loop information *must* be populated before entering this function.
549 bool Contains(const HBasicBlock& block) const;
550
551 // Returns whether this loop information is an inner loop of `other`.
552 // Note that `other` *must* be populated before entering this function.
553 bool IsIn(const HLoopInformation& other) const;
554
555 const ArenaBitVector& GetBlocks() const { return blocks_; }
556
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000557 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000558 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000559
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000560 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100561 // Internal recursive implementation of `Populate`.
562 void PopulateRecursive(HBasicBlock* block);
563
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000564 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100565 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000566 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100567 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000568
569 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
570};
571
David Brazdilec16f792015-08-19 15:04:01 +0100572// Stores try/catch information for basic blocks.
573// Note that HGraph is constructed so that catch blocks cannot simultaneously
574// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100575class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100576 public:
577 // Try block information constructor.
578 explicit TryCatchInformation(const HTryBoundary& try_entry)
579 : try_entry_(&try_entry),
580 catch_dex_file_(nullptr),
581 catch_type_index_(DexFile::kDexNoIndex16) {
582 DCHECK(try_entry_ != nullptr);
583 }
584
585 // Catch block information constructor.
586 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
587 : try_entry_(nullptr),
588 catch_dex_file_(&dex_file),
589 catch_type_index_(catch_type_index) {}
590
591 bool IsTryBlock() const { return try_entry_ != nullptr; }
592
593 const HTryBoundary& GetTryEntry() const {
594 DCHECK(IsTryBlock());
595 return *try_entry_;
596 }
597
598 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
599
600 bool IsCatchAllTypeIndex() const {
601 DCHECK(IsCatchBlock());
602 return catch_type_index_ == DexFile::kDexNoIndex16;
603 }
604
605 uint16_t GetCatchTypeIndex() const {
606 DCHECK(IsCatchBlock());
607 return catch_type_index_;
608 }
609
610 const DexFile& GetCatchDexFile() const {
611 DCHECK(IsCatchBlock());
612 return *catch_dex_file_;
613 }
614
615 private:
616 // One of possibly several TryBoundary instructions entering the block's try.
617 // Only set for try blocks.
618 const HTryBoundary* try_entry_;
619
620 // Exception type information. Only set for catch blocks.
621 const DexFile* catch_dex_file_;
622 const uint16_t catch_type_index_;
623};
624
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100625static constexpr size_t kNoLifetime = -1;
626
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000627// A block in a method. Contains the list of instructions represented
628// as a double linked list. Each block knows its predecessors and
629// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100630
Vladimir Markof9f64412015-09-02 14:05:49 +0100631class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600633 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000634 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000635 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
636 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000637 loop_information_(nullptr),
638 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000639 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100640 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100641 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100642 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000643 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000644 try_catch_information_(nullptr) {
645 predecessors_.reserve(kDefaultNumberOfPredecessors);
646 successors_.reserve(kDefaultNumberOfSuccessors);
647 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
648 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000649
Vladimir Marko60584552015-09-03 13:35:12 +0000650 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100651 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652 }
653
Vladimir Marko60584552015-09-03 13:35:12 +0000654 HBasicBlock* GetPredecessor(size_t pred_idx) const {
655 DCHECK_LT(pred_idx, predecessors_.size());
656 return predecessors_[pred_idx];
657 }
658
659 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100660 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000661 }
662
Vladimir Marko60584552015-09-03 13:35:12 +0000663 HBasicBlock* GetSuccessor(size_t succ_idx) const {
664 DCHECK_LT(succ_idx, successors_.size());
665 return successors_[succ_idx];
666 }
667
668 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
669 return ContainsElement(successors_, block, start_from);
670 }
671
672 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100673 return dominated_blocks_;
674 }
675
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100676 bool IsEntryBlock() const {
677 return graph_->GetEntryBlock() == this;
678 }
679
680 bool IsExitBlock() const {
681 return graph_->GetExitBlock() == this;
682 }
683
David Brazdil46e2a392015-03-16 17:31:52 +0000684 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000685 bool IsSingleTryBoundary() const;
686
687 // Returns true if this block emits nothing but a jump.
688 bool IsSingleJump() const {
689 HLoopInformation* loop_info = GetLoopInformation();
690 return (IsSingleGoto() || IsSingleTryBoundary())
691 // Back edges generate a suspend check.
692 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
693 }
David Brazdil46e2a392015-03-16 17:31:52 +0000694
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000695 void AddBackEdge(HBasicBlock* back_edge) {
696 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000697 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000698 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100699 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000700 loop_information_->AddBackEdge(back_edge);
701 }
702
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000703 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000704 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000705
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000706 int GetBlockId() const { return block_id_; }
707 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600708 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000709
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000710 HBasicBlock* GetDominator() const { return dominator_; }
711 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000712 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
713
714 void RemoveDominatedBlock(HBasicBlock* block) {
715 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100716 }
Vladimir Marko60584552015-09-03 13:35:12 +0000717
718 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
719 ReplaceElement(dominated_blocks_, existing, new_block);
720 }
721
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100722 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000723
724 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100725 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000726 }
727
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100728 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
729 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100730 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100731 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100732 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
733 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000734
735 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000736 successors_.push_back(block);
737 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000738 }
739
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100740 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
741 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100742 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000743 new_block->predecessors_.push_back(this);
744 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000745 }
746
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000747 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
748 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000749 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000750 new_block->successors_.push_back(this);
751 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000752 }
753
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100754 // Insert `this` between `predecessor` and `successor. This method
755 // preserves the indicies, and will update the first edge found between
756 // `predecessor` and `successor`.
757 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
758 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100759 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000760 successor->predecessors_[predecessor_index] = this;
761 predecessor->successors_[successor_index] = this;
762 successors_.push_back(successor);
763 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100764 }
765
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100766 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000767 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100768 }
769
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000770 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000771 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000772 }
773
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100774 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000775 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100776 }
777
778 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000779 predecessors_.push_back(block);
780 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100781 }
782
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100783 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000784 DCHECK_EQ(predecessors_.size(), 2u);
785 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100786 }
787
David Brazdil769c9e52015-04-27 13:54:09 +0100788 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000789 DCHECK_EQ(successors_.size(), 2u);
790 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100791 }
792
David Brazdilfc6a86a2015-06-26 10:33:45 +0000793 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000794 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100795 }
796
David Brazdilfc6a86a2015-06-26 10:33:45 +0000797 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000798 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100799 }
800
David Brazdilfc6a86a2015-06-26 10:33:45 +0000801 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000802 DCHECK_EQ(GetPredecessors().size(), 1u);
803 return GetPredecessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000804 }
805
806 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000807 DCHECK_EQ(GetSuccessors().size(), 1u);
808 return GetSuccessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000809 }
810
811 // Returns whether the first occurrence of `predecessor` in the list of
812 // predecessors is at index `idx`.
813 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000814 DCHECK_EQ(GetPredecessor(idx), predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000815 return GetPredecessorIndexOf(predecessor) == idx;
816 }
817
David Brazdilffee3d32015-07-06 11:48:53 +0100818 // Returns the number of non-exceptional successors. SsaChecker ensures that
819 // these are stored at the beginning of the successor list.
820 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000821 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100822 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000823
824 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100825 // created, latter block. Note that this method will add the block to the
826 // graph, create a Goto at the end of the former block and will create an edge
827 // between the blocks. It will not, however, update the reverse post order or
828 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000829 HBasicBlock* SplitBefore(HInstruction* cursor);
830
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000831 // Split the block into two blocks just after `cursor`. Returns the newly
832 // created block. Note that this method just updates raw block information,
833 // like predecessors, successors, dominators, and instruction list. It does not
834 // update the graph, reverse post order, loop information, nor make sure the
835 // blocks are consistent (for example ending with a control flow instruction).
836 HBasicBlock* SplitAfter(HInstruction* cursor);
837
838 // Merge `other` at the end of `this`. Successors and dominated blocks of
839 // `other` are changed to be successors and dominated blocks of `this`. Note
840 // that this method does not update the graph, reverse post order, loop
841 // information, nor make sure the blocks are consistent (for example ending
842 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100843 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000844
845 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
846 // of `this` are moved to `other`.
847 // Note that this method does not update the graph, reverse post order, loop
848 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000849 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000850 void ReplaceWith(HBasicBlock* other);
851
David Brazdil2d7352b2015-04-20 14:52:42 +0100852 // Merge `other` at the end of `this`. This method updates loops, reverse post
853 // order, links to predecessors, successors, dominators and deletes the block
854 // from the graph. The two blocks must be successive, i.e. `this` the only
855 // predecessor of `other` and vice versa.
856 void MergeWith(HBasicBlock* other);
857
858 // Disconnects `this` from all its predecessors, successors and dominator,
859 // removes it from all loops it is included in and eventually from the graph.
860 // The block must not dominate any other block. Predecessors and successors
861 // are safely updated.
862 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000863
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000864 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100865 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100866 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100867 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100868 // Replace instruction `initial` with `replacement` within this block.
869 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
870 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100871 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100872 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000873 // RemoveInstruction and RemovePhi delete a given instruction from the respective
874 // instruction list. With 'ensure_safety' set to true, it verifies that the
875 // instruction is not in use and removes it from the use lists of its inputs.
876 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
877 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100878 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100879
880 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100881 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100882 }
883
Roland Levillain6b879dd2014-09-22 17:13:44 +0100884 bool IsLoopPreHeaderFirstPredecessor() const {
885 DCHECK(IsLoopHeader());
Vladimir Marko60584552015-09-03 13:35:12 +0000886 return GetPredecessor(0) == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100887 }
888
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100889 HLoopInformation* GetLoopInformation() const {
890 return loop_information_;
891 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000892
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000893 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100894 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000895 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100896 void SetInLoop(HLoopInformation* info) {
897 if (IsLoopHeader()) {
898 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100899 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100900 loop_information_ = info;
901 } else if (loop_information_->Contains(*info->GetHeader())) {
902 // Block is currently part of an outer loop. Make it part of this inner loop.
903 // Note that a non loop header having a loop information means this loop information
904 // has already been populated
905 loop_information_ = info;
906 } else {
907 // Block is part of an inner loop. Do not update the loop information.
908 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
909 // at this point, because this method is being called while populating `info`.
910 }
911 }
912
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000913 // Raw update of the loop information.
914 void SetLoopInformation(HLoopInformation* info) {
915 loop_information_ = info;
916 }
917
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100918 bool IsInLoop() const { return loop_information_ != nullptr; }
919
David Brazdilec16f792015-08-19 15:04:01 +0100920 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
921
922 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
923 try_catch_information_ = try_catch_information;
924 }
925
926 bool IsTryBlock() const {
927 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
928 }
929
930 bool IsCatchBlock() const {
931 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
932 }
David Brazdilffee3d32015-07-06 11:48:53 +0100933
934 // Returns the try entry that this block's successors should have. They will
935 // be in the same try, unless the block ends in a try boundary. In that case,
936 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100937 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100938
David Brazdila4b8c212015-05-07 09:59:30 +0100939 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100940 bool Dominates(HBasicBlock* block) const;
941
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100942 size_t GetLifetimeStart() const { return lifetime_start_; }
943 size_t GetLifetimeEnd() const { return lifetime_end_; }
944
945 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
946 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
947
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100948
David Brazdil8d5b8b22015-03-24 10:51:52 +0000949 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000950 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100951 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000952 bool HasSinglePhi() const;
953
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000954 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000955 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000956 ArenaVector<HBasicBlock*> predecessors_;
957 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100958 HInstructionList instructions_;
959 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000960 HLoopInformation* loop_information_;
961 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000962 ArenaVector<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100964 // The dex program counter of the first instruction of this block.
965 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100966 size_t lifetime_start_;
967 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100968 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100969
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000970 friend class HGraph;
971 friend class HInstruction;
972
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000973 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
974};
975
David Brazdilb2bd1c52015-03-25 11:17:37 +0000976// Iterates over the LoopInformation of all loops which contain 'block'
977// from the innermost to the outermost.
978class HLoopInformationOutwardIterator : public ValueObject {
979 public:
980 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
981 : current_(block.GetLoopInformation()) {}
982
983 bool Done() const { return current_ == nullptr; }
984
985 void Advance() {
986 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100987 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000988 }
989
990 HLoopInformation* Current() const {
991 DCHECK(!Done());
992 return current_;
993 }
994
995 private:
996 HLoopInformation* current_;
997
998 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
999};
1000
Alexandre Ramesef20f712015-06-09 10:29:30 +01001001#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001002 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001003 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001004 M(ArrayGet, Instruction) \
1005 M(ArrayLength, Instruction) \
1006 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +01001007 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001008 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001009 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001010 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001011 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001012 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001013 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001014 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001015 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001016 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001017 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001018 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001019 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001020 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001021 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001022 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001023 M(FloatConstant, Constant) \
1024 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001025 M(GreaterThan, Condition) \
1026 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001027 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001028 M(InstanceFieldGet, Instruction) \
1029 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001030 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001031 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001032 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001033 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001034 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001035 M(LessThan, Condition) \
1036 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001037 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001038 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001039 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001040 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001041 M(Local, Instruction) \
1042 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001043 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001044 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001045 M(Mul, BinaryOperation) \
1046 M(Neg, UnaryOperation) \
1047 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001048 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001049 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001050 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001051 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001052 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001053 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001054 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001055 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001056 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001057 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001058 M(Return, Instruction) \
1059 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001060 M(Shl, BinaryOperation) \
1061 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001062 M(StaticFieldGet, Instruction) \
1063 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001064 M(StoreLocal, Instruction) \
1065 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001066 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001067 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001068 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001069 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001070 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001071 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001072 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001073
Alexandre Ramesef20f712015-06-09 10:29:30 +01001074#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1075
1076#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1077
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001078#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1079
Mark Mendell0616ae02015-04-17 12:49:27 -04001080#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1081 M(X86ComputeBaseMethodAddress, Instruction) \
1082 M(X86LoadFromConstantTable, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001083
1084#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1085
1086#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1087 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1088 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1089 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001090 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001091 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1092 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1093
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001094#define FOR_EACH_INSTRUCTION(M) \
1095 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1096 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001097 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001098 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001099 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001100
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001101#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001102FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1103#undef FORWARD_DECLARATION
1104
Roland Levillainccc07a92014-09-16 14:48:16 +01001105#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001106 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1107 const char* DebugName() const OVERRIDE { return #type; } \
1108 const H##type* As##type() const OVERRIDE { return this; } \
1109 H##type* As##type() OVERRIDE { return this; } \
1110 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001111 return other->Is##type(); \
1112 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001113 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001114
David Brazdiled596192015-01-23 10:39:45 +00001115template <typename T> class HUseList;
1116
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001117template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001118class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001119 public:
David Brazdiled596192015-01-23 10:39:45 +00001120 HUseListNode* GetPrevious() const { return prev_; }
1121 HUseListNode* GetNext() const { return next_; }
1122 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001123 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001124 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001125
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001126 private:
David Brazdiled596192015-01-23 10:39:45 +00001127 HUseListNode(T user, size_t index)
1128 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1129
1130 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001131 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001132 HUseListNode<T>* prev_;
1133 HUseListNode<T>* next_;
1134
1135 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001136
1137 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1138};
1139
David Brazdiled596192015-01-23 10:39:45 +00001140template <typename T>
1141class HUseList : public ValueObject {
1142 public:
1143 HUseList() : first_(nullptr) {}
1144
1145 void Clear() {
1146 first_ = nullptr;
1147 }
1148
1149 // Adds a new entry at the beginning of the use list and returns
1150 // the newly created node.
1151 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001152 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001153 if (IsEmpty()) {
1154 first_ = new_node;
1155 } else {
1156 first_->prev_ = new_node;
1157 new_node->next_ = first_;
1158 first_ = new_node;
1159 }
1160 return new_node;
1161 }
1162
1163 HUseListNode<T>* GetFirst() const {
1164 return first_;
1165 }
1166
1167 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001168 DCHECK(node != nullptr);
1169 DCHECK(Contains(node));
1170
David Brazdiled596192015-01-23 10:39:45 +00001171 if (node->prev_ != nullptr) {
1172 node->prev_->next_ = node->next_;
1173 }
1174 if (node->next_ != nullptr) {
1175 node->next_->prev_ = node->prev_;
1176 }
1177 if (node == first_) {
1178 first_ = node->next_;
1179 }
1180 }
1181
David Brazdil1abb4192015-02-17 18:33:36 +00001182 bool Contains(const HUseListNode<T>* node) const {
1183 if (node == nullptr) {
1184 return false;
1185 }
1186 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1187 if (current == node) {
1188 return true;
1189 }
1190 }
1191 return false;
1192 }
1193
David Brazdiled596192015-01-23 10:39:45 +00001194 bool IsEmpty() const {
1195 return first_ == nullptr;
1196 }
1197
1198 bool HasOnlyOneUse() const {
1199 return first_ != nullptr && first_->next_ == nullptr;
1200 }
1201
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001202 size_t SizeSlow() const {
1203 size_t count = 0;
1204 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1205 ++count;
1206 }
1207 return count;
1208 }
1209
David Brazdiled596192015-01-23 10:39:45 +00001210 private:
1211 HUseListNode<T>* first_;
1212};
1213
1214template<typename T>
1215class HUseIterator : public ValueObject {
1216 public:
1217 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1218
1219 bool Done() const { return current_ == nullptr; }
1220
1221 void Advance() {
1222 DCHECK(!Done());
1223 current_ = current_->GetNext();
1224 }
1225
1226 HUseListNode<T>* Current() const {
1227 DCHECK(!Done());
1228 return current_;
1229 }
1230
1231 private:
1232 HUseListNode<T>* current_;
1233
1234 friend class HValue;
1235};
1236
David Brazdil1abb4192015-02-17 18:33:36 +00001237// This class is used by HEnvironment and HInstruction classes to record the
1238// instructions they use and pointers to the corresponding HUseListNodes kept
1239// by the used instructions.
1240template <typename T>
1241class HUserRecord : public ValueObject {
1242 public:
1243 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1244 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1245
1246 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1247 : instruction_(old_record.instruction_), use_node_(use_node) {
1248 DCHECK(instruction_ != nullptr);
1249 DCHECK(use_node_ != nullptr);
1250 DCHECK(old_record.use_node_ == nullptr);
1251 }
1252
1253 HInstruction* GetInstruction() const { return instruction_; }
1254 HUseListNode<T>* GetUseNode() const { return use_node_; }
1255
1256 private:
1257 // Instruction used by the user.
1258 HInstruction* instruction_;
1259
1260 // Corresponding entry in the use list kept by 'instruction_'.
1261 HUseListNode<T>* use_node_;
1262};
1263
Aart Bik854a02b2015-07-14 16:07:00 -07001264/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001265 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001266 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001267 * For write/read dependences on fields/arrays, the dependence analysis uses
1268 * type disambiguation (e.g. a float field write cannot modify the value of an
1269 * integer field read) and the access type (e.g. a reference array write cannot
1270 * modify the value of a reference field read [although it may modify the
1271 * reference fetch prior to reading the field, which is represented by its own
1272 * write/read dependence]). The analysis makes conservative points-to
1273 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1274 * the same, and any reference read depends on any reference read without
1275 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001276 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001277 * The internal representation uses 38-bit and is described in the table below.
1278 * The first line indicates the side effect, and for field/array accesses the
1279 * second line indicates the type of the access (in the order of the
1280 * Primitive::Type enum).
1281 * The two numbered lines below indicate the bit position in the bitfield (read
1282 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001283 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001284 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1285 * +-------------+---------+---------+--------------+---------+---------+
1286 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1287 * | 3 |333333322|222222221| 1 |111111110|000000000|
1288 * | 7 |654321098|765432109| 8 |765432109|876543210|
1289 *
1290 * Note that, to ease the implementation, 'changes' bits are least significant
1291 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001292 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001293class SideEffects : public ValueObject {
1294 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001295 SideEffects() : flags_(0) {}
1296
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001297 static SideEffects None() {
1298 return SideEffects(0);
1299 }
1300
1301 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001302 return SideEffects(kAllChangeBits | kAllDependOnBits);
1303 }
1304
1305 static SideEffects AllChanges() {
1306 return SideEffects(kAllChangeBits);
1307 }
1308
1309 static SideEffects AllDependencies() {
1310 return SideEffects(kAllDependOnBits);
1311 }
1312
1313 static SideEffects AllExceptGCDependency() {
1314 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1315 }
1316
1317 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001318 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001319 }
1320
Aart Bik34c3ba92015-07-20 14:08:59 -07001321 static SideEffects AllWrites() {
1322 return SideEffects(kAllWrites);
1323 }
1324
1325 static SideEffects AllReads() {
1326 return SideEffects(kAllReads);
1327 }
1328
1329 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1330 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001331 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001332 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001333 }
1334
Aart Bik854a02b2015-07-14 16:07:00 -07001335 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1336 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001337 }
1338
Aart Bik34c3ba92015-07-20 14:08:59 -07001339 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1340 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001341 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001342 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001343 }
1344
1345 static SideEffects ArrayReadOfType(Primitive::Type type) {
1346 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1347 }
1348
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001349 static SideEffects CanTriggerGC() {
1350 return SideEffects(1ULL << kCanTriggerGCBit);
1351 }
1352
1353 static SideEffects DependsOnGC() {
1354 return SideEffects(1ULL << kDependsOnGCBit);
1355 }
1356
Aart Bik854a02b2015-07-14 16:07:00 -07001357 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001358 SideEffects Union(SideEffects other) const {
1359 return SideEffects(flags_ | other.flags_);
1360 }
1361
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001362 SideEffects Exclusion(SideEffects other) const {
1363 return SideEffects(flags_ & ~other.flags_);
1364 }
1365
1366 bool Includes(SideEffects other) const {
1367 return (other.flags_ & flags_) == other.flags_;
1368 }
1369
1370 bool HasSideEffects() const {
1371 return (flags_ & kAllChangeBits);
1372 }
1373
1374 bool HasDependencies() const {
1375 return (flags_ & kAllDependOnBits);
1376 }
1377
1378 // Returns true if there are no side effects or dependencies.
1379 bool DoesNothing() const {
1380 return flags_ == 0;
1381 }
1382
Aart Bik854a02b2015-07-14 16:07:00 -07001383 // Returns true if something is written.
1384 bool DoesAnyWrite() const {
1385 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001386 }
1387
Aart Bik854a02b2015-07-14 16:07:00 -07001388 // Returns true if something is read.
1389 bool DoesAnyRead() const {
1390 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001391 }
1392
Aart Bik854a02b2015-07-14 16:07:00 -07001393 // Returns true if potentially everything is written and read
1394 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001395 bool DoesAllReadWrite() const {
1396 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1397 }
1398
Aart Bik854a02b2015-07-14 16:07:00 -07001399 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001400 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001401 }
1402
1403 // Returns true if this may read something written by other.
1404 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1406 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001407 }
1408
1409 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001410 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001411 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001412 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001413 for (int s = kLastBit; s >= 0; s--) {
1414 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1415 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1416 // This is a bit for the GC side effect.
1417 if (current_bit_is_set) {
1418 flags += "GC";
1419 }
Aart Bik854a02b2015-07-14 16:07:00 -07001420 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001421 } else {
1422 // This is a bit for the array/field analysis.
1423 // The underscore character stands for the 'can trigger GC' bit.
1424 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1425 if (current_bit_is_set) {
1426 flags += kDebug[s];
1427 }
1428 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1429 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1430 flags += "|";
1431 }
1432 }
Aart Bik854a02b2015-07-14 16:07:00 -07001433 }
1434 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001435 }
1436
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001437 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001438
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001439 private:
1440 static constexpr int kFieldArrayAnalysisBits = 9;
1441
1442 static constexpr int kFieldWriteOffset = 0;
1443 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1444 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1445 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1446
1447 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1448
1449 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1450 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1451 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1452 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1453
1454 static constexpr int kLastBit = kDependsOnGCBit;
1455 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1456
1457 // Aliases.
1458
1459 static_assert(kChangeBits == kDependOnBits,
1460 "the 'change' bits should match the 'depend on' bits.");
1461
1462 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1463 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1464 static constexpr uint64_t kAllWrites =
1465 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1466 static constexpr uint64_t kAllReads =
1467 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001468
Aart Bik854a02b2015-07-14 16:07:00 -07001469 // Work around the fact that HIR aliases I/F and J/D.
1470 // TODO: remove this interceptor once HIR types are clean
1471 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1472 switch (type) {
1473 case Primitive::kPrimInt:
1474 case Primitive::kPrimFloat:
1475 return TypeFlag(Primitive::kPrimInt, offset) |
1476 TypeFlag(Primitive::kPrimFloat, offset);
1477 case Primitive::kPrimLong:
1478 case Primitive::kPrimDouble:
1479 return TypeFlag(Primitive::kPrimLong, offset) |
1480 TypeFlag(Primitive::kPrimDouble, offset);
1481 default:
1482 return TypeFlag(type, offset);
1483 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001484 }
1485
Aart Bik854a02b2015-07-14 16:07:00 -07001486 // Translates type to bit flag.
1487 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1488 CHECK_NE(type, Primitive::kPrimVoid);
1489 const uint64_t one = 1;
1490 const int shift = type; // 0-based consecutive enum
1491 DCHECK_LE(kFieldWriteOffset, shift);
1492 DCHECK_LT(shift, kArrayWriteOffset);
1493 return one << (type + offset);
1494 }
1495
1496 // Private constructor on direct flags value.
1497 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1498
1499 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001500};
1501
David Brazdiled596192015-01-23 10:39:45 +00001502// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001503class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001504 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001505 HEnvironment(ArenaAllocator* arena,
1506 size_t number_of_vregs,
1507 const DexFile& dex_file,
1508 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001509 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001510 InvokeType invoke_type,
1511 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001512 : vregs_(arena, number_of_vregs),
1513 locations_(arena, number_of_vregs),
1514 parent_(nullptr),
1515 dex_file_(dex_file),
1516 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001517 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001518 invoke_type_(invoke_type),
1519 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001520 vregs_.SetSize(number_of_vregs);
1521 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001522 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001523 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001524
1525 locations_.SetSize(number_of_vregs);
1526 for (size_t i = 0; i < number_of_vregs; ++i) {
1527 locations_.Put(i, Location());
1528 }
1529 }
1530
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001531 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001532 : HEnvironment(arena,
1533 to_copy.Size(),
1534 to_copy.GetDexFile(),
1535 to_copy.GetMethodIdx(),
1536 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001537 to_copy.GetInvokeType(),
1538 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001539
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001540 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001541 if (parent_ != nullptr) {
1542 parent_->SetAndCopyParentChain(allocator, parent);
1543 } else {
1544 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1545 parent_->CopyFrom(parent);
1546 if (parent->GetParent() != nullptr) {
1547 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1548 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001549 }
David Brazdiled596192015-01-23 10:39:45 +00001550 }
1551
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001552 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1553 void CopyFrom(HEnvironment* environment);
1554
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001555 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1556 // input to the loop phi instead. This is for inserting instructions that
1557 // require an environment (like HDeoptimization) in the loop pre-header.
1558 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001559
1560 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001561 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001562 }
1563
1564 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001565 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001566 }
1567
David Brazdil1abb4192015-02-17 18:33:36 +00001568 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001569
1570 size_t Size() const { return vregs_.Size(); }
1571
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001572 HEnvironment* GetParent() const { return parent_; }
1573
1574 void SetLocationAt(size_t index, Location location) {
1575 locations_.Put(index, location);
1576 }
1577
1578 Location GetLocationAt(size_t index) const {
1579 return locations_.Get(index);
1580 }
1581
1582 uint32_t GetDexPc() const {
1583 return dex_pc_;
1584 }
1585
1586 uint32_t GetMethodIdx() const {
1587 return method_idx_;
1588 }
1589
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001590 InvokeType GetInvokeType() const {
1591 return invoke_type_;
1592 }
1593
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001594 const DexFile& GetDexFile() const {
1595 return dex_file_;
1596 }
1597
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001598 HInstruction* GetHolder() const {
1599 return holder_;
1600 }
1601
David Brazdiled596192015-01-23 10:39:45 +00001602 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001603 // Record instructions' use entries of this environment for constant-time removal.
1604 // It should only be called by HInstruction when a new environment use is added.
1605 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1606 DCHECK(env_use->GetUser() == this);
1607 size_t index = env_use->GetIndex();
1608 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1609 }
David Brazdiled596192015-01-23 10:39:45 +00001610
David Brazdil1abb4192015-02-17 18:33:36 +00001611 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001612 GrowableArray<Location> locations_;
1613 HEnvironment* parent_;
1614 const DexFile& dex_file_;
1615 const uint32_t method_idx_;
1616 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001617 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001618
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001619 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001620 HInstruction* const holder_;
1621
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001622 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001623
1624 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1625};
1626
Calin Juravleacf735c2015-02-12 15:25:22 +00001627class ReferenceTypeInfo : ValueObject {
1628 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001629 typedef Handle<mirror::Class> TypeHandle;
1630
Calin Juravle2e768302015-07-28 14:41:11 +00001631 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1632 // The constructor will check that the type_handle is valid.
1633 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001634 }
1635
Calin Juravle2e768302015-07-28 14:41:11 +00001636 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1637
1638 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1639 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001640 }
1641
Calin Juravle2e768302015-07-28 14:41:11 +00001642 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1643 return IsValidHandle(type_handle_);
1644 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001645 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001646
1647 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1648 DCHECK(IsValid());
1649 return GetTypeHandle()->IsObjectClass();
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
1656 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1657
Mathieu Chartier90443472015-07-16 20:32:27 -07001658 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001659 DCHECK(IsValid());
1660 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001661 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1662 }
1663
1664 // Returns true if the type information provide the same amount of details.
1665 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001666 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001667 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001668 if (!IsValid() && !rti.IsValid()) {
1669 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001670 return true;
1671 }
Calin Juravle2e768302015-07-28 14:41:11 +00001672 if (!IsValid() || !rti.IsValid()) {
1673 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001674 return false;
1675 }
Calin Juravle2e768302015-07-28 14:41:11 +00001676 return IsExact() == rti.IsExact()
1677 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001678 }
1679
1680 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001681 ReferenceTypeInfo();
1682 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001683
Calin Juravleacf735c2015-02-12 15:25:22 +00001684 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001685 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001686 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001687 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001688 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001689};
1690
1691std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1692
Vladimir Markof9f64412015-09-02 14:05:49 +01001693class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001694 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001695 HInstruction(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001696 : previous_(nullptr),
1697 next_(nullptr),
1698 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001699 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001700 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001701 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001702 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001703 locations_(nullptr),
1704 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001705 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001706 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001707 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001708
Dave Allison20dfc792014-06-16 20:44:29 -07001709 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001710
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001711#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001712 enum InstructionKind {
1713 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1714 };
1715#undef DECLARE_KIND
1716
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001717 HInstruction* GetNext() const { return next_; }
1718 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001719
Calin Juravle77520bc2015-01-12 18:45:46 +00001720 HInstruction* GetNextDisregardingMoves() const;
1721 HInstruction* GetPreviousDisregardingMoves() const;
1722
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001723 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001724 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001725 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001726 bool IsInBlock() const { return block_ != nullptr; }
1727 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001728 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001729
Roland Levillain6b879dd2014-09-22 17:13:44 +01001730 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001731 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001732
1733 virtual void Accept(HGraphVisitor* visitor) = 0;
1734 virtual const char* DebugName() const = 0;
1735
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001736 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001737 void SetRawInputAt(size_t index, HInstruction* input) {
1738 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1739 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001740
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001741 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001742
1743 uint32_t GetDexPc() const { return dex_pc_; }
1744
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001745 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001746
Roland Levillaine161a2a2014-10-03 12:45:18 +01001747 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001748 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001749
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001750 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001751 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001752
Calin Juravle10e244f2015-01-26 18:54:32 +00001753 // Does not apply for all instructions, but having this at top level greatly
1754 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001755 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001756 virtual bool CanBeNull() const {
1757 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1758 return true;
1759 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001760
Calin Juravle641547a2015-04-21 22:08:51 +01001761 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1762 UNUSED(obj);
1763 return false;
1764 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001765
Calin Juravle2e768302015-07-28 14:41:11 +00001766 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001767
Calin Juravle61d544b2015-02-23 16:46:57 +00001768 ReferenceTypeInfo GetReferenceTypeInfo() const {
1769 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1770 return reference_type_info_;
1771 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001772
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001773 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001774 DCHECK(user != nullptr);
1775 HUseListNode<HInstruction*>* use =
1776 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1777 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001778 }
1779
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001780 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001781 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001782 HUseListNode<HEnvironment*>* env_use =
1783 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1784 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001785 }
1786
David Brazdil1abb4192015-02-17 18:33:36 +00001787 void RemoveAsUserOfInput(size_t input) {
1788 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1789 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1790 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001791
David Brazdil1abb4192015-02-17 18:33:36 +00001792 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1793 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001794
David Brazdiled596192015-01-23 10:39:45 +00001795 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1796 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001797 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001798 bool HasOnlyOneNonEnvironmentUse() const {
1799 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1800 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001801
Roland Levillain6c82d402014-10-13 16:10:27 +01001802 // Does this instruction strictly dominate `other_instruction`?
1803 // Returns false if this instruction and `other_instruction` are the same.
1804 // Aborts if this instruction and `other_instruction` are both phis.
1805 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001806
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001807 int GetId() const { return id_; }
1808 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001809
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001810 int GetSsaIndex() const { return ssa_index_; }
1811 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1812 bool HasSsaIndex() const { return ssa_index_ != -1; }
1813
1814 bool HasEnvironment() const { return environment_ != nullptr; }
1815 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001816 // Set the `environment_` field. Raw because this method does not
1817 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001818 void SetRawEnvironment(HEnvironment* environment) {
1819 DCHECK(environment_ == nullptr);
1820 DCHECK_EQ(environment->GetHolder(), this);
1821 environment_ = environment;
1822 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001823
1824 // Set the environment of this instruction, copying it from `environment`. While
1825 // copying, the uses lists are being updated.
1826 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001827 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001828 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001829 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001830 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001831 if (environment->GetParent() != nullptr) {
1832 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1833 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001834 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001835
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001836 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1837 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001838 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001839 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001840 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001841 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001842 if (environment->GetParent() != nullptr) {
1843 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1844 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001845 }
1846
Nicolas Geoffray39468442014-09-02 15:17:15 +01001847 // Returns the number of entries in the environment. Typically, that is the
1848 // number of dex registers in a method. It could be more in case of inlining.
1849 size_t EnvironmentSize() const;
1850
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001851 LocationSummary* GetLocations() const { return locations_; }
1852 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001853
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001854 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001855 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001856
Alexandre Rames188d4312015-04-09 18:30:21 +01001857 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1858 // uses of this instruction by `other` are *not* updated.
1859 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1860 ReplaceWith(other);
1861 other->ReplaceInput(this, use_index);
1862 }
1863
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001864 // Move `this` instruction before `cursor`.
1865 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001866
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001867#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001868 bool Is##type() const { return (As##type() != nullptr); } \
1869 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001870 virtual H##type* As##type() { return nullptr; }
1871
1872 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1873#undef INSTRUCTION_TYPE_CHECK
1874
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001875 // Returns whether the instruction can be moved within the graph.
1876 virtual bool CanBeMoved() const { return false; }
1877
1878 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001879 virtual bool InstructionTypeEquals(HInstruction* other) const {
1880 UNUSED(other);
1881 return false;
1882 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001883
1884 // Returns whether any data encoded in the two instructions is equal.
1885 // This method does not look at the inputs. Both instructions must be
1886 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001887 virtual bool InstructionDataEquals(HInstruction* other) const {
1888 UNUSED(other);
1889 return false;
1890 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001891
1892 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001893 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001894 // 2) Their inputs are identical.
1895 bool Equals(HInstruction* other) const;
1896
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001897 virtual InstructionKind GetKind() const = 0;
1898
1899 virtual size_t ComputeHashCode() const {
1900 size_t result = GetKind();
1901 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1902 result = (result * 31) + InputAt(i)->GetId();
1903 }
1904 return result;
1905 }
1906
1907 SideEffects GetSideEffects() const { return side_effects_; }
1908
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001909 size_t GetLifetimePosition() const { return lifetime_position_; }
1910 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1911 LiveInterval* GetLiveInterval() const { return live_interval_; }
1912 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1913 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1914
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001915 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1916
1917 // Returns whether the code generation of the instruction will require to have access
1918 // to the current method. Such instructions are:
1919 // (1): Instructions that require an environment, as calling the runtime requires
1920 // to walk the stack and have the current method stored at a specific stack address.
1921 // (2): Object literals like classes and strings, that are loaded from the dex cache
1922 // fields of the current method.
1923 bool NeedsCurrentMethod() const {
1924 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1925 }
1926
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001927 virtual bool NeedsDexCache() const { return false; }
1928
Mark Mendellc4701932015-04-10 13:18:51 -04001929 // Does this instruction have any use in an environment before
1930 // control flow hits 'other'?
1931 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1932
1933 // Remove all references to environment uses of this instruction.
1934 // The caller must ensure that this is safe to do.
1935 void RemoveEnvironmentUsers();
1936
David Brazdil1abb4192015-02-17 18:33:36 +00001937 protected:
1938 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1939 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1940
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001941 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001942 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1943
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001944 HInstruction* previous_;
1945 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001946 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001947 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001948
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001949 // An instruction gets an id when it is added to the graph.
1950 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001951 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001952 int id_;
1953
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001954 // When doing liveness analysis, instructions that have uses get an SSA index.
1955 int ssa_index_;
1956
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001958 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959
1960 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001961 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001962
Nicolas Geoffray39468442014-09-02 15:17:15 +01001963 // The environment associated with this instruction. Not null if the instruction
1964 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001965 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001966
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001967 // Set by the code generator.
1968 LocationSummary* locations_;
1969
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001970 // Set by the liveness analysis.
1971 LiveInterval* live_interval_;
1972
1973 // Set by the liveness analysis, this is the position in a linear
1974 // order of blocks where this instruction's live interval start.
1975 size_t lifetime_position_;
1976
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001977 const SideEffects side_effects_;
1978
Calin Juravleacf735c2015-02-12 15:25:22 +00001979 // TODO: for primitive types this should be marked as invalid.
1980 ReferenceTypeInfo reference_type_info_;
1981
David Brazdil1abb4192015-02-17 18:33:36 +00001982 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001983 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001984 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001985 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001986 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001987
1988 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1989};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001990std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001991
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001992class HInputIterator : public ValueObject {
1993 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001994 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001995
1996 bool Done() const { return index_ == instruction_->InputCount(); }
1997 HInstruction* Current() const { return instruction_->InputAt(index_); }
1998 void Advance() { index_++; }
1999
2000 private:
2001 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002002 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002003
2004 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2005};
2006
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002007class HInstructionIterator : public ValueObject {
2008 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002009 explicit HInstructionIterator(const HInstructionList& instructions)
2010 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002011 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002012 }
2013
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002014 bool Done() const { return instruction_ == nullptr; }
2015 HInstruction* Current() const { return instruction_; }
2016 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002017 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002018 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002019 }
2020
2021 private:
2022 HInstruction* instruction_;
2023 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002024
2025 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002026};
2027
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002028class HBackwardInstructionIterator : public ValueObject {
2029 public:
2030 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2031 : instruction_(instructions.last_instruction_) {
2032 next_ = Done() ? nullptr : instruction_->GetPrevious();
2033 }
2034
2035 bool Done() const { return instruction_ == nullptr; }
2036 HInstruction* Current() const { return instruction_; }
2037 void Advance() {
2038 instruction_ = next_;
2039 next_ = Done() ? nullptr : instruction_->GetPrevious();
2040 }
2041
2042 private:
2043 HInstruction* instruction_;
2044 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002045
2046 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002047};
2048
Vladimir Markof9f64412015-09-02 14:05:49 +01002049template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002050class HTemplateInstruction: public HInstruction {
2051 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002052 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2053 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002054 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002056 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002057
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002058 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002059 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2060 DCHECK_LT(i, N);
2061 return inputs_[i];
2062 }
David Brazdil1abb4192015-02-17 18:33:36 +00002063
2064 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002065 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002066 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002067 }
2068
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002069 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002070 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002071
2072 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002073};
2074
Vladimir Markof9f64412015-09-02 14:05:49 +01002075// HTemplateInstruction specialization for N=0.
2076template<>
2077class HTemplateInstruction<0>: public HInstruction {
2078 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002079 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2080 : HInstruction(side_effects, dex_pc) {}
2081
Vladimir Markof9f64412015-09-02 14:05:49 +01002082 virtual ~HTemplateInstruction() {}
2083
2084 size_t InputCount() const OVERRIDE { return 0; }
2085
2086 protected:
2087 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2088 LOG(FATAL) << "Unreachable";
2089 UNREACHABLE();
2090 }
2091
2092 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2093 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2094 LOG(FATAL) << "Unreachable";
2095 UNREACHABLE();
2096 }
2097
2098 private:
2099 friend class SsaBuilder;
2100};
2101
Dave Allison20dfc792014-06-16 20:44:29 -07002102template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002103class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002104 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002105 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2106 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002107 virtual ~HExpression() {}
2108
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002109 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002110
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002111 protected:
2112 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002113};
2114
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002115// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2116// instruction that branches to the exit block.
2117class HReturnVoid : public HTemplateInstruction<0> {
2118 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002119 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2120 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002121
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002122 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002123
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002124 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002125
2126 private:
2127 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2128};
2129
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002130// Represents dex's RETURN opcodes. A HReturn is a control flow
2131// instruction that branches to the exit block.
2132class HReturn : public HTemplateInstruction<1> {
2133 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002134 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2135 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002136 SetRawInputAt(0, value);
2137 }
2138
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002139 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002140
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002141 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002142
2143 private:
2144 DISALLOW_COPY_AND_ASSIGN(HReturn);
2145};
2146
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002147// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002148// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002149// exit block.
2150class HExit : public HTemplateInstruction<0> {
2151 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002152 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002153
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002154 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002155
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002156 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157
2158 private:
2159 DISALLOW_COPY_AND_ASSIGN(HExit);
2160};
2161
2162// Jumps from one block to another.
2163class HGoto : public HTemplateInstruction<0> {
2164 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002165 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002166
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002167 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002168
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002169 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002170 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002171 }
2172
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002173 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002174
2175 private:
2176 DISALLOW_COPY_AND_ASSIGN(HGoto);
2177};
2178
Roland Levillain9867bc72015-08-05 10:21:34 +01002179class HConstant : public HExpression<0> {
2180 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002181 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2182 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002183
2184 bool CanBeMoved() const OVERRIDE { return true; }
2185
2186 virtual bool IsMinusOne() const { return false; }
2187 virtual bool IsZero() const { return false; }
2188 virtual bool IsOne() const { return false; }
2189
2190 DECLARE_INSTRUCTION(Constant);
2191
2192 private:
2193 DISALLOW_COPY_AND_ASSIGN(HConstant);
2194};
2195
2196class HNullConstant : public HConstant {
2197 public:
2198 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2199 return true;
2200 }
2201
2202 size_t ComputeHashCode() const OVERRIDE { return 0; }
2203
2204 DECLARE_INSTRUCTION(NullConstant);
2205
2206 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002207 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002208
2209 friend class HGraph;
2210 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2211};
2212
2213// Constants of the type int. Those can be from Dex instructions, or
2214// synthesized (for example with the if-eqz instruction).
2215class HIntConstant : public HConstant {
2216 public:
2217 int32_t GetValue() const { return value_; }
2218
2219 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2220 DCHECK(other->IsIntConstant());
2221 return other->AsIntConstant()->value_ == value_;
2222 }
2223
2224 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2225
2226 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2227 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2228 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2229
2230 DECLARE_INSTRUCTION(IntConstant);
2231
2232 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002233 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2234 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2235 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2236 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002237
2238 const int32_t value_;
2239
2240 friend class HGraph;
2241 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2242 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2243 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2244};
2245
2246class HLongConstant : public HConstant {
2247 public:
2248 int64_t GetValue() const { return value_; }
2249
2250 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2251 DCHECK(other->IsLongConstant());
2252 return other->AsLongConstant()->value_ == value_;
2253 }
2254
2255 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2256
2257 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2258 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2259 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2260
2261 DECLARE_INSTRUCTION(LongConstant);
2262
2263 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002264 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2265 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002266
2267 const int64_t value_;
2268
2269 friend class HGraph;
2270 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2271};
Dave Allison20dfc792014-06-16 20:44:29 -07002272
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002273// Conditional branch. A block ending with an HIf instruction must have
2274// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002275class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002276 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002277 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2278 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002279 SetRawInputAt(0, input);
2280 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002281
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002282 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002283
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002284 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002285 return GetBlock()->GetSuccessor(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002286 }
2287
2288 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002289 return GetBlock()->GetSuccessor(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002290 }
2291
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002292 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002293
2294 private:
2295 DISALLOW_COPY_AND_ASSIGN(HIf);
2296};
2297
David Brazdilfc6a86a2015-06-26 10:33:45 +00002298
2299// Abstract instruction which marks the beginning and/or end of a try block and
2300// links it to the respective exception handlers. Behaves the same as a Goto in
2301// non-exceptional control flow.
2302// Normal-flow successor is stored at index zero, exception handlers under
2303// higher indices in no particular order.
2304class HTryBoundary : public HTemplateInstruction<0> {
2305 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002306 enum BoundaryKind {
2307 kEntry,
2308 kExit,
2309 };
2310
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002311 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2312 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002313
2314 bool IsControlFlow() const OVERRIDE { return true; }
2315
2316 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko60584552015-09-03 13:35:12 +00002317 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessor(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002318
2319 // Returns whether `handler` is among its exception handlers (non-zero index
2320 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002321 bool HasExceptionHandler(const HBasicBlock& handler) const {
2322 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002323 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002324 }
2325
2326 // If not present already, adds `handler` to its block's list of exception
2327 // handlers.
2328 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002329 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002330 GetBlock()->AddSuccessor(handler);
2331 }
2332 }
2333
David Brazdil56e1acc2015-06-30 15:41:36 +01002334 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002335
David Brazdilffee3d32015-07-06 11:48:53 +01002336 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2337
David Brazdilfc6a86a2015-06-26 10:33:45 +00002338 DECLARE_INSTRUCTION(TryBoundary);
2339
2340 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002341 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002342
2343 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2344};
2345
David Brazdilffee3d32015-07-06 11:48:53 +01002346// Iterator over exception handlers of a given HTryBoundary, i.e. over
2347// exceptional successors of its basic block.
2348class HExceptionHandlerIterator : public ValueObject {
2349 public:
2350 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2351 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2352
Vladimir Marko60584552015-09-03 13:35:12 +00002353 bool Done() const { return index_ == block_.GetSuccessors().size(); }
2354 HBasicBlock* Current() const { return block_.GetSuccessor(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002355 size_t CurrentSuccessorIndex() const { return index_; }
2356 void Advance() { ++index_; }
2357
2358 private:
2359 const HBasicBlock& block_;
2360 size_t index_;
2361
2362 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2363};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002364
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002365// Deoptimize to interpreter, upon checking a condition.
2366class HDeoptimize : public HTemplateInstruction<1> {
2367 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002368 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2369 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002370 SetRawInputAt(0, cond);
2371 }
2372
2373 bool NeedsEnvironment() const OVERRIDE { return true; }
2374 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002375
2376 DECLARE_INSTRUCTION(Deoptimize);
2377
2378 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002379 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2380};
2381
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002382// Represents the ArtMethod that was passed as a first argument to
2383// the method. It is used by instructions that depend on it, like
2384// instructions that work with the dex cache.
2385class HCurrentMethod : public HExpression<0> {
2386 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002387 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2388 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002389
2390 DECLARE_INSTRUCTION(CurrentMethod);
2391
2392 private:
2393 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2394};
2395
Roland Levillain88cb1752014-10-20 16:36:47 +01002396class HUnaryOperation : public HExpression<1> {
2397 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002398 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2399 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002400 SetRawInputAt(0, input);
2401 }
2402
2403 HInstruction* GetInput() const { return InputAt(0); }
2404 Primitive::Type GetResultType() const { return GetType(); }
2405
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002406 bool CanBeMoved() const OVERRIDE { return true; }
2407 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002408 UNUSED(other);
2409 return true;
2410 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002411
Roland Levillain9240d6a2014-10-20 16:47:04 +01002412 // Try to statically evaluate `operation` and return a HConstant
2413 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002414 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002415 HConstant* TryStaticEvaluation() const;
2416
2417 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002418 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2419 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002420
Roland Levillain88cb1752014-10-20 16:36:47 +01002421 DECLARE_INSTRUCTION(UnaryOperation);
2422
2423 private:
2424 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2425};
2426
Dave Allison20dfc792014-06-16 20:44:29 -07002427class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002428 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002429 HBinaryOperation(Primitive::Type result_type,
2430 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002431 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002432 SideEffects side_effects = SideEffects::None(),
2433 uint32_t dex_pc = kNoDexPc)
2434 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002435 SetRawInputAt(0, left);
2436 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002437 }
2438
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002439 HInstruction* GetLeft() const { return InputAt(0); }
2440 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002441 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002442
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002443 virtual bool IsCommutative() const { return false; }
2444
2445 // Put constant on the right.
2446 // Returns whether order is changed.
2447 bool OrderInputsWithConstantOnTheRight() {
2448 HInstruction* left = InputAt(0);
2449 HInstruction* right = InputAt(1);
2450 if (left->IsConstant() && !right->IsConstant()) {
2451 ReplaceInput(right, 0);
2452 ReplaceInput(left, 1);
2453 return true;
2454 }
2455 return false;
2456 }
2457
2458 // Order inputs by instruction id, but favor constant on the right side.
2459 // This helps GVN for commutative ops.
2460 void OrderInputs() {
2461 DCHECK(IsCommutative());
2462 HInstruction* left = InputAt(0);
2463 HInstruction* right = InputAt(1);
2464 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2465 return;
2466 }
2467 if (OrderInputsWithConstantOnTheRight()) {
2468 return;
2469 }
2470 // Order according to instruction id.
2471 if (left->GetId() > right->GetId()) {
2472 ReplaceInput(right, 0);
2473 ReplaceInput(left, 1);
2474 }
2475 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002476
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002477 bool CanBeMoved() const OVERRIDE { return true; }
2478 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002479 UNUSED(other);
2480 return true;
2481 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002482
Roland Levillain9240d6a2014-10-20 16:47:04 +01002483 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002484 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002485 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002486 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002487
2488 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002489 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2490 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2491 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2492 HLongConstant* y ATTRIBUTE_UNUSED) const {
2493 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2494 return nullptr;
2495 }
2496 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2497 HIntConstant* y ATTRIBUTE_UNUSED) const {
2498 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2499 return nullptr;
2500 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002501
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002502 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002503 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002504 HConstant* GetConstantRight() const;
2505
2506 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002507 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002508 HInstruction* GetLeastConstantLeft() const;
2509
Roland Levillainccc07a92014-09-16 14:48:16 +01002510 DECLARE_INSTRUCTION(BinaryOperation);
2511
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002512 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002513 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2514};
2515
Mark Mendellc4701932015-04-10 13:18:51 -04002516// The comparison bias applies for floating point operations and indicates how NaN
2517// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002518enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002519 kNoBias, // bias is not applicable (i.e. for long operation)
2520 kGtBias, // return 1 for NaN comparisons
2521 kLtBias, // return -1 for NaN comparisons
2522};
2523
Dave Allison20dfc792014-06-16 20:44:29 -07002524class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002525 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002526 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2527 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002528 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002529 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002530
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002531 bool NeedsMaterialization() const { return needs_materialization_; }
2532 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002533
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002534 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002535 // `instruction`, and disregard moves in between.
2536 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002537
Dave Allison20dfc792014-06-16 20:44:29 -07002538 DECLARE_INSTRUCTION(Condition);
2539
2540 virtual IfCondition GetCondition() const = 0;
2541
Mark Mendellc4701932015-04-10 13:18:51 -04002542 virtual IfCondition GetOppositeCondition() const = 0;
2543
Roland Levillain4fa13f62015-07-06 18:11:54 +01002544 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002545
2546 void SetBias(ComparisonBias bias) { bias_ = bias; }
2547
2548 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2549 return bias_ == other->AsCondition()->bias_;
2550 }
2551
Roland Levillain4fa13f62015-07-06 18:11:54 +01002552 bool IsFPConditionTrueIfNaN() const {
2553 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2554 IfCondition if_cond = GetCondition();
2555 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2556 }
2557
2558 bool IsFPConditionFalseIfNaN() const {
2559 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2560 IfCondition if_cond = GetCondition();
2561 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2562 }
2563
Dave Allison20dfc792014-06-16 20:44:29 -07002564 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002565 // For register allocation purposes, returns whether this instruction needs to be
2566 // materialized (that is, not just be in the processor flags).
2567 bool needs_materialization_;
2568
Mark Mendellc4701932015-04-10 13:18:51 -04002569 // Needed if we merge a HCompare into a HCondition.
2570 ComparisonBias bias_;
2571
Dave Allison20dfc792014-06-16 20:44:29 -07002572 DISALLOW_COPY_AND_ASSIGN(HCondition);
2573};
2574
2575// Instruction to check if two inputs are equal to each other.
2576class HEqual : public HCondition {
2577 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002578 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2579 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002580
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002581 bool IsCommutative() const OVERRIDE { return true; }
2582
Roland Levillain9867bc72015-08-05 10:21:34 +01002583 template <typename T> bool Compute(T x, T y) const { return x == y; }
2584
2585 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002586 return GetBlock()->GetGraph()->GetIntConstant(
2587 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002588 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002589 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002590 return GetBlock()->GetGraph()->GetIntConstant(
2591 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002592 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002593
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002594 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002595
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002596 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002597 return kCondEQ;
2598 }
2599
Mark Mendellc4701932015-04-10 13:18:51 -04002600 IfCondition GetOppositeCondition() const OVERRIDE {
2601 return kCondNE;
2602 }
2603
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002604 private:
2605 DISALLOW_COPY_AND_ASSIGN(HEqual);
2606};
2607
Dave Allison20dfc792014-06-16 20:44:29 -07002608class HNotEqual : public HCondition {
2609 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002610 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2611 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002612
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002613 bool IsCommutative() const OVERRIDE { return true; }
2614
Roland Levillain9867bc72015-08-05 10:21:34 +01002615 template <typename T> bool Compute(T x, T y) const { return x != y; }
2616
2617 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002618 return GetBlock()->GetGraph()->GetIntConstant(
2619 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002620 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002621 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002622 return GetBlock()->GetGraph()->GetIntConstant(
2623 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002624 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002625
Dave Allison20dfc792014-06-16 20:44:29 -07002626 DECLARE_INSTRUCTION(NotEqual);
2627
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002628 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002629 return kCondNE;
2630 }
2631
Mark Mendellc4701932015-04-10 13:18:51 -04002632 IfCondition GetOppositeCondition() const OVERRIDE {
2633 return kCondEQ;
2634 }
2635
Dave Allison20dfc792014-06-16 20:44:29 -07002636 private:
2637 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2638};
2639
2640class HLessThan : public HCondition {
2641 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002642 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2643 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002644
Roland Levillain9867bc72015-08-05 10:21:34 +01002645 template <typename T> bool Compute(T x, T y) const { return x < y; }
2646
2647 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002648 return GetBlock()->GetGraph()->GetIntConstant(
2649 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002650 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002651 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002652 return GetBlock()->GetGraph()->GetIntConstant(
2653 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002654 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002655
Dave Allison20dfc792014-06-16 20:44:29 -07002656 DECLARE_INSTRUCTION(LessThan);
2657
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002658 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002659 return kCondLT;
2660 }
2661
Mark Mendellc4701932015-04-10 13:18:51 -04002662 IfCondition GetOppositeCondition() const OVERRIDE {
2663 return kCondGE;
2664 }
2665
Dave Allison20dfc792014-06-16 20:44:29 -07002666 private:
2667 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2668};
2669
2670class HLessThanOrEqual : public HCondition {
2671 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002672 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2673 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002674
Roland Levillain9867bc72015-08-05 10:21:34 +01002675 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2676
2677 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002678 return GetBlock()->GetGraph()->GetIntConstant(
2679 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002680 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002681 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002682 return GetBlock()->GetGraph()->GetIntConstant(
2683 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002684 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002685
Dave Allison20dfc792014-06-16 20:44:29 -07002686 DECLARE_INSTRUCTION(LessThanOrEqual);
2687
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002688 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002689 return kCondLE;
2690 }
2691
Mark Mendellc4701932015-04-10 13:18:51 -04002692 IfCondition GetOppositeCondition() const OVERRIDE {
2693 return kCondGT;
2694 }
2695
Dave Allison20dfc792014-06-16 20:44:29 -07002696 private:
2697 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2698};
2699
2700class HGreaterThan : public HCondition {
2701 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002702 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2703 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002704
Roland Levillain9867bc72015-08-05 10:21:34 +01002705 template <typename T> bool Compute(T x, T y) const { return x > y; }
2706
2707 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002708 return GetBlock()->GetGraph()->GetIntConstant(
2709 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002710 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002711 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002712 return GetBlock()->GetGraph()->GetIntConstant(
2713 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002714 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002715
Dave Allison20dfc792014-06-16 20:44:29 -07002716 DECLARE_INSTRUCTION(GreaterThan);
2717
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002718 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002719 return kCondGT;
2720 }
2721
Mark Mendellc4701932015-04-10 13:18:51 -04002722 IfCondition GetOppositeCondition() const OVERRIDE {
2723 return kCondLE;
2724 }
2725
Dave Allison20dfc792014-06-16 20:44:29 -07002726 private:
2727 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2728};
2729
2730class HGreaterThanOrEqual : public HCondition {
2731 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002732 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2733 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002734
Roland Levillain9867bc72015-08-05 10:21:34 +01002735 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2736
2737 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002738 return GetBlock()->GetGraph()->GetIntConstant(
2739 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002740 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002741 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002742 return GetBlock()->GetGraph()->GetIntConstant(
2743 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002744 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002745
Dave Allison20dfc792014-06-16 20:44:29 -07002746 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2747
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002748 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002749 return kCondGE;
2750 }
2751
Mark Mendellc4701932015-04-10 13:18:51 -04002752 IfCondition GetOppositeCondition() const OVERRIDE {
2753 return kCondLT;
2754 }
2755
Dave Allison20dfc792014-06-16 20:44:29 -07002756 private:
2757 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2758};
2759
2760
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002761// Instruction to check how two inputs compare to each other.
2762// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2763class HCompare : public HBinaryOperation {
2764 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002765 HCompare(Primitive::Type type,
2766 HInstruction* first,
2767 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002768 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002769 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002770 : HBinaryOperation(Primitive::kPrimInt,
2771 first,
2772 second,
2773 SideEffectsForArchRuntimeCalls(type),
2774 dex_pc),
2775 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002776 DCHECK_EQ(type, first->GetType());
2777 DCHECK_EQ(type, second->GetType());
2778 }
2779
Roland Levillain9867bc72015-08-05 10:21:34 +01002780 template <typename T>
2781 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002782
Roland Levillain9867bc72015-08-05 10:21:34 +01002783 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002784 return GetBlock()->GetGraph()->GetIntConstant(
2785 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002786 }
2787 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002788 return GetBlock()->GetGraph()->GetIntConstant(
2789 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002790 }
2791
Calin Juravleddb7df22014-11-25 20:56:51 +00002792 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2793 return bias_ == other->AsCompare()->bias_;
2794 }
2795
Mark Mendellc4701932015-04-10 13:18:51 -04002796 ComparisonBias GetBias() const { return bias_; }
2797
Roland Levillain4fa13f62015-07-06 18:11:54 +01002798 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002799
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002800
2801 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2802 // MIPS64 uses a runtime call for FP comparisons.
2803 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2804 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002805
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002806 DECLARE_INSTRUCTION(Compare);
2807
2808 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002809 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002810
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002811 DISALLOW_COPY_AND_ASSIGN(HCompare);
2812};
2813
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002814// A local in the graph. Corresponds to a Dex register.
2815class HLocal : public HTemplateInstruction<0> {
2816 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002817 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002818 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002819
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002820 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002821
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002822 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002823
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002824 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002825 // The Dex register number.
2826 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002827
2828 DISALLOW_COPY_AND_ASSIGN(HLocal);
2829};
2830
2831// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002832class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002833 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002834 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2835 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002836 SetRawInputAt(0, local);
2837 }
2838
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002839 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2840
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002841 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002842
2843 private:
2844 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2845};
2846
2847// Store a value in a given local. This instruction has two inputs: the value
2848// and the local.
2849class HStoreLocal : public HTemplateInstruction<2> {
2850 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002851 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2852 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002853 SetRawInputAt(0, local);
2854 SetRawInputAt(1, value);
2855 }
2856
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002857 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2858
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002859 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002860
2861 private:
2862 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2863};
2864
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002865class HFloatConstant : public HConstant {
2866 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002867 float GetValue() const { return value_; }
2868
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002869 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002870 DCHECK(other->IsFloatConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002871 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2872 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002873 }
2874
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002875 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002876
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002877 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002878 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002879 }
2880 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002881 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002882 }
2883 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002884 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2885 }
2886 bool IsNaN() const {
2887 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002888 }
2889
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002890 DECLARE_INSTRUCTION(FloatConstant);
2891
2892 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002893 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2894 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2895 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2896 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002897
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002898 const float value_;
2899
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002900 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002901 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002902 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002903 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2904};
2905
2906class HDoubleConstant : public HConstant {
2907 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002908 double GetValue() const { return value_; }
2909
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002910 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002911 DCHECK(other->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002912 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2913 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002914 }
2915
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002916 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002917
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002918 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002919 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002920 }
2921 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002922 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002923 }
2924 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002925 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2926 }
2927 bool IsNaN() const {
2928 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002929 }
2930
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002931 DECLARE_INSTRUCTION(DoubleConstant);
2932
2933 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002934 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2935 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2936 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2937 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002938
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002939 const double value_;
2940
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002941 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002942 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002943 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002944 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2945};
2946
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002947enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002948#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002949#include "intrinsics_list.h"
2950 kNone,
2951 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2952#undef INTRINSICS_LIST
2953#undef OPTIMIZING_INTRINSICS
2954};
2955std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2956
Agi Csaki05f20562015-08-19 14:58:14 -07002957enum IntrinsicNeedsEnvironmentOrCache {
2958 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
2959 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07002960};
2961
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002962class HInvoke : public HInstruction {
2963 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002964 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002965
2966 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2967 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07002968 bool NeedsEnvironment() const OVERRIDE {
2969 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
2970 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002971
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002972 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002973 SetRawInputAt(index, argument);
2974 }
2975
Roland Levillain3e3d7332015-04-28 11:00:54 +01002976 // Return the number of arguments. This number can be lower than
2977 // the number of inputs returned by InputCount(), as some invoke
2978 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2979 // inputs at the end of their list of inputs.
2980 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2981
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002982 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002983
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002984
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002985 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002986 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002987
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002988 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2989
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002990 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002991 return intrinsic_;
2992 }
2993
Agi Csaki05f20562015-08-19 14:58:14 -07002994 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002995 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07002996 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002997 }
2998
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002999 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003000 return GetEnvironment()->GetParent() != nullptr;
3001 }
3002
3003 bool CanThrow() const OVERRIDE { return true; }
3004
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003005 DECLARE_INSTRUCTION(Invoke);
3006
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003007 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003008 HInvoke(ArenaAllocator* arena,
3009 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003010 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003011 Primitive::Type return_type,
3012 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003013 uint32_t dex_method_index,
3014 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003015 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003016 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003017 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003018 inputs_(arena, number_of_arguments),
3019 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003020 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003021 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003022 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07003023 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003024 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
3025 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003026 }
3027
David Brazdil1abb4192015-02-17 18:33:36 +00003028 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3029 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3030 inputs_.Put(index, input);
3031 }
3032
Roland Levillain3e3d7332015-04-28 11:00:54 +01003033 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00003034 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003035 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003036 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003037 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003038 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003039 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003040
3041 private:
3042 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3043};
3044
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003045class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003046 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003047 // Requirements of this method call regarding the class
3048 // initialization (clinit) check of its declaring class.
3049 enum class ClinitCheckRequirement {
3050 kNone, // Class already initialized.
3051 kExplicit, // Static call having explicit clinit check as last input.
3052 kImplicit, // Static call implicitly requiring a clinit check.
3053 };
3054
Vladimir Marko58155012015-08-19 12:49:41 +00003055 // Determines how to load the target ArtMethod*.
3056 enum class MethodLoadKind {
3057 // Use a String init ArtMethod* loaded from Thread entrypoints.
3058 kStringInit,
3059
3060 // Use the method's own ArtMethod* loaded by the register allocator.
3061 kRecursive,
3062
3063 // Use ArtMethod* at a known address, embed the direct address in the code.
3064 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3065 kDirectAddress,
3066
3067 // Use ArtMethod* at an address that will be known at link time, embed the direct
3068 // address in the code. If the image is relocatable, emit .patch_oat entry.
3069 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3070 // the image relocatable or not.
3071 kDirectAddressWithFixup,
3072
3073 // Load from resoved methods array in the dex cache using a PC-relative load.
3074 // Used when we need to use the dex cache, for example for invoke-static that
3075 // may cause class initialization (the entry may point to a resolution method),
3076 // and we know that we can access the dex cache arrays using a PC-relative load.
3077 kDexCachePcRelative,
3078
3079 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3080 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3081 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3082 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3083 kDexCacheViaMethod,
3084 };
3085
3086 // Determines the location of the code pointer.
3087 enum class CodePtrLocation {
3088 // Recursive call, use local PC-relative call instruction.
3089 kCallSelf,
3090
3091 // Use PC-relative call instruction patched at link time.
3092 // Used for calls within an oat file, boot->boot or app->app.
3093 kCallPCRelative,
3094
3095 // Call to a known target address, embed the direct address in code.
3096 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3097 kCallDirect,
3098
3099 // Call to a target address that will be known at link time, embed the direct
3100 // address in code. If the image is relocatable, emit .patch_oat entry.
3101 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3102 // the image relocatable or not.
3103 kCallDirectWithFixup,
3104
3105 // Use code pointer from the ArtMethod*.
3106 // Used when we don't know the target code. This is also the last-resort-kind used when
3107 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3108 kCallArtMethod,
3109 };
3110
3111 struct DispatchInfo {
3112 const MethodLoadKind method_load_kind;
3113 const CodePtrLocation code_ptr_location;
3114 // The method load data holds
3115 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3116 // Note that there are multiple string init methods, each having its own offset.
3117 // - the method address for kDirectAddress
3118 // - the dex cache arrays offset for kDexCachePcRel.
3119 const uint64_t method_load_data;
3120 const uint64_t direct_code_ptr;
3121 };
3122
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003123 HInvokeStaticOrDirect(ArenaAllocator* arena,
3124 uint32_t number_of_arguments,
3125 Primitive::Type return_type,
3126 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003127 uint32_t method_index,
3128 MethodReference target_method,
3129 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003130 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003131 InvokeType invoke_type,
3132 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003133 : HInvoke(arena,
3134 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003135 // There is one extra argument for the HCurrentMethod node, and
3136 // potentially one other if the clinit check is explicit, and one other
3137 // if the method is a string factory.
3138 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003139 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003140 return_type,
3141 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003142 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003143 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003144 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003145 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003146 target_method_(target_method),
3147 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003148
Calin Juravle641547a2015-04-21 22:08:51 +01003149 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3150 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003151 // We access the method via the dex cache so we can't do an implicit null check.
3152 // TODO: for intrinsics we can generate implicit null checks.
3153 return false;
3154 }
3155
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003156 bool CanBeNull() const OVERRIDE {
3157 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3158 }
3159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003160 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003161 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3162 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3163 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003164 bool NeedsDexCache() const OVERRIDE {
3165 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3166 return !IsRecursive() && !IsStringInit();
3167 }
Vladimir Marko58155012015-08-19 12:49:41 +00003168 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003169 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003170 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3171 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3172 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3173 MethodReference GetTargetMethod() const { return target_method_; }
3174
3175 int32_t GetStringInitOffset() const {
3176 DCHECK(IsStringInit());
3177 return dispatch_info_.method_load_data;
3178 }
3179
3180 uint64_t GetMethodAddress() const {
3181 DCHECK(HasMethodAddress());
3182 return dispatch_info_.method_load_data;
3183 }
3184
3185 uint32_t GetDexCacheArrayOffset() const {
3186 DCHECK(HasPcRelDexCache());
3187 return dispatch_info_.method_load_data;
3188 }
3189
3190 uint64_t GetDirectCodePtr() const {
3191 DCHECK(HasDirectCodePtr());
3192 return dispatch_info_.direct_code_ptr;
3193 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003194
Calin Juravle68ad6492015-08-18 17:08:12 +01003195 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3196
Roland Levillain4c0eb422015-04-24 16:43:49 +01003197 // Is this instruction a call to a static method?
3198 bool IsStatic() const {
3199 return GetInvokeType() == kStatic;
3200 }
3201
Roland Levillain3e3d7332015-04-28 11:00:54 +01003202 // Remove the art::HLoadClass instruction set as last input by
3203 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3204 // the initial art::HClinitCheck instruction (only relevant for
3205 // static calls with explicit clinit check).
3206 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003207 DCHECK(IsStaticWithExplicitClinitCheck());
3208 size_t last_input_index = InputCount() - 1;
3209 HInstruction* last_input = InputAt(last_input_index);
3210 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003211 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003212 RemoveAsUserOfInput(last_input_index);
3213 inputs_.DeleteAt(last_input_index);
3214 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3215 DCHECK(IsStaticWithImplicitClinitCheck());
3216 }
3217
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003218 bool IsStringFactoryFor(HFakeString* str) const {
3219 if (!IsStringInit()) return false;
3220 // +1 for the current method.
3221 if (InputCount() == (number_of_arguments_ + 1)) return false;
3222 return InputAt(InputCount() - 1)->AsFakeString() == str;
3223 }
3224
3225 void RemoveFakeStringArgumentAsLastInput() {
3226 DCHECK(IsStringInit());
3227 size_t last_input_index = InputCount() - 1;
3228 HInstruction* last_input = InputAt(last_input_index);
3229 DCHECK(last_input != nullptr);
3230 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3231 RemoveAsUserOfInput(last_input_index);
3232 inputs_.DeleteAt(last_input_index);
3233 }
3234
Roland Levillain4c0eb422015-04-24 16:43:49 +01003235 // Is this a call to a static method whose declaring class has an
3236 // explicit intialization check in the graph?
3237 bool IsStaticWithExplicitClinitCheck() const {
3238 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3239 }
3240
3241 // Is this a call to a static method whose declaring class has an
3242 // implicit intialization check requirement?
3243 bool IsStaticWithImplicitClinitCheck() const {
3244 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3245 }
3246
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003247 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003248
Roland Levillain4c0eb422015-04-24 16:43:49 +01003249 protected:
3250 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3251 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3252 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3253 HInstruction* input = input_record.GetInstruction();
3254 // `input` is the last input of a static invoke marked as having
3255 // an explicit clinit check. It must either be:
3256 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3257 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3258 DCHECK(input != nullptr);
3259 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3260 }
3261 return input_record;
3262 }
3263
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003264 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003265 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003266 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003267 // The target method may refer to different dex file or method index than the original
3268 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3269 // in derived class to increase visibility.
3270 MethodReference target_method_;
3271 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003272
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003273 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003274};
3275
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003276class HInvokeVirtual : public HInvoke {
3277 public:
3278 HInvokeVirtual(ArenaAllocator* arena,
3279 uint32_t number_of_arguments,
3280 Primitive::Type return_type,
3281 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003282 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003283 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003284 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003285 vtable_index_(vtable_index) {}
3286
Calin Juravle641547a2015-04-21 22:08:51 +01003287 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003288 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003289 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003290 }
3291
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003292 uint32_t GetVTableIndex() const { return vtable_index_; }
3293
3294 DECLARE_INSTRUCTION(InvokeVirtual);
3295
3296 private:
3297 const uint32_t vtable_index_;
3298
3299 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3300};
3301
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003302class HInvokeInterface : public HInvoke {
3303 public:
3304 HInvokeInterface(ArenaAllocator* arena,
3305 uint32_t number_of_arguments,
3306 Primitive::Type return_type,
3307 uint32_t dex_pc,
3308 uint32_t dex_method_index,
3309 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003310 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003311 imt_index_(imt_index) {}
3312
Calin Juravle641547a2015-04-21 22:08:51 +01003313 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003314 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003315 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003316 }
3317
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003318 uint32_t GetImtIndex() const { return imt_index_; }
3319 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3320
3321 DECLARE_INSTRUCTION(InvokeInterface);
3322
3323 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003324 const uint32_t imt_index_;
3325
3326 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3327};
3328
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003329class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003330 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003331 HNewInstance(HCurrentMethod* current_method,
3332 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003333 uint16_t type_index,
3334 const DexFile& dex_file,
3335 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003336 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003337 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003338 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003339 entrypoint_(entrypoint) {
3340 SetRawInputAt(0, current_method);
3341 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003342
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003343 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003344 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003345
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003346 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003347 bool NeedsEnvironment() const OVERRIDE { return true; }
3348 // It may throw when called on:
3349 // - interfaces
3350 // - abstract/innaccessible/unknown classes
3351 // TODO: optimize when possible.
3352 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003353
Calin Juravle10e244f2015-01-26 18:54:32 +00003354 bool CanBeNull() const OVERRIDE { return false; }
3355
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003356 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3357
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003358 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003359
3360 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003361 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003362 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003363 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003364
3365 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3366};
3367
Roland Levillain88cb1752014-10-20 16:36:47 +01003368class HNeg : public HUnaryOperation {
3369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003370 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3371 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003372
Roland Levillain9867bc72015-08-05 10:21:34 +01003373 template <typename T> T Compute(T x) const { return -x; }
3374
3375 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003376 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003377 }
3378 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003379 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003380 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003381
Roland Levillain88cb1752014-10-20 16:36:47 +01003382 DECLARE_INSTRUCTION(Neg);
3383
3384 private:
3385 DISALLOW_COPY_AND_ASSIGN(HNeg);
3386};
3387
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003388class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003389 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003390 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003391 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003392 uint32_t dex_pc,
3393 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003394 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003395 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003396 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003397 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003398 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003399 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003400 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003401 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003402 }
3403
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003404 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003405 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003406
3407 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003408 bool NeedsEnvironment() const OVERRIDE { return true; }
3409
Mingyao Yang0c365e62015-03-31 15:09:29 -07003410 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3411 bool CanThrow() const OVERRIDE { return true; }
3412
Calin Juravle10e244f2015-01-26 18:54:32 +00003413 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003414
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003415 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3416
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003417 DECLARE_INSTRUCTION(NewArray);
3418
3419 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003420 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003421 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003422 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003423
3424 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3425};
3426
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003427class HAdd : public HBinaryOperation {
3428 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003429 HAdd(Primitive::Type result_type,
3430 HInstruction* left,
3431 HInstruction* right,
3432 uint32_t dex_pc = kNoDexPc)
3433 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003434
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003435 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003436
Roland Levillain9867bc72015-08-05 10:21:34 +01003437 template <typename T> T Compute(T x, T y) const { return x + y; }
3438
3439 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003440 return GetBlock()->GetGraph()->GetIntConstant(
3441 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003442 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003443 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003444 return GetBlock()->GetGraph()->GetLongConstant(
3445 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003446 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003447
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003448 DECLARE_INSTRUCTION(Add);
3449
3450 private:
3451 DISALLOW_COPY_AND_ASSIGN(HAdd);
3452};
3453
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003454class HSub : public HBinaryOperation {
3455 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003456 HSub(Primitive::Type result_type,
3457 HInstruction* left,
3458 HInstruction* right,
3459 uint32_t dex_pc = kNoDexPc)
3460 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003461
Roland Levillain9867bc72015-08-05 10:21:34 +01003462 template <typename T> T Compute(T x, T y) const { return x - y; }
3463
3464 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003465 return GetBlock()->GetGraph()->GetIntConstant(
3466 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003467 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003468 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003469 return GetBlock()->GetGraph()->GetLongConstant(
3470 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003471 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003472
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003473 DECLARE_INSTRUCTION(Sub);
3474
3475 private:
3476 DISALLOW_COPY_AND_ASSIGN(HSub);
3477};
3478
Calin Juravle34bacdf2014-10-07 20:23:36 +01003479class HMul : public HBinaryOperation {
3480 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003481 HMul(Primitive::Type result_type,
3482 HInstruction* left,
3483 HInstruction* right,
3484 uint32_t dex_pc = kNoDexPc)
3485 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003486
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003487 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003488
Roland Levillain9867bc72015-08-05 10:21:34 +01003489 template <typename T> T Compute(T x, T y) const { return x * y; }
3490
3491 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003492 return GetBlock()->GetGraph()->GetIntConstant(
3493 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003494 }
3495 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003496 return GetBlock()->GetGraph()->GetLongConstant(
3497 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003498 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003499
3500 DECLARE_INSTRUCTION(Mul);
3501
3502 private:
3503 DISALLOW_COPY_AND_ASSIGN(HMul);
3504};
3505
Calin Juravle7c4954d2014-10-28 16:57:40 +00003506class HDiv : public HBinaryOperation {
3507 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003508 HDiv(Primitive::Type result_type,
3509 HInstruction* left,
3510 HInstruction* right,
3511 uint32_t dex_pc)
3512 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003513
Roland Levillain9867bc72015-08-05 10:21:34 +01003514 template <typename T>
3515 T Compute(T x, T y) const {
3516 // Our graph structure ensures we never have 0 for `y` during
3517 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003518 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003519 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003520 return (y == -1) ? -x : x / y;
3521 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003522
Roland Levillain9867bc72015-08-05 10:21:34 +01003523 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003524 return GetBlock()->GetGraph()->GetIntConstant(
3525 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003526 }
3527 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003528 return GetBlock()->GetGraph()->GetLongConstant(
3529 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003530 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003531
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003532 static SideEffects SideEffectsForArchRuntimeCalls() {
3533 // The generated code can use a runtime call.
3534 return SideEffects::CanTriggerGC();
3535 }
3536
Calin Juravle7c4954d2014-10-28 16:57:40 +00003537 DECLARE_INSTRUCTION(Div);
3538
3539 private:
3540 DISALLOW_COPY_AND_ASSIGN(HDiv);
3541};
3542
Calin Juravlebacfec32014-11-14 15:54:36 +00003543class HRem : public HBinaryOperation {
3544 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003545 HRem(Primitive::Type result_type,
3546 HInstruction* left,
3547 HInstruction* right,
3548 uint32_t dex_pc)
3549 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003550
Roland Levillain9867bc72015-08-05 10:21:34 +01003551 template <typename T>
3552 T Compute(T x, T y) const {
3553 // Our graph structure ensures we never have 0 for `y` during
3554 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003555 DCHECK_NE(y, 0);
3556 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3557 return (y == -1) ? 0 : x % y;
3558 }
3559
Roland Levillain9867bc72015-08-05 10:21:34 +01003560 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003561 return GetBlock()->GetGraph()->GetIntConstant(
3562 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003563 }
3564 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003565 return GetBlock()->GetGraph()->GetLongConstant(
3566 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003567 }
3568
Calin Juravlebacfec32014-11-14 15:54:36 +00003569
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003570 static SideEffects SideEffectsForArchRuntimeCalls() {
3571 return SideEffects::CanTriggerGC();
3572 }
3573
Calin Juravlebacfec32014-11-14 15:54:36 +00003574 DECLARE_INSTRUCTION(Rem);
3575
3576 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003577 DISALLOW_COPY_AND_ASSIGN(HRem);
3578};
3579
Calin Juravled0d48522014-11-04 16:40:20 +00003580class HDivZeroCheck : public HExpression<1> {
3581 public:
3582 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003583 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003584 SetRawInputAt(0, value);
3585 }
3586
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003587 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3588
Calin Juravled0d48522014-11-04 16:40:20 +00003589 bool CanBeMoved() const OVERRIDE { return true; }
3590
3591 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3592 UNUSED(other);
3593 return true;
3594 }
3595
3596 bool NeedsEnvironment() const OVERRIDE { return true; }
3597 bool CanThrow() const OVERRIDE { return true; }
3598
Calin Juravled0d48522014-11-04 16:40:20 +00003599 DECLARE_INSTRUCTION(DivZeroCheck);
3600
3601 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003602 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3603};
3604
Calin Juravle9aec02f2014-11-18 23:06:35 +00003605class HShl : public HBinaryOperation {
3606 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003607 HShl(Primitive::Type result_type,
3608 HInstruction* left,
3609 HInstruction* right,
3610 uint32_t dex_pc = kNoDexPc)
3611 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003612
Roland Levillain9867bc72015-08-05 10:21:34 +01003613 template <typename T, typename U, typename V>
3614 T Compute(T x, U y, V max_shift_value) const {
3615 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3616 "V is not the unsigned integer type corresponding to T");
3617 return x << (y & max_shift_value);
3618 }
3619
3620 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3621 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003622 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003623 }
3624 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3625 // case is handled as `x << static_cast<int>(y)`.
3626 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3627 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003628 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003629 }
3630 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3631 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003632 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003633 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003634
3635 DECLARE_INSTRUCTION(Shl);
3636
3637 private:
3638 DISALLOW_COPY_AND_ASSIGN(HShl);
3639};
3640
3641class HShr : public HBinaryOperation {
3642 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003643 HShr(Primitive::Type result_type,
3644 HInstruction* left,
3645 HInstruction* right,
3646 uint32_t dex_pc = kNoDexPc)
3647 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003648
Roland Levillain9867bc72015-08-05 10:21:34 +01003649 template <typename T, typename U, typename V>
3650 T Compute(T x, U y, V max_shift_value) const {
3651 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3652 "V is not the unsigned integer type corresponding to T");
3653 return x >> (y & max_shift_value);
3654 }
3655
3656 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3657 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003658 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003659 }
3660 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3661 // case is handled as `x >> static_cast<int>(y)`.
3662 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3663 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003664 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003665 }
3666 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3667 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003668 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003669 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003670
3671 DECLARE_INSTRUCTION(Shr);
3672
3673 private:
3674 DISALLOW_COPY_AND_ASSIGN(HShr);
3675};
3676
3677class HUShr : public HBinaryOperation {
3678 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003679 HUShr(Primitive::Type result_type,
3680 HInstruction* left,
3681 HInstruction* right,
3682 uint32_t dex_pc = kNoDexPc)
3683 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003684
Roland Levillain9867bc72015-08-05 10:21:34 +01003685 template <typename T, typename U, typename V>
3686 T Compute(T x, U y, V max_shift_value) const {
3687 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3688 "V is not the unsigned integer type corresponding to T");
3689 V ux = static_cast<V>(x);
3690 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003691 }
3692
Roland Levillain9867bc72015-08-05 10:21:34 +01003693 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3694 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003695 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003696 }
3697 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3698 // case is handled as `x >>> static_cast<int>(y)`.
3699 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3700 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003701 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003702 }
3703 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3704 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003705 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003706 }
3707
3708 DECLARE_INSTRUCTION(UShr);
3709
3710 private:
3711 DISALLOW_COPY_AND_ASSIGN(HUShr);
3712};
3713
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003714class HAnd : public HBinaryOperation {
3715 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003716 HAnd(Primitive::Type result_type,
3717 HInstruction* left,
3718 HInstruction* right,
3719 uint32_t dex_pc = kNoDexPc)
3720 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003721
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003722 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003723
Roland Levillain9867bc72015-08-05 10:21:34 +01003724 template <typename T, typename U>
3725 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3726
3727 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003728 return GetBlock()->GetGraph()->GetIntConstant(
3729 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003730 }
3731 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003732 return GetBlock()->GetGraph()->GetLongConstant(
3733 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003734 }
3735 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003736 return GetBlock()->GetGraph()->GetLongConstant(
3737 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003738 }
3739 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003740 return GetBlock()->GetGraph()->GetLongConstant(
3741 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003742 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003743
3744 DECLARE_INSTRUCTION(And);
3745
3746 private:
3747 DISALLOW_COPY_AND_ASSIGN(HAnd);
3748};
3749
3750class HOr : public HBinaryOperation {
3751 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003752 HOr(Primitive::Type result_type,
3753 HInstruction* left,
3754 HInstruction* right,
3755 uint32_t dex_pc = kNoDexPc)
3756 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003757
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003758 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003759
Roland Levillain9867bc72015-08-05 10:21:34 +01003760 template <typename T, typename U>
3761 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3762
3763 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003764 return GetBlock()->GetGraph()->GetIntConstant(
3765 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003766 }
3767 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003768 return GetBlock()->GetGraph()->GetLongConstant(
3769 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003770 }
3771 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003772 return GetBlock()->GetGraph()->GetLongConstant(
3773 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003774 }
3775 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003776 return GetBlock()->GetGraph()->GetLongConstant(
3777 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003778 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003779
3780 DECLARE_INSTRUCTION(Or);
3781
3782 private:
3783 DISALLOW_COPY_AND_ASSIGN(HOr);
3784};
3785
3786class HXor : public HBinaryOperation {
3787 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003788 HXor(Primitive::Type result_type,
3789 HInstruction* left,
3790 HInstruction* right,
3791 uint32_t dex_pc = kNoDexPc)
3792 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003793
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003794 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003795
Roland Levillain9867bc72015-08-05 10:21:34 +01003796 template <typename T, typename U>
3797 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3798
3799 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003800 return GetBlock()->GetGraph()->GetIntConstant(
3801 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003802 }
3803 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003804 return GetBlock()->GetGraph()->GetLongConstant(
3805 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003806 }
3807 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003808 return GetBlock()->GetGraph()->GetLongConstant(
3809 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003810 }
3811 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003812 return GetBlock()->GetGraph()->GetLongConstant(
3813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003814 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003815
3816 DECLARE_INSTRUCTION(Xor);
3817
3818 private:
3819 DISALLOW_COPY_AND_ASSIGN(HXor);
3820};
3821
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003822// The value of a parameter in this method. Its location depends on
3823// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003824class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003825 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003826 HParameterValue(uint8_t index,
3827 Primitive::Type parameter_type,
3828 bool is_this = false)
3829 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003830 index_(index),
3831 is_this_(is_this),
3832 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003833
3834 uint8_t GetIndex() const { return index_; }
3835
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003836 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3837 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003838
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003839 bool IsThis() const { return is_this_; }
3840
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003841 DECLARE_INSTRUCTION(ParameterValue);
3842
3843 private:
3844 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003845 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003846 const uint8_t index_;
3847
Calin Juravle10e244f2015-01-26 18:54:32 +00003848 // Whether or not the parameter value corresponds to 'this' argument.
3849 const bool is_this_;
3850
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003851 bool can_be_null_;
3852
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003853 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3854};
3855
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003856class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003857 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003858 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3859 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003860
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003861 bool CanBeMoved() const OVERRIDE { return true; }
3862 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003863 UNUSED(other);
3864 return true;
3865 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003866
Roland Levillain9867bc72015-08-05 10:21:34 +01003867 template <typename T> T Compute(T x) const { return ~x; }
3868
3869 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003870 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003871 }
3872 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003873 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003874 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003875
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003876 DECLARE_INSTRUCTION(Not);
3877
3878 private:
3879 DISALLOW_COPY_AND_ASSIGN(HNot);
3880};
3881
David Brazdil66d126e2015-04-03 16:02:44 +01003882class HBooleanNot : public HUnaryOperation {
3883 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003884 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3885 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003886
3887 bool CanBeMoved() const OVERRIDE { return true; }
3888 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3889 UNUSED(other);
3890 return true;
3891 }
3892
Roland Levillain9867bc72015-08-05 10:21:34 +01003893 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003894 DCHECK(IsUint<1>(x));
3895 return !x;
3896 }
3897
Roland Levillain9867bc72015-08-05 10:21:34 +01003898 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003899 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003900 }
3901 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3902 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003903 UNREACHABLE();
3904 }
3905
3906 DECLARE_INSTRUCTION(BooleanNot);
3907
3908 private:
3909 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3910};
3911
Roland Levillaindff1f282014-11-05 14:15:05 +00003912class HTypeConversion : public HExpression<1> {
3913 public:
3914 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003915 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003916 : HExpression(result_type,
3917 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
3918 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003919 SetRawInputAt(0, input);
3920 DCHECK_NE(input->GetType(), result_type);
3921 }
3922
3923 HInstruction* GetInput() const { return InputAt(0); }
3924 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3925 Primitive::Type GetResultType() const { return GetType(); }
3926
Roland Levillain624279f2014-12-04 11:54:28 +00003927 // Required by the x86 and ARM code generators when producing calls
3928 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003929
Roland Levillaindff1f282014-11-05 14:15:05 +00003930 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003931 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003932
Mark Mendelle82549b2015-05-06 10:55:34 -04003933 // Try to statically evaluate the conversion and return a HConstant
3934 // containing the result. If the input cannot be converted, return nullptr.
3935 HConstant* TryStaticEvaluation() const;
3936
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003937 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3938 Primitive::Type result_type) {
3939 // Some architectures may not require the 'GC' side effects, but at this point
3940 // in the compilation process we do not know what architecture we will
3941 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003942 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3943 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003944 return SideEffects::CanTriggerGC();
3945 }
3946 return SideEffects::None();
3947 }
3948
Roland Levillaindff1f282014-11-05 14:15:05 +00003949 DECLARE_INSTRUCTION(TypeConversion);
3950
3951 private:
3952 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3953};
3954
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003955static constexpr uint32_t kNoRegNumber = -1;
3956
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003957class HPhi : public HInstruction {
3958 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003959 HPhi(ArenaAllocator* arena,
3960 uint32_t reg_number,
3961 size_t number_of_inputs,
3962 Primitive::Type type,
3963 uint32_t dex_pc = kNoDexPc)
3964 : HInstruction(SideEffects::None(), dex_pc),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003965 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003966 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003967 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003968 is_live_(false),
3969 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003970 inputs_.SetSize(number_of_inputs);
3971 }
3972
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003973 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3974 static Primitive::Type ToPhiType(Primitive::Type type) {
3975 switch (type) {
3976 case Primitive::kPrimBoolean:
3977 case Primitive::kPrimByte:
3978 case Primitive::kPrimShort:
3979 case Primitive::kPrimChar:
3980 return Primitive::kPrimInt;
3981 default:
3982 return type;
3983 }
3984 }
3985
David Brazdilffee3d32015-07-06 11:48:53 +01003986 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3987
Calin Juravle10e244f2015-01-26 18:54:32 +00003988 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003989
3990 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003991 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003992
Calin Juravle10e244f2015-01-26 18:54:32 +00003993 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003994 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003995
Calin Juravle10e244f2015-01-26 18:54:32 +00003996 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3997 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3998
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003999 uint32_t GetRegNumber() const { return reg_number_; }
4000
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004001 void SetDead() { is_live_ = false; }
4002 void SetLive() { is_live_ = true; }
4003 bool IsDead() const { return !is_live_; }
4004 bool IsLive() const { return is_live_; }
4005
Calin Juravlea4f88312015-04-16 12:57:19 +01004006 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4007 // An equivalent phi is a phi having the same dex register and type.
4008 // It assumes that phis with the same dex register are adjacent.
4009 HPhi* GetNextEquivalentPhiWithSameType() {
4010 HInstruction* next = GetNext();
4011 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4012 if (next->GetType() == GetType()) {
4013 return next->AsPhi();
4014 }
4015 next = next->GetNext();
4016 }
4017 return nullptr;
4018 }
4019
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004020 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004021
David Brazdil1abb4192015-02-17 18:33:36 +00004022 protected:
4023 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
4024
4025 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
4026 inputs_.Put(index, input);
4027 }
4028
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004029 private:
David Brazdil1abb4192015-02-17 18:33:36 +00004030 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004031 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004032 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004033 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004034 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004035
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004036 DISALLOW_COPY_AND_ASSIGN(HPhi);
4037};
4038
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004039class HNullCheck : public HExpression<1> {
4040 public:
4041 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004042 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004043 SetRawInputAt(0, value);
4044 }
4045
Calin Juravle10e244f2015-01-26 18:54:32 +00004046 bool CanBeMoved() const OVERRIDE { return true; }
4047 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004048 UNUSED(other);
4049 return true;
4050 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004051
Calin Juravle10e244f2015-01-26 18:54:32 +00004052 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004053
Calin Juravle10e244f2015-01-26 18:54:32 +00004054 bool CanThrow() const OVERRIDE { return true; }
4055
4056 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004057
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004058
4059 DECLARE_INSTRUCTION(NullCheck);
4060
4061 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004062 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4063};
4064
4065class FieldInfo : public ValueObject {
4066 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004067 FieldInfo(MemberOffset field_offset,
4068 Primitive::Type field_type,
4069 bool is_volatile,
4070 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004071 const DexFile& dex_file,
4072 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004073 : field_offset_(field_offset),
4074 field_type_(field_type),
4075 is_volatile_(is_volatile),
4076 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004077 dex_file_(dex_file),
4078 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004079
4080 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004081 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004082 uint32_t GetFieldIndex() const { return index_; }
4083 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004084 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004085 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004086
4087 private:
4088 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004089 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004090 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004091 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004092 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004093 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004094};
4095
4096class HInstanceFieldGet : public HExpression<1> {
4097 public:
4098 HInstanceFieldGet(HInstruction* value,
4099 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004100 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004101 bool is_volatile,
4102 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004103 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004104 Handle<mirror::DexCache> dex_cache,
4105 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004106 : HExpression(
4107 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004108 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004109 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004110 SetRawInputAt(0, value);
4111 }
4112
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004113 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004114
4115 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4116 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4117 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004118 }
4119
Calin Juravle641547a2015-04-21 22:08:51 +01004120 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4121 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004122 }
4123
4124 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004125 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4126 }
4127
Calin Juravle52c48962014-12-16 17:02:57 +00004128 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004129 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004130 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004131 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004132
4133 DECLARE_INSTRUCTION(InstanceFieldGet);
4134
4135 private:
4136 const FieldInfo field_info_;
4137
4138 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4139};
4140
4141class HInstanceFieldSet : public HTemplateInstruction<2> {
4142 public:
4143 HInstanceFieldSet(HInstruction* object,
4144 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004145 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004146 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004147 bool is_volatile,
4148 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004149 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004150 Handle<mirror::DexCache> dex_cache,
4151 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004152 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004153 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004154 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004155 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004156 SetRawInputAt(0, object);
4157 SetRawInputAt(1, value);
4158 }
4159
Calin Juravle641547a2015-04-21 22:08:51 +01004160 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4161 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004162 }
4163
Calin Juravle52c48962014-12-16 17:02:57 +00004164 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004165 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004166 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004167 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004168 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004169 bool GetValueCanBeNull() const { return value_can_be_null_; }
4170 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004171
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004172 DECLARE_INSTRUCTION(InstanceFieldSet);
4173
4174 private:
4175 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004176 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004177
4178 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4179};
4180
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004181class HArrayGet : public HExpression<2> {
4182 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004183 HArrayGet(HInstruction* array,
4184 HInstruction* index,
4185 Primitive::Type type,
4186 uint32_t dex_pc = kNoDexPc)
4187 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004188 SetRawInputAt(0, array);
4189 SetRawInputAt(1, index);
4190 }
4191
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004192 bool CanBeMoved() const OVERRIDE { return true; }
4193 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004194 UNUSED(other);
4195 return true;
4196 }
Calin Juravle641547a2015-04-21 22:08:51 +01004197 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4198 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004199 // TODO: We can be smarter here.
4200 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4201 // which generates the implicit null check. There are cases when these can be removed
4202 // to produce better code. If we ever add optimizations to do so we should allow an
4203 // implicit check here (as long as the address falls in the first page).
4204 return false;
4205 }
4206
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004207 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004208
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004209 HInstruction* GetArray() const { return InputAt(0); }
4210 HInstruction* GetIndex() const { return InputAt(1); }
4211
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004212 DECLARE_INSTRUCTION(ArrayGet);
4213
4214 private:
4215 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4216};
4217
4218class HArraySet : public HTemplateInstruction<3> {
4219 public:
4220 HArraySet(HInstruction* array,
4221 HInstruction* index,
4222 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004223 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004224 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004225 : HTemplateInstruction(
4226 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004227 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004228 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004229 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4230 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004231 SetRawInputAt(0, array);
4232 SetRawInputAt(1, index);
4233 SetRawInputAt(2, value);
4234 }
4235
Calin Juravle77520bc2015-01-12 18:45:46 +00004236 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004237 // We currently always call a runtime method to catch array store
4238 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004239 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004240 }
4241
Mingyao Yang81014cb2015-06-02 03:16:27 -07004242 // Can throw ArrayStoreException.
4243 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4244
Calin Juravle641547a2015-04-21 22:08:51 +01004245 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4246 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004247 // TODO: Same as for ArrayGet.
4248 return false;
4249 }
4250
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004251 void ClearNeedsTypeCheck() {
4252 needs_type_check_ = false;
4253 }
4254
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004255 void ClearValueCanBeNull() {
4256 value_can_be_null_ = false;
4257 }
4258
4259 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004260 bool NeedsTypeCheck() const { return needs_type_check_; }
4261
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004262 HInstruction* GetArray() const { return InputAt(0); }
4263 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004264 HInstruction* GetValue() const { return InputAt(2); }
4265
4266 Primitive::Type GetComponentType() const {
4267 // The Dex format does not type floating point index operations. Since the
4268 // `expected_component_type_` is set during building and can therefore not
4269 // be correct, we also check what is the value type. If it is a floating
4270 // point type, we must use that type.
4271 Primitive::Type value_type = GetValue()->GetType();
4272 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4273 ? value_type
4274 : expected_component_type_;
4275 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004276
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004277 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4278 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4279 }
4280
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004281 DECLARE_INSTRUCTION(ArraySet);
4282
4283 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004284 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004285 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004286 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004287
4288 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4289};
4290
4291class HArrayLength : public HExpression<1> {
4292 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004293 explicit HArrayLength(HInstruction* array, uint32_t dex_pc = kNoDexPc)
4294 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004295 // Note that arrays do not change length, so the instruction does not
4296 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004297 SetRawInputAt(0, array);
4298 }
4299
Calin Juravle77520bc2015-01-12 18:45:46 +00004300 bool CanBeMoved() const OVERRIDE { return true; }
4301 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004302 UNUSED(other);
4303 return true;
4304 }
Calin Juravle641547a2015-04-21 22:08:51 +01004305 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4306 return obj == InputAt(0);
4307 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004308
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004309 DECLARE_INSTRUCTION(ArrayLength);
4310
4311 private:
4312 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4313};
4314
4315class HBoundsCheck : public HExpression<2> {
4316 public:
4317 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004318 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004319 DCHECK(index->GetType() == Primitive::kPrimInt);
4320 SetRawInputAt(0, index);
4321 SetRawInputAt(1, length);
4322 }
4323
Alexandre Rames2ed20af2015-03-06 13:55:35 +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 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004329
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004330 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004331
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004332 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004333
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004334
4335 DECLARE_INSTRUCTION(BoundsCheck);
4336
4337 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004338 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4339};
4340
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004341/**
4342 * Some DEX instructions are folded into multiple HInstructions that need
4343 * to stay live until the last HInstruction. This class
4344 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004345 * HInstruction stays live. `index` represents the stack location index of the
4346 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004347 */
4348class HTemporary : public HTemplateInstruction<0> {
4349 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004350 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4351 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004352
4353 size_t GetIndex() const { return index_; }
4354
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004355 Primitive::Type GetType() const OVERRIDE {
4356 // The previous instruction is the one that will be stored in the temporary location.
4357 DCHECK(GetPrevious() != nullptr);
4358 return GetPrevious()->GetType();
4359 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004360
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004361 DECLARE_INSTRUCTION(Temporary);
4362
4363 private:
4364 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004365 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4366};
4367
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004368class HSuspendCheck : public HTemplateInstruction<0> {
4369 public:
4370 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004371 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004372
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004373 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004374 return true;
4375 }
4376
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004377 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4378 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004379
4380 DECLARE_INSTRUCTION(SuspendCheck);
4381
4382 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004383 // Only used for code generation, in order to share the same slow path between back edges
4384 // of a same loop.
4385 SlowPathCode* slow_path_;
4386
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004387 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4388};
4389
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004390/**
4391 * Instruction to load a Class object.
4392 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004393class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004394 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004395 HLoadClass(HCurrentMethod* current_method,
4396 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004397 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004398 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004399 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004400 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004401 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004402 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004403 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004404 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004405 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004406 SetRawInputAt(0, current_method);
4407 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004408
4409 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004410
4411 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4412 return other->AsLoadClass()->type_index_ == type_index_;
4413 }
4414
4415 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4416
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004417 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004418 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004419 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004420
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004421 bool NeedsEnvironment() const OVERRIDE {
4422 // Will call runtime and load the class if the class is not loaded yet.
4423 // TODO: finer grain decision.
4424 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004425 }
4426
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004427 bool MustGenerateClinitCheck() const {
4428 return generate_clinit_check_;
4429 }
4430
Calin Juravle0ba218d2015-05-19 18:46:01 +01004431 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4432 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004433 }
4434
4435 bool CanCallRuntime() const {
4436 return MustGenerateClinitCheck() || !is_referrers_class_;
4437 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004438
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004439 bool CanThrow() const OVERRIDE {
4440 // May call runtime and and therefore can throw.
4441 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004442 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004443 }
4444
Calin Juravleacf735c2015-02-12 15:25:22 +00004445 ReferenceTypeInfo GetLoadedClassRTI() {
4446 return loaded_class_rti_;
4447 }
4448
4449 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4450 // Make sure we only set exact types (the loaded class should never be merged).
4451 DCHECK(rti.IsExact());
4452 loaded_class_rti_ = rti;
4453 }
4454
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004455 const DexFile& GetDexFile() { return dex_file_; }
4456
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004457 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4458
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004459 static SideEffects SideEffectsForArchRuntimeCalls() {
4460 return SideEffects::CanTriggerGC();
4461 }
4462
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004463 DECLARE_INSTRUCTION(LoadClass);
4464
4465 private:
4466 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004467 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004468 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004469 // Whether this instruction must generate the initialization check.
4470 // Used for code generation.
4471 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004472
Calin Juravleacf735c2015-02-12 15:25:22 +00004473 ReferenceTypeInfo loaded_class_rti_;
4474
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004475 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4476};
4477
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004478class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004479 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004480 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004481 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4482 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004483 SetRawInputAt(0, current_method);
4484 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004485
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004486 bool CanBeMoved() const OVERRIDE { return true; }
4487
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004488 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4489 return other->AsLoadString()->string_index_ == string_index_;
4490 }
4491
4492 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4493
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004494 uint32_t GetStringIndex() const { return string_index_; }
4495
4496 // TODO: Can we deopt or debug when we resolve a string?
4497 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004498 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004499 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004500
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004501 static SideEffects SideEffectsForArchRuntimeCalls() {
4502 return SideEffects::CanTriggerGC();
4503 }
4504
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004505 DECLARE_INSTRUCTION(LoadString);
4506
4507 private:
4508 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004509
4510 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4511};
4512
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004513/**
4514 * Performs an initialization check on its Class object input.
4515 */
4516class HClinitCheck : public HExpression<1> {
4517 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004518 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004519 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004520 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004521 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4522 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004523 SetRawInputAt(0, constant);
4524 }
4525
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004526 bool CanBeMoved() const OVERRIDE { return true; }
4527 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4528 UNUSED(other);
4529 return true;
4530 }
4531
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004532 bool NeedsEnvironment() const OVERRIDE {
4533 // May call runtime to initialize the class.
4534 return true;
4535 }
4536
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004537
4538 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4539
4540 DECLARE_INSTRUCTION(ClinitCheck);
4541
4542 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004543 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4544};
4545
4546class HStaticFieldGet : public HExpression<1> {
4547 public:
4548 HStaticFieldGet(HInstruction* cls,
4549 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004550 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004551 bool is_volatile,
4552 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004553 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004554 Handle<mirror::DexCache> dex_cache,
4555 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004556 : HExpression(
4557 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004558 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004559 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004560 SetRawInputAt(0, cls);
4561 }
4562
Calin Juravle52c48962014-12-16 17:02:57 +00004563
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004564 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004565
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004566 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004567 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4568 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004569 }
4570
4571 size_t ComputeHashCode() const OVERRIDE {
4572 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4573 }
4574
Calin Juravle52c48962014-12-16 17:02:57 +00004575 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004576 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4577 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004578 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004579
4580 DECLARE_INSTRUCTION(StaticFieldGet);
4581
4582 private:
4583 const FieldInfo field_info_;
4584
4585 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4586};
4587
4588class HStaticFieldSet : public HTemplateInstruction<2> {
4589 public:
4590 HStaticFieldSet(HInstruction* cls,
4591 HInstruction* value,
4592 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004593 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004594 bool is_volatile,
4595 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004596 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004597 Handle<mirror::DexCache> dex_cache,
4598 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004599 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004600 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004601 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004602 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004603 SetRawInputAt(0, cls);
4604 SetRawInputAt(1, value);
4605 }
4606
Calin Juravle52c48962014-12-16 17:02:57 +00004607 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004608 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4609 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004610 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004611
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004612 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004613 bool GetValueCanBeNull() const { return value_can_be_null_; }
4614 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004615
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004616 DECLARE_INSTRUCTION(StaticFieldSet);
4617
4618 private:
4619 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004620 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004621
4622 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4623};
4624
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004625// Implement the move-exception DEX instruction.
4626class HLoadException : public HExpression<0> {
4627 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004628 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4629 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004630
David Brazdilbbd733e2015-08-18 17:48:17 +01004631 bool CanBeNull() const OVERRIDE { return false; }
4632
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004633 DECLARE_INSTRUCTION(LoadException);
4634
4635 private:
4636 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4637};
4638
David Brazdilcb1c0552015-08-04 16:22:25 +01004639// Implicit part of move-exception which clears thread-local exception storage.
4640// Must not be removed because the runtime expects the TLS to get cleared.
4641class HClearException : public HTemplateInstruction<0> {
4642 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004643 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4644 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004645
4646 DECLARE_INSTRUCTION(ClearException);
4647
4648 private:
4649 DISALLOW_COPY_AND_ASSIGN(HClearException);
4650};
4651
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004652class HThrow : public HTemplateInstruction<1> {
4653 public:
4654 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004655 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004656 SetRawInputAt(0, exception);
4657 }
4658
4659 bool IsControlFlow() const OVERRIDE { return true; }
4660
4661 bool NeedsEnvironment() const OVERRIDE { return true; }
4662
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004663 bool CanThrow() const OVERRIDE { return true; }
4664
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004665
4666 DECLARE_INSTRUCTION(Throw);
4667
4668 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004669 DISALLOW_COPY_AND_ASSIGN(HThrow);
4670};
4671
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004672class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004673 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004674 HInstanceOf(HInstruction* object,
4675 HLoadClass* constant,
4676 bool class_is_final,
4677 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004678 : HExpression(Primitive::kPrimBoolean,
4679 SideEffectsForArchRuntimeCalls(class_is_final),
4680 dex_pc),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004681 class_is_final_(class_is_final),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004682 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004683 SetRawInputAt(0, object);
4684 SetRawInputAt(1, constant);
4685 }
4686
4687 bool CanBeMoved() const OVERRIDE { return true; }
4688
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004689 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004690 return true;
4691 }
4692
4693 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004694 return false;
4695 }
4696
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004697 bool IsClassFinal() const { return class_is_final_; }
4698
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004699 // Used only in code generation.
4700 bool MustDoNullCheck() const { return must_do_null_check_; }
4701 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4702
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004703 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4704 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4705 }
4706
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004707 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004708
4709 private:
4710 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004711 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004712
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004713 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4714};
4715
Calin Juravleb1498f62015-02-16 13:13:29 +00004716class HBoundType : public HExpression<1> {
4717 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004718 // Constructs an HBoundType with the given upper_bound.
4719 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004720 HBoundType(HInstruction* input,
4721 ReferenceTypeInfo upper_bound,
4722 bool upper_can_be_null,
4723 uint32_t dex_pc = kNoDexPc)
4724 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004725 upper_bound_(upper_bound),
4726 upper_can_be_null_(upper_can_be_null),
4727 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004728 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004729 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004730 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004731 }
4732
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004733 // GetUpper* should only be used in reference type propagation.
4734 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4735 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004736
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004737 void SetCanBeNull(bool can_be_null) {
4738 DCHECK(upper_can_be_null_ || !can_be_null);
4739 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004740 }
4741
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004742 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4743
Calin Juravleb1498f62015-02-16 13:13:29 +00004744 DECLARE_INSTRUCTION(BoundType);
4745
4746 private:
4747 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004748 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4749 // It is used to bound the type in cases like:
4750 // if (x instanceof ClassX) {
4751 // // uper_bound_ will be ClassX
4752 // }
4753 const ReferenceTypeInfo upper_bound_;
4754 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4755 // is false then can_be_null_ cannot be true).
4756 const bool upper_can_be_null_;
4757 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004758
4759 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4760};
4761
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004762class HCheckCast : public HTemplateInstruction<2> {
4763 public:
4764 HCheckCast(HInstruction* object,
4765 HLoadClass* constant,
4766 bool class_is_final,
4767 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004768 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004769 class_is_final_(class_is_final),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004770 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004771 SetRawInputAt(0, object);
4772 SetRawInputAt(1, constant);
4773 }
4774
4775 bool CanBeMoved() const OVERRIDE { return true; }
4776
4777 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4778 return true;
4779 }
4780
4781 bool NeedsEnvironment() const OVERRIDE {
4782 // Instruction may throw a CheckCastError.
4783 return true;
4784 }
4785
4786 bool CanThrow() const OVERRIDE { return true; }
4787
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004788 bool MustDoNullCheck() const { return must_do_null_check_; }
4789 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4790
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004791
4792 bool IsClassFinal() const { return class_is_final_; }
4793
4794 DECLARE_INSTRUCTION(CheckCast);
4795
4796 private:
4797 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004798 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004799
4800 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004801};
4802
Calin Juravle27df7582015-04-17 19:12:31 +01004803class HMemoryBarrier : public HTemplateInstruction<0> {
4804 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004805 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004806 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004807 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004808 barrier_kind_(barrier_kind) {}
4809
4810 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4811
4812 DECLARE_INSTRUCTION(MemoryBarrier);
4813
4814 private:
4815 const MemBarrierKind barrier_kind_;
4816
4817 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4818};
4819
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004820class HMonitorOperation : public HTemplateInstruction<1> {
4821 public:
4822 enum OperationKind {
4823 kEnter,
4824 kExit,
4825 };
4826
4827 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004828 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004829 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
4830 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004831 SetRawInputAt(0, object);
4832 }
4833
4834 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004835 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4836
4837 bool CanThrow() const OVERRIDE {
4838 // Verifier guarantees that monitor-exit cannot throw.
4839 // This is important because it allows the HGraphBuilder to remove
4840 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4841 return IsEnter();
4842 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004843
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004844
4845 bool IsEnter() const { return kind_ == kEnter; }
4846
4847 DECLARE_INSTRUCTION(MonitorOperation);
4848
Calin Juravle52c48962014-12-16 17:02:57 +00004849 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004850 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004851
4852 private:
4853 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4854};
4855
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004856/**
4857 * A HInstruction used as a marker for the replacement of new + <init>
4858 * of a String to a call to a StringFactory. Only baseline will see
4859 * the node at code generation, where it will be be treated as null.
4860 * When compiling non-baseline, `HFakeString` instructions are being removed
4861 * in the instruction simplifier.
4862 */
4863class HFakeString : public HTemplateInstruction<0> {
4864 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004865 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
4866 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004867
4868 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4869
4870 DECLARE_INSTRUCTION(FakeString);
4871
4872 private:
4873 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4874};
4875
Vladimir Markof9f64412015-09-02 14:05:49 +01004876class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004877 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004878 MoveOperands(Location source,
4879 Location destination,
4880 Primitive::Type type,
4881 HInstruction* instruction)
4882 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004883
4884 Location GetSource() const { return source_; }
4885 Location GetDestination() const { return destination_; }
4886
4887 void SetSource(Location value) { source_ = value; }
4888 void SetDestination(Location value) { destination_ = value; }
4889
4890 // The parallel move resolver marks moves as "in-progress" by clearing the
4891 // destination (but not the source).
4892 Location MarkPending() {
4893 DCHECK(!IsPending());
4894 Location dest = destination_;
4895 destination_ = Location::NoLocation();
4896 return dest;
4897 }
4898
4899 void ClearPending(Location dest) {
4900 DCHECK(IsPending());
4901 destination_ = dest;
4902 }
4903
4904 bool IsPending() const {
4905 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4906 return destination_.IsInvalid() && !source_.IsInvalid();
4907 }
4908
4909 // True if this blocks a move from the given location.
4910 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004911 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004912 }
4913
4914 // A move is redundant if it's been eliminated, if its source and
4915 // destination are the same, or if its destination is unneeded.
4916 bool IsRedundant() const {
4917 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4918 }
4919
4920 // We clear both operands to indicate move that's been eliminated.
4921 void Eliminate() {
4922 source_ = destination_ = Location::NoLocation();
4923 }
4924
4925 bool IsEliminated() const {
4926 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4927 return source_.IsInvalid();
4928 }
4929
Alexey Frunze4dda3372015-06-01 18:31:49 -07004930 Primitive::Type GetType() const { return type_; }
4931
Nicolas Geoffray90218252015-04-15 11:56:51 +01004932 bool Is64BitMove() const {
4933 return Primitive::Is64BitType(type_);
4934 }
4935
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004936 HInstruction* GetInstruction() const { return instruction_; }
4937
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004938 private:
4939 Location source_;
4940 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004941 // The type this move is for.
4942 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004943 // The instruction this move is assocatied with. Null when this move is
4944 // for moving an input in the expected locations of user (including a phi user).
4945 // This is only used in debug mode, to ensure we do not connect interval siblings
4946 // in the same parallel move.
4947 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004948};
4949
4950static constexpr size_t kDefaultNumberOfMoves = 4;
4951
4952class HParallelMove : public HTemplateInstruction<0> {
4953 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004954 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
4955 : HTemplateInstruction(SideEffects::None(), dex_pc), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004956
Nicolas Geoffray90218252015-04-15 11:56:51 +01004957 void AddMove(Location source,
4958 Location destination,
4959 Primitive::Type type,
4960 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004961 DCHECK(source.IsValid());
4962 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004963 if (kIsDebugBuild) {
4964 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004965 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004966 if (moves_.Get(i).GetInstruction() == instruction) {
4967 // Special case the situation where the move is for the spill slot
4968 // of the instruction.
4969 if ((GetPrevious() == instruction)
4970 || ((GetPrevious() == nullptr)
4971 && instruction->IsPhi()
4972 && instruction->GetBlock() == GetBlock())) {
4973 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4974 << "Doing parallel moves for the same instruction.";
4975 } else {
4976 DCHECK(false) << "Doing parallel moves for the same instruction.";
4977 }
4978 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004979 }
4980 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004981 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004982 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004983 << "Overlapped destination for two moves in a parallel move: "
4984 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4985 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004986 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004987 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004988 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004989 }
4990
4991 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004992 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004993 }
4994
4995 size_t NumMoves() const { return moves_.Size(); }
4996
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004997 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004998
4999 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005000 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005001
5002 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5003};
5004
Mark Mendell0616ae02015-04-17 12:49:27 -04005005} // namespace art
5006
5007#ifdef ART_ENABLE_CODEGEN_x86
5008#include "nodes_x86.h"
5009#endif
5010
5011namespace art {
5012
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005013class HGraphVisitor : public ValueObject {
5014 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005015 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5016 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005017
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005018 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005019 virtual void VisitBasicBlock(HBasicBlock* block);
5020
Roland Levillain633021e2014-10-01 14:12:25 +01005021 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005022 void VisitInsertionOrder();
5023
Roland Levillain633021e2014-10-01 14:12:25 +01005024 // Visit the graph following dominator tree reverse post-order.
5025 void VisitReversePostOrder();
5026
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005027 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005028
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005029 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005030#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005031 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5032
5033 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5034
5035#undef DECLARE_VISIT_INSTRUCTION
5036
5037 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005038 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005039
5040 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5041};
5042
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005043class HGraphDelegateVisitor : public HGraphVisitor {
5044 public:
5045 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5046 virtual ~HGraphDelegateVisitor() {}
5047
5048 // Visit functions that delegate to to super class.
5049#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005050 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005051
5052 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5053
5054#undef DECLARE_VISIT_INSTRUCTION
5055
5056 private:
5057 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5058};
5059
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005060class HInsertionOrderIterator : public ValueObject {
5061 public:
5062 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5063
5064 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
5065 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
5066 void Advance() { ++index_; }
5067
5068 private:
5069 const HGraph& graph_;
5070 size_t index_;
5071
5072 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5073};
5074
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005075class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005076 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005077 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5078 // Check that reverse post order of the graph has been built.
5079 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5080 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005081
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005082 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
5083 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005084 void Advance() { ++index_; }
5085
5086 private:
5087 const HGraph& graph_;
5088 size_t index_;
5089
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005090 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005091};
5092
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005093class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005094 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005095 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00005096 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
5097 // Check that reverse post order of the graph has been built.
5098 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5099 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005100
5101 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005102 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005103 void Advance() { --index_; }
5104
5105 private:
5106 const HGraph& graph_;
5107 size_t index_;
5108
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005109 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005110};
5111
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005112class HLinearPostOrderIterator : public ValueObject {
5113 public:
5114 explicit HLinearPostOrderIterator(const HGraph& graph)
5115 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
5116
5117 bool Done() const { return index_ == 0; }
5118
5119 HBasicBlock* Current() const { return order_.Get(index_ -1); }
5120
5121 void Advance() {
5122 --index_;
5123 DCHECK_GE(index_, 0U);
5124 }
5125
5126 private:
5127 const GrowableArray<HBasicBlock*>& order_;
5128 size_t index_;
5129
5130 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5131};
5132
5133class HLinearOrderIterator : public ValueObject {
5134 public:
5135 explicit HLinearOrderIterator(const HGraph& graph)
5136 : order_(graph.GetLinearOrder()), index_(0) {}
5137
5138 bool Done() const { return index_ == order_.Size(); }
5139 HBasicBlock* Current() const { return order_.Get(index_); }
5140 void Advance() { ++index_; }
5141
5142 private:
5143 const GrowableArray<HBasicBlock*>& order_;
5144 size_t index_;
5145
5146 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5147};
5148
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005149// Iterator over the blocks that art part of the loop. Includes blocks part
5150// of an inner loop. The order in which the blocks are iterated is on their
5151// block id.
5152class HBlocksInLoopIterator : public ValueObject {
5153 public:
5154 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5155 : blocks_in_loop_(info.GetBlocks()),
5156 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5157 index_(0) {
5158 if (!blocks_in_loop_.IsBitSet(index_)) {
5159 Advance();
5160 }
5161 }
5162
5163 bool Done() const { return index_ == blocks_.Size(); }
5164 HBasicBlock* Current() const { return blocks_.Get(index_); }
5165 void Advance() {
5166 ++index_;
5167 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5168 if (blocks_in_loop_.IsBitSet(index_)) {
5169 break;
5170 }
5171 }
5172 }
5173
5174 private:
5175 const BitVector& blocks_in_loop_;
5176 const GrowableArray<HBasicBlock*>& blocks_;
5177 size_t index_;
5178
5179 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5180};
5181
Mingyao Yang3584bce2015-05-19 16:01:59 -07005182// Iterator over the blocks that art part of the loop. Includes blocks part
5183// of an inner loop. The order in which the blocks are iterated is reverse
5184// post order.
5185class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5186 public:
5187 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5188 : blocks_in_loop_(info.GetBlocks()),
5189 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5190 index_(0) {
5191 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5192 Advance();
5193 }
5194 }
5195
5196 bool Done() const { return index_ == blocks_.Size(); }
5197 HBasicBlock* Current() const { return blocks_.Get(index_); }
5198 void Advance() {
5199 ++index_;
5200 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5201 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5202 break;
5203 }
5204 }
5205 }
5206
5207 private:
5208 const BitVector& blocks_in_loop_;
5209 const GrowableArray<HBasicBlock*>& blocks_;
5210 size_t index_;
5211
5212 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5213};
5214
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005215inline int64_t Int64FromConstant(HConstant* constant) {
5216 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5217 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5218 : constant->AsLongConstant()->GetValue();
5219}
5220
Vladimir Marko58155012015-08-19 12:49:41 +00005221inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5222 // For the purposes of the compiler, the dex files must actually be the same object
5223 // if we want to safely treat them as the same. This is especially important for JIT
5224 // as custom class loaders can open the same underlying file (or memory) multiple
5225 // times and provide different class resolution but no two class loaders should ever
5226 // use the same DexFile object - doing so is an unsupported hack that can lead to
5227 // all sorts of weird failures.
5228 return &lhs == &rhs;
5229}
5230
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005231} // namespace art
5232
5233#endif // ART_COMPILER_OPTIMIZING_NODES_H_