blob: 7498f44726c62203c2504f30a3b04c1d17e0f36c [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),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100150 blocks_(arena->Adapter(kArenaAllocBlockList)),
151 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
152 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700153 entry_block_(nullptr),
154 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100155 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100156 number_of_vregs_(0),
157 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000158 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400159 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000160 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000161 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000162 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100163 dex_file_(dex_file),
164 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100165 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100166 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100167 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700168 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000169 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100170 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
171 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
172 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
173 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
174 cached_current_method_(nullptr) {
175 blocks_.reserve(kDefaultNumberOfBlocks);
176 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000177
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000178 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100179 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
180
181 HBasicBlock* GetBlock(size_t id) const {
182 DCHECK_LT(id, blocks_.size());
183 return blocks_[id];
184 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000185
David Brazdil69ba7b72015-06-23 18:27:30 +0100186 bool IsInSsaForm() const { return in_ssa_form_; }
187
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000188 HBasicBlock* GetEntryBlock() const { return entry_block_; }
189 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100190 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000191
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000192 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
193 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000194
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000195 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100196
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000197 // Try building the SSA form of this graph, with dominance computation and loop
198 // recognition. Returns whether it was successful in doing all these steps.
199 bool TryBuildingSsa() {
200 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000201 // The SSA builder requires loops to all be natural. Specifically, the dead phi
202 // elimination phase checks the consistency of the graph when doing a post-order
203 // visit for eliminating dead phis: a dead phi can only have loop header phi
204 // users remaining when being visited.
205 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100206 // Precompute per-block try membership before entering the SSA builder,
207 // which needs the information to build catch block phis from values of
208 // locals at throwing instructions inside try blocks.
209 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000210 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100211 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000212 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 }
214
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100215 void ComputeDominanceInformation();
216 void ClearDominanceInformation();
217
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000218 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000219 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100220 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100221 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000222
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000223 // Analyze all natural loops in this graph. Returns false if one
224 // loop is not natural, that is the header does not dominate the
225 // back edge.
226 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100227
David Brazdilffee3d32015-07-06 11:48:53 +0100228 // Iterate over blocks to compute try block membership. Needs reverse post
229 // order and loop information.
230 void ComputeTryBlockInformation();
231
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000232 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000233 // Returns the instruction used to replace the invoke expression or null if the
234 // invoke is for a void method.
235 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000236
Mingyao Yang3584bce2015-05-19 16:01:59 -0700237 // Need to add a couple of blocks to test if the loop body is entered and
238 // put deoptimization instructions, etc.
239 void TransformLoopHeaderForBCE(HBasicBlock* header);
240
David Brazdil2d7352b2015-04-20 14:52:42 +0100241 // Removes `block` from the graph.
242 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000243
David Brazdilfc6a86a2015-06-26 10:33:45 +0000244 // Splits the edge between `block` and `successor` while preserving the
245 // indices in the predecessor/successor lists. If there are multiple edges
246 // between the blocks, the lowest indices are used.
247 // Returns the new block which is empty and has the same dex pc as `successor`.
248 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
249
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
251 void SimplifyLoop(HBasicBlock* header);
252
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000253 int32_t GetNextInstructionId() {
254 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000255 return current_instruction_id_++;
256 }
257
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000258 int32_t GetCurrentInstructionId() const {
259 return current_instruction_id_;
260 }
261
262 void SetCurrentInstructionId(int32_t id) {
263 current_instruction_id_ = id;
264 }
265
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100266 uint16_t GetMaximumNumberOfOutVRegs() const {
267 return maximum_number_of_out_vregs_;
268 }
269
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000270 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
271 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100272 }
273
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100274 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
275 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
276 }
277
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000278 void UpdateTemporariesVRegSlots(size_t slots) {
279 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100280 }
281
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000282 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100283 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000284 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100285 }
286
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100287 void SetNumberOfVRegs(uint16_t number_of_vregs) {
288 number_of_vregs_ = number_of_vregs;
289 }
290
291 uint16_t GetNumberOfVRegs() const {
292 return number_of_vregs_;
293 }
294
295 void SetNumberOfInVRegs(uint16_t value) {
296 number_of_in_vregs_ = value;
297 }
298
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100299 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100300 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100301 return number_of_vregs_ - number_of_in_vregs_;
302 }
303
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100304 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100305 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100306 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100307
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100308 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100309 return linear_order_;
310 }
311
Mark Mendell1152c922015-04-24 17:06:35 -0400312 bool HasBoundsChecks() const {
313 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800314 }
315
Mark Mendell1152c922015-04-24 17:06:35 -0400316 void SetHasBoundsChecks(bool value) {
317 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800318 }
319
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100320 bool ShouldGenerateConstructorBarrier() const {
321 return should_generate_constructor_barrier_;
322 }
323
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000324 bool IsDebuggable() const { return debuggable_; }
325
David Brazdil8d5b8b22015-03-24 10:51:52 +0000326 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000327 // already, it is created and inserted into the graph. This method is only for
328 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600329 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000330
331 // TODO: This is problematic for the consistency of reference type propagation
332 // because it can be created anytime after the pass and thus it will be left
333 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600334 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000335
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600336 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
337 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000338 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600339 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
340 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000341 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600342 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
343 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000344 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600345 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
346 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000347 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000348
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100349 HCurrentMethod* GetCurrentMethod();
350
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000351 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100352
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100353 const DexFile& GetDexFile() const {
354 return dex_file_;
355 }
356
357 uint32_t GetMethodIdx() const {
358 return method_idx_;
359 }
360
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100361 InvokeType GetInvokeType() const {
362 return invoke_type_;
363 }
364
Mark Mendellc4701932015-04-10 13:18:51 -0400365 InstructionSet GetInstructionSet() const {
366 return instruction_set_;
367 }
368
David Brazdil77a48ae2015-09-15 12:34:04 +0000369 bool HasTryCatch() const { return has_try_catch_; }
370 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100371
David Brazdil2d7352b2015-04-20 14:52:42 +0100372 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100373 void FindBackEdges(ArenaBitVector* visited);
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.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100410 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000411
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100412 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100413 ArenaVector<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.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100416 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100417
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
David Brazdil77a48ae2015-09-15 12:34:04 +0000436 // Flag whether there are any try/catch blocks in the graph. We will skip
437 // try/catch-related passes if false.
438 bool has_try_catch_;
439
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000440 // Indicates whether the graph should be compiled in a way that
441 // ensures full debuggability. If false, we can apply more
442 // aggressive optimizations that may limit the level of debugging.
443 const bool debuggable_;
444
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000445 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000447
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100448 // The dex file from which the method is from.
449 const DexFile& dex_file_;
450
451 // The method index in the dex file.
452 const uint32_t method_idx_;
453
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100454 // If inlined, this encodes how the callee is being invoked.
455 const InvokeType invoke_type_;
456
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100457 // Whether the graph has been transformed to SSA form. Only used
458 // in debug mode to ensure we are not using properties only valid
459 // for non-SSA form (like the number of temporaries).
460 bool in_ssa_form_;
461
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100462 const bool should_generate_constructor_barrier_;
463
Mathieu Chartiere401d142015-04-22 13:56:20 -0700464 const InstructionSet instruction_set_;
465
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000466 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000467 HNullConstant* cached_null_constant_;
468 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000469 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000470 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000471 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000472
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100473 HCurrentMethod* cached_current_method_;
474
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000475 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100476 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000477 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000478 DISALLOW_COPY_AND_ASSIGN(HGraph);
479};
480
Vladimir Markof9f64412015-09-02 14:05:49 +0100481class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000482 public:
483 HLoopInformation(HBasicBlock* header, HGraph* graph)
484 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100485 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100486 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100487 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100488 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
489 back_edges_.reserve(kDefaultNumberOfBackEdges);
490 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100491
492 HBasicBlock* GetHeader() const {
493 return header_;
494 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000495
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000496 void SetHeader(HBasicBlock* block) {
497 header_ = block;
498 }
499
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100500 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
501 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
502 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
503
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000504 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100505 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000506 }
507
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100508 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100509 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100510 }
511
David Brazdil46e2a392015-03-16 17:31:52 +0000512 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100513 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100514 }
515
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000516 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100517 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000518 }
519
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100520 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100521
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100522 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100523 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100524 }
525
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100526 // Returns the lifetime position of the back edge that has the
527 // greatest lifetime position.
528 size_t GetLifetimeEnd() const;
529
530 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100531 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100532 }
533
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100534 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100535 // that is the header dominates the back edge.
536 bool Populate();
537
David Brazdila4b8c212015-05-07 09:59:30 +0100538 // Reanalyzes the loop by removing loop info from its blocks and re-running
539 // Populate(). If there are no back edges left, the loop info is completely
540 // removed as well as its SuspendCheck instruction. It must be run on nested
541 // inner loops first.
542 void Update();
543
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100544 // Returns whether this loop information contains `block`.
545 // Note that this loop information *must* be populated before entering this function.
546 bool Contains(const HBasicBlock& block) const;
547
548 // Returns whether this loop information is an inner loop of `other`.
549 // Note that `other` *must* be populated before entering this function.
550 bool IsIn(const HLoopInformation& other) const;
551
552 const ArenaBitVector& GetBlocks() const { return blocks_; }
553
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000554 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000555 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000556
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000557 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100558 // Internal recursive implementation of `Populate`.
559 void PopulateRecursive(HBasicBlock* block);
560
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100562 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100563 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100564 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000565
566 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
567};
568
David Brazdilec16f792015-08-19 15:04:01 +0100569// Stores try/catch information for basic blocks.
570// Note that HGraph is constructed so that catch blocks cannot simultaneously
571// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100572class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100573 public:
574 // Try block information constructor.
575 explicit TryCatchInformation(const HTryBoundary& try_entry)
576 : try_entry_(&try_entry),
577 catch_dex_file_(nullptr),
578 catch_type_index_(DexFile::kDexNoIndex16) {
579 DCHECK(try_entry_ != nullptr);
580 }
581
582 // Catch block information constructor.
583 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
584 : try_entry_(nullptr),
585 catch_dex_file_(&dex_file),
586 catch_type_index_(catch_type_index) {}
587
588 bool IsTryBlock() const { return try_entry_ != nullptr; }
589
590 const HTryBoundary& GetTryEntry() const {
591 DCHECK(IsTryBlock());
592 return *try_entry_;
593 }
594
595 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
596
597 bool IsCatchAllTypeIndex() const {
598 DCHECK(IsCatchBlock());
599 return catch_type_index_ == DexFile::kDexNoIndex16;
600 }
601
602 uint16_t GetCatchTypeIndex() const {
603 DCHECK(IsCatchBlock());
604 return catch_type_index_;
605 }
606
607 const DexFile& GetCatchDexFile() const {
608 DCHECK(IsCatchBlock());
609 return *catch_dex_file_;
610 }
611
612 private:
613 // One of possibly several TryBoundary instructions entering the block's try.
614 // Only set for try blocks.
615 const HTryBoundary* try_entry_;
616
617 // Exception type information. Only set for catch blocks.
618 const DexFile* catch_dex_file_;
619 const uint16_t catch_type_index_;
620};
621
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100622static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100623static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100624
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000625// A block in a method. Contains the list of instructions represented
626// as a double linked list. Each block knows its predecessors and
627// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100628
Vladimir Markof9f64412015-09-02 14:05:49 +0100629class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000630 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600631 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000633 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
634 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000635 loop_information_(nullptr),
636 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000637 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100638 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100639 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100640 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000641 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000642 try_catch_information_(nullptr) {
643 predecessors_.reserve(kDefaultNumberOfPredecessors);
644 successors_.reserve(kDefaultNumberOfSuccessors);
645 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
646 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000647
Vladimir Marko60584552015-09-03 13:35:12 +0000648 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100649 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000650 }
651
Vladimir Marko60584552015-09-03 13:35:12 +0000652 HBasicBlock* GetPredecessor(size_t pred_idx) const {
653 DCHECK_LT(pred_idx, predecessors_.size());
654 return predecessors_[pred_idx];
655 }
656
657 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000659 }
660
Vladimir Marko60584552015-09-03 13:35:12 +0000661 HBasicBlock* GetSuccessor(size_t succ_idx) const {
662 DCHECK_LT(succ_idx, successors_.size());
663 return successors_[succ_idx];
664 }
665
666 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
667 return ContainsElement(successors_, block, start_from);
668 }
669
670 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100671 return dominated_blocks_;
672 }
673
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100674 bool IsEntryBlock() const {
675 return graph_->GetEntryBlock() == this;
676 }
677
678 bool IsExitBlock() const {
679 return graph_->GetExitBlock() == this;
680 }
681
David Brazdil46e2a392015-03-16 17:31:52 +0000682 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000683 bool IsSingleTryBoundary() const;
684
685 // Returns true if this block emits nothing but a jump.
686 bool IsSingleJump() const {
687 HLoopInformation* loop_info = GetLoopInformation();
688 return (IsSingleGoto() || IsSingleTryBoundary())
689 // Back edges generate a suspend check.
690 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
691 }
David Brazdil46e2a392015-03-16 17:31:52 +0000692
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693 void AddBackEdge(HBasicBlock* back_edge) {
694 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000695 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000696 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000698 loop_information_->AddBackEdge(back_edge);
699 }
700
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000701 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000702 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000703
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100704 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000705 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600706 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000707
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000708 HBasicBlock* GetDominator() const { return dominator_; }
709 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000710 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
711
712 void RemoveDominatedBlock(HBasicBlock* block) {
713 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100714 }
Vladimir Marko60584552015-09-03 13:35:12 +0000715
716 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
717 ReplaceElement(dominated_blocks_, existing, new_block);
718 }
719
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100720 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000721
722 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100723 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000724 }
725
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100726 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
727 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100728 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100729 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100730 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
731 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000732
733 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000734 successors_.push_back(block);
735 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000736 }
737
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100738 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
739 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100740 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000741 new_block->predecessors_.push_back(this);
742 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000743 }
744
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000745 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
746 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000747 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000748 new_block->successors_.push_back(this);
749 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000750 }
751
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100752 // Insert `this` between `predecessor` and `successor. This method
753 // preserves the indicies, and will update the first edge found between
754 // `predecessor` and `successor`.
755 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
756 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100757 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000758 successor->predecessors_[predecessor_index] = this;
759 predecessor->successors_[successor_index] = this;
760 successors_.push_back(successor);
761 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100762 }
763
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100764 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000765 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100766 }
767
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000768 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000769 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000770 }
771
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100772 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000773 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100774 }
775
776 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000777 predecessors_.push_back(block);
778 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100779 }
780
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100781 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000782 DCHECK_EQ(predecessors_.size(), 2u);
783 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100784 }
785
David Brazdil769c9e52015-04-27 13:54:09 +0100786 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000787 DCHECK_EQ(successors_.size(), 2u);
788 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100789 }
790
David Brazdilfc6a86a2015-06-26 10:33:45 +0000791 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000792 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100793 }
794
David Brazdilfc6a86a2015-06-26 10:33:45 +0000795 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000796 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100797 }
798
David Brazdilfc6a86a2015-06-26 10:33:45 +0000799 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000800 DCHECK_EQ(GetPredecessors().size(), 1u);
801 return GetPredecessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000802 }
803
804 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000805 DCHECK_EQ(GetSuccessors().size(), 1u);
806 return GetSuccessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000807 }
808
809 // Returns whether the first occurrence of `predecessor` in the list of
810 // predecessors is at index `idx`.
811 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000812 DCHECK_EQ(GetPredecessor(idx), predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 return GetPredecessorIndexOf(predecessor) == idx;
814 }
815
David Brazdilffee3d32015-07-06 11:48:53 +0100816 // Returns the number of non-exceptional successors. SsaChecker ensures that
817 // these are stored at the beginning of the successor list.
818 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000819 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100820 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000821
822 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100823 // created, latter block. Note that this method will add the block to the
824 // graph, create a Goto at the end of the former block and will create an edge
825 // between the blocks. It will not, however, update the reverse post order or
826 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000827 HBasicBlock* SplitBefore(HInstruction* cursor);
828
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000829 // Split the block into two blocks just after `cursor`. Returns the newly
830 // created block. Note that this method just updates raw block information,
831 // like predecessors, successors, dominators, and instruction list. It does not
832 // update the graph, reverse post order, loop information, nor make sure the
833 // blocks are consistent (for example ending with a control flow instruction).
834 HBasicBlock* SplitAfter(HInstruction* cursor);
835
836 // Merge `other` at the end of `this`. Successors and dominated blocks of
837 // `other` are changed to be successors and dominated blocks of `this`. Note
838 // that this method does not update the graph, reverse post order, loop
839 // information, nor make sure the blocks are consistent (for example ending
840 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100841 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000842
843 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
844 // of `this` are moved to `other`.
845 // Note that this method does not update the graph, reverse post order, loop
846 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000847 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848 void ReplaceWith(HBasicBlock* other);
849
David Brazdil2d7352b2015-04-20 14:52:42 +0100850 // Merge `other` at the end of `this`. This method updates loops, reverse post
851 // order, links to predecessors, successors, dominators and deletes the block
852 // from the graph. The two blocks must be successive, i.e. `this` the only
853 // predecessor of `other` and vice versa.
854 void MergeWith(HBasicBlock* other);
855
856 // Disconnects `this` from all its predecessors, successors and dominator,
857 // removes it from all loops it is included in and eventually from the graph.
858 // The block must not dominate any other block. Predecessors and successors
859 // are safely updated.
860 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000861
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000862 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100863 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100864 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100865 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100866 // Replace instruction `initial` with `replacement` within this block.
867 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
868 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100869 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100870 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000871 // RemoveInstruction and RemovePhi delete a given instruction from the respective
872 // instruction list. With 'ensure_safety' set to true, it verifies that the
873 // instruction is not in use and removes it from the use lists of its inputs.
874 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
875 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100876 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100877
878 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100879 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100880 }
881
Roland Levillain6b879dd2014-09-22 17:13:44 +0100882 bool IsLoopPreHeaderFirstPredecessor() const {
883 DCHECK(IsLoopHeader());
Vladimir Marko60584552015-09-03 13:35:12 +0000884 return GetPredecessor(0) == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100885 }
886
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100887 HLoopInformation* GetLoopInformation() const {
888 return loop_information_;
889 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000890
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000891 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100892 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000893 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100894 void SetInLoop(HLoopInformation* info) {
895 if (IsLoopHeader()) {
896 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100897 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 loop_information_ = info;
899 } else if (loop_information_->Contains(*info->GetHeader())) {
900 // Block is currently part of an outer loop. Make it part of this inner loop.
901 // Note that a non loop header having a loop information means this loop information
902 // has already been populated
903 loop_information_ = info;
904 } else {
905 // Block is part of an inner loop. Do not update the loop information.
906 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
907 // at this point, because this method is being called while populating `info`.
908 }
909 }
910
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000911 // Raw update of the loop information.
912 void SetLoopInformation(HLoopInformation* info) {
913 loop_information_ = info;
914 }
915
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100916 bool IsInLoop() const { return loop_information_ != nullptr; }
917
David Brazdilec16f792015-08-19 15:04:01 +0100918 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
919
920 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
921 try_catch_information_ = try_catch_information;
922 }
923
924 bool IsTryBlock() const {
925 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
926 }
927
928 bool IsCatchBlock() const {
929 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
930 }
David Brazdilffee3d32015-07-06 11:48:53 +0100931
932 // Returns the try entry that this block's successors should have. They will
933 // be in the same try, unless the block ends in a try boundary. In that case,
934 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100935 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100936
David Brazdila4b8c212015-05-07 09:59:30 +0100937 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100938 bool Dominates(HBasicBlock* block) const;
939
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100940 size_t GetLifetimeStart() const { return lifetime_start_; }
941 size_t GetLifetimeEnd() const { return lifetime_end_; }
942
943 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
944 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
945
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100946
David Brazdil8d5b8b22015-03-24 10:51:52 +0000947 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000948 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100949 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000950 bool HasSinglePhi() const;
951
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000953 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000954 ArenaVector<HBasicBlock*> predecessors_;
955 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100956 HInstructionList instructions_;
957 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000958 HLoopInformation* loop_information_;
959 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000960 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100961 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100962 // The dex program counter of the first instruction of this block.
963 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100964 size_t lifetime_start_;
965 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100966 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100967
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000968 friend class HGraph;
969 friend class HInstruction;
970
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
972};
973
David Brazdilb2bd1c52015-03-25 11:17:37 +0000974// Iterates over the LoopInformation of all loops which contain 'block'
975// from the innermost to the outermost.
976class HLoopInformationOutwardIterator : public ValueObject {
977 public:
978 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
979 : current_(block.GetLoopInformation()) {}
980
981 bool Done() const { return current_ == nullptr; }
982
983 void Advance() {
984 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100985 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000986 }
987
988 HLoopInformation* Current() const {
989 DCHECK(!Done());
990 return current_;
991 }
992
993 private:
994 HLoopInformation* current_;
995
996 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
997};
998
Alexandre Ramesef20f712015-06-09 10:29:30 +0100999#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001000 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001001 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001002 M(ArrayGet, Instruction) \
1003 M(ArrayLength, Instruction) \
1004 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +01001005 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001006 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001007 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001008 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001009 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001010 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001011 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001012 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001013 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001014 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001015 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001016 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001017 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001018 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001019 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001020 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001021 M(FloatConstant, Constant) \
1022 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001023 M(GreaterThan, Condition) \
1024 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001025 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001026 M(InstanceFieldGet, Instruction) \
1027 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001028 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001029 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001030 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001031 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001032 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001033 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001034 M(LessThan, Condition) \
1035 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001036 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001037 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001039 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001040 M(Local, Instruction) \
1041 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001042 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001043 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001044 M(Mul, BinaryOperation) \
1045 M(Neg, UnaryOperation) \
1046 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001047 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001048 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001049 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001050 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001051 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001052 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001053 M(PackedSwitch, Instruction) \
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>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001241class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001242 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)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001512 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1513 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001514 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) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001520 }
1521
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001522 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001523 : HEnvironment(arena,
1524 to_copy.Size(),
1525 to_copy.GetDexFile(),
1526 to_copy.GetMethodIdx(),
1527 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001528 to_copy.GetInvokeType(),
1529 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001530
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001531 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001532 if (parent_ != nullptr) {
1533 parent_->SetAndCopyParentChain(allocator, parent);
1534 } else {
1535 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1536 parent_->CopyFrom(parent);
1537 if (parent->GetParent() != nullptr) {
1538 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1539 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001540 }
David Brazdiled596192015-01-23 10:39:45 +00001541 }
1542
Vladimir Marko71bf8092015-09-15 15:33:14 +01001543 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001544 void CopyFrom(HEnvironment* environment);
1545
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001546 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1547 // input to the loop phi instead. This is for inserting instructions that
1548 // require an environment (like HDeoptimization) in the loop pre-header.
1549 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001550
1551 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001552 DCHECK_LT(index, Size());
1553 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001554 }
1555
1556 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001557 DCHECK_LT(index, Size());
1558 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001559 }
1560
David Brazdil1abb4192015-02-17 18:33:36 +00001561 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001562
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001563 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001564
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001565 HEnvironment* GetParent() const { return parent_; }
1566
1567 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001568 DCHECK_LT(index, Size());
1569 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001570 }
1571
1572 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001573 DCHECK_LT(index, Size());
1574 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001575 }
1576
1577 uint32_t GetDexPc() const {
1578 return dex_pc_;
1579 }
1580
1581 uint32_t GetMethodIdx() const {
1582 return method_idx_;
1583 }
1584
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001585 InvokeType GetInvokeType() const {
1586 return invoke_type_;
1587 }
1588
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001589 const DexFile& GetDexFile() const {
1590 return dex_file_;
1591 }
1592
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001593 HInstruction* GetHolder() const {
1594 return holder_;
1595 }
1596
David Brazdiled596192015-01-23 10:39:45 +00001597 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001598 // Record instructions' use entries of this environment for constant-time removal.
1599 // It should only be called by HInstruction when a new environment use is added.
1600 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1601 DCHECK(env_use->GetUser() == this);
1602 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001603 DCHECK_LT(index, Size());
1604 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001605 }
David Brazdiled596192015-01-23 10:39:45 +00001606
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001607 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1608 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001609 HEnvironment* parent_;
1610 const DexFile& dex_file_;
1611 const uint32_t method_idx_;
1612 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001613 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001614
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001615 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001616 HInstruction* const holder_;
1617
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001618 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001619
1620 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1621};
1622
Calin Juravleacf735c2015-02-12 15:25:22 +00001623class ReferenceTypeInfo : ValueObject {
1624 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001625 typedef Handle<mirror::Class> TypeHandle;
1626
Calin Juravle2e768302015-07-28 14:41:11 +00001627 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1628 // The constructor will check that the type_handle is valid.
1629 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001630 }
1631
Calin Juravle2e768302015-07-28 14:41:11 +00001632 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1633
1634 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1635 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001636 }
1637
Calin Juravle2e768302015-07-28 14:41:11 +00001638 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1639 return IsValidHandle(type_handle_);
1640 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001641 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001642
1643 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1644 DCHECK(IsValid());
1645 return GetTypeHandle()->IsObjectClass();
1646 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001647 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001648 DCHECK(IsValid());
1649 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001650 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001651
1652 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1653
Mathieu Chartier90443472015-07-16 20:32:27 -07001654 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001655 DCHECK(IsValid());
1656 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001657 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1658 }
1659
1660 // Returns true if the type information provide the same amount of details.
1661 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001662 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001663 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001664 if (!IsValid() && !rti.IsValid()) {
1665 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001666 return true;
1667 }
Calin Juravle2e768302015-07-28 14:41:11 +00001668 if (!IsValid() || !rti.IsValid()) {
1669 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001670 return false;
1671 }
Calin Juravle2e768302015-07-28 14:41:11 +00001672 return IsExact() == rti.IsExact()
1673 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001674 }
1675
1676 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001677 ReferenceTypeInfo();
1678 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001679
Calin Juravleacf735c2015-02-12 15:25:22 +00001680 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001681 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001682 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001683 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001684 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001685};
1686
1687std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1688
Vladimir Markof9f64412015-09-02 14:05:49 +01001689class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001690 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001691 HInstruction(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001692 : previous_(nullptr),
1693 next_(nullptr),
1694 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001695 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001696 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001697 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001698 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001699 locations_(nullptr),
1700 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001701 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001702 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001703 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001704
Dave Allison20dfc792014-06-16 20:44:29 -07001705 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001706
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001707#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001708 enum InstructionKind {
1709 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1710 };
1711#undef DECLARE_KIND
1712
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001713 HInstruction* GetNext() const { return next_; }
1714 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001715
Calin Juravle77520bc2015-01-12 18:45:46 +00001716 HInstruction* GetNextDisregardingMoves() const;
1717 HInstruction* GetPreviousDisregardingMoves() const;
1718
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001719 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001720 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001721 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001722 bool IsInBlock() const { return block_ != nullptr; }
1723 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001724 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001725
Roland Levillain6b879dd2014-09-22 17:13:44 +01001726 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001727 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001728
1729 virtual void Accept(HGraphVisitor* visitor) = 0;
1730 virtual const char* DebugName() const = 0;
1731
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001732 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001733 void SetRawInputAt(size_t index, HInstruction* input) {
1734 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1735 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001736
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001737 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001738
1739 uint32_t GetDexPc() const { return dex_pc_; }
1740
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001741 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001742
Roland Levillaine161a2a2014-10-03 12:45:18 +01001743 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001744 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001745
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001746 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001747 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001748
Calin Juravle10e244f2015-01-26 18:54:32 +00001749 // Does not apply for all instructions, but having this at top level greatly
1750 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001751 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001752 virtual bool CanBeNull() const {
1753 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1754 return true;
1755 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001756
Calin Juravle641547a2015-04-21 22:08:51 +01001757 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1758 UNUSED(obj);
1759 return false;
1760 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001761
Calin Juravle2e768302015-07-28 14:41:11 +00001762 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001763
Calin Juravle61d544b2015-02-23 16:46:57 +00001764 ReferenceTypeInfo GetReferenceTypeInfo() const {
1765 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1766 return reference_type_info_;
1767 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001768
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001769 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001770 DCHECK(user != nullptr);
1771 HUseListNode<HInstruction*>* use =
1772 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1773 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001774 }
1775
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001776 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001777 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001778 HUseListNode<HEnvironment*>* env_use =
1779 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1780 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001781 }
1782
David Brazdil1abb4192015-02-17 18:33:36 +00001783 void RemoveAsUserOfInput(size_t input) {
1784 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1785 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1786 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001787
David Brazdil1abb4192015-02-17 18:33:36 +00001788 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1789 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001790
David Brazdiled596192015-01-23 10:39:45 +00001791 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1792 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001793 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001794 bool HasOnlyOneNonEnvironmentUse() const {
1795 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1796 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001797
Roland Levillain6c82d402014-10-13 16:10:27 +01001798 // Does this instruction strictly dominate `other_instruction`?
1799 // Returns false if this instruction and `other_instruction` are the same.
1800 // Aborts if this instruction and `other_instruction` are both phis.
1801 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001802
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001803 int GetId() const { return id_; }
1804 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001805
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001806 int GetSsaIndex() const { return ssa_index_; }
1807 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1808 bool HasSsaIndex() const { return ssa_index_ != -1; }
1809
1810 bool HasEnvironment() const { return environment_ != nullptr; }
1811 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001812 // Set the `environment_` field. Raw because this method does not
1813 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001814 void SetRawEnvironment(HEnvironment* environment) {
1815 DCHECK(environment_ == nullptr);
1816 DCHECK_EQ(environment->GetHolder(), this);
1817 environment_ = environment;
1818 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001819
1820 // Set the environment of this instruction, copying it from `environment`. While
1821 // copying, the uses lists are being updated.
1822 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001823 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001824 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001825 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001826 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001827 if (environment->GetParent() != nullptr) {
1828 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1829 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001830 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001831
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001832 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1833 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001834 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001835 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001836 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001837 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001838 if (environment->GetParent() != nullptr) {
1839 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1840 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001841 }
1842
Nicolas Geoffray39468442014-09-02 15:17:15 +01001843 // Returns the number of entries in the environment. Typically, that is the
1844 // number of dex registers in a method. It could be more in case of inlining.
1845 size_t EnvironmentSize() const;
1846
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001847 LocationSummary* GetLocations() const { return locations_; }
1848 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001849
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001850 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001851 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001852
Alexandre Rames188d4312015-04-09 18:30:21 +01001853 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1854 // uses of this instruction by `other` are *not* updated.
1855 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1856 ReplaceWith(other);
1857 other->ReplaceInput(this, use_index);
1858 }
1859
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001860 // Move `this` instruction before `cursor`.
1861 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001862
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001863#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001864 bool Is##type() const { return (As##type() != nullptr); } \
1865 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001866 virtual H##type* As##type() { return nullptr; }
1867
1868 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1869#undef INSTRUCTION_TYPE_CHECK
1870
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001871 // Returns whether the instruction can be moved within the graph.
1872 virtual bool CanBeMoved() const { return false; }
1873
1874 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001875 virtual bool InstructionTypeEquals(HInstruction* other) const {
1876 UNUSED(other);
1877 return false;
1878 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001879
1880 // Returns whether any data encoded in the two instructions is equal.
1881 // This method does not look at the inputs. Both instructions must be
1882 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001883 virtual bool InstructionDataEquals(HInstruction* other) const {
1884 UNUSED(other);
1885 return false;
1886 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001887
1888 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001889 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001890 // 2) Their inputs are identical.
1891 bool Equals(HInstruction* other) const;
1892
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001893 virtual InstructionKind GetKind() const = 0;
1894
1895 virtual size_t ComputeHashCode() const {
1896 size_t result = GetKind();
1897 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1898 result = (result * 31) + InputAt(i)->GetId();
1899 }
1900 return result;
1901 }
1902
1903 SideEffects GetSideEffects() const { return side_effects_; }
1904
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001905 size_t GetLifetimePosition() const { return lifetime_position_; }
1906 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1907 LiveInterval* GetLiveInterval() const { return live_interval_; }
1908 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1909 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1910
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001911 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1912
1913 // Returns whether the code generation of the instruction will require to have access
1914 // to the current method. Such instructions are:
1915 // (1): Instructions that require an environment, as calling the runtime requires
1916 // to walk the stack and have the current method stored at a specific stack address.
1917 // (2): Object literals like classes and strings, that are loaded from the dex cache
1918 // fields of the current method.
1919 bool NeedsCurrentMethod() const {
1920 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1921 }
1922
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001923 virtual bool NeedsDexCache() const { return false; }
1924
Mark Mendellc4701932015-04-10 13:18:51 -04001925 // Does this instruction have any use in an environment before
1926 // control flow hits 'other'?
1927 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1928
1929 // Remove all references to environment uses of this instruction.
1930 // The caller must ensure that this is safe to do.
1931 void RemoveEnvironmentUsers();
1932
David Brazdil1abb4192015-02-17 18:33:36 +00001933 protected:
1934 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1935 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1936
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001937 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001938 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1939
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001940 HInstruction* previous_;
1941 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001942 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001943 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001944
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001945 // An instruction gets an id when it is added to the graph.
1946 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001947 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001948 int id_;
1949
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001950 // When doing liveness analysis, instructions that have uses get an SSA index.
1951 int ssa_index_;
1952
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001953 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001954 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001955
1956 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001957 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001958
Nicolas Geoffray39468442014-09-02 15:17:15 +01001959 // The environment associated with this instruction. Not null if the instruction
1960 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001962
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001963 // Set by the code generator.
1964 LocationSummary* locations_;
1965
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001966 // Set by the liveness analysis.
1967 LiveInterval* live_interval_;
1968
1969 // Set by the liveness analysis, this is the position in a linear
1970 // order of blocks where this instruction's live interval start.
1971 size_t lifetime_position_;
1972
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001973 const SideEffects side_effects_;
1974
Calin Juravleacf735c2015-02-12 15:25:22 +00001975 // TODO: for primitive types this should be marked as invalid.
1976 ReferenceTypeInfo reference_type_info_;
1977
David Brazdil1abb4192015-02-17 18:33:36 +00001978 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001979 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001980 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001981 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001982 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001983
1984 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1985};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001986std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001987
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001988class HInputIterator : public ValueObject {
1989 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001990 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001991
1992 bool Done() const { return index_ == instruction_->InputCount(); }
1993 HInstruction* Current() const { return instruction_->InputAt(index_); }
1994 void Advance() { index_++; }
1995
1996 private:
1997 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001998 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001999
2000 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2001};
2002
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002003class HInstructionIterator : public ValueObject {
2004 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002005 explicit HInstructionIterator(const HInstructionList& instructions)
2006 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002007 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002008 }
2009
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002010 bool Done() const { return instruction_ == nullptr; }
2011 HInstruction* Current() const { return instruction_; }
2012 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002013 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002014 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002015 }
2016
2017 private:
2018 HInstruction* instruction_;
2019 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002020
2021 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002022};
2023
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002024class HBackwardInstructionIterator : public ValueObject {
2025 public:
2026 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2027 : instruction_(instructions.last_instruction_) {
2028 next_ = Done() ? nullptr : instruction_->GetPrevious();
2029 }
2030
2031 bool Done() const { return instruction_ == nullptr; }
2032 HInstruction* Current() const { return instruction_; }
2033 void Advance() {
2034 instruction_ = next_;
2035 next_ = Done() ? nullptr : instruction_->GetPrevious();
2036 }
2037
2038 private:
2039 HInstruction* instruction_;
2040 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002041
2042 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002043};
2044
Vladimir Markof9f64412015-09-02 14:05:49 +01002045template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002046class HTemplateInstruction: public HInstruction {
2047 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002048 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2049 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002050 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002051
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002052 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002053
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002054 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002055 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2056 DCHECK_LT(i, N);
2057 return inputs_[i];
2058 }
David Brazdil1abb4192015-02-17 18:33:36 +00002059
2060 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002061 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002062 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002063 }
2064
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002065 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002066 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002067
2068 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002069};
2070
Vladimir Markof9f64412015-09-02 14:05:49 +01002071// HTemplateInstruction specialization for N=0.
2072template<>
2073class HTemplateInstruction<0>: public HInstruction {
2074 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002075 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2076 : HInstruction(side_effects, dex_pc) {}
2077
Vladimir Markof9f64412015-09-02 14:05:49 +01002078 virtual ~HTemplateInstruction() {}
2079
2080 size_t InputCount() const OVERRIDE { return 0; }
2081
2082 protected:
2083 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2084 LOG(FATAL) << "Unreachable";
2085 UNREACHABLE();
2086 }
2087
2088 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2089 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2090 LOG(FATAL) << "Unreachable";
2091 UNREACHABLE();
2092 }
2093
2094 private:
2095 friend class SsaBuilder;
2096};
2097
Dave Allison20dfc792014-06-16 20:44:29 -07002098template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002099class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002100 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002101 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2102 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002103 virtual ~HExpression() {}
2104
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002105 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002106
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002107 protected:
2108 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002109};
2110
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002111// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2112// instruction that branches to the exit block.
2113class HReturnVoid : public HTemplateInstruction<0> {
2114 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002115 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2116 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002117
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002118 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002120 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002121
2122 private:
2123 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2124};
2125
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002126// Represents dex's RETURN opcodes. A HReturn is a control flow
2127// instruction that branches to the exit block.
2128class HReturn : public HTemplateInstruction<1> {
2129 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002130 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2131 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002132 SetRawInputAt(0, value);
2133 }
2134
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002135 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002136
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002137 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002138
2139 private:
2140 DISALLOW_COPY_AND_ASSIGN(HReturn);
2141};
2142
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002143// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002144// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002145// exit block.
2146class HExit : public HTemplateInstruction<0> {
2147 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002148 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002149
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002150 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002151
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002152 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002153
2154 private:
2155 DISALLOW_COPY_AND_ASSIGN(HExit);
2156};
2157
2158// Jumps from one block to another.
2159class HGoto : public HTemplateInstruction<0> {
2160 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002161 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002162
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002163 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002164
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002165 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002166 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002167 }
2168
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002169 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002170
2171 private:
2172 DISALLOW_COPY_AND_ASSIGN(HGoto);
2173};
2174
Roland Levillain9867bc72015-08-05 10:21:34 +01002175class HConstant : public HExpression<0> {
2176 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002177 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2178 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002179
2180 bool CanBeMoved() const OVERRIDE { return true; }
2181
2182 virtual bool IsMinusOne() const { return false; }
2183 virtual bool IsZero() const { return false; }
2184 virtual bool IsOne() const { return false; }
2185
David Brazdil77a48ae2015-09-15 12:34:04 +00002186 virtual uint64_t GetValueAsUint64() const = 0;
2187
Roland Levillain9867bc72015-08-05 10:21:34 +01002188 DECLARE_INSTRUCTION(Constant);
2189
2190 private:
2191 DISALLOW_COPY_AND_ASSIGN(HConstant);
2192};
2193
2194class HNullConstant : public HConstant {
2195 public:
2196 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2197 return true;
2198 }
2199
David Brazdil77a48ae2015-09-15 12:34:04 +00002200 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2201
Roland Levillain9867bc72015-08-05 10:21:34 +01002202 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
David Brazdil77a48ae2015-09-15 12:34:04 +00002219 uint64_t GetValueAsUint64() const OVERRIDE { return static_cast<uint64_t>(value_); }
2220
Roland Levillain9867bc72015-08-05 10:21:34 +01002221 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2222 DCHECK(other->IsIntConstant());
2223 return other->AsIntConstant()->value_ == value_;
2224 }
2225
2226 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2227
2228 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2229 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2230 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2231
2232 DECLARE_INSTRUCTION(IntConstant);
2233
2234 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002235 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2236 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2237 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2238 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002239
2240 const int32_t value_;
2241
2242 friend class HGraph;
2243 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2244 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2245 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2246};
2247
2248class HLongConstant : public HConstant {
2249 public:
2250 int64_t GetValue() const { return value_; }
2251
David Brazdil77a48ae2015-09-15 12:34:04 +00002252 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2253
Roland Levillain9867bc72015-08-05 10:21:34 +01002254 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2255 DCHECK(other->IsLongConstant());
2256 return other->AsLongConstant()->value_ == value_;
2257 }
2258
2259 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2260
2261 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2262 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2263 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2264
2265 DECLARE_INSTRUCTION(LongConstant);
2266
2267 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002268 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2269 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002270
2271 const int64_t value_;
2272
2273 friend class HGraph;
2274 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2275};
Dave Allison20dfc792014-06-16 20:44:29 -07002276
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002277// Conditional branch. A block ending with an HIf instruction must have
2278// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002279class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002280 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002281 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2282 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002283 SetRawInputAt(0, input);
2284 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002285
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002286 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002287
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002288 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002289 return GetBlock()->GetSuccessor(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002290 }
2291
2292 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002293 return GetBlock()->GetSuccessor(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002294 }
2295
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002296 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002297
2298 private:
2299 DISALLOW_COPY_AND_ASSIGN(HIf);
2300};
2301
David Brazdilfc6a86a2015-06-26 10:33:45 +00002302
2303// Abstract instruction which marks the beginning and/or end of a try block and
2304// links it to the respective exception handlers. Behaves the same as a Goto in
2305// non-exceptional control flow.
2306// Normal-flow successor is stored at index zero, exception handlers under
2307// higher indices in no particular order.
2308class HTryBoundary : public HTemplateInstruction<0> {
2309 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002310 enum BoundaryKind {
2311 kEntry,
2312 kExit,
2313 };
2314
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002315 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2316 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002317
2318 bool IsControlFlow() const OVERRIDE { return true; }
2319
2320 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko60584552015-09-03 13:35:12 +00002321 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessor(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002322
2323 // Returns whether `handler` is among its exception handlers (non-zero index
2324 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002325 bool HasExceptionHandler(const HBasicBlock& handler) const {
2326 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002327 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002328 }
2329
2330 // If not present already, adds `handler` to its block's list of exception
2331 // handlers.
2332 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002333 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002334 GetBlock()->AddSuccessor(handler);
2335 }
2336 }
2337
David Brazdil56e1acc2015-06-30 15:41:36 +01002338 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002339
David Brazdilffee3d32015-07-06 11:48:53 +01002340 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2341
David Brazdilfc6a86a2015-06-26 10:33:45 +00002342 DECLARE_INSTRUCTION(TryBoundary);
2343
2344 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002345 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002346
2347 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2348};
2349
David Brazdilffee3d32015-07-06 11:48:53 +01002350// Iterator over exception handlers of a given HTryBoundary, i.e. over
2351// exceptional successors of its basic block.
2352class HExceptionHandlerIterator : public ValueObject {
2353 public:
2354 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2355 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2356
Vladimir Marko60584552015-09-03 13:35:12 +00002357 bool Done() const { return index_ == block_.GetSuccessors().size(); }
2358 HBasicBlock* Current() const { return block_.GetSuccessor(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002359 size_t CurrentSuccessorIndex() const { return index_; }
2360 void Advance() { ++index_; }
2361
2362 private:
2363 const HBasicBlock& block_;
2364 size_t index_;
2365
2366 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2367};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002368
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002369// Deoptimize to interpreter, upon checking a condition.
2370class HDeoptimize : public HTemplateInstruction<1> {
2371 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002372 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2373 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002374 SetRawInputAt(0, cond);
2375 }
2376
2377 bool NeedsEnvironment() const OVERRIDE { return true; }
2378 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002379
2380 DECLARE_INSTRUCTION(Deoptimize);
2381
2382 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002383 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2384};
2385
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002386// Represents the ArtMethod that was passed as a first argument to
2387// the method. It is used by instructions that depend on it, like
2388// instructions that work with the dex cache.
2389class HCurrentMethod : public HExpression<0> {
2390 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002391 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2392 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002393
2394 DECLARE_INSTRUCTION(CurrentMethod);
2395
2396 private:
2397 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2398};
2399
Mark Mendellfe57faa2015-09-18 09:26:15 -04002400// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2401// have one successor for each entry in the switch table, and the final successor
2402// will be the block containing the next Dex opcode.
2403class HPackedSwitch : public HTemplateInstruction<1> {
2404 public:
2405 HPackedSwitch(int32_t start_value, int32_t num_entries, HInstruction* input,
2406 uint32_t dex_pc = kNoDexPc)
2407 : HTemplateInstruction(SideEffects::None(), dex_pc),
2408 start_value_(start_value),
2409 num_entries_(num_entries) {
2410 SetRawInputAt(0, input);
2411 }
2412
2413 bool IsControlFlow() const OVERRIDE { return true; }
2414
2415 int32_t GetStartValue() const { return start_value_; }
2416
2417 int32_t GetNumEntries() const { return num_entries_; }
2418
2419 HBasicBlock* GetDefaultBlock() const {
2420 // Last entry is the default block.
2421 return GetBlock()->GetSuccessor(num_entries_);
2422 }
2423 DECLARE_INSTRUCTION(PackedSwitch);
2424
2425 private:
2426 int32_t start_value_;
2427 int32_t num_entries_;
2428
2429 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2430};
2431
Roland Levillain88cb1752014-10-20 16:36:47 +01002432class HUnaryOperation : public HExpression<1> {
2433 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002434 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2435 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002436 SetRawInputAt(0, input);
2437 }
2438
2439 HInstruction* GetInput() const { return InputAt(0); }
2440 Primitive::Type GetResultType() const { return GetType(); }
2441
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002442 bool CanBeMoved() const OVERRIDE { return true; }
2443 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002444 UNUSED(other);
2445 return true;
2446 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002447
Roland Levillain9240d6a2014-10-20 16:47:04 +01002448 // Try to statically evaluate `operation` and return a HConstant
2449 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002450 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002451 HConstant* TryStaticEvaluation() const;
2452
2453 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002454 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2455 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002456
Roland Levillain88cb1752014-10-20 16:36:47 +01002457 DECLARE_INSTRUCTION(UnaryOperation);
2458
2459 private:
2460 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2461};
2462
Dave Allison20dfc792014-06-16 20:44:29 -07002463class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002464 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002465 HBinaryOperation(Primitive::Type result_type,
2466 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002467 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002468 SideEffects side_effects = SideEffects::None(),
2469 uint32_t dex_pc = kNoDexPc)
2470 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002471 SetRawInputAt(0, left);
2472 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002473 }
2474
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002475 HInstruction* GetLeft() const { return InputAt(0); }
2476 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002477 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002478
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002479 virtual bool IsCommutative() const { return false; }
2480
2481 // Put constant on the right.
2482 // Returns whether order is changed.
2483 bool OrderInputsWithConstantOnTheRight() {
2484 HInstruction* left = InputAt(0);
2485 HInstruction* right = InputAt(1);
2486 if (left->IsConstant() && !right->IsConstant()) {
2487 ReplaceInput(right, 0);
2488 ReplaceInput(left, 1);
2489 return true;
2490 }
2491 return false;
2492 }
2493
2494 // Order inputs by instruction id, but favor constant on the right side.
2495 // This helps GVN for commutative ops.
2496 void OrderInputs() {
2497 DCHECK(IsCommutative());
2498 HInstruction* left = InputAt(0);
2499 HInstruction* right = InputAt(1);
2500 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2501 return;
2502 }
2503 if (OrderInputsWithConstantOnTheRight()) {
2504 return;
2505 }
2506 // Order according to instruction id.
2507 if (left->GetId() > right->GetId()) {
2508 ReplaceInput(right, 0);
2509 ReplaceInput(left, 1);
2510 }
2511 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002512
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002513 bool CanBeMoved() const OVERRIDE { return true; }
2514 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002515 UNUSED(other);
2516 return true;
2517 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002518
Roland Levillain9240d6a2014-10-20 16:47:04 +01002519 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002520 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002521 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002522 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002523
2524 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002525 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2526 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2527 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2528 HLongConstant* y ATTRIBUTE_UNUSED) const {
2529 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2530 return nullptr;
2531 }
2532 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2533 HIntConstant* y ATTRIBUTE_UNUSED) const {
2534 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2535 return nullptr;
2536 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002537
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002538 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002539 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002540 HConstant* GetConstantRight() const;
2541
2542 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002543 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002544 HInstruction* GetLeastConstantLeft() const;
2545
Roland Levillainccc07a92014-09-16 14:48:16 +01002546 DECLARE_INSTRUCTION(BinaryOperation);
2547
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002548 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002549 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2550};
2551
Mark Mendellc4701932015-04-10 13:18:51 -04002552// The comparison bias applies for floating point operations and indicates how NaN
2553// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002554enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002555 kNoBias, // bias is not applicable (i.e. for long operation)
2556 kGtBias, // return 1 for NaN comparisons
2557 kLtBias, // return -1 for NaN comparisons
2558};
2559
Dave Allison20dfc792014-06-16 20:44:29 -07002560class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002561 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002562 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2563 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002564 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002565 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002566
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002567 bool NeedsMaterialization() const { return needs_materialization_; }
2568 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002569
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002570 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002571 // `instruction`, and disregard moves in between.
2572 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002573
Dave Allison20dfc792014-06-16 20:44:29 -07002574 DECLARE_INSTRUCTION(Condition);
2575
2576 virtual IfCondition GetCondition() const = 0;
2577
Mark Mendellc4701932015-04-10 13:18:51 -04002578 virtual IfCondition GetOppositeCondition() const = 0;
2579
Roland Levillain4fa13f62015-07-06 18:11:54 +01002580 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002581
2582 void SetBias(ComparisonBias bias) { bias_ = bias; }
2583
2584 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2585 return bias_ == other->AsCondition()->bias_;
2586 }
2587
Roland Levillain4fa13f62015-07-06 18:11:54 +01002588 bool IsFPConditionTrueIfNaN() const {
2589 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2590 IfCondition if_cond = GetCondition();
2591 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2592 }
2593
2594 bool IsFPConditionFalseIfNaN() const {
2595 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2596 IfCondition if_cond = GetCondition();
2597 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2598 }
2599
Dave Allison20dfc792014-06-16 20:44:29 -07002600 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002601 // For register allocation purposes, returns whether this instruction needs to be
2602 // materialized (that is, not just be in the processor flags).
2603 bool needs_materialization_;
2604
Mark Mendellc4701932015-04-10 13:18:51 -04002605 // Needed if we merge a HCompare into a HCondition.
2606 ComparisonBias bias_;
2607
Dave Allison20dfc792014-06-16 20:44:29 -07002608 DISALLOW_COPY_AND_ASSIGN(HCondition);
2609};
2610
2611// Instruction to check if two inputs are equal to each other.
2612class HEqual : public HCondition {
2613 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002614 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2615 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002616
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002617 bool IsCommutative() const OVERRIDE { return true; }
2618
Roland Levillain9867bc72015-08-05 10:21:34 +01002619 template <typename T> bool Compute(T x, T y) const { return x == y; }
2620
2621 HConstant* Evaluate(HIntConstant* x, HIntConstant* 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 Levillain9867bc72015-08-05 10:21:34 +01002625 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002626 return GetBlock()->GetGraph()->GetIntConstant(
2627 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002628 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002629
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002630 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002631
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002632 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002633 return kCondEQ;
2634 }
2635
Mark Mendellc4701932015-04-10 13:18:51 -04002636 IfCondition GetOppositeCondition() const OVERRIDE {
2637 return kCondNE;
2638 }
2639
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002640 private:
2641 DISALLOW_COPY_AND_ASSIGN(HEqual);
2642};
2643
Dave Allison20dfc792014-06-16 20:44:29 -07002644class HNotEqual : public HCondition {
2645 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002646 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2647 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002648
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002649 bool IsCommutative() const OVERRIDE { return true; }
2650
Roland Levillain9867bc72015-08-05 10:21:34 +01002651 template <typename T> bool Compute(T x, T y) const { return x != y; }
2652
2653 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002654 return GetBlock()->GetGraph()->GetIntConstant(
2655 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002656 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002657 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002658 return GetBlock()->GetGraph()->GetIntConstant(
2659 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002660 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002661
Dave Allison20dfc792014-06-16 20:44:29 -07002662 DECLARE_INSTRUCTION(NotEqual);
2663
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002664 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002665 return kCondNE;
2666 }
2667
Mark Mendellc4701932015-04-10 13:18:51 -04002668 IfCondition GetOppositeCondition() const OVERRIDE {
2669 return kCondEQ;
2670 }
2671
Dave Allison20dfc792014-06-16 20:44:29 -07002672 private:
2673 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2674};
2675
2676class HLessThan : public HCondition {
2677 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002678 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2679 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002680
Roland Levillain9867bc72015-08-05 10:21:34 +01002681 template <typename T> bool Compute(T x, T y) const { return x < y; }
2682
2683 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002684 return GetBlock()->GetGraph()->GetIntConstant(
2685 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002686 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002687 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002688 return GetBlock()->GetGraph()->GetIntConstant(
2689 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002690 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002691
Dave Allison20dfc792014-06-16 20:44:29 -07002692 DECLARE_INSTRUCTION(LessThan);
2693
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002694 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002695 return kCondLT;
2696 }
2697
Mark Mendellc4701932015-04-10 13:18:51 -04002698 IfCondition GetOppositeCondition() const OVERRIDE {
2699 return kCondGE;
2700 }
2701
Dave Allison20dfc792014-06-16 20:44:29 -07002702 private:
2703 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2704};
2705
2706class HLessThanOrEqual : public HCondition {
2707 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002708 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2709 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002710
Roland Levillain9867bc72015-08-05 10:21:34 +01002711 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2712
2713 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002714 return GetBlock()->GetGraph()->GetIntConstant(
2715 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002716 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002717 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002718 return GetBlock()->GetGraph()->GetIntConstant(
2719 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002720 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002721
Dave Allison20dfc792014-06-16 20:44:29 -07002722 DECLARE_INSTRUCTION(LessThanOrEqual);
2723
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002724 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002725 return kCondLE;
2726 }
2727
Mark Mendellc4701932015-04-10 13:18:51 -04002728 IfCondition GetOppositeCondition() const OVERRIDE {
2729 return kCondGT;
2730 }
2731
Dave Allison20dfc792014-06-16 20:44:29 -07002732 private:
2733 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2734};
2735
2736class HGreaterThan : public HCondition {
2737 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002738 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2739 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002740
Roland Levillain9867bc72015-08-05 10:21:34 +01002741 template <typename T> bool Compute(T x, T y) const { return x > y; }
2742
2743 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002744 return GetBlock()->GetGraph()->GetIntConstant(
2745 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002746 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002747 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002748 return GetBlock()->GetGraph()->GetIntConstant(
2749 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002750 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002751
Dave Allison20dfc792014-06-16 20:44:29 -07002752 DECLARE_INSTRUCTION(GreaterThan);
2753
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002754 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002755 return kCondGT;
2756 }
2757
Mark Mendellc4701932015-04-10 13:18:51 -04002758 IfCondition GetOppositeCondition() const OVERRIDE {
2759 return kCondLE;
2760 }
2761
Dave Allison20dfc792014-06-16 20:44:29 -07002762 private:
2763 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2764};
2765
2766class HGreaterThanOrEqual : public HCondition {
2767 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002768 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2769 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002770
Roland Levillain9867bc72015-08-05 10:21:34 +01002771 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2772
2773 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002774 return GetBlock()->GetGraph()->GetIntConstant(
2775 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002776 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002777 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002778 return GetBlock()->GetGraph()->GetIntConstant(
2779 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002780 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002781
Dave Allison20dfc792014-06-16 20:44:29 -07002782 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2783
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002784 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002785 return kCondGE;
2786 }
2787
Mark Mendellc4701932015-04-10 13:18:51 -04002788 IfCondition GetOppositeCondition() const OVERRIDE {
2789 return kCondLT;
2790 }
2791
Dave Allison20dfc792014-06-16 20:44:29 -07002792 private:
2793 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2794};
2795
2796
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002797// Instruction to check how two inputs compare to each other.
2798// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2799class HCompare : public HBinaryOperation {
2800 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002801 HCompare(Primitive::Type type,
2802 HInstruction* first,
2803 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002804 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002805 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002806 : HBinaryOperation(Primitive::kPrimInt,
2807 first,
2808 second,
2809 SideEffectsForArchRuntimeCalls(type),
2810 dex_pc),
2811 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002812 DCHECK_EQ(type, first->GetType());
2813 DCHECK_EQ(type, second->GetType());
2814 }
2815
Roland Levillain9867bc72015-08-05 10:21:34 +01002816 template <typename T>
2817 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002818
Roland Levillain9867bc72015-08-05 10:21:34 +01002819 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002820 return GetBlock()->GetGraph()->GetIntConstant(
2821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002822 }
2823 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002824 return GetBlock()->GetGraph()->GetIntConstant(
2825 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002826 }
2827
Calin Juravleddb7df22014-11-25 20:56:51 +00002828 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2829 return bias_ == other->AsCompare()->bias_;
2830 }
2831
Mark Mendellc4701932015-04-10 13:18:51 -04002832 ComparisonBias GetBias() const { return bias_; }
2833
Roland Levillain4fa13f62015-07-06 18:11:54 +01002834 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002835
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002836
2837 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2838 // MIPS64 uses a runtime call for FP comparisons.
2839 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2840 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002841
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002842 DECLARE_INSTRUCTION(Compare);
2843
2844 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002845 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002846
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002847 DISALLOW_COPY_AND_ASSIGN(HCompare);
2848};
2849
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002850// A local in the graph. Corresponds to a Dex register.
2851class HLocal : public HTemplateInstruction<0> {
2852 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002853 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002854 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002855
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002856 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002857
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002858 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002859
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002860 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002861 // The Dex register number.
2862 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002863
2864 DISALLOW_COPY_AND_ASSIGN(HLocal);
2865};
2866
2867// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002868class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002869 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002870 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2871 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002872 SetRawInputAt(0, local);
2873 }
2874
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002875 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2876
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002877 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002878
2879 private:
2880 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2881};
2882
2883// Store a value in a given local. This instruction has two inputs: the value
2884// and the local.
2885class HStoreLocal : public HTemplateInstruction<2> {
2886 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002887 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2888 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002889 SetRawInputAt(0, local);
2890 SetRawInputAt(1, value);
2891 }
2892
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002893 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2894
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002895 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002896
2897 private:
2898 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2899};
2900
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002901class HFloatConstant : public HConstant {
2902 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002903 float GetValue() const { return value_; }
2904
David Brazdil77a48ae2015-09-15 12:34:04 +00002905 uint64_t GetValueAsUint64() const OVERRIDE {
2906 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2907 }
2908
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002909 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002910 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002911 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002912 }
2913
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002914 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002915
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002916 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002917 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002918 }
2919 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002920 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002921 }
2922 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002923 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2924 }
2925 bool IsNaN() const {
2926 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002927 }
2928
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002929 DECLARE_INSTRUCTION(FloatConstant);
2930
2931 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002932 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2933 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2934 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2935 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002936
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002937 const float value_;
2938
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002939 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002940 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002941 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002942 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2943};
2944
2945class HDoubleConstant : public HConstant {
2946 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002947 double GetValue() const { return value_; }
2948
David Brazdil77a48ae2015-09-15 12:34:04 +00002949 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2950
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002951 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002952 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002953 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002954 }
2955
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002956 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002957
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002958 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002959 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002960 }
2961 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002962 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002963 }
2964 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002965 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2966 }
2967 bool IsNaN() const {
2968 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002969 }
2970
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002971 DECLARE_INSTRUCTION(DoubleConstant);
2972
2973 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002974 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2975 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2976 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2977 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002978
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002979 const double value_;
2980
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002981 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002982 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002983 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002984 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2985};
2986
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002987enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002988#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002989#include "intrinsics_list.h"
2990 kNone,
2991 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2992#undef INTRINSICS_LIST
2993#undef OPTIMIZING_INTRINSICS
2994};
2995std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2996
Agi Csaki05f20562015-08-19 14:58:14 -07002997enum IntrinsicNeedsEnvironmentOrCache {
2998 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
2999 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003000};
3001
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003002class HInvoke : public HInstruction {
3003 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003004 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003005
3006 // Runtime needs to walk the stack, so Dex -> Dex calls need to
3007 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07003008 bool NeedsEnvironment() const OVERRIDE {
3009 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
3010 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003011
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003012 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003013 SetRawInputAt(index, argument);
3014 }
3015
Roland Levillain3e3d7332015-04-28 11:00:54 +01003016 // Return the number of arguments. This number can be lower than
3017 // the number of inputs returned by InputCount(), as some invoke
3018 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3019 // inputs at the end of their list of inputs.
3020 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3021
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003022 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003023
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003024
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003025 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003026 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003027
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003028 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3029
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003030 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003031 return intrinsic_;
3032 }
3033
Agi Csaki05f20562015-08-19 14:58:14 -07003034 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003035 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07003036 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003037 }
3038
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003039 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003040 return GetEnvironment()->GetParent() != nullptr;
3041 }
3042
3043 bool CanThrow() const OVERRIDE { return true; }
3044
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003045 DECLARE_INSTRUCTION(Invoke);
3046
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003047 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003048 HInvoke(ArenaAllocator* arena,
3049 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003050 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003051 Primitive::Type return_type,
3052 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003053 uint32_t dex_method_index,
3054 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003055 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003056 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003057 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003058 inputs_(number_of_arguments + number_of_other_inputs,
3059 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003060 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003061 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003062 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003063 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07003064 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003065 }
3066
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003067 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
3068 DCHECK_LT(index, InputCount());
3069 return inputs_[index];
3070 }
3071
David Brazdil1abb4192015-02-17 18:33:36 +00003072 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003073 DCHECK_LT(index, InputCount());
3074 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003075 }
3076
Roland Levillain3e3d7332015-04-28 11:00:54 +01003077 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003078 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003079 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003080 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003081 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003082 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003083 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003084
3085 private:
3086 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3087};
3088
Calin Juravle175dc732015-08-25 15:42:32 +01003089class HInvokeUnresolved : public HInvoke {
3090 public:
3091 HInvokeUnresolved(ArenaAllocator* arena,
3092 uint32_t number_of_arguments,
3093 Primitive::Type return_type,
3094 uint32_t dex_pc,
3095 uint32_t dex_method_index,
3096 InvokeType invoke_type)
3097 : HInvoke(arena,
3098 number_of_arguments,
3099 0u /* number_of_other_inputs */,
3100 return_type,
3101 dex_pc,
3102 dex_method_index,
3103 invoke_type) {
3104 }
3105
3106 DECLARE_INSTRUCTION(InvokeUnresolved);
3107
3108 private:
3109 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3110};
3111
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003112class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003113 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003114 // Requirements of this method call regarding the class
3115 // initialization (clinit) check of its declaring class.
3116 enum class ClinitCheckRequirement {
3117 kNone, // Class already initialized.
3118 kExplicit, // Static call having explicit clinit check as last input.
3119 kImplicit, // Static call implicitly requiring a clinit check.
3120 };
3121
Vladimir Marko58155012015-08-19 12:49:41 +00003122 // Determines how to load the target ArtMethod*.
3123 enum class MethodLoadKind {
3124 // Use a String init ArtMethod* loaded from Thread entrypoints.
3125 kStringInit,
3126
3127 // Use the method's own ArtMethod* loaded by the register allocator.
3128 kRecursive,
3129
3130 // Use ArtMethod* at a known address, embed the direct address in the code.
3131 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3132 kDirectAddress,
3133
3134 // Use ArtMethod* at an address that will be known at link time, embed the direct
3135 // address in the code. If the image is relocatable, emit .patch_oat entry.
3136 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3137 // the image relocatable or not.
3138 kDirectAddressWithFixup,
3139
3140 // Load from resoved methods array in the dex cache using a PC-relative load.
3141 // Used when we need to use the dex cache, for example for invoke-static that
3142 // may cause class initialization (the entry may point to a resolution method),
3143 // and we know that we can access the dex cache arrays using a PC-relative load.
3144 kDexCachePcRelative,
3145
3146 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3147 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3148 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3149 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3150 kDexCacheViaMethod,
3151 };
3152
3153 // Determines the location of the code pointer.
3154 enum class CodePtrLocation {
3155 // Recursive call, use local PC-relative call instruction.
3156 kCallSelf,
3157
3158 // Use PC-relative call instruction patched at link time.
3159 // Used for calls within an oat file, boot->boot or app->app.
3160 kCallPCRelative,
3161
3162 // Call to a known target address, embed the direct address in code.
3163 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3164 kCallDirect,
3165
3166 // Call to a target address that will be known at link time, embed the direct
3167 // address in code. If the image is relocatable, emit .patch_oat entry.
3168 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3169 // the image relocatable or not.
3170 kCallDirectWithFixup,
3171
3172 // Use code pointer from the ArtMethod*.
3173 // Used when we don't know the target code. This is also the last-resort-kind used when
3174 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3175 kCallArtMethod,
3176 };
3177
3178 struct DispatchInfo {
3179 const MethodLoadKind method_load_kind;
3180 const CodePtrLocation code_ptr_location;
3181 // The method load data holds
3182 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3183 // Note that there are multiple string init methods, each having its own offset.
3184 // - the method address for kDirectAddress
3185 // - the dex cache arrays offset for kDexCachePcRel.
3186 const uint64_t method_load_data;
3187 const uint64_t direct_code_ptr;
3188 };
3189
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003190 HInvokeStaticOrDirect(ArenaAllocator* arena,
3191 uint32_t number_of_arguments,
3192 Primitive::Type return_type,
3193 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003194 uint32_t method_index,
3195 MethodReference target_method,
3196 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003197 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003198 InvokeType invoke_type,
3199 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003200 : HInvoke(arena,
3201 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003202 // There is one extra argument for the HCurrentMethod node, and
3203 // potentially one other if the clinit check is explicit, and one other
3204 // if the method is a string factory.
3205 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003206 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003207 return_type,
3208 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003209 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003210 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003211 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003212 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003213 target_method_(target_method),
3214 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003215
Calin Juravle641547a2015-04-21 22:08:51 +01003216 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3217 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003218 // We access the method via the dex cache so we can't do an implicit null check.
3219 // TODO: for intrinsics we can generate implicit null checks.
3220 return false;
3221 }
3222
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003223 bool CanBeNull() const OVERRIDE {
3224 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3225 }
3226
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003227 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003228 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3229 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3230 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003231 bool NeedsDexCache() const OVERRIDE {
3232 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3233 return !IsRecursive() && !IsStringInit();
3234 }
Vladimir Marko58155012015-08-19 12:49:41 +00003235 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003236 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003237 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3238 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3239 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3240 MethodReference GetTargetMethod() const { return target_method_; }
3241
3242 int32_t GetStringInitOffset() const {
3243 DCHECK(IsStringInit());
3244 return dispatch_info_.method_load_data;
3245 }
3246
3247 uint64_t GetMethodAddress() const {
3248 DCHECK(HasMethodAddress());
3249 return dispatch_info_.method_load_data;
3250 }
3251
3252 uint32_t GetDexCacheArrayOffset() const {
3253 DCHECK(HasPcRelDexCache());
3254 return dispatch_info_.method_load_data;
3255 }
3256
3257 uint64_t GetDirectCodePtr() const {
3258 DCHECK(HasDirectCodePtr());
3259 return dispatch_info_.direct_code_ptr;
3260 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003261
Calin Juravle68ad6492015-08-18 17:08:12 +01003262 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3263
Roland Levillain4c0eb422015-04-24 16:43:49 +01003264 // Is this instruction a call to a static method?
3265 bool IsStatic() const {
3266 return GetInvokeType() == kStatic;
3267 }
3268
Roland Levillain3e3d7332015-04-28 11:00:54 +01003269 // Remove the art::HLoadClass instruction set as last input by
3270 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3271 // the initial art::HClinitCheck instruction (only relevant for
3272 // static calls with explicit clinit check).
3273 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003274 DCHECK(IsStaticWithExplicitClinitCheck());
3275 size_t last_input_index = InputCount() - 1;
3276 HInstruction* last_input = InputAt(last_input_index);
3277 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003278 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003279 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003280 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003281 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3282 DCHECK(IsStaticWithImplicitClinitCheck());
3283 }
3284
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003285 bool IsStringFactoryFor(HFakeString* str) const {
3286 if (!IsStringInit()) return false;
3287 // +1 for the current method.
3288 if (InputCount() == (number_of_arguments_ + 1)) return false;
3289 return InputAt(InputCount() - 1)->AsFakeString() == str;
3290 }
3291
3292 void RemoveFakeStringArgumentAsLastInput() {
3293 DCHECK(IsStringInit());
3294 size_t last_input_index = InputCount() - 1;
3295 HInstruction* last_input = InputAt(last_input_index);
3296 DCHECK(last_input != nullptr);
3297 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3298 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003299 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003300 }
3301
Roland Levillain4c0eb422015-04-24 16:43:49 +01003302 // Is this a call to a static method whose declaring class has an
3303 // explicit intialization check in the graph?
3304 bool IsStaticWithExplicitClinitCheck() const {
3305 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3306 }
3307
3308 // Is this a call to a static method whose declaring class has an
3309 // implicit intialization check requirement?
3310 bool IsStaticWithImplicitClinitCheck() const {
3311 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3312 }
3313
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003314 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003315
Roland Levillain4c0eb422015-04-24 16:43:49 +01003316 protected:
3317 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3318 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3319 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3320 HInstruction* input = input_record.GetInstruction();
3321 // `input` is the last input of a static invoke marked as having
3322 // an explicit clinit check. It must either be:
3323 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3324 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3325 DCHECK(input != nullptr);
3326 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3327 }
3328 return input_record;
3329 }
3330
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003331 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003332 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003333 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003334 // The target method may refer to different dex file or method index than the original
3335 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3336 // in derived class to increase visibility.
3337 MethodReference target_method_;
3338 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003339
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003340 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003341};
3342
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003343class HInvokeVirtual : public HInvoke {
3344 public:
3345 HInvokeVirtual(ArenaAllocator* arena,
3346 uint32_t number_of_arguments,
3347 Primitive::Type return_type,
3348 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003349 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003350 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003351 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003352 vtable_index_(vtable_index) {}
3353
Calin Juravle641547a2015-04-21 22:08:51 +01003354 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003355 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003356 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003357 }
3358
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003359 uint32_t GetVTableIndex() const { return vtable_index_; }
3360
3361 DECLARE_INSTRUCTION(InvokeVirtual);
3362
3363 private:
3364 const uint32_t vtable_index_;
3365
3366 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3367};
3368
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003369class HInvokeInterface : public HInvoke {
3370 public:
3371 HInvokeInterface(ArenaAllocator* arena,
3372 uint32_t number_of_arguments,
3373 Primitive::Type return_type,
3374 uint32_t dex_pc,
3375 uint32_t dex_method_index,
3376 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003377 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003378 imt_index_(imt_index) {}
3379
Calin Juravle641547a2015-04-21 22:08:51 +01003380 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003381 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003382 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003383 }
3384
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003385 uint32_t GetImtIndex() const { return imt_index_; }
3386 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3387
3388 DECLARE_INSTRUCTION(InvokeInterface);
3389
3390 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003391 const uint32_t imt_index_;
3392
3393 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3394};
3395
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003396class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003397 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003398 HNewInstance(HCurrentMethod* current_method,
3399 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003400 uint16_t type_index,
3401 const DexFile& dex_file,
3402 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003403 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003404 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003405 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003406 entrypoint_(entrypoint) {
3407 SetRawInputAt(0, current_method);
3408 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003409
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003410 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003411 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003412
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003413 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003414 bool NeedsEnvironment() const OVERRIDE { return true; }
3415 // It may throw when called on:
3416 // - interfaces
3417 // - abstract/innaccessible/unknown classes
3418 // TODO: optimize when possible.
3419 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003420
Calin Juravle10e244f2015-01-26 18:54:32 +00003421 bool CanBeNull() const OVERRIDE { return false; }
3422
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003423 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3424
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003425 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003426
3427 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003428 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003429 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003430 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003431
3432 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3433};
3434
Roland Levillain88cb1752014-10-20 16:36:47 +01003435class HNeg : public HUnaryOperation {
3436 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003437 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3438 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003439
Roland Levillain9867bc72015-08-05 10:21:34 +01003440 template <typename T> T Compute(T x) const { return -x; }
3441
3442 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003443 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003444 }
3445 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003446 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003447 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003448
Roland Levillain88cb1752014-10-20 16:36:47 +01003449 DECLARE_INSTRUCTION(Neg);
3450
3451 private:
3452 DISALLOW_COPY_AND_ASSIGN(HNeg);
3453};
3454
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003455class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003456 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003457 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003458 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003459 uint32_t dex_pc,
3460 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003461 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003462 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003463 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003464 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003465 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003466 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003467 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003468 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003469 }
3470
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003471 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003472 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003473
3474 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003475 bool NeedsEnvironment() const OVERRIDE { return true; }
3476
Mingyao Yang0c365e62015-03-31 15:09:29 -07003477 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3478 bool CanThrow() const OVERRIDE { return true; }
3479
Calin Juravle10e244f2015-01-26 18:54:32 +00003480 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003481
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003482 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3483
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003484 DECLARE_INSTRUCTION(NewArray);
3485
3486 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003487 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003488 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003489 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003490
3491 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3492};
3493
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003494class HAdd : public HBinaryOperation {
3495 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003496 HAdd(Primitive::Type result_type,
3497 HInstruction* left,
3498 HInstruction* right,
3499 uint32_t dex_pc = kNoDexPc)
3500 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003501
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003502 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003503
Roland Levillain9867bc72015-08-05 10:21:34 +01003504 template <typename T> T Compute(T x, T y) const { return x + y; }
3505
3506 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003507 return GetBlock()->GetGraph()->GetIntConstant(
3508 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003509 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003510 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003511 return GetBlock()->GetGraph()->GetLongConstant(
3512 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003513 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003514
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003515 DECLARE_INSTRUCTION(Add);
3516
3517 private:
3518 DISALLOW_COPY_AND_ASSIGN(HAdd);
3519};
3520
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003521class HSub : public HBinaryOperation {
3522 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003523 HSub(Primitive::Type result_type,
3524 HInstruction* left,
3525 HInstruction* right,
3526 uint32_t dex_pc = kNoDexPc)
3527 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003528
Roland Levillain9867bc72015-08-05 10:21:34 +01003529 template <typename T> T Compute(T x, T y) const { return x - y; }
3530
3531 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003532 return GetBlock()->GetGraph()->GetIntConstant(
3533 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003534 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003535 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003536 return GetBlock()->GetGraph()->GetLongConstant(
3537 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003538 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003539
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003540 DECLARE_INSTRUCTION(Sub);
3541
3542 private:
3543 DISALLOW_COPY_AND_ASSIGN(HSub);
3544};
3545
Calin Juravle34bacdf2014-10-07 20:23:36 +01003546class HMul : public HBinaryOperation {
3547 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003548 HMul(Primitive::Type result_type,
3549 HInstruction* left,
3550 HInstruction* right,
3551 uint32_t dex_pc = kNoDexPc)
3552 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003553
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003554 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003555
Roland Levillain9867bc72015-08-05 10:21:34 +01003556 template <typename T> T Compute(T x, T y) const { return x * y; }
3557
3558 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003559 return GetBlock()->GetGraph()->GetIntConstant(
3560 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003561 }
3562 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003563 return GetBlock()->GetGraph()->GetLongConstant(
3564 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003565 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003566
3567 DECLARE_INSTRUCTION(Mul);
3568
3569 private:
3570 DISALLOW_COPY_AND_ASSIGN(HMul);
3571};
3572
Calin Juravle7c4954d2014-10-28 16:57:40 +00003573class HDiv : public HBinaryOperation {
3574 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003575 HDiv(Primitive::Type result_type,
3576 HInstruction* left,
3577 HInstruction* right,
3578 uint32_t dex_pc)
3579 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003580
Roland Levillain9867bc72015-08-05 10:21:34 +01003581 template <typename T>
3582 T Compute(T x, T y) const {
3583 // Our graph structure ensures we never have 0 for `y` during
3584 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003585 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003586 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003587 return (y == -1) ? -x : x / y;
3588 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003589
Roland Levillain9867bc72015-08-05 10:21:34 +01003590 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003591 return GetBlock()->GetGraph()->GetIntConstant(
3592 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003593 }
3594 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003595 return GetBlock()->GetGraph()->GetLongConstant(
3596 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003597 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003598
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003599 static SideEffects SideEffectsForArchRuntimeCalls() {
3600 // The generated code can use a runtime call.
3601 return SideEffects::CanTriggerGC();
3602 }
3603
Calin Juravle7c4954d2014-10-28 16:57:40 +00003604 DECLARE_INSTRUCTION(Div);
3605
3606 private:
3607 DISALLOW_COPY_AND_ASSIGN(HDiv);
3608};
3609
Calin Juravlebacfec32014-11-14 15:54:36 +00003610class HRem : public HBinaryOperation {
3611 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003612 HRem(Primitive::Type result_type,
3613 HInstruction* left,
3614 HInstruction* right,
3615 uint32_t dex_pc)
3616 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003617
Roland Levillain9867bc72015-08-05 10:21:34 +01003618 template <typename T>
3619 T Compute(T x, T y) const {
3620 // Our graph structure ensures we never have 0 for `y` during
3621 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003622 DCHECK_NE(y, 0);
3623 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3624 return (y == -1) ? 0 : x % y;
3625 }
3626
Roland Levillain9867bc72015-08-05 10:21:34 +01003627 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003628 return GetBlock()->GetGraph()->GetIntConstant(
3629 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003630 }
3631 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003632 return GetBlock()->GetGraph()->GetLongConstant(
3633 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003634 }
3635
Calin Juravlebacfec32014-11-14 15:54:36 +00003636
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003637 static SideEffects SideEffectsForArchRuntimeCalls() {
3638 return SideEffects::CanTriggerGC();
3639 }
3640
Calin Juravlebacfec32014-11-14 15:54:36 +00003641 DECLARE_INSTRUCTION(Rem);
3642
3643 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003644 DISALLOW_COPY_AND_ASSIGN(HRem);
3645};
3646
Calin Juravled0d48522014-11-04 16:40:20 +00003647class HDivZeroCheck : public HExpression<1> {
3648 public:
3649 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003650 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003651 SetRawInputAt(0, value);
3652 }
3653
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003654 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3655
Calin Juravled0d48522014-11-04 16:40:20 +00003656 bool CanBeMoved() const OVERRIDE { return true; }
3657
3658 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3659 UNUSED(other);
3660 return true;
3661 }
3662
3663 bool NeedsEnvironment() const OVERRIDE { return true; }
3664 bool CanThrow() const OVERRIDE { return true; }
3665
Calin Juravled0d48522014-11-04 16:40:20 +00003666 DECLARE_INSTRUCTION(DivZeroCheck);
3667
3668 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003669 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3670};
3671
Calin Juravle9aec02f2014-11-18 23:06:35 +00003672class HShl : public HBinaryOperation {
3673 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003674 HShl(Primitive::Type result_type,
3675 HInstruction* left,
3676 HInstruction* right,
3677 uint32_t dex_pc = kNoDexPc)
3678 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003679
Roland Levillain9867bc72015-08-05 10:21:34 +01003680 template <typename T, typename U, typename V>
3681 T Compute(T x, U y, V max_shift_value) const {
3682 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3683 "V is not the unsigned integer type corresponding to T");
3684 return x << (y & max_shift_value);
3685 }
3686
3687 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3688 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003689 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003690 }
3691 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3692 // case is handled as `x << static_cast<int>(y)`.
3693 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3694 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003695 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003696 }
3697 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3698 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003699 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003700 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003701
3702 DECLARE_INSTRUCTION(Shl);
3703
3704 private:
3705 DISALLOW_COPY_AND_ASSIGN(HShl);
3706};
3707
3708class HShr : public HBinaryOperation {
3709 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003710 HShr(Primitive::Type result_type,
3711 HInstruction* left,
3712 HInstruction* right,
3713 uint32_t dex_pc = kNoDexPc)
3714 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003715
Roland Levillain9867bc72015-08-05 10:21:34 +01003716 template <typename T, typename U, typename V>
3717 T Compute(T x, U y, V max_shift_value) const {
3718 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3719 "V is not the unsigned integer type corresponding to T");
3720 return x >> (y & max_shift_value);
3721 }
3722
3723 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3724 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003725 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003726 }
3727 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3728 // case is handled as `x >> static_cast<int>(y)`.
3729 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3730 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003731 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003732 }
3733 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3734 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003735 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003736 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003737
3738 DECLARE_INSTRUCTION(Shr);
3739
3740 private:
3741 DISALLOW_COPY_AND_ASSIGN(HShr);
3742};
3743
3744class HUShr : public HBinaryOperation {
3745 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003746 HUShr(Primitive::Type result_type,
3747 HInstruction* left,
3748 HInstruction* right,
3749 uint32_t dex_pc = kNoDexPc)
3750 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003751
Roland Levillain9867bc72015-08-05 10:21:34 +01003752 template <typename T, typename U, typename V>
3753 T Compute(T x, U y, V max_shift_value) const {
3754 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3755 "V is not the unsigned integer type corresponding to T");
3756 V ux = static_cast<V>(x);
3757 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003758 }
3759
Roland Levillain9867bc72015-08-05 10:21:34 +01003760 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3761 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003762 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003763 }
3764 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3765 // case is handled as `x >>> static_cast<int>(y)`.
3766 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3767 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003768 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003769 }
3770 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3771 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003772 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003773 }
3774
3775 DECLARE_INSTRUCTION(UShr);
3776
3777 private:
3778 DISALLOW_COPY_AND_ASSIGN(HUShr);
3779};
3780
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003781class HAnd : public HBinaryOperation {
3782 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003783 HAnd(Primitive::Type result_type,
3784 HInstruction* left,
3785 HInstruction* right,
3786 uint32_t dex_pc = kNoDexPc)
3787 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003788
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003789 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003790
Roland Levillain9867bc72015-08-05 10:21:34 +01003791 template <typename T, typename U>
3792 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3793
3794 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003795 return GetBlock()->GetGraph()->GetIntConstant(
3796 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003797 }
3798 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003799 return GetBlock()->GetGraph()->GetLongConstant(
3800 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003801 }
3802 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003803 return GetBlock()->GetGraph()->GetLongConstant(
3804 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003805 }
3806 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003807 return GetBlock()->GetGraph()->GetLongConstant(
3808 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003809 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003810
3811 DECLARE_INSTRUCTION(And);
3812
3813 private:
3814 DISALLOW_COPY_AND_ASSIGN(HAnd);
3815};
3816
3817class HOr : public HBinaryOperation {
3818 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003819 HOr(Primitive::Type result_type,
3820 HInstruction* left,
3821 HInstruction* right,
3822 uint32_t dex_pc = kNoDexPc)
3823 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003824
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003825 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003826
Roland Levillain9867bc72015-08-05 10:21:34 +01003827 template <typename T, typename U>
3828 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3829
3830 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003831 return GetBlock()->GetGraph()->GetIntConstant(
3832 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003833 }
3834 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003835 return GetBlock()->GetGraph()->GetLongConstant(
3836 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003837 }
3838 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003839 return GetBlock()->GetGraph()->GetLongConstant(
3840 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003841 }
3842 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003843 return GetBlock()->GetGraph()->GetLongConstant(
3844 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003845 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003846
3847 DECLARE_INSTRUCTION(Or);
3848
3849 private:
3850 DISALLOW_COPY_AND_ASSIGN(HOr);
3851};
3852
3853class HXor : public HBinaryOperation {
3854 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003855 HXor(Primitive::Type result_type,
3856 HInstruction* left,
3857 HInstruction* right,
3858 uint32_t dex_pc = kNoDexPc)
3859 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003860
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003861 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003862
Roland Levillain9867bc72015-08-05 10:21:34 +01003863 template <typename T, typename U>
3864 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3865
3866 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003867 return GetBlock()->GetGraph()->GetIntConstant(
3868 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003869 }
3870 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003871 return GetBlock()->GetGraph()->GetLongConstant(
3872 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003873 }
3874 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003875 return GetBlock()->GetGraph()->GetLongConstant(
3876 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003877 }
3878 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003879 return GetBlock()->GetGraph()->GetLongConstant(
3880 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003881 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003882
3883 DECLARE_INSTRUCTION(Xor);
3884
3885 private:
3886 DISALLOW_COPY_AND_ASSIGN(HXor);
3887};
3888
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003889// The value of a parameter in this method. Its location depends on
3890// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003891class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003892 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003893 HParameterValue(uint8_t index,
3894 Primitive::Type parameter_type,
3895 bool is_this = false)
3896 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003897 index_(index),
3898 is_this_(is_this),
3899 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003900
3901 uint8_t GetIndex() const { return index_; }
3902
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003903 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3904 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003905
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003906 bool IsThis() const { return is_this_; }
3907
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003908 DECLARE_INSTRUCTION(ParameterValue);
3909
3910 private:
3911 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003912 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003913 const uint8_t index_;
3914
Calin Juravle10e244f2015-01-26 18:54:32 +00003915 // Whether or not the parameter value corresponds to 'this' argument.
3916 const bool is_this_;
3917
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003918 bool can_be_null_;
3919
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003920 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3921};
3922
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003923class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003924 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003925 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3926 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003928 bool CanBeMoved() const OVERRIDE { return true; }
3929 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003930 UNUSED(other);
3931 return true;
3932 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003933
Roland Levillain9867bc72015-08-05 10:21:34 +01003934 template <typename T> T Compute(T x) const { return ~x; }
3935
3936 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003937 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003938 }
3939 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003940 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003941 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003942
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003943 DECLARE_INSTRUCTION(Not);
3944
3945 private:
3946 DISALLOW_COPY_AND_ASSIGN(HNot);
3947};
3948
David Brazdil66d126e2015-04-03 16:02:44 +01003949class HBooleanNot : public HUnaryOperation {
3950 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003951 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3952 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003953
3954 bool CanBeMoved() const OVERRIDE { return true; }
3955 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3956 UNUSED(other);
3957 return true;
3958 }
3959
Roland Levillain9867bc72015-08-05 10:21:34 +01003960 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003961 DCHECK(IsUint<1>(x));
3962 return !x;
3963 }
3964
Roland Levillain9867bc72015-08-05 10:21:34 +01003965 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003966 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003967 }
3968 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3969 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003970 UNREACHABLE();
3971 }
3972
3973 DECLARE_INSTRUCTION(BooleanNot);
3974
3975 private:
3976 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3977};
3978
Roland Levillaindff1f282014-11-05 14:15:05 +00003979class HTypeConversion : public HExpression<1> {
3980 public:
3981 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003982 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003983 : HExpression(result_type,
3984 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
3985 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003986 SetRawInputAt(0, input);
3987 DCHECK_NE(input->GetType(), result_type);
3988 }
3989
3990 HInstruction* GetInput() const { return InputAt(0); }
3991 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3992 Primitive::Type GetResultType() const { return GetType(); }
3993
Roland Levillain624279f2014-12-04 11:54:28 +00003994 // Required by the x86 and ARM code generators when producing calls
3995 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003996
Roland Levillaindff1f282014-11-05 14:15:05 +00003997 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003998 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003999
Mark Mendelle82549b2015-05-06 10:55:34 -04004000 // Try to statically evaluate the conversion and return a HConstant
4001 // containing the result. If the input cannot be converted, return nullptr.
4002 HConstant* TryStaticEvaluation() const;
4003
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004004 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4005 Primitive::Type result_type) {
4006 // Some architectures may not require the 'GC' side effects, but at this point
4007 // in the compilation process we do not know what architecture we will
4008 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004009 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4010 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004011 return SideEffects::CanTriggerGC();
4012 }
4013 return SideEffects::None();
4014 }
4015
Roland Levillaindff1f282014-11-05 14:15:05 +00004016 DECLARE_INSTRUCTION(TypeConversion);
4017
4018 private:
4019 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4020};
4021
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004022static constexpr uint32_t kNoRegNumber = -1;
4023
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004024class HPhi : public HInstruction {
4025 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004026 HPhi(ArenaAllocator* arena,
4027 uint32_t reg_number,
4028 size_t number_of_inputs,
4029 Primitive::Type type,
4030 uint32_t dex_pc = kNoDexPc)
4031 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004032 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004033 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004034 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004035 is_live_(false),
4036 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004037 }
4038
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004039 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4040 static Primitive::Type ToPhiType(Primitive::Type type) {
4041 switch (type) {
4042 case Primitive::kPrimBoolean:
4043 case Primitive::kPrimByte:
4044 case Primitive::kPrimShort:
4045 case Primitive::kPrimChar:
4046 return Primitive::kPrimInt;
4047 default:
4048 return type;
4049 }
4050 }
4051
David Brazdilffee3d32015-07-06 11:48:53 +01004052 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4053
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004054 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004055
4056 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004057 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004058
Calin Juravle10e244f2015-01-26 18:54:32 +00004059 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004060 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004061
Calin Juravle10e244f2015-01-26 18:54:32 +00004062 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4063 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4064
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004065 uint32_t GetRegNumber() const { return reg_number_; }
4066
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004067 void SetDead() { is_live_ = false; }
4068 void SetLive() { is_live_ = true; }
4069 bool IsDead() const { return !is_live_; }
4070 bool IsLive() const { return is_live_; }
4071
David Brazdil77a48ae2015-09-15 12:34:04 +00004072 bool IsVRegEquivalentOf(HInstruction* other) const {
4073 return other != nullptr
4074 && other->IsPhi()
4075 && other->AsPhi()->GetBlock() == GetBlock()
4076 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4077 }
4078
Calin Juravlea4f88312015-04-16 12:57:19 +01004079 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4080 // An equivalent phi is a phi having the same dex register and type.
4081 // It assumes that phis with the same dex register are adjacent.
4082 HPhi* GetNextEquivalentPhiWithSameType() {
4083 HInstruction* next = GetNext();
4084 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4085 if (next->GetType() == GetType()) {
4086 return next->AsPhi();
4087 }
4088 next = next->GetNext();
4089 }
4090 return nullptr;
4091 }
4092
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004093 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004094
David Brazdil1abb4192015-02-17 18:33:36 +00004095 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004096 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
4097 DCHECK_LE(index, InputCount());
4098 return inputs_[index];
4099 }
David Brazdil1abb4192015-02-17 18:33:36 +00004100
4101 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004102 DCHECK_LE(index, InputCount());
4103 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004104 }
4105
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004106 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004107 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004108 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004109 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004110 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004111 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004112
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004113 DISALLOW_COPY_AND_ASSIGN(HPhi);
4114};
4115
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004116class HNullCheck : public HExpression<1> {
4117 public:
4118 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004119 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004120 SetRawInputAt(0, value);
4121 }
4122
Calin Juravle10e244f2015-01-26 18:54:32 +00004123 bool CanBeMoved() const OVERRIDE { return true; }
4124 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004125 UNUSED(other);
4126 return true;
4127 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004128
Calin Juravle10e244f2015-01-26 18:54:32 +00004129 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004130
Calin Juravle10e244f2015-01-26 18:54:32 +00004131 bool CanThrow() const OVERRIDE { return true; }
4132
4133 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004134
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004135
4136 DECLARE_INSTRUCTION(NullCheck);
4137
4138 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004139 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4140};
4141
4142class FieldInfo : public ValueObject {
4143 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004144 FieldInfo(MemberOffset field_offset,
4145 Primitive::Type field_type,
4146 bool is_volatile,
4147 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004148 const DexFile& dex_file,
4149 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004150 : field_offset_(field_offset),
4151 field_type_(field_type),
4152 is_volatile_(is_volatile),
4153 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004154 dex_file_(dex_file),
4155 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004156
4157 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004158 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004159 uint32_t GetFieldIndex() const { return index_; }
4160 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004161 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004162 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004163
4164 private:
4165 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004166 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004167 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004168 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004169 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004170 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004171};
4172
4173class HInstanceFieldGet : public HExpression<1> {
4174 public:
4175 HInstanceFieldGet(HInstruction* value,
4176 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004177 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004178 bool is_volatile,
4179 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004180 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004181 Handle<mirror::DexCache> dex_cache,
4182 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004183 : HExpression(
4184 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004185 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004186 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004187 SetRawInputAt(0, value);
4188 }
4189
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004190 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004191
4192 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4193 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4194 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004195 }
4196
Calin Juravle641547a2015-04-21 22:08:51 +01004197 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4198 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004199 }
4200
4201 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004202 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4203 }
4204
Calin Juravle52c48962014-12-16 17:02:57 +00004205 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004206 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004207 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004208 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004209
4210 DECLARE_INSTRUCTION(InstanceFieldGet);
4211
4212 private:
4213 const FieldInfo field_info_;
4214
4215 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4216};
4217
4218class HInstanceFieldSet : public HTemplateInstruction<2> {
4219 public:
4220 HInstanceFieldSet(HInstruction* object,
4221 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004222 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004223 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004224 bool is_volatile,
4225 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004226 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004227 Handle<mirror::DexCache> dex_cache,
4228 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004229 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004230 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004231 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004232 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004233 SetRawInputAt(0, object);
4234 SetRawInputAt(1, value);
4235 }
4236
Calin Juravle641547a2015-04-21 22:08:51 +01004237 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4238 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004239 }
4240
Calin Juravle52c48962014-12-16 17:02:57 +00004241 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004242 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004243 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004244 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004245 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004246 bool GetValueCanBeNull() const { return value_can_be_null_; }
4247 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004248
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004249 DECLARE_INSTRUCTION(InstanceFieldSet);
4250
4251 private:
4252 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004253 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004254
4255 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4256};
4257
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004258class HArrayGet : public HExpression<2> {
4259 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004260 HArrayGet(HInstruction* array,
4261 HInstruction* index,
4262 Primitive::Type type,
4263 uint32_t dex_pc = kNoDexPc)
4264 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004265 SetRawInputAt(0, array);
4266 SetRawInputAt(1, index);
4267 }
4268
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004269 bool CanBeMoved() const OVERRIDE { return true; }
4270 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004271 UNUSED(other);
4272 return true;
4273 }
Calin Juravle641547a2015-04-21 22:08:51 +01004274 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4275 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004276 // TODO: We can be smarter here.
4277 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4278 // which generates the implicit null check. There are cases when these can be removed
4279 // to produce better code. If we ever add optimizations to do so we should allow an
4280 // implicit check here (as long as the address falls in the first page).
4281 return false;
4282 }
4283
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004284 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004285
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004286 HInstruction* GetArray() const { return InputAt(0); }
4287 HInstruction* GetIndex() const { return InputAt(1); }
4288
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004289 DECLARE_INSTRUCTION(ArrayGet);
4290
4291 private:
4292 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4293};
4294
4295class HArraySet : public HTemplateInstruction<3> {
4296 public:
4297 HArraySet(HInstruction* array,
4298 HInstruction* index,
4299 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004300 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004301 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004302 : HTemplateInstruction(
4303 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004304 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004305 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004306 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4307 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004308 SetRawInputAt(0, array);
4309 SetRawInputAt(1, index);
4310 SetRawInputAt(2, value);
4311 }
4312
Calin Juravle77520bc2015-01-12 18:45:46 +00004313 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004314 // We currently always call a runtime method to catch array store
4315 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004316 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004317 }
4318
Mingyao Yang81014cb2015-06-02 03:16:27 -07004319 // Can throw ArrayStoreException.
4320 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4321
Calin Juravle641547a2015-04-21 22:08:51 +01004322 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4323 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004324 // TODO: Same as for ArrayGet.
4325 return false;
4326 }
4327
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004328 void ClearNeedsTypeCheck() {
4329 needs_type_check_ = false;
4330 }
4331
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004332 void ClearValueCanBeNull() {
4333 value_can_be_null_ = false;
4334 }
4335
4336 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004337 bool NeedsTypeCheck() const { return needs_type_check_; }
4338
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004339 HInstruction* GetArray() const { return InputAt(0); }
4340 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004341 HInstruction* GetValue() const { return InputAt(2); }
4342
4343 Primitive::Type GetComponentType() const {
4344 // The Dex format does not type floating point index operations. Since the
4345 // `expected_component_type_` is set during building and can therefore not
4346 // be correct, we also check what is the value type. If it is a floating
4347 // point type, we must use that type.
4348 Primitive::Type value_type = GetValue()->GetType();
4349 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4350 ? value_type
4351 : expected_component_type_;
4352 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004353
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004354 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4355 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4356 }
4357
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004358 DECLARE_INSTRUCTION(ArraySet);
4359
4360 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004361 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004362 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004363 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004364
4365 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4366};
4367
4368class HArrayLength : public HExpression<1> {
4369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004370 explicit HArrayLength(HInstruction* array, uint32_t dex_pc = kNoDexPc)
4371 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004372 // Note that arrays do not change length, so the instruction does not
4373 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004374 SetRawInputAt(0, array);
4375 }
4376
Calin Juravle77520bc2015-01-12 18:45:46 +00004377 bool CanBeMoved() const OVERRIDE { return true; }
4378 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004379 UNUSED(other);
4380 return true;
4381 }
Calin Juravle641547a2015-04-21 22:08:51 +01004382 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4383 return obj == InputAt(0);
4384 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004385
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004386 DECLARE_INSTRUCTION(ArrayLength);
4387
4388 private:
4389 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4390};
4391
4392class HBoundsCheck : public HExpression<2> {
4393 public:
4394 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004395 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004396 DCHECK(index->GetType() == Primitive::kPrimInt);
4397 SetRawInputAt(0, index);
4398 SetRawInputAt(1, length);
4399 }
4400
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004401 bool CanBeMoved() const OVERRIDE { return true; }
4402 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004403 UNUSED(other);
4404 return true;
4405 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004406
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004407 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004408
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004409 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004410
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004411
4412 DECLARE_INSTRUCTION(BoundsCheck);
4413
4414 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004415 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4416};
4417
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004418/**
4419 * Some DEX instructions are folded into multiple HInstructions that need
4420 * to stay live until the last HInstruction. This class
4421 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004422 * HInstruction stays live. `index` represents the stack location index of the
4423 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004424 */
4425class HTemporary : public HTemplateInstruction<0> {
4426 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004427 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4428 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004429
4430 size_t GetIndex() const { return index_; }
4431
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004432 Primitive::Type GetType() const OVERRIDE {
4433 // The previous instruction is the one that will be stored in the temporary location.
4434 DCHECK(GetPrevious() != nullptr);
4435 return GetPrevious()->GetType();
4436 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004437
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004438 DECLARE_INSTRUCTION(Temporary);
4439
4440 private:
4441 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004442 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4443};
4444
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004445class HSuspendCheck : public HTemplateInstruction<0> {
4446 public:
4447 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004448 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004449
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004450 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004451 return true;
4452 }
4453
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004454 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4455 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004456
4457 DECLARE_INSTRUCTION(SuspendCheck);
4458
4459 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004460 // Only used for code generation, in order to share the same slow path between back edges
4461 // of a same loop.
4462 SlowPathCode* slow_path_;
4463
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004464 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4465};
4466
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004467/**
4468 * Instruction to load a Class object.
4469 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004470class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004471 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004472 HLoadClass(HCurrentMethod* current_method,
4473 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004474 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004475 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004476 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004477 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004478 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004479 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004480 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004481 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004482 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004483 SetRawInputAt(0, current_method);
4484 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004485
4486 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004487
4488 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4489 return other->AsLoadClass()->type_index_ == type_index_;
4490 }
4491
4492 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4493
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004494 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004495 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004496 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004497
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004498 bool NeedsEnvironment() const OVERRIDE {
4499 // Will call runtime and load the class if the class is not loaded yet.
4500 // TODO: finer grain decision.
4501 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004502 }
4503
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004504 bool MustGenerateClinitCheck() const {
4505 return generate_clinit_check_;
4506 }
4507
Calin Juravle0ba218d2015-05-19 18:46:01 +01004508 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4509 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004510 }
4511
4512 bool CanCallRuntime() const {
4513 return MustGenerateClinitCheck() || !is_referrers_class_;
4514 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004515
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004516 bool CanThrow() const OVERRIDE {
4517 // May call runtime and and therefore can throw.
4518 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004519 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004520 }
4521
Calin Juravleacf735c2015-02-12 15:25:22 +00004522 ReferenceTypeInfo GetLoadedClassRTI() {
4523 return loaded_class_rti_;
4524 }
4525
4526 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4527 // Make sure we only set exact types (the loaded class should never be merged).
4528 DCHECK(rti.IsExact());
4529 loaded_class_rti_ = rti;
4530 }
4531
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004532 const DexFile& GetDexFile() { return dex_file_; }
4533
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004534 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4535
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004536 static SideEffects SideEffectsForArchRuntimeCalls() {
4537 return SideEffects::CanTriggerGC();
4538 }
4539
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004540 DECLARE_INSTRUCTION(LoadClass);
4541
4542 private:
4543 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004544 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004545 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004546 // Whether this instruction must generate the initialization check.
4547 // Used for code generation.
4548 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004549
Calin Juravleacf735c2015-02-12 15:25:22 +00004550 ReferenceTypeInfo loaded_class_rti_;
4551
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004552 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4553};
4554
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004555class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004556 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004557 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004558 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4559 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004560 SetRawInputAt(0, current_method);
4561 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004562
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004563 bool CanBeMoved() const OVERRIDE { return true; }
4564
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004565 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4566 return other->AsLoadString()->string_index_ == string_index_;
4567 }
4568
4569 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4570
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004571 uint32_t GetStringIndex() const { return string_index_; }
4572
4573 // TODO: Can we deopt or debug when we resolve a string?
4574 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004575 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004576 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004577
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004578 static SideEffects SideEffectsForArchRuntimeCalls() {
4579 return SideEffects::CanTriggerGC();
4580 }
4581
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004582 DECLARE_INSTRUCTION(LoadString);
4583
4584 private:
4585 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004586
4587 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4588};
4589
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004590/**
4591 * Performs an initialization check on its Class object input.
4592 */
4593class HClinitCheck : public HExpression<1> {
4594 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004595 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004596 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004597 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004598 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4599 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004600 SetRawInputAt(0, constant);
4601 }
4602
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004603 bool CanBeMoved() const OVERRIDE { return true; }
4604 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4605 UNUSED(other);
4606 return true;
4607 }
4608
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004609 bool NeedsEnvironment() const OVERRIDE {
4610 // May call runtime to initialize the class.
4611 return true;
4612 }
4613
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004614
4615 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4616
4617 DECLARE_INSTRUCTION(ClinitCheck);
4618
4619 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004620 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4621};
4622
4623class HStaticFieldGet : public HExpression<1> {
4624 public:
4625 HStaticFieldGet(HInstruction* cls,
4626 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004627 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004628 bool is_volatile,
4629 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004630 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004631 Handle<mirror::DexCache> dex_cache,
4632 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004633 : HExpression(
4634 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004635 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004636 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004637 SetRawInputAt(0, cls);
4638 }
4639
Calin Juravle52c48962014-12-16 17:02:57 +00004640
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004641 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004642
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004643 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004644 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4645 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004646 }
4647
4648 size_t ComputeHashCode() const OVERRIDE {
4649 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4650 }
4651
Calin Juravle52c48962014-12-16 17:02:57 +00004652 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004653 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4654 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004655 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004656
4657 DECLARE_INSTRUCTION(StaticFieldGet);
4658
4659 private:
4660 const FieldInfo field_info_;
4661
4662 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4663};
4664
4665class HStaticFieldSet : public HTemplateInstruction<2> {
4666 public:
4667 HStaticFieldSet(HInstruction* cls,
4668 HInstruction* value,
4669 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004670 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004671 bool is_volatile,
4672 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004673 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004674 Handle<mirror::DexCache> dex_cache,
4675 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004676 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004677 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004678 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004679 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004680 SetRawInputAt(0, cls);
4681 SetRawInputAt(1, value);
4682 }
4683
Calin Juravle52c48962014-12-16 17:02:57 +00004684 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004685 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4686 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004687 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004688
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004689 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004690 bool GetValueCanBeNull() const { return value_can_be_null_; }
4691 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004692
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004693 DECLARE_INSTRUCTION(StaticFieldSet);
4694
4695 private:
4696 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004697 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004698
4699 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4700};
4701
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004702// Implement the move-exception DEX instruction.
4703class HLoadException : public HExpression<0> {
4704 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004705 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4706 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004707
David Brazdilbbd733e2015-08-18 17:48:17 +01004708 bool CanBeNull() const OVERRIDE { return false; }
4709
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004710 DECLARE_INSTRUCTION(LoadException);
4711
4712 private:
4713 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4714};
4715
David Brazdilcb1c0552015-08-04 16:22:25 +01004716// Implicit part of move-exception which clears thread-local exception storage.
4717// Must not be removed because the runtime expects the TLS to get cleared.
4718class HClearException : public HTemplateInstruction<0> {
4719 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004720 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4721 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004722
4723 DECLARE_INSTRUCTION(ClearException);
4724
4725 private:
4726 DISALLOW_COPY_AND_ASSIGN(HClearException);
4727};
4728
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004729class HThrow : public HTemplateInstruction<1> {
4730 public:
4731 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004732 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004733 SetRawInputAt(0, exception);
4734 }
4735
4736 bool IsControlFlow() const OVERRIDE { return true; }
4737
4738 bool NeedsEnvironment() const OVERRIDE { return true; }
4739
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004740 bool CanThrow() const OVERRIDE { return true; }
4741
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004742
4743 DECLARE_INSTRUCTION(Throw);
4744
4745 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004746 DISALLOW_COPY_AND_ASSIGN(HThrow);
4747};
4748
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004749/**
4750 * Implementation strategies for the code generator of a HInstanceOf
4751 * or `HCheckCast`.
4752 */
4753enum class TypeCheckKind {
4754 kExactCheck, // Can do a single class compare.
4755 kClassHierarchyCheck, // Can just walk the super class chain.
4756 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
4757 kInterfaceCheck, // No optimization yet when checking against an interface.
4758 kArrayObjectCheck, // Can just check if the array is not primitive.
4759 kArrayCheck // No optimization yet when checking against a generic array.
4760};
4761
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004762class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004763 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004764 HInstanceOf(HInstruction* object,
4765 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004766 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004767 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004768 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004769 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004770 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004771 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004772 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004773 SetRawInputAt(0, object);
4774 SetRawInputAt(1, constant);
4775 }
4776
4777 bool CanBeMoved() const OVERRIDE { return true; }
4778
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004779 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004780 return true;
4781 }
4782
4783 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004784 return false;
4785 }
4786
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004787 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
4788
4789 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004790
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004791 // Used only in code generation.
4792 bool MustDoNullCheck() const { return must_do_null_check_; }
4793 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4794
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004795 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
4796 return (check_kind == TypeCheckKind::kExactCheck)
4797 ? SideEffects::None()
4798 // Mips currently does runtime calls for any other checks.
4799 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004800 }
4801
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004802 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004803
4804 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004805 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004806 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004807
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004808 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4809};
4810
Calin Juravleb1498f62015-02-16 13:13:29 +00004811class HBoundType : public HExpression<1> {
4812 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004813 // Constructs an HBoundType with the given upper_bound.
4814 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004815 HBoundType(HInstruction* input,
4816 ReferenceTypeInfo upper_bound,
4817 bool upper_can_be_null,
4818 uint32_t dex_pc = kNoDexPc)
4819 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004820 upper_bound_(upper_bound),
4821 upper_can_be_null_(upper_can_be_null),
4822 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004823 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004824 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004825 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004826 }
4827
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004828 // GetUpper* should only be used in reference type propagation.
4829 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4830 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004831
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004832 void SetCanBeNull(bool can_be_null) {
4833 DCHECK(upper_can_be_null_ || !can_be_null);
4834 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004835 }
4836
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004837 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4838
Calin Juravleb1498f62015-02-16 13:13:29 +00004839 DECLARE_INSTRUCTION(BoundType);
4840
4841 private:
4842 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004843 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4844 // It is used to bound the type in cases like:
4845 // if (x instanceof ClassX) {
4846 // // uper_bound_ will be ClassX
4847 // }
4848 const ReferenceTypeInfo upper_bound_;
4849 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4850 // is false then can_be_null_ cannot be true).
4851 const bool upper_can_be_null_;
4852 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004853
4854 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4855};
4856
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004857class HCheckCast : public HTemplateInstruction<2> {
4858 public:
4859 HCheckCast(HInstruction* object,
4860 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004861 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004862 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004863 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004864 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004865 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004866 SetRawInputAt(0, object);
4867 SetRawInputAt(1, constant);
4868 }
4869
4870 bool CanBeMoved() const OVERRIDE { return true; }
4871
4872 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4873 return true;
4874 }
4875
4876 bool NeedsEnvironment() const OVERRIDE {
4877 // Instruction may throw a CheckCastError.
4878 return true;
4879 }
4880
4881 bool CanThrow() const OVERRIDE { return true; }
4882
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004883 bool MustDoNullCheck() const { return must_do_null_check_; }
4884 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004885 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004886
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004887 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004888
4889 DECLARE_INSTRUCTION(CheckCast);
4890
4891 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004892 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004893 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004894
4895 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004896};
4897
Calin Juravle27df7582015-04-17 19:12:31 +01004898class HMemoryBarrier : public HTemplateInstruction<0> {
4899 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004900 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004901 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004902 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004903 barrier_kind_(barrier_kind) {}
4904
4905 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4906
4907 DECLARE_INSTRUCTION(MemoryBarrier);
4908
4909 private:
4910 const MemBarrierKind barrier_kind_;
4911
4912 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4913};
4914
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004915class HMonitorOperation : public HTemplateInstruction<1> {
4916 public:
4917 enum OperationKind {
4918 kEnter,
4919 kExit,
4920 };
4921
4922 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004923 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004924 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
4925 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004926 SetRawInputAt(0, object);
4927 }
4928
4929 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004930 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4931
4932 bool CanThrow() const OVERRIDE {
4933 // Verifier guarantees that monitor-exit cannot throw.
4934 // This is important because it allows the HGraphBuilder to remove
4935 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4936 return IsEnter();
4937 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004938
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004939
4940 bool IsEnter() const { return kind_ == kEnter; }
4941
4942 DECLARE_INSTRUCTION(MonitorOperation);
4943
Calin Juravle52c48962014-12-16 17:02:57 +00004944 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004945 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004946
4947 private:
4948 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4949};
4950
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004951/**
4952 * A HInstruction used as a marker for the replacement of new + <init>
4953 * of a String to a call to a StringFactory. Only baseline will see
4954 * the node at code generation, where it will be be treated as null.
4955 * When compiling non-baseline, `HFakeString` instructions are being removed
4956 * in the instruction simplifier.
4957 */
4958class HFakeString : public HTemplateInstruction<0> {
4959 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004960 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
4961 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004962
4963 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4964
4965 DECLARE_INSTRUCTION(FakeString);
4966
4967 private:
4968 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4969};
4970
Vladimir Markof9f64412015-09-02 14:05:49 +01004971class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004972 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004973 MoveOperands(Location source,
4974 Location destination,
4975 Primitive::Type type,
4976 HInstruction* instruction)
4977 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004978
4979 Location GetSource() const { return source_; }
4980 Location GetDestination() const { return destination_; }
4981
4982 void SetSource(Location value) { source_ = value; }
4983 void SetDestination(Location value) { destination_ = value; }
4984
4985 // The parallel move resolver marks moves as "in-progress" by clearing the
4986 // destination (but not the source).
4987 Location MarkPending() {
4988 DCHECK(!IsPending());
4989 Location dest = destination_;
4990 destination_ = Location::NoLocation();
4991 return dest;
4992 }
4993
4994 void ClearPending(Location dest) {
4995 DCHECK(IsPending());
4996 destination_ = dest;
4997 }
4998
4999 bool IsPending() const {
5000 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5001 return destination_.IsInvalid() && !source_.IsInvalid();
5002 }
5003
5004 // True if this blocks a move from the given location.
5005 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005006 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005007 }
5008
5009 // A move is redundant if it's been eliminated, if its source and
5010 // destination are the same, or if its destination is unneeded.
5011 bool IsRedundant() const {
5012 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5013 }
5014
5015 // We clear both operands to indicate move that's been eliminated.
5016 void Eliminate() {
5017 source_ = destination_ = Location::NoLocation();
5018 }
5019
5020 bool IsEliminated() const {
5021 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5022 return source_.IsInvalid();
5023 }
5024
Alexey Frunze4dda3372015-06-01 18:31:49 -07005025 Primitive::Type GetType() const { return type_; }
5026
Nicolas Geoffray90218252015-04-15 11:56:51 +01005027 bool Is64BitMove() const {
5028 return Primitive::Is64BitType(type_);
5029 }
5030
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005031 HInstruction* GetInstruction() const { return instruction_; }
5032
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005033 private:
5034 Location source_;
5035 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005036 // The type this move is for.
5037 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005038 // The instruction this move is assocatied with. Null when this move is
5039 // for moving an input in the expected locations of user (including a phi user).
5040 // This is only used in debug mode, to ensure we do not connect interval siblings
5041 // in the same parallel move.
5042 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005043};
5044
5045static constexpr size_t kDefaultNumberOfMoves = 4;
5046
5047class HParallelMove : public HTemplateInstruction<0> {
5048 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005049 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
5050 : HTemplateInstruction(SideEffects::None(), dex_pc), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005051
Nicolas Geoffray90218252015-04-15 11:56:51 +01005052 void AddMove(Location source,
5053 Location destination,
5054 Primitive::Type type,
5055 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005056 DCHECK(source.IsValid());
5057 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005058 if (kIsDebugBuild) {
5059 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005060 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005061 if (moves_.Get(i).GetInstruction() == instruction) {
5062 // Special case the situation where the move is for the spill slot
5063 // of the instruction.
5064 if ((GetPrevious() == instruction)
5065 || ((GetPrevious() == nullptr)
5066 && instruction->IsPhi()
5067 && instruction->GetBlock() == GetBlock())) {
5068 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
5069 << "Doing parallel moves for the same instruction.";
5070 } else {
5071 DCHECK(false) << "Doing parallel moves for the same instruction.";
5072 }
5073 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005074 }
5075 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005076 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08005077 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005078 << "Overlapped destination for two moves in a parallel move: "
5079 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
5080 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005081 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005082 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01005083 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005084 }
5085
5086 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005087 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005088 }
5089
5090 size_t NumMoves() const { return moves_.Size(); }
5091
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005092 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005093
5094 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005095 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005096
5097 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5098};
5099
Mark Mendell0616ae02015-04-17 12:49:27 -04005100} // namespace art
5101
5102#ifdef ART_ENABLE_CODEGEN_x86
5103#include "nodes_x86.h"
5104#endif
5105
5106namespace art {
5107
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005108class HGraphVisitor : public ValueObject {
5109 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005110 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5111 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005112
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005113 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005114 virtual void VisitBasicBlock(HBasicBlock* block);
5115
Roland Levillain633021e2014-10-01 14:12:25 +01005116 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005117 void VisitInsertionOrder();
5118
Roland Levillain633021e2014-10-01 14:12:25 +01005119 // Visit the graph following dominator tree reverse post-order.
5120 void VisitReversePostOrder();
5121
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005122 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005123
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005124 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005125#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005126 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5127
5128 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5129
5130#undef DECLARE_VISIT_INSTRUCTION
5131
5132 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005133 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005134
5135 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5136};
5137
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005138class HGraphDelegateVisitor : public HGraphVisitor {
5139 public:
5140 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5141 virtual ~HGraphDelegateVisitor() {}
5142
5143 // Visit functions that delegate to to super class.
5144#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005145 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005146
5147 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5148
5149#undef DECLARE_VISIT_INSTRUCTION
5150
5151 private:
5152 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5153};
5154
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005155class HInsertionOrderIterator : public ValueObject {
5156 public:
5157 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5158
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005159 bool Done() const { return index_ == graph_.GetBlocks().size(); }
5160 HBasicBlock* Current() const { return graph_.GetBlock(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005161 void Advance() { ++index_; }
5162
5163 private:
5164 const HGraph& graph_;
5165 size_t index_;
5166
5167 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5168};
5169
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005170class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005171 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005172 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5173 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005174 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005175 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005176
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005177 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5178 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005179 void Advance() { ++index_; }
5180
5181 private:
5182 const HGraph& graph_;
5183 size_t index_;
5184
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005185 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005186};
5187
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005188class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005189 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005190 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005191 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005192 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005193 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005194 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005195
5196 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005197 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005198 void Advance() { --index_; }
5199
5200 private:
5201 const HGraph& graph_;
5202 size_t index_;
5203
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005204 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005205};
5206
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005207class HLinearPostOrderIterator : public ValueObject {
5208 public:
5209 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005210 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005211
5212 bool Done() const { return index_ == 0; }
5213
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005214 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005215
5216 void Advance() {
5217 --index_;
5218 DCHECK_GE(index_, 0U);
5219 }
5220
5221 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005222 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005223 size_t index_;
5224
5225 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5226};
5227
5228class HLinearOrderIterator : public ValueObject {
5229 public:
5230 explicit HLinearOrderIterator(const HGraph& graph)
5231 : order_(graph.GetLinearOrder()), index_(0) {}
5232
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005233 bool Done() const { return index_ == order_.size(); }
5234 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005235 void Advance() { ++index_; }
5236
5237 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005238 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005239 size_t index_;
5240
5241 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5242};
5243
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005244// Iterator over the blocks that art part of the loop. Includes blocks part
5245// of an inner loop. The order in which the blocks are iterated is on their
5246// block id.
5247class HBlocksInLoopIterator : public ValueObject {
5248 public:
5249 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5250 : blocks_in_loop_(info.GetBlocks()),
5251 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5252 index_(0) {
5253 if (!blocks_in_loop_.IsBitSet(index_)) {
5254 Advance();
5255 }
5256 }
5257
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005258 bool Done() const { return index_ == blocks_.size(); }
5259 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005260 void Advance() {
5261 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005262 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005263 if (blocks_in_loop_.IsBitSet(index_)) {
5264 break;
5265 }
5266 }
5267 }
5268
5269 private:
5270 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005271 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005272 size_t index_;
5273
5274 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5275};
5276
Mingyao Yang3584bce2015-05-19 16:01:59 -07005277// Iterator over the blocks that art part of the loop. Includes blocks part
5278// of an inner loop. The order in which the blocks are iterated is reverse
5279// post order.
5280class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5281 public:
5282 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5283 : blocks_in_loop_(info.GetBlocks()),
5284 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5285 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005286 DCHECK(!blocks_.empty());
5287 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005288 Advance();
5289 }
5290 }
5291
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005292 bool Done() const { return index_ == blocks_.size(); }
5293 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005294 void Advance() {
5295 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005296 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5297 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005298 break;
5299 }
5300 }
5301 }
5302
5303 private:
5304 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005305 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005306 size_t index_;
5307
5308 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5309};
5310
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005311inline int64_t Int64FromConstant(HConstant* constant) {
5312 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5313 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5314 : constant->AsLongConstant()->GetValue();
5315}
5316
Vladimir Marko58155012015-08-19 12:49:41 +00005317inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5318 // For the purposes of the compiler, the dex files must actually be the same object
5319 // if we want to safely treat them as the same. This is especially important for JIT
5320 // as custom class loaders can open the same underlying file (or memory) multiple
5321 // times and provide different class resolution but no two class loaders should ever
5322 // use the same DexFile object - doing so is an unsupported hack that can lead to
5323 // all sorts of weird failures.
5324 return &lhs == &rhs;
5325}
5326
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005327} // namespace art
5328
5329#endif // ART_COMPILER_OPTIMIZING_NODES_H_