blob: 26df2419a7e103b6da535e9ac282d4d9d4b57a27 [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
David Brazdild7558da2015-09-22 13:04:14 +0100822 // Create a new block between this block and its predecessors. The new block
823 // is added to the graph, all predecessor edges are relinked to it and an edge
824 // is created to `this`. Returns the new empty block. Reverse post order or
825 // loop and try/catch information are not updated.
826 HBasicBlock* CreateImmediateDominator();
827
David Brazdilfc6a86a2015-06-26 10:33:45 +0000828 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100829 // created, latter block. Note that this method will add the block to the
830 // graph, create a Goto at the end of the former block and will create an edge
831 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100832 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000833 HBasicBlock* SplitBefore(HInstruction* cursor);
834
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000835 // Split the block into two blocks just after `cursor`. Returns the newly
836 // created block. Note that this method just updates raw block information,
837 // like predecessors, successors, dominators, and instruction list. It does not
838 // update the graph, reverse post order, loop information, nor make sure the
839 // blocks are consistent (for example ending with a control flow instruction).
840 HBasicBlock* SplitAfter(HInstruction* cursor);
841
842 // Merge `other` at the end of `this`. Successors and dominated blocks of
843 // `other` are changed to be successors and dominated blocks of `this`. Note
844 // that this method does not update the graph, reverse post order, loop
845 // information, nor make sure the blocks are consistent (for example ending
846 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100847 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848
849 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
850 // of `this` are moved to `other`.
851 // Note that this method does not update the graph, reverse post order, loop
852 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000853 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000854 void ReplaceWith(HBasicBlock* other);
855
David Brazdil2d7352b2015-04-20 14:52:42 +0100856 // Merge `other` at the end of `this`. This method updates loops, reverse post
857 // order, links to predecessors, successors, dominators and deletes the block
858 // from the graph. The two blocks must be successive, i.e. `this` the only
859 // predecessor of `other` and vice versa.
860 void MergeWith(HBasicBlock* other);
861
862 // Disconnects `this` from all its predecessors, successors and dominator,
863 // removes it from all loops it is included in and eventually from the graph.
864 // The block must not dominate any other block. Predecessors and successors
865 // are safely updated.
866 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000867
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100869 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100870 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100871 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100872 // Replace instruction `initial` with `replacement` within this block.
873 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
874 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100875 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100876 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000877 // RemoveInstruction and RemovePhi delete a given instruction from the respective
878 // instruction list. With 'ensure_safety' set to true, it verifies that the
879 // instruction is not in use and removes it from the use lists of its inputs.
880 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
881 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100882 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100883
884 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100885 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100886 }
887
Roland Levillain6b879dd2014-09-22 17:13:44 +0100888 bool IsLoopPreHeaderFirstPredecessor() const {
889 DCHECK(IsLoopHeader());
Vladimir Marko60584552015-09-03 13:35:12 +0000890 return GetPredecessor(0) == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100891 }
892
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100893 HLoopInformation* GetLoopInformation() const {
894 return loop_information_;
895 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000896
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000897 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000899 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100900 void SetInLoop(HLoopInformation* info) {
901 if (IsLoopHeader()) {
902 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100903 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 loop_information_ = info;
905 } else if (loop_information_->Contains(*info->GetHeader())) {
906 // Block is currently part of an outer loop. Make it part of this inner loop.
907 // Note that a non loop header having a loop information means this loop information
908 // has already been populated
909 loop_information_ = info;
910 } else {
911 // Block is part of an inner loop. Do not update the loop information.
912 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
913 // at this point, because this method is being called while populating `info`.
914 }
915 }
916
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000917 // Raw update of the loop information.
918 void SetLoopInformation(HLoopInformation* info) {
919 loop_information_ = info;
920 }
921
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100922 bool IsInLoop() const { return loop_information_ != nullptr; }
923
David Brazdilec16f792015-08-19 15:04:01 +0100924 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
925
926 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
927 try_catch_information_ = try_catch_information;
928 }
929
930 bool IsTryBlock() const {
931 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
932 }
933
934 bool IsCatchBlock() const {
935 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
936 }
David Brazdilffee3d32015-07-06 11:48:53 +0100937
938 // Returns the try entry that this block's successors should have. They will
939 // be in the same try, unless the block ends in a try boundary. In that case,
940 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100941 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100942
David Brazdild7558da2015-09-22 13:04:14 +0100943 bool HasThrowingInstructions() const;
944
David Brazdila4b8c212015-05-07 09:59:30 +0100945 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100946 bool Dominates(HBasicBlock* block) const;
947
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100948 size_t GetLifetimeStart() const { return lifetime_start_; }
949 size_t GetLifetimeEnd() const { return lifetime_end_; }
950
951 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
952 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
953
David Brazdil8d5b8b22015-03-24 10:51:52 +0000954 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000955 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100956 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000957 bool HasSinglePhi() const;
958
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000959 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000960 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000961 ArenaVector<HBasicBlock*> predecessors_;
962 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100963 HInstructionList instructions_;
964 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000965 HLoopInformation* loop_information_;
966 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000967 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100968 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100969 // The dex program counter of the first instruction of this block.
970 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100971 size_t lifetime_start_;
972 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100973 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100974
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000975 friend class HGraph;
976 friend class HInstruction;
977
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000978 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
979};
980
David Brazdilb2bd1c52015-03-25 11:17:37 +0000981// Iterates over the LoopInformation of all loops which contain 'block'
982// from the innermost to the outermost.
983class HLoopInformationOutwardIterator : public ValueObject {
984 public:
985 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
986 : current_(block.GetLoopInformation()) {}
987
988 bool Done() const { return current_ == nullptr; }
989
990 void Advance() {
991 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100992 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000993 }
994
995 HLoopInformation* Current() const {
996 DCHECK(!Done());
997 return current_;
998 }
999
1000 private:
1001 HLoopInformation* current_;
1002
1003 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1004};
1005
Alexandre Ramesef20f712015-06-09 10:29:30 +01001006#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001007 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001008 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001009 M(ArrayGet, Instruction) \
1010 M(ArrayLength, Instruction) \
1011 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +01001012 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001013 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001014 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001015 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001016 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001017 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001018 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001019 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001020 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001021 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001022 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001023 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001024 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001025 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001026 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001027 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001028 M(FloatConstant, Constant) \
1029 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001030 M(GreaterThan, Condition) \
1031 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001033 M(InstanceFieldGet, Instruction) \
1034 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001035 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001036 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001037 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001038 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001039 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001040 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001041 M(LessThan, Condition) \
1042 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001043 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001044 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001045 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001046 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001047 M(Local, Instruction) \
1048 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001049 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001050 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001051 M(Mul, BinaryOperation) \
1052 M(Neg, UnaryOperation) \
1053 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001054 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001055 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001056 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001057 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001058 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001059 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001060 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001061 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001062 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001063 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001064 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001065 M(Return, Instruction) \
1066 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001067 M(Shl, BinaryOperation) \
1068 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001069 M(StaticFieldGet, Instruction) \
1070 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001071 M(StoreLocal, Instruction) \
1072 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001073 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001074 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001075 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001076 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001077 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001078 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001079 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001080
Alexandre Ramesef20f712015-06-09 10:29:30 +01001081#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1082
1083#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1084
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001085#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1086
Mark Mendell0616ae02015-04-17 12:49:27 -04001087#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1088 M(X86ComputeBaseMethodAddress, Instruction) \
1089 M(X86LoadFromConstantTable, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001090
1091#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1092
1093#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1094 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1095 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1096 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001097 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001098 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1099 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1100
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001101#define FOR_EACH_INSTRUCTION(M) \
1102 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1103 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001104 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001105 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001106 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001107
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001108#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001109FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1110#undef FORWARD_DECLARATION
1111
Roland Levillainccc07a92014-09-16 14:48:16 +01001112#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001113 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1114 const char* DebugName() const OVERRIDE { return #type; } \
1115 const H##type* As##type() const OVERRIDE { return this; } \
1116 H##type* As##type() OVERRIDE { return this; } \
1117 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001118 return other->Is##type(); \
1119 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001120 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001121
David Brazdiled596192015-01-23 10:39:45 +00001122template <typename T> class HUseList;
1123
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001124template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001125class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001126 public:
David Brazdiled596192015-01-23 10:39:45 +00001127 HUseListNode* GetPrevious() const { return prev_; }
1128 HUseListNode* GetNext() const { return next_; }
1129 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001130 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001131 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001132
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001133 private:
David Brazdiled596192015-01-23 10:39:45 +00001134 HUseListNode(T user, size_t index)
1135 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1136
1137 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001138 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001139 HUseListNode<T>* prev_;
1140 HUseListNode<T>* next_;
1141
1142 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001143
1144 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1145};
1146
David Brazdiled596192015-01-23 10:39:45 +00001147template <typename T>
1148class HUseList : public ValueObject {
1149 public:
1150 HUseList() : first_(nullptr) {}
1151
1152 void Clear() {
1153 first_ = nullptr;
1154 }
1155
1156 // Adds a new entry at the beginning of the use list and returns
1157 // the newly created node.
1158 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001159 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001160 if (IsEmpty()) {
1161 first_ = new_node;
1162 } else {
1163 first_->prev_ = new_node;
1164 new_node->next_ = first_;
1165 first_ = new_node;
1166 }
1167 return new_node;
1168 }
1169
1170 HUseListNode<T>* GetFirst() const {
1171 return first_;
1172 }
1173
1174 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001175 DCHECK(node != nullptr);
1176 DCHECK(Contains(node));
1177
David Brazdiled596192015-01-23 10:39:45 +00001178 if (node->prev_ != nullptr) {
1179 node->prev_->next_ = node->next_;
1180 }
1181 if (node->next_ != nullptr) {
1182 node->next_->prev_ = node->prev_;
1183 }
1184 if (node == first_) {
1185 first_ = node->next_;
1186 }
1187 }
1188
David Brazdil1abb4192015-02-17 18:33:36 +00001189 bool Contains(const HUseListNode<T>* node) const {
1190 if (node == nullptr) {
1191 return false;
1192 }
1193 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1194 if (current == node) {
1195 return true;
1196 }
1197 }
1198 return false;
1199 }
1200
David Brazdiled596192015-01-23 10:39:45 +00001201 bool IsEmpty() const {
1202 return first_ == nullptr;
1203 }
1204
1205 bool HasOnlyOneUse() const {
1206 return first_ != nullptr && first_->next_ == nullptr;
1207 }
1208
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001209 size_t SizeSlow() const {
1210 size_t count = 0;
1211 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1212 ++count;
1213 }
1214 return count;
1215 }
1216
David Brazdiled596192015-01-23 10:39:45 +00001217 private:
1218 HUseListNode<T>* first_;
1219};
1220
1221template<typename T>
1222class HUseIterator : public ValueObject {
1223 public:
1224 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1225
1226 bool Done() const { return current_ == nullptr; }
1227
1228 void Advance() {
1229 DCHECK(!Done());
1230 current_ = current_->GetNext();
1231 }
1232
1233 HUseListNode<T>* Current() const {
1234 DCHECK(!Done());
1235 return current_;
1236 }
1237
1238 private:
1239 HUseListNode<T>* current_;
1240
1241 friend class HValue;
1242};
1243
David Brazdil1abb4192015-02-17 18:33:36 +00001244// This class is used by HEnvironment and HInstruction classes to record the
1245// instructions they use and pointers to the corresponding HUseListNodes kept
1246// by the used instructions.
1247template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001248class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001249 public:
1250 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1251 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1252
1253 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1254 : instruction_(old_record.instruction_), use_node_(use_node) {
1255 DCHECK(instruction_ != nullptr);
1256 DCHECK(use_node_ != nullptr);
1257 DCHECK(old_record.use_node_ == nullptr);
1258 }
1259
1260 HInstruction* GetInstruction() const { return instruction_; }
1261 HUseListNode<T>* GetUseNode() const { return use_node_; }
1262
1263 private:
1264 // Instruction used by the user.
1265 HInstruction* instruction_;
1266
1267 // Corresponding entry in the use list kept by 'instruction_'.
1268 HUseListNode<T>* use_node_;
1269};
1270
Aart Bik854a02b2015-07-14 16:07:00 -07001271/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001272 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001273 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001274 * For write/read dependences on fields/arrays, the dependence analysis uses
1275 * type disambiguation (e.g. a float field write cannot modify the value of an
1276 * integer field read) and the access type (e.g. a reference array write cannot
1277 * modify the value of a reference field read [although it may modify the
1278 * reference fetch prior to reading the field, which is represented by its own
1279 * write/read dependence]). The analysis makes conservative points-to
1280 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1281 * the same, and any reference read depends on any reference read without
1282 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001283 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001284 * The internal representation uses 38-bit and is described in the table below.
1285 * The first line indicates the side effect, and for field/array accesses the
1286 * second line indicates the type of the access (in the order of the
1287 * Primitive::Type enum).
1288 * The two numbered lines below indicate the bit position in the bitfield (read
1289 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001290 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001291 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1292 * +-------------+---------+---------+--------------+---------+---------+
1293 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1294 * | 3 |333333322|222222221| 1 |111111110|000000000|
1295 * | 7 |654321098|765432109| 8 |765432109|876543210|
1296 *
1297 * Note that, to ease the implementation, 'changes' bits are least significant
1298 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001299 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001300class SideEffects : public ValueObject {
1301 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001302 SideEffects() : flags_(0) {}
1303
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001304 static SideEffects None() {
1305 return SideEffects(0);
1306 }
1307
1308 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001309 return SideEffects(kAllChangeBits | kAllDependOnBits);
1310 }
1311
1312 static SideEffects AllChanges() {
1313 return SideEffects(kAllChangeBits);
1314 }
1315
1316 static SideEffects AllDependencies() {
1317 return SideEffects(kAllDependOnBits);
1318 }
1319
1320 static SideEffects AllExceptGCDependency() {
1321 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1322 }
1323
1324 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001325 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001326 }
1327
Aart Bik34c3ba92015-07-20 14:08:59 -07001328 static SideEffects AllWrites() {
1329 return SideEffects(kAllWrites);
1330 }
1331
1332 static SideEffects AllReads() {
1333 return SideEffects(kAllReads);
1334 }
1335
1336 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1337 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001338 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001339 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001340 }
1341
Aart Bik854a02b2015-07-14 16:07:00 -07001342 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1343 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001344 }
1345
Aart Bik34c3ba92015-07-20 14:08:59 -07001346 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1347 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001348 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001349 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001350 }
1351
1352 static SideEffects ArrayReadOfType(Primitive::Type type) {
1353 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1354 }
1355
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001356 static SideEffects CanTriggerGC() {
1357 return SideEffects(1ULL << kCanTriggerGCBit);
1358 }
1359
1360 static SideEffects DependsOnGC() {
1361 return SideEffects(1ULL << kDependsOnGCBit);
1362 }
1363
Aart Bik854a02b2015-07-14 16:07:00 -07001364 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001365 SideEffects Union(SideEffects other) const {
1366 return SideEffects(flags_ | other.flags_);
1367 }
1368
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001369 SideEffects Exclusion(SideEffects other) const {
1370 return SideEffects(flags_ & ~other.flags_);
1371 }
1372
1373 bool Includes(SideEffects other) const {
1374 return (other.flags_ & flags_) == other.flags_;
1375 }
1376
1377 bool HasSideEffects() const {
1378 return (flags_ & kAllChangeBits);
1379 }
1380
1381 bool HasDependencies() const {
1382 return (flags_ & kAllDependOnBits);
1383 }
1384
1385 // Returns true if there are no side effects or dependencies.
1386 bool DoesNothing() const {
1387 return flags_ == 0;
1388 }
1389
Aart Bik854a02b2015-07-14 16:07:00 -07001390 // Returns true if something is written.
1391 bool DoesAnyWrite() const {
1392 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001393 }
1394
Aart Bik854a02b2015-07-14 16:07:00 -07001395 // Returns true if something is read.
1396 bool DoesAnyRead() const {
1397 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001398 }
1399
Aart Bik854a02b2015-07-14 16:07:00 -07001400 // Returns true if potentially everything is written and read
1401 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001402 bool DoesAllReadWrite() const {
1403 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1404 }
1405
Aart Bik854a02b2015-07-14 16:07:00 -07001406 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001407 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001408 }
1409
1410 // Returns true if this may read something written by other.
1411 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001412 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1413 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001414 }
1415
1416 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001417 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001418 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001419 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001420 for (int s = kLastBit; s >= 0; s--) {
1421 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1422 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1423 // This is a bit for the GC side effect.
1424 if (current_bit_is_set) {
1425 flags += "GC";
1426 }
Aart Bik854a02b2015-07-14 16:07:00 -07001427 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001428 } else {
1429 // This is a bit for the array/field analysis.
1430 // The underscore character stands for the 'can trigger GC' bit.
1431 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1432 if (current_bit_is_set) {
1433 flags += kDebug[s];
1434 }
1435 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1436 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1437 flags += "|";
1438 }
1439 }
Aart Bik854a02b2015-07-14 16:07:00 -07001440 }
1441 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001442 }
1443
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001444 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001445
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001446 private:
1447 static constexpr int kFieldArrayAnalysisBits = 9;
1448
1449 static constexpr int kFieldWriteOffset = 0;
1450 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1451 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1452 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1453
1454 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1455
1456 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1457 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1458 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1459 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1460
1461 static constexpr int kLastBit = kDependsOnGCBit;
1462 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1463
1464 // Aliases.
1465
1466 static_assert(kChangeBits == kDependOnBits,
1467 "the 'change' bits should match the 'depend on' bits.");
1468
1469 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1470 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1471 static constexpr uint64_t kAllWrites =
1472 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1473 static constexpr uint64_t kAllReads =
1474 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475
Aart Bik854a02b2015-07-14 16:07:00 -07001476 // Work around the fact that HIR aliases I/F and J/D.
1477 // TODO: remove this interceptor once HIR types are clean
1478 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1479 switch (type) {
1480 case Primitive::kPrimInt:
1481 case Primitive::kPrimFloat:
1482 return TypeFlag(Primitive::kPrimInt, offset) |
1483 TypeFlag(Primitive::kPrimFloat, offset);
1484 case Primitive::kPrimLong:
1485 case Primitive::kPrimDouble:
1486 return TypeFlag(Primitive::kPrimLong, offset) |
1487 TypeFlag(Primitive::kPrimDouble, offset);
1488 default:
1489 return TypeFlag(type, offset);
1490 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001491 }
1492
Aart Bik854a02b2015-07-14 16:07:00 -07001493 // Translates type to bit flag.
1494 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1495 CHECK_NE(type, Primitive::kPrimVoid);
1496 const uint64_t one = 1;
1497 const int shift = type; // 0-based consecutive enum
1498 DCHECK_LE(kFieldWriteOffset, shift);
1499 DCHECK_LT(shift, kArrayWriteOffset);
1500 return one << (type + offset);
1501 }
1502
1503 // Private constructor on direct flags value.
1504 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1505
1506 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001507};
1508
David Brazdiled596192015-01-23 10:39:45 +00001509// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001510class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001511 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001512 HEnvironment(ArenaAllocator* arena,
1513 size_t number_of_vregs,
1514 const DexFile& dex_file,
1515 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001516 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001517 InvokeType invoke_type,
1518 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001519 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1520 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001521 parent_(nullptr),
1522 dex_file_(dex_file),
1523 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001524 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001525 invoke_type_(invoke_type),
1526 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001527 }
1528
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001529 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001530 : HEnvironment(arena,
1531 to_copy.Size(),
1532 to_copy.GetDexFile(),
1533 to_copy.GetMethodIdx(),
1534 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001535 to_copy.GetInvokeType(),
1536 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001537
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001538 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001539 if (parent_ != nullptr) {
1540 parent_->SetAndCopyParentChain(allocator, parent);
1541 } else {
1542 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1543 parent_->CopyFrom(parent);
1544 if (parent->GetParent() != nullptr) {
1545 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1546 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001547 }
David Brazdiled596192015-01-23 10:39:45 +00001548 }
1549
Vladimir Marko71bf8092015-09-15 15:33:14 +01001550 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001551 void CopyFrom(HEnvironment* environment);
1552
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001553 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1554 // input to the loop phi instead. This is for inserting instructions that
1555 // require an environment (like HDeoptimization) in the loop pre-header.
1556 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001557
1558 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001559 DCHECK_LT(index, Size());
1560 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001561 }
1562
1563 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001564 DCHECK_LT(index, Size());
1565 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001566 }
1567
David Brazdil1abb4192015-02-17 18:33:36 +00001568 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001569
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001570 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001571
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001572 HEnvironment* GetParent() const { return parent_; }
1573
1574 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001575 DCHECK_LT(index, Size());
1576 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001577 }
1578
1579 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001580 DCHECK_LT(index, Size());
1581 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001582 }
1583
1584 uint32_t GetDexPc() const {
1585 return dex_pc_;
1586 }
1587
1588 uint32_t GetMethodIdx() const {
1589 return method_idx_;
1590 }
1591
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001592 InvokeType GetInvokeType() const {
1593 return invoke_type_;
1594 }
1595
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001596 const DexFile& GetDexFile() const {
1597 return dex_file_;
1598 }
1599
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001600 HInstruction* GetHolder() const {
1601 return holder_;
1602 }
1603
David Brazdiled596192015-01-23 10:39:45 +00001604 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001605 // Record instructions' use entries of this environment for constant-time removal.
1606 // It should only be called by HInstruction when a new environment use is added.
1607 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1608 DCHECK(env_use->GetUser() == this);
1609 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001610 DCHECK_LT(index, Size());
1611 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001612 }
David Brazdiled596192015-01-23 10:39:45 +00001613
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001614 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1615 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001616 HEnvironment* parent_;
1617 const DexFile& dex_file_;
1618 const uint32_t method_idx_;
1619 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001620 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001621
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001622 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001623 HInstruction* const holder_;
1624
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001625 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001626
1627 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1628};
1629
Calin Juravleacf735c2015-02-12 15:25:22 +00001630class ReferenceTypeInfo : ValueObject {
1631 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001632 typedef Handle<mirror::Class> TypeHandle;
1633
Calin Juravle2e768302015-07-28 14:41:11 +00001634 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1635 // The constructor will check that the type_handle is valid.
1636 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001637 }
1638
Calin Juravle2e768302015-07-28 14:41:11 +00001639 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1640
1641 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1642 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001643 }
1644
Calin Juravle2e768302015-07-28 14:41:11 +00001645 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1646 return IsValidHandle(type_handle_);
1647 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001648 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001649
1650 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1651 DCHECK(IsValid());
1652 return GetTypeHandle()->IsObjectClass();
1653 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001654 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001655 DCHECK(IsValid());
1656 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001657 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001658
1659 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1660
Mathieu Chartier90443472015-07-16 20:32:27 -07001661 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001662 DCHECK(IsValid());
1663 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001664 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1665 }
1666
1667 // Returns true if the type information provide the same amount of details.
1668 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001669 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001670 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001671 if (!IsValid() && !rti.IsValid()) {
1672 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001673 return true;
1674 }
Calin Juravle2e768302015-07-28 14:41:11 +00001675 if (!IsValid() || !rti.IsValid()) {
1676 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001677 return false;
1678 }
Calin Juravle2e768302015-07-28 14:41:11 +00001679 return IsExact() == rti.IsExact()
1680 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001681 }
1682
1683 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001684 ReferenceTypeInfo();
1685 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001686
Calin Juravleacf735c2015-02-12 15:25:22 +00001687 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001688 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001689 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001690 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001691 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001692};
1693
1694std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1695
Vladimir Markof9f64412015-09-02 14:05:49 +01001696class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001697 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001698 HInstruction(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001699 : previous_(nullptr),
1700 next_(nullptr),
1701 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001702 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001703 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001704 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001705 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001706 locations_(nullptr),
1707 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001708 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001709 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001710 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001711
Dave Allison20dfc792014-06-16 20:44:29 -07001712 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001713
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001714#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001715 enum InstructionKind {
1716 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1717 };
1718#undef DECLARE_KIND
1719
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001720 HInstruction* GetNext() const { return next_; }
1721 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001722
Calin Juravle77520bc2015-01-12 18:45:46 +00001723 HInstruction* GetNextDisregardingMoves() const;
1724 HInstruction* GetPreviousDisregardingMoves() const;
1725
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001726 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001727 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001728 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001729 bool IsInBlock() const { return block_ != nullptr; }
1730 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001731 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001732
Roland Levillain6b879dd2014-09-22 17:13:44 +01001733 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001734 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001735
1736 virtual void Accept(HGraphVisitor* visitor) = 0;
1737 virtual const char* DebugName() const = 0;
1738
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001739 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001740 void SetRawInputAt(size_t index, HInstruction* input) {
1741 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1742 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001743
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001744 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001745
1746 uint32_t GetDexPc() const { return dex_pc_; }
1747
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001748 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001749
Roland Levillaine161a2a2014-10-03 12:45:18 +01001750 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001751 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001752
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001753 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001754 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001755
Calin Juravle10e244f2015-01-26 18:54:32 +00001756 // Does not apply for all instructions, but having this at top level greatly
1757 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001758 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001759 virtual bool CanBeNull() const {
1760 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1761 return true;
1762 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001763
Calin Juravle641547a2015-04-21 22:08:51 +01001764 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1765 UNUSED(obj);
1766 return false;
1767 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001768
Calin Juravle2e768302015-07-28 14:41:11 +00001769 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001770
Calin Juravle61d544b2015-02-23 16:46:57 +00001771 ReferenceTypeInfo GetReferenceTypeInfo() const {
1772 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1773 return reference_type_info_;
1774 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001775
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001776 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001777 DCHECK(user != nullptr);
1778 HUseListNode<HInstruction*>* use =
1779 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1780 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001781 }
1782
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001783 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001784 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001785 HUseListNode<HEnvironment*>* env_use =
1786 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1787 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001788 }
1789
David Brazdil1abb4192015-02-17 18:33:36 +00001790 void RemoveAsUserOfInput(size_t input) {
1791 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1792 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1793 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001794
David Brazdil1abb4192015-02-17 18:33:36 +00001795 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1796 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001797
David Brazdiled596192015-01-23 10:39:45 +00001798 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1799 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001800 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001801 bool HasOnlyOneNonEnvironmentUse() const {
1802 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1803 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001804
Roland Levillain6c82d402014-10-13 16:10:27 +01001805 // Does this instruction strictly dominate `other_instruction`?
1806 // Returns false if this instruction and `other_instruction` are the same.
1807 // Aborts if this instruction and `other_instruction` are both phis.
1808 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001809
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001810 int GetId() const { return id_; }
1811 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001812
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001813 int GetSsaIndex() const { return ssa_index_; }
1814 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1815 bool HasSsaIndex() const { return ssa_index_ != -1; }
1816
1817 bool HasEnvironment() const { return environment_ != nullptr; }
1818 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001819 // Set the `environment_` field. Raw because this method does not
1820 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001821 void SetRawEnvironment(HEnvironment* environment) {
1822 DCHECK(environment_ == nullptr);
1823 DCHECK_EQ(environment->GetHolder(), this);
1824 environment_ = environment;
1825 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001826
1827 // Set the environment of this instruction, copying it from `environment`. While
1828 // copying, the uses lists are being updated.
1829 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001830 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001831 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001832 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001833 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001834 if (environment->GetParent() != nullptr) {
1835 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1836 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001837 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001838
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001839 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1840 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001841 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001842 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001843 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001844 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001845 if (environment->GetParent() != nullptr) {
1846 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1847 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001848 }
1849
Nicolas Geoffray39468442014-09-02 15:17:15 +01001850 // Returns the number of entries in the environment. Typically, that is the
1851 // number of dex registers in a method. It could be more in case of inlining.
1852 size_t EnvironmentSize() const;
1853
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001854 LocationSummary* GetLocations() const { return locations_; }
1855 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001856
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001857 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001858 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001859
Alexandre Rames188d4312015-04-09 18:30:21 +01001860 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1861 // uses of this instruction by `other` are *not* updated.
1862 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1863 ReplaceWith(other);
1864 other->ReplaceInput(this, use_index);
1865 }
1866
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001867 // Move `this` instruction before `cursor`.
1868 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001869
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001870#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001871 bool Is##type() const { return (As##type() != nullptr); } \
1872 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001873 virtual H##type* As##type() { return nullptr; }
1874
1875 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1876#undef INSTRUCTION_TYPE_CHECK
1877
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001878 // Returns whether the instruction can be moved within the graph.
1879 virtual bool CanBeMoved() const { return false; }
1880
1881 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001882 virtual bool InstructionTypeEquals(HInstruction* other) const {
1883 UNUSED(other);
1884 return false;
1885 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001886
1887 // Returns whether any data encoded in the two instructions is equal.
1888 // This method does not look at the inputs. Both instructions must be
1889 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001890 virtual bool InstructionDataEquals(HInstruction* other) const {
1891 UNUSED(other);
1892 return false;
1893 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001894
1895 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001896 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001897 // 2) Their inputs are identical.
1898 bool Equals(HInstruction* other) const;
1899
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001900 virtual InstructionKind GetKind() const = 0;
1901
1902 virtual size_t ComputeHashCode() const {
1903 size_t result = GetKind();
1904 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1905 result = (result * 31) + InputAt(i)->GetId();
1906 }
1907 return result;
1908 }
1909
1910 SideEffects GetSideEffects() const { return side_effects_; }
1911
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001912 size_t GetLifetimePosition() const { return lifetime_position_; }
1913 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1914 LiveInterval* GetLiveInterval() const { return live_interval_; }
1915 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1916 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1917
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001918 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1919
1920 // Returns whether the code generation of the instruction will require to have access
1921 // to the current method. Such instructions are:
1922 // (1): Instructions that require an environment, as calling the runtime requires
1923 // to walk the stack and have the current method stored at a specific stack address.
1924 // (2): Object literals like classes and strings, that are loaded from the dex cache
1925 // fields of the current method.
1926 bool NeedsCurrentMethod() const {
1927 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1928 }
1929
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001930 virtual bool NeedsDexCache() const { return false; }
1931
Mark Mendellc4701932015-04-10 13:18:51 -04001932 // Does this instruction have any use in an environment before
1933 // control flow hits 'other'?
1934 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1935
1936 // Remove all references to environment uses of this instruction.
1937 // The caller must ensure that this is safe to do.
1938 void RemoveEnvironmentUsers();
1939
David Brazdil1abb4192015-02-17 18:33:36 +00001940 protected:
1941 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1942 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1943
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001944 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001945 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1946
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001947 HInstruction* previous_;
1948 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001949 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001950 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001951
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001952 // An instruction gets an id when it is added to the graph.
1953 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001954 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001955 int id_;
1956
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001957 // When doing liveness analysis, instructions that have uses get an SSA index.
1958 int ssa_index_;
1959
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001961 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001962
1963 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001964 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001965
Nicolas Geoffray39468442014-09-02 15:17:15 +01001966 // The environment associated with this instruction. Not null if the instruction
1967 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001968 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001969
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001970 // Set by the code generator.
1971 LocationSummary* locations_;
1972
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001973 // Set by the liveness analysis.
1974 LiveInterval* live_interval_;
1975
1976 // Set by the liveness analysis, this is the position in a linear
1977 // order of blocks where this instruction's live interval start.
1978 size_t lifetime_position_;
1979
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001980 const SideEffects side_effects_;
1981
Calin Juravleacf735c2015-02-12 15:25:22 +00001982 // TODO: for primitive types this should be marked as invalid.
1983 ReferenceTypeInfo reference_type_info_;
1984
David Brazdil1abb4192015-02-17 18:33:36 +00001985 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001986 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001987 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001988 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001989 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001990
1991 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1992};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001993std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001994
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001995class HInputIterator : public ValueObject {
1996 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001997 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001998
1999 bool Done() const { return index_ == instruction_->InputCount(); }
2000 HInstruction* Current() const { return instruction_->InputAt(index_); }
2001 void Advance() { index_++; }
2002
2003 private:
2004 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002005 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002006
2007 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2008};
2009
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002010class HInstructionIterator : public ValueObject {
2011 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002012 explicit HInstructionIterator(const HInstructionList& instructions)
2013 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002014 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002015 }
2016
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002017 bool Done() const { return instruction_ == nullptr; }
2018 HInstruction* Current() const { return instruction_; }
2019 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002020 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002021 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002022 }
2023
2024 private:
2025 HInstruction* instruction_;
2026 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002027
2028 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002029};
2030
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002031class HBackwardInstructionIterator : public ValueObject {
2032 public:
2033 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2034 : instruction_(instructions.last_instruction_) {
2035 next_ = Done() ? nullptr : instruction_->GetPrevious();
2036 }
2037
2038 bool Done() const { return instruction_ == nullptr; }
2039 HInstruction* Current() const { return instruction_; }
2040 void Advance() {
2041 instruction_ = next_;
2042 next_ = Done() ? nullptr : instruction_->GetPrevious();
2043 }
2044
2045 private:
2046 HInstruction* instruction_;
2047 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002048
2049 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002050};
2051
Vladimir Markof9f64412015-09-02 14:05:49 +01002052template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002053class HTemplateInstruction: public HInstruction {
2054 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002055 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2056 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002057 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002058
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002059 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002060
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002061 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002062 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2063 DCHECK_LT(i, N);
2064 return inputs_[i];
2065 }
David Brazdil1abb4192015-02-17 18:33:36 +00002066
2067 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002068 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002069 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002070 }
2071
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002072 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002073 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002074
2075 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002076};
2077
Vladimir Markof9f64412015-09-02 14:05:49 +01002078// HTemplateInstruction specialization for N=0.
2079template<>
2080class HTemplateInstruction<0>: public HInstruction {
2081 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002082 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2083 : HInstruction(side_effects, dex_pc) {}
2084
Vladimir Markof9f64412015-09-02 14:05:49 +01002085 virtual ~HTemplateInstruction() {}
2086
2087 size_t InputCount() const OVERRIDE { return 0; }
2088
2089 protected:
2090 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2091 LOG(FATAL) << "Unreachable";
2092 UNREACHABLE();
2093 }
2094
2095 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2096 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2097 LOG(FATAL) << "Unreachable";
2098 UNREACHABLE();
2099 }
2100
2101 private:
2102 friend class SsaBuilder;
2103};
2104
Dave Allison20dfc792014-06-16 20:44:29 -07002105template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002106class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002107 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002108 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2109 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002110 virtual ~HExpression() {}
2111
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002112 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002113
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002114 protected:
2115 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002116};
2117
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002118// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2119// instruction that branches to the exit block.
2120class HReturnVoid : public HTemplateInstruction<0> {
2121 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002122 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2123 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002124
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002125 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002126
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002127 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002128
2129 private:
2130 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2131};
2132
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002133// Represents dex's RETURN opcodes. A HReturn is a control flow
2134// instruction that branches to the exit block.
2135class HReturn : public HTemplateInstruction<1> {
2136 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002137 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2138 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002139 SetRawInputAt(0, value);
2140 }
2141
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002142 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002143
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002144 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002145
2146 private:
2147 DISALLOW_COPY_AND_ASSIGN(HReturn);
2148};
2149
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002150// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002151// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152// exit block.
2153class HExit : public HTemplateInstruction<0> {
2154 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002155 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002156
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002157 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002158
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002159 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002160
2161 private:
2162 DISALLOW_COPY_AND_ASSIGN(HExit);
2163};
2164
2165// Jumps from one block to another.
2166class HGoto : public HTemplateInstruction<0> {
2167 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002168 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002169
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002170 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002171
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002172 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002173 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002174 }
2175
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002176 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002177
2178 private:
2179 DISALLOW_COPY_AND_ASSIGN(HGoto);
2180};
2181
Roland Levillain9867bc72015-08-05 10:21:34 +01002182class HConstant : public HExpression<0> {
2183 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002184 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2185 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002186
2187 bool CanBeMoved() const OVERRIDE { return true; }
2188
2189 virtual bool IsMinusOne() const { return false; }
2190 virtual bool IsZero() const { return false; }
2191 virtual bool IsOne() const { return false; }
2192
David Brazdil77a48ae2015-09-15 12:34:04 +00002193 virtual uint64_t GetValueAsUint64() const = 0;
2194
Roland Levillain9867bc72015-08-05 10:21:34 +01002195 DECLARE_INSTRUCTION(Constant);
2196
2197 private:
2198 DISALLOW_COPY_AND_ASSIGN(HConstant);
2199};
2200
2201class HNullConstant : public HConstant {
2202 public:
2203 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2204 return true;
2205 }
2206
David Brazdil77a48ae2015-09-15 12:34:04 +00002207 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2208
Roland Levillain9867bc72015-08-05 10:21:34 +01002209 size_t ComputeHashCode() const OVERRIDE { return 0; }
2210
2211 DECLARE_INSTRUCTION(NullConstant);
2212
2213 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002214 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002215
2216 friend class HGraph;
2217 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2218};
2219
2220// Constants of the type int. Those can be from Dex instructions, or
2221// synthesized (for example with the if-eqz instruction).
2222class HIntConstant : public HConstant {
2223 public:
2224 int32_t GetValue() const { return value_; }
2225
David Brazdil77a48ae2015-09-15 12:34:04 +00002226 uint64_t GetValueAsUint64() const OVERRIDE { return static_cast<uint64_t>(value_); }
2227
Roland Levillain9867bc72015-08-05 10:21:34 +01002228 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2229 DCHECK(other->IsIntConstant());
2230 return other->AsIntConstant()->value_ == value_;
2231 }
2232
2233 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2234
2235 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2236 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2237 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2238
2239 DECLARE_INSTRUCTION(IntConstant);
2240
2241 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002242 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2243 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2244 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2245 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002246
2247 const int32_t value_;
2248
2249 friend class HGraph;
2250 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2251 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2252 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2253};
2254
2255class HLongConstant : public HConstant {
2256 public:
2257 int64_t GetValue() const { return value_; }
2258
David Brazdil77a48ae2015-09-15 12:34:04 +00002259 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2260
Roland Levillain9867bc72015-08-05 10:21:34 +01002261 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2262 DCHECK(other->IsLongConstant());
2263 return other->AsLongConstant()->value_ == value_;
2264 }
2265
2266 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2267
2268 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2269 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2270 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2271
2272 DECLARE_INSTRUCTION(LongConstant);
2273
2274 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002275 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2276 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002277
2278 const int64_t value_;
2279
2280 friend class HGraph;
2281 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2282};
Dave Allison20dfc792014-06-16 20:44:29 -07002283
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002284// Conditional branch. A block ending with an HIf instruction must have
2285// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002286class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002287 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002288 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2289 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002290 SetRawInputAt(0, input);
2291 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002292
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002293 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002294
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002295 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002296 return GetBlock()->GetSuccessor(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002297 }
2298
2299 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002300 return GetBlock()->GetSuccessor(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002301 }
2302
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002303 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002304
2305 private:
2306 DISALLOW_COPY_AND_ASSIGN(HIf);
2307};
2308
David Brazdilfc6a86a2015-06-26 10:33:45 +00002309
2310// Abstract instruction which marks the beginning and/or end of a try block and
2311// links it to the respective exception handlers. Behaves the same as a Goto in
2312// non-exceptional control flow.
2313// Normal-flow successor is stored at index zero, exception handlers under
2314// higher indices in no particular order.
2315class HTryBoundary : public HTemplateInstruction<0> {
2316 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002317 enum BoundaryKind {
2318 kEntry,
2319 kExit,
2320 };
2321
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002322 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2323 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002324
2325 bool IsControlFlow() const OVERRIDE { return true; }
2326
2327 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko60584552015-09-03 13:35:12 +00002328 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessor(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002329
2330 // Returns whether `handler` is among its exception handlers (non-zero index
2331 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002332 bool HasExceptionHandler(const HBasicBlock& handler) const {
2333 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002334 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002335 }
2336
2337 // If not present already, adds `handler` to its block's list of exception
2338 // handlers.
2339 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002340 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002341 GetBlock()->AddSuccessor(handler);
2342 }
2343 }
2344
David Brazdil56e1acc2015-06-30 15:41:36 +01002345 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002346
David Brazdilffee3d32015-07-06 11:48:53 +01002347 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2348
David Brazdilfc6a86a2015-06-26 10:33:45 +00002349 DECLARE_INSTRUCTION(TryBoundary);
2350
2351 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002352 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002353
2354 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2355};
2356
David Brazdilffee3d32015-07-06 11:48:53 +01002357// Iterator over exception handlers of a given HTryBoundary, i.e. over
2358// exceptional successors of its basic block.
2359class HExceptionHandlerIterator : public ValueObject {
2360 public:
2361 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2362 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2363
Vladimir Marko60584552015-09-03 13:35:12 +00002364 bool Done() const { return index_ == block_.GetSuccessors().size(); }
2365 HBasicBlock* Current() const { return block_.GetSuccessor(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002366 size_t CurrentSuccessorIndex() const { return index_; }
2367 void Advance() { ++index_; }
2368
2369 private:
2370 const HBasicBlock& block_;
2371 size_t index_;
2372
2373 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2374};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002375
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002376// Deoptimize to interpreter, upon checking a condition.
2377class HDeoptimize : public HTemplateInstruction<1> {
2378 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002379 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2380 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002381 SetRawInputAt(0, cond);
2382 }
2383
2384 bool NeedsEnvironment() const OVERRIDE { return true; }
2385 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002386
2387 DECLARE_INSTRUCTION(Deoptimize);
2388
2389 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002390 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2391};
2392
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002393// Represents the ArtMethod that was passed as a first argument to
2394// the method. It is used by instructions that depend on it, like
2395// instructions that work with the dex cache.
2396class HCurrentMethod : public HExpression<0> {
2397 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002398 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2399 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002400
2401 DECLARE_INSTRUCTION(CurrentMethod);
2402
2403 private:
2404 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2405};
2406
Mark Mendellfe57faa2015-09-18 09:26:15 -04002407// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2408// have one successor for each entry in the switch table, and the final successor
2409// will be the block containing the next Dex opcode.
2410class HPackedSwitch : public HTemplateInstruction<1> {
2411 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002412 HPackedSwitch(int32_t start_value,
2413 uint32_t num_entries,
2414 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002415 uint32_t dex_pc = kNoDexPc)
2416 : HTemplateInstruction(SideEffects::None(), dex_pc),
2417 start_value_(start_value),
2418 num_entries_(num_entries) {
2419 SetRawInputAt(0, input);
2420 }
2421
2422 bool IsControlFlow() const OVERRIDE { return true; }
2423
2424 int32_t GetStartValue() const { return start_value_; }
2425
Vladimir Marko211c2112015-09-24 16:52:33 +01002426 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002427
2428 HBasicBlock* GetDefaultBlock() const {
2429 // Last entry is the default block.
2430 return GetBlock()->GetSuccessor(num_entries_);
2431 }
2432 DECLARE_INSTRUCTION(PackedSwitch);
2433
2434 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002435 const int32_t start_value_;
2436 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002437
2438 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2439};
2440
Roland Levillain88cb1752014-10-20 16:36:47 +01002441class HUnaryOperation : public HExpression<1> {
2442 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002443 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2444 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002445 SetRawInputAt(0, input);
2446 }
2447
2448 HInstruction* GetInput() const { return InputAt(0); }
2449 Primitive::Type GetResultType() const { return GetType(); }
2450
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002451 bool CanBeMoved() const OVERRIDE { return true; }
2452 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002453 UNUSED(other);
2454 return true;
2455 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002456
Roland Levillain9240d6a2014-10-20 16:47:04 +01002457 // Try to statically evaluate `operation` and return a HConstant
2458 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002459 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002460 HConstant* TryStaticEvaluation() const;
2461
2462 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002463 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2464 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002465
Roland Levillain88cb1752014-10-20 16:36:47 +01002466 DECLARE_INSTRUCTION(UnaryOperation);
2467
2468 private:
2469 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2470};
2471
Dave Allison20dfc792014-06-16 20:44:29 -07002472class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002473 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002474 HBinaryOperation(Primitive::Type result_type,
2475 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002476 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002477 SideEffects side_effects = SideEffects::None(),
2478 uint32_t dex_pc = kNoDexPc)
2479 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002480 SetRawInputAt(0, left);
2481 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002482 }
2483
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002484 HInstruction* GetLeft() const { return InputAt(0); }
2485 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002486 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002487
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002488 virtual bool IsCommutative() const { return false; }
2489
2490 // Put constant on the right.
2491 // Returns whether order is changed.
2492 bool OrderInputsWithConstantOnTheRight() {
2493 HInstruction* left = InputAt(0);
2494 HInstruction* right = InputAt(1);
2495 if (left->IsConstant() && !right->IsConstant()) {
2496 ReplaceInput(right, 0);
2497 ReplaceInput(left, 1);
2498 return true;
2499 }
2500 return false;
2501 }
2502
2503 // Order inputs by instruction id, but favor constant on the right side.
2504 // This helps GVN for commutative ops.
2505 void OrderInputs() {
2506 DCHECK(IsCommutative());
2507 HInstruction* left = InputAt(0);
2508 HInstruction* right = InputAt(1);
2509 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2510 return;
2511 }
2512 if (OrderInputsWithConstantOnTheRight()) {
2513 return;
2514 }
2515 // Order according to instruction id.
2516 if (left->GetId() > right->GetId()) {
2517 ReplaceInput(right, 0);
2518 ReplaceInput(left, 1);
2519 }
2520 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002521
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002522 bool CanBeMoved() const OVERRIDE { return true; }
2523 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002524 UNUSED(other);
2525 return true;
2526 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002527
Roland Levillain9240d6a2014-10-20 16:47:04 +01002528 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002529 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002530 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002531 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002532
2533 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002534 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2535 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2536 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2537 HLongConstant* y ATTRIBUTE_UNUSED) const {
2538 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2539 return nullptr;
2540 }
2541 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2542 HIntConstant* y ATTRIBUTE_UNUSED) const {
2543 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2544 return nullptr;
2545 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002546
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002547 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002548 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002549 HConstant* GetConstantRight() const;
2550
2551 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002552 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002553 HInstruction* GetLeastConstantLeft() const;
2554
Roland Levillainccc07a92014-09-16 14:48:16 +01002555 DECLARE_INSTRUCTION(BinaryOperation);
2556
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002557 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002558 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2559};
2560
Mark Mendellc4701932015-04-10 13:18:51 -04002561// The comparison bias applies for floating point operations and indicates how NaN
2562// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002563enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002564 kNoBias, // bias is not applicable (i.e. for long operation)
2565 kGtBias, // return 1 for NaN comparisons
2566 kLtBias, // return -1 for NaN comparisons
2567};
2568
Dave Allison20dfc792014-06-16 20:44:29 -07002569class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002570 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002571 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2572 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002573 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002574 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002575
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002576 bool NeedsMaterialization() const { return needs_materialization_; }
2577 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002578
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002579 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002580 // `instruction`, and disregard moves in between.
2581 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002582
Dave Allison20dfc792014-06-16 20:44:29 -07002583 DECLARE_INSTRUCTION(Condition);
2584
2585 virtual IfCondition GetCondition() const = 0;
2586
Mark Mendellc4701932015-04-10 13:18:51 -04002587 virtual IfCondition GetOppositeCondition() const = 0;
2588
Roland Levillain4fa13f62015-07-06 18:11:54 +01002589 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002590
2591 void SetBias(ComparisonBias bias) { bias_ = bias; }
2592
2593 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2594 return bias_ == other->AsCondition()->bias_;
2595 }
2596
Roland Levillain4fa13f62015-07-06 18:11:54 +01002597 bool IsFPConditionTrueIfNaN() const {
2598 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2599 IfCondition if_cond = GetCondition();
2600 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2601 }
2602
2603 bool IsFPConditionFalseIfNaN() const {
2604 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2605 IfCondition if_cond = GetCondition();
2606 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2607 }
2608
Dave Allison20dfc792014-06-16 20:44:29 -07002609 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002610 // For register allocation purposes, returns whether this instruction needs to be
2611 // materialized (that is, not just be in the processor flags).
2612 bool needs_materialization_;
2613
Mark Mendellc4701932015-04-10 13:18:51 -04002614 // Needed if we merge a HCompare into a HCondition.
2615 ComparisonBias bias_;
2616
Dave Allison20dfc792014-06-16 20:44:29 -07002617 DISALLOW_COPY_AND_ASSIGN(HCondition);
2618};
2619
2620// Instruction to check if two inputs are equal to each other.
2621class HEqual : public HCondition {
2622 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002623 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2624 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002625
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002626 bool IsCommutative() const OVERRIDE { return true; }
2627
Roland Levillain9867bc72015-08-05 10:21:34 +01002628 template <typename T> bool Compute(T x, T y) const { return x == y; }
2629
2630 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002631 return GetBlock()->GetGraph()->GetIntConstant(
2632 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002633 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002634 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002635 return GetBlock()->GetGraph()->GetIntConstant(
2636 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002637 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002638
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002639 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002640
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002641 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002642 return kCondEQ;
2643 }
2644
Mark Mendellc4701932015-04-10 13:18:51 -04002645 IfCondition GetOppositeCondition() const OVERRIDE {
2646 return kCondNE;
2647 }
2648
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002649 private:
2650 DISALLOW_COPY_AND_ASSIGN(HEqual);
2651};
2652
Dave Allison20dfc792014-06-16 20:44:29 -07002653class HNotEqual : public HCondition {
2654 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002655 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2656 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002657
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002658 bool IsCommutative() const OVERRIDE { return true; }
2659
Roland Levillain9867bc72015-08-05 10:21:34 +01002660 template <typename T> bool Compute(T x, T y) const { return x != y; }
2661
2662 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002663 return GetBlock()->GetGraph()->GetIntConstant(
2664 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002665 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002666 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002667 return GetBlock()->GetGraph()->GetIntConstant(
2668 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002669 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002670
Dave Allison20dfc792014-06-16 20:44:29 -07002671 DECLARE_INSTRUCTION(NotEqual);
2672
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002673 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002674 return kCondNE;
2675 }
2676
Mark Mendellc4701932015-04-10 13:18:51 -04002677 IfCondition GetOppositeCondition() const OVERRIDE {
2678 return kCondEQ;
2679 }
2680
Dave Allison20dfc792014-06-16 20:44:29 -07002681 private:
2682 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2683};
2684
2685class HLessThan : public HCondition {
2686 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002687 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2688 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002689
Roland Levillain9867bc72015-08-05 10:21:34 +01002690 template <typename T> bool Compute(T x, T y) const { return x < y; }
2691
2692 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002693 return GetBlock()->GetGraph()->GetIntConstant(
2694 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002695 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002696 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002697 return GetBlock()->GetGraph()->GetIntConstant(
2698 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002699 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002700
Dave Allison20dfc792014-06-16 20:44:29 -07002701 DECLARE_INSTRUCTION(LessThan);
2702
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002703 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002704 return kCondLT;
2705 }
2706
Mark Mendellc4701932015-04-10 13:18:51 -04002707 IfCondition GetOppositeCondition() const OVERRIDE {
2708 return kCondGE;
2709 }
2710
Dave Allison20dfc792014-06-16 20:44:29 -07002711 private:
2712 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2713};
2714
2715class HLessThanOrEqual : public HCondition {
2716 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002717 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2718 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002719
Roland Levillain9867bc72015-08-05 10:21:34 +01002720 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2721
2722 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002723 return GetBlock()->GetGraph()->GetIntConstant(
2724 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002725 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002726 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002727 return GetBlock()->GetGraph()->GetIntConstant(
2728 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002729 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002730
Dave Allison20dfc792014-06-16 20:44:29 -07002731 DECLARE_INSTRUCTION(LessThanOrEqual);
2732
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002733 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002734 return kCondLE;
2735 }
2736
Mark Mendellc4701932015-04-10 13:18:51 -04002737 IfCondition GetOppositeCondition() const OVERRIDE {
2738 return kCondGT;
2739 }
2740
Dave Allison20dfc792014-06-16 20:44:29 -07002741 private:
2742 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2743};
2744
2745class HGreaterThan : public HCondition {
2746 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002747 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2748 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002749
Roland Levillain9867bc72015-08-05 10:21:34 +01002750 template <typename T> bool Compute(T x, T y) const { return x > y; }
2751
2752 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002753 return GetBlock()->GetGraph()->GetIntConstant(
2754 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002755 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002756 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002757 return GetBlock()->GetGraph()->GetIntConstant(
2758 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002759 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002760
Dave Allison20dfc792014-06-16 20:44:29 -07002761 DECLARE_INSTRUCTION(GreaterThan);
2762
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002763 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002764 return kCondGT;
2765 }
2766
Mark Mendellc4701932015-04-10 13:18:51 -04002767 IfCondition GetOppositeCondition() const OVERRIDE {
2768 return kCondLE;
2769 }
2770
Dave Allison20dfc792014-06-16 20:44:29 -07002771 private:
2772 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2773};
2774
2775class HGreaterThanOrEqual : public HCondition {
2776 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002777 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2778 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002779
Roland Levillain9867bc72015-08-05 10:21:34 +01002780 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2781
2782 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002783 return GetBlock()->GetGraph()->GetIntConstant(
2784 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002785 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002786 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002787 return GetBlock()->GetGraph()->GetIntConstant(
2788 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002789 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002790
Dave Allison20dfc792014-06-16 20:44:29 -07002791 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2792
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002793 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002794 return kCondGE;
2795 }
2796
Mark Mendellc4701932015-04-10 13:18:51 -04002797 IfCondition GetOppositeCondition() const OVERRIDE {
2798 return kCondLT;
2799 }
2800
Dave Allison20dfc792014-06-16 20:44:29 -07002801 private:
2802 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2803};
2804
2805
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002806// Instruction to check how two inputs compare to each other.
2807// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2808class HCompare : public HBinaryOperation {
2809 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002810 HCompare(Primitive::Type type,
2811 HInstruction* first,
2812 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002813 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002814 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002815 : HBinaryOperation(Primitive::kPrimInt,
2816 first,
2817 second,
2818 SideEffectsForArchRuntimeCalls(type),
2819 dex_pc),
2820 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002821 DCHECK_EQ(type, first->GetType());
2822 DCHECK_EQ(type, second->GetType());
2823 }
2824
Roland Levillain9867bc72015-08-05 10:21:34 +01002825 template <typename T>
2826 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002827
Roland Levillain9867bc72015-08-05 10:21:34 +01002828 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002829 return GetBlock()->GetGraph()->GetIntConstant(
2830 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002831 }
2832 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002833 return GetBlock()->GetGraph()->GetIntConstant(
2834 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002835 }
2836
Calin Juravleddb7df22014-11-25 20:56:51 +00002837 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2838 return bias_ == other->AsCompare()->bias_;
2839 }
2840
Mark Mendellc4701932015-04-10 13:18:51 -04002841 ComparisonBias GetBias() const { return bias_; }
2842
Roland Levillain4fa13f62015-07-06 18:11:54 +01002843 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002844
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002845
2846 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2847 // MIPS64 uses a runtime call for FP comparisons.
2848 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2849 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002850
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002851 DECLARE_INSTRUCTION(Compare);
2852
2853 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002854 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002855
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002856 DISALLOW_COPY_AND_ASSIGN(HCompare);
2857};
2858
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002859// A local in the graph. Corresponds to a Dex register.
2860class HLocal : public HTemplateInstruction<0> {
2861 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002862 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002863 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002864
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002865 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002866
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002867 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002868
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002869 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002870 // The Dex register number.
2871 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002872
2873 DISALLOW_COPY_AND_ASSIGN(HLocal);
2874};
2875
2876// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002877class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002878 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002879 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2880 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002881 SetRawInputAt(0, local);
2882 }
2883
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002884 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2885
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002886 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002887
2888 private:
2889 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2890};
2891
2892// Store a value in a given local. This instruction has two inputs: the value
2893// and the local.
2894class HStoreLocal : public HTemplateInstruction<2> {
2895 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002896 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2897 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002898 SetRawInputAt(0, local);
2899 SetRawInputAt(1, value);
2900 }
2901
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002902 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2903
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002904 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002905
2906 private:
2907 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2908};
2909
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002910class HFloatConstant : public HConstant {
2911 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002912 float GetValue() const { return value_; }
2913
David Brazdil77a48ae2015-09-15 12:34:04 +00002914 uint64_t GetValueAsUint64() const OVERRIDE {
2915 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2916 }
2917
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002918 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002919 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002920 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002921 }
2922
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002923 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002924
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002925 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002926 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002927 }
2928 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002929 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002930 }
2931 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002932 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2933 }
2934 bool IsNaN() const {
2935 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002936 }
2937
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002938 DECLARE_INSTRUCTION(FloatConstant);
2939
2940 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002941 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2942 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2943 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2944 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002945
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002946 const float value_;
2947
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002948 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002949 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002950 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002951 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2952};
2953
2954class HDoubleConstant : public HConstant {
2955 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002956 double GetValue() const { return value_; }
2957
David Brazdil77a48ae2015-09-15 12:34:04 +00002958 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2959
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002960 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002961 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002962 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002963 }
2964
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002965 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002966
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002967 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002968 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002969 }
2970 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002971 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002972 }
2973 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002974 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2975 }
2976 bool IsNaN() const {
2977 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002978 }
2979
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002980 DECLARE_INSTRUCTION(DoubleConstant);
2981
2982 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002983 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2984 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2985 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2986 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002987
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002988 const double value_;
2989
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002990 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002991 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002992 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002993 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2994};
2995
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002996enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002997#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002998#include "intrinsics_list.h"
2999 kNone,
3000 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3001#undef INTRINSICS_LIST
3002#undef OPTIMIZING_INTRINSICS
3003};
3004std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3005
Agi Csaki05f20562015-08-19 14:58:14 -07003006enum IntrinsicNeedsEnvironmentOrCache {
3007 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3008 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003009};
3010
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003011class HInvoke : public HInstruction {
3012 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003013 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003014
3015 // Runtime needs to walk the stack, so Dex -> Dex calls need to
3016 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07003017 bool NeedsEnvironment() const OVERRIDE {
3018 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
3019 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003020
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003021 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003022 SetRawInputAt(index, argument);
3023 }
3024
Roland Levillain3e3d7332015-04-28 11:00:54 +01003025 // Return the number of arguments. This number can be lower than
3026 // the number of inputs returned by InputCount(), as some invoke
3027 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3028 // inputs at the end of their list of inputs.
3029 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3030
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003031 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003032
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003033
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003034 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003035 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003036
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003037 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3038
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003039 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003040 return intrinsic_;
3041 }
3042
Agi Csaki05f20562015-08-19 14:58:14 -07003043 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003044 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07003045 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003046 }
3047
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003048 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003049 return GetEnvironment()->GetParent() != nullptr;
3050 }
3051
3052 bool CanThrow() const OVERRIDE { return true; }
3053
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003054 DECLARE_INSTRUCTION(Invoke);
3055
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003056 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003057 HInvoke(ArenaAllocator* arena,
3058 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003059 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003060 Primitive::Type return_type,
3061 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003062 uint32_t dex_method_index,
3063 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003064 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003065 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003066 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003067 inputs_(number_of_arguments + number_of_other_inputs,
3068 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003069 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003070 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003071 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003072 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07003073 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003074 }
3075
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003076 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
3077 DCHECK_LT(index, InputCount());
3078 return inputs_[index];
3079 }
3080
David Brazdil1abb4192015-02-17 18:33:36 +00003081 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003082 DCHECK_LT(index, InputCount());
3083 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003084 }
3085
Roland Levillain3e3d7332015-04-28 11:00:54 +01003086 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003087 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003088 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003089 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003090 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003091 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003092 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003093
3094 private:
3095 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3096};
3097
Calin Juravle175dc732015-08-25 15:42:32 +01003098class HInvokeUnresolved : public HInvoke {
3099 public:
3100 HInvokeUnresolved(ArenaAllocator* arena,
3101 uint32_t number_of_arguments,
3102 Primitive::Type return_type,
3103 uint32_t dex_pc,
3104 uint32_t dex_method_index,
3105 InvokeType invoke_type)
3106 : HInvoke(arena,
3107 number_of_arguments,
3108 0u /* number_of_other_inputs */,
3109 return_type,
3110 dex_pc,
3111 dex_method_index,
3112 invoke_type) {
3113 }
3114
3115 DECLARE_INSTRUCTION(InvokeUnresolved);
3116
3117 private:
3118 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3119};
3120
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003121class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003122 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003123 // Requirements of this method call regarding the class
3124 // initialization (clinit) check of its declaring class.
3125 enum class ClinitCheckRequirement {
3126 kNone, // Class already initialized.
3127 kExplicit, // Static call having explicit clinit check as last input.
3128 kImplicit, // Static call implicitly requiring a clinit check.
3129 };
3130
Vladimir Marko58155012015-08-19 12:49:41 +00003131 // Determines how to load the target ArtMethod*.
3132 enum class MethodLoadKind {
3133 // Use a String init ArtMethod* loaded from Thread entrypoints.
3134 kStringInit,
3135
3136 // Use the method's own ArtMethod* loaded by the register allocator.
3137 kRecursive,
3138
3139 // Use ArtMethod* at a known address, embed the direct address in the code.
3140 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3141 kDirectAddress,
3142
3143 // Use ArtMethod* at an address that will be known at link time, embed the direct
3144 // address in the code. If the image is relocatable, emit .patch_oat entry.
3145 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3146 // the image relocatable or not.
3147 kDirectAddressWithFixup,
3148
3149 // Load from resoved methods array in the dex cache using a PC-relative load.
3150 // Used when we need to use the dex cache, for example for invoke-static that
3151 // may cause class initialization (the entry may point to a resolution method),
3152 // and we know that we can access the dex cache arrays using a PC-relative load.
3153 kDexCachePcRelative,
3154
3155 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3156 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3157 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3158 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3159 kDexCacheViaMethod,
3160 };
3161
3162 // Determines the location of the code pointer.
3163 enum class CodePtrLocation {
3164 // Recursive call, use local PC-relative call instruction.
3165 kCallSelf,
3166
3167 // Use PC-relative call instruction patched at link time.
3168 // Used for calls within an oat file, boot->boot or app->app.
3169 kCallPCRelative,
3170
3171 // Call to a known target address, embed the direct address in code.
3172 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3173 kCallDirect,
3174
3175 // Call to a target address that will be known at link time, embed the direct
3176 // address in code. If the image is relocatable, emit .patch_oat entry.
3177 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3178 // the image relocatable or not.
3179 kCallDirectWithFixup,
3180
3181 // Use code pointer from the ArtMethod*.
3182 // Used when we don't know the target code. This is also the last-resort-kind used when
3183 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3184 kCallArtMethod,
3185 };
3186
3187 struct DispatchInfo {
3188 const MethodLoadKind method_load_kind;
3189 const CodePtrLocation code_ptr_location;
3190 // The method load data holds
3191 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3192 // Note that there are multiple string init methods, each having its own offset.
3193 // - the method address for kDirectAddress
3194 // - the dex cache arrays offset for kDexCachePcRel.
3195 const uint64_t method_load_data;
3196 const uint64_t direct_code_ptr;
3197 };
3198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003199 HInvokeStaticOrDirect(ArenaAllocator* arena,
3200 uint32_t number_of_arguments,
3201 Primitive::Type return_type,
3202 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003203 uint32_t method_index,
3204 MethodReference target_method,
3205 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003206 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003207 InvokeType invoke_type,
3208 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003209 : HInvoke(arena,
3210 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003211 // There is one extra argument for the HCurrentMethod node, and
3212 // potentially one other if the clinit check is explicit, and one other
3213 // if the method is a string factory.
3214 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003215 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003216 return_type,
3217 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003218 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003219 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003220 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003221 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003222 target_method_(target_method),
3223 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003224
Calin Juravle641547a2015-04-21 22:08:51 +01003225 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3226 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003227 // We access the method via the dex cache so we can't do an implicit null check.
3228 // TODO: for intrinsics we can generate implicit null checks.
3229 return false;
3230 }
3231
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003232 bool CanBeNull() const OVERRIDE {
3233 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3234 }
3235
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003236 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003237 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3238 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3239 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003240 bool NeedsDexCache() const OVERRIDE {
3241 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3242 return !IsRecursive() && !IsStringInit();
3243 }
Vladimir Marko58155012015-08-19 12:49:41 +00003244 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003245 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003246 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3247 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3248 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3249 MethodReference GetTargetMethod() const { return target_method_; }
3250
3251 int32_t GetStringInitOffset() const {
3252 DCHECK(IsStringInit());
3253 return dispatch_info_.method_load_data;
3254 }
3255
3256 uint64_t GetMethodAddress() const {
3257 DCHECK(HasMethodAddress());
3258 return dispatch_info_.method_load_data;
3259 }
3260
3261 uint32_t GetDexCacheArrayOffset() const {
3262 DCHECK(HasPcRelDexCache());
3263 return dispatch_info_.method_load_data;
3264 }
3265
3266 uint64_t GetDirectCodePtr() const {
3267 DCHECK(HasDirectCodePtr());
3268 return dispatch_info_.direct_code_ptr;
3269 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003270
Calin Juravle68ad6492015-08-18 17:08:12 +01003271 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3272
Roland Levillain4c0eb422015-04-24 16:43:49 +01003273 // Is this instruction a call to a static method?
3274 bool IsStatic() const {
3275 return GetInvokeType() == kStatic;
3276 }
3277
Roland Levillain3e3d7332015-04-28 11:00:54 +01003278 // Remove the art::HLoadClass instruction set as last input by
3279 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3280 // the initial art::HClinitCheck instruction (only relevant for
3281 // static calls with explicit clinit check).
3282 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003283 DCHECK(IsStaticWithExplicitClinitCheck());
3284 size_t last_input_index = InputCount() - 1;
3285 HInstruction* last_input = InputAt(last_input_index);
3286 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003287 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003288 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003289 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003290 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3291 DCHECK(IsStaticWithImplicitClinitCheck());
3292 }
3293
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003294 bool IsStringFactoryFor(HFakeString* str) const {
3295 if (!IsStringInit()) return false;
3296 // +1 for the current method.
3297 if (InputCount() == (number_of_arguments_ + 1)) return false;
3298 return InputAt(InputCount() - 1)->AsFakeString() == str;
3299 }
3300
3301 void RemoveFakeStringArgumentAsLastInput() {
3302 DCHECK(IsStringInit());
3303 size_t last_input_index = InputCount() - 1;
3304 HInstruction* last_input = InputAt(last_input_index);
3305 DCHECK(last_input != nullptr);
3306 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3307 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003308 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003309 }
3310
Roland Levillain4c0eb422015-04-24 16:43:49 +01003311 // Is this a call to a static method whose declaring class has an
3312 // explicit intialization check in the graph?
3313 bool IsStaticWithExplicitClinitCheck() const {
3314 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3315 }
3316
3317 // Is this a call to a static method whose declaring class has an
3318 // implicit intialization check requirement?
3319 bool IsStaticWithImplicitClinitCheck() const {
3320 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3321 }
3322
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003323 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003324
Roland Levillain4c0eb422015-04-24 16:43:49 +01003325 protected:
3326 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3327 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3328 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3329 HInstruction* input = input_record.GetInstruction();
3330 // `input` is the last input of a static invoke marked as having
3331 // an explicit clinit check. It must either be:
3332 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3333 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3334 DCHECK(input != nullptr);
3335 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3336 }
3337 return input_record;
3338 }
3339
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003340 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003341 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003342 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003343 // The target method may refer to different dex file or method index than the original
3344 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3345 // in derived class to increase visibility.
3346 MethodReference target_method_;
3347 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003348
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003349 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003350};
3351
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003352class HInvokeVirtual : public HInvoke {
3353 public:
3354 HInvokeVirtual(ArenaAllocator* arena,
3355 uint32_t number_of_arguments,
3356 Primitive::Type return_type,
3357 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003358 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003359 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003360 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003361 vtable_index_(vtable_index) {}
3362
Calin Juravle641547a2015-04-21 22:08:51 +01003363 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003364 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003365 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003366 }
3367
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003368 uint32_t GetVTableIndex() const { return vtable_index_; }
3369
3370 DECLARE_INSTRUCTION(InvokeVirtual);
3371
3372 private:
3373 const uint32_t vtable_index_;
3374
3375 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3376};
3377
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003378class HInvokeInterface : public HInvoke {
3379 public:
3380 HInvokeInterface(ArenaAllocator* arena,
3381 uint32_t number_of_arguments,
3382 Primitive::Type return_type,
3383 uint32_t dex_pc,
3384 uint32_t dex_method_index,
3385 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003386 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003387 imt_index_(imt_index) {}
3388
Calin Juravle641547a2015-04-21 22:08:51 +01003389 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003390 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003391 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003392 }
3393
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003394 uint32_t GetImtIndex() const { return imt_index_; }
3395 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3396
3397 DECLARE_INSTRUCTION(InvokeInterface);
3398
3399 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003400 const uint32_t imt_index_;
3401
3402 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3403};
3404
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003405class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003406 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003407 HNewInstance(HCurrentMethod* current_method,
3408 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003409 uint16_t type_index,
3410 const DexFile& dex_file,
3411 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003412 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003413 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003414 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003415 entrypoint_(entrypoint) {
3416 SetRawInputAt(0, current_method);
3417 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003418
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003419 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003420 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003421
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003422 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003423 bool NeedsEnvironment() const OVERRIDE { return true; }
3424 // It may throw when called on:
3425 // - interfaces
3426 // - abstract/innaccessible/unknown classes
3427 // TODO: optimize when possible.
3428 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003429
Calin Juravle10e244f2015-01-26 18:54:32 +00003430 bool CanBeNull() const OVERRIDE { return false; }
3431
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003432 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3433
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003434 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003435
3436 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003437 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003438 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003439 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003440
3441 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3442};
3443
Roland Levillain88cb1752014-10-20 16:36:47 +01003444class HNeg : public HUnaryOperation {
3445 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003446 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3447 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003448
Roland Levillain9867bc72015-08-05 10:21:34 +01003449 template <typename T> T Compute(T x) const { return -x; }
3450
3451 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003452 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003453 }
3454 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003455 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003456 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003457
Roland Levillain88cb1752014-10-20 16:36:47 +01003458 DECLARE_INSTRUCTION(Neg);
3459
3460 private:
3461 DISALLOW_COPY_AND_ASSIGN(HNeg);
3462};
3463
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003464class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003465 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003466 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003467 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003468 uint32_t dex_pc,
3469 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003470 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003471 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003472 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003473 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003474 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003475 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003476 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003477 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003478 }
3479
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003480 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003481 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003482
3483 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003484 bool NeedsEnvironment() const OVERRIDE { return true; }
3485
Mingyao Yang0c365e62015-03-31 15:09:29 -07003486 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3487 bool CanThrow() const OVERRIDE { return true; }
3488
Calin Juravle10e244f2015-01-26 18:54:32 +00003489 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003490
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003491 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3492
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003493 DECLARE_INSTRUCTION(NewArray);
3494
3495 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003496 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003497 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003498 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003499
3500 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3501};
3502
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003503class HAdd : public HBinaryOperation {
3504 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003505 HAdd(Primitive::Type result_type,
3506 HInstruction* left,
3507 HInstruction* right,
3508 uint32_t dex_pc = kNoDexPc)
3509 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003510
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003511 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003512
Roland Levillain9867bc72015-08-05 10:21:34 +01003513 template <typename T> T Compute(T x, T y) const { return x + y; }
3514
3515 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003516 return GetBlock()->GetGraph()->GetIntConstant(
3517 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003518 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003519 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003520 return GetBlock()->GetGraph()->GetLongConstant(
3521 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003522 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003523
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003524 DECLARE_INSTRUCTION(Add);
3525
3526 private:
3527 DISALLOW_COPY_AND_ASSIGN(HAdd);
3528};
3529
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003530class HSub : public HBinaryOperation {
3531 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003532 HSub(Primitive::Type result_type,
3533 HInstruction* left,
3534 HInstruction* right,
3535 uint32_t dex_pc = kNoDexPc)
3536 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003537
Roland Levillain9867bc72015-08-05 10:21:34 +01003538 template <typename T> T Compute(T x, T y) const { return x - y; }
3539
3540 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003541 return GetBlock()->GetGraph()->GetIntConstant(
3542 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003543 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003544 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003545 return GetBlock()->GetGraph()->GetLongConstant(
3546 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003547 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003548
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003549 DECLARE_INSTRUCTION(Sub);
3550
3551 private:
3552 DISALLOW_COPY_AND_ASSIGN(HSub);
3553};
3554
Calin Juravle34bacdf2014-10-07 20:23:36 +01003555class HMul : public HBinaryOperation {
3556 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003557 HMul(Primitive::Type result_type,
3558 HInstruction* left,
3559 HInstruction* right,
3560 uint32_t dex_pc = kNoDexPc)
3561 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003562
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003563 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003564
Roland Levillain9867bc72015-08-05 10:21:34 +01003565 template <typename T> T Compute(T x, T y) const { return x * y; }
3566
3567 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003568 return GetBlock()->GetGraph()->GetIntConstant(
3569 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003570 }
3571 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003572 return GetBlock()->GetGraph()->GetLongConstant(
3573 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003574 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003575
3576 DECLARE_INSTRUCTION(Mul);
3577
3578 private:
3579 DISALLOW_COPY_AND_ASSIGN(HMul);
3580};
3581
Calin Juravle7c4954d2014-10-28 16:57:40 +00003582class HDiv : public HBinaryOperation {
3583 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003584 HDiv(Primitive::Type result_type,
3585 HInstruction* left,
3586 HInstruction* right,
3587 uint32_t dex_pc)
3588 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003589
Roland Levillain9867bc72015-08-05 10:21:34 +01003590 template <typename T>
3591 T Compute(T x, T y) const {
3592 // Our graph structure ensures we never have 0 for `y` during
3593 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003594 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003595 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003596 return (y == -1) ? -x : x / y;
3597 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003598
Roland Levillain9867bc72015-08-05 10:21:34 +01003599 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003600 return GetBlock()->GetGraph()->GetIntConstant(
3601 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003602 }
3603 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003604 return GetBlock()->GetGraph()->GetLongConstant(
3605 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003606 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003607
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003608 static SideEffects SideEffectsForArchRuntimeCalls() {
3609 // The generated code can use a runtime call.
3610 return SideEffects::CanTriggerGC();
3611 }
3612
Calin Juravle7c4954d2014-10-28 16:57:40 +00003613 DECLARE_INSTRUCTION(Div);
3614
3615 private:
3616 DISALLOW_COPY_AND_ASSIGN(HDiv);
3617};
3618
Calin Juravlebacfec32014-11-14 15:54:36 +00003619class HRem : public HBinaryOperation {
3620 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003621 HRem(Primitive::Type result_type,
3622 HInstruction* left,
3623 HInstruction* right,
3624 uint32_t dex_pc)
3625 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003626
Roland Levillain9867bc72015-08-05 10:21:34 +01003627 template <typename T>
3628 T Compute(T x, T y) const {
3629 // Our graph structure ensures we never have 0 for `y` during
3630 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003631 DCHECK_NE(y, 0);
3632 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3633 return (y == -1) ? 0 : x % y;
3634 }
3635
Roland Levillain9867bc72015-08-05 10:21:34 +01003636 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003637 return GetBlock()->GetGraph()->GetIntConstant(
3638 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003639 }
3640 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003641 return GetBlock()->GetGraph()->GetLongConstant(
3642 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003643 }
3644
Calin Juravlebacfec32014-11-14 15:54:36 +00003645
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003646 static SideEffects SideEffectsForArchRuntimeCalls() {
3647 return SideEffects::CanTriggerGC();
3648 }
3649
Calin Juravlebacfec32014-11-14 15:54:36 +00003650 DECLARE_INSTRUCTION(Rem);
3651
3652 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003653 DISALLOW_COPY_AND_ASSIGN(HRem);
3654};
3655
Calin Juravled0d48522014-11-04 16:40:20 +00003656class HDivZeroCheck : public HExpression<1> {
3657 public:
3658 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003659 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003660 SetRawInputAt(0, value);
3661 }
3662
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003663 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3664
Calin Juravled0d48522014-11-04 16:40:20 +00003665 bool CanBeMoved() const OVERRIDE { return true; }
3666
3667 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3668 UNUSED(other);
3669 return true;
3670 }
3671
3672 bool NeedsEnvironment() const OVERRIDE { return true; }
3673 bool CanThrow() const OVERRIDE { return true; }
3674
Calin Juravled0d48522014-11-04 16:40:20 +00003675 DECLARE_INSTRUCTION(DivZeroCheck);
3676
3677 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003678 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3679};
3680
Calin Juravle9aec02f2014-11-18 23:06:35 +00003681class HShl : public HBinaryOperation {
3682 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003683 HShl(Primitive::Type result_type,
3684 HInstruction* left,
3685 HInstruction* right,
3686 uint32_t dex_pc = kNoDexPc)
3687 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003688
Roland Levillain9867bc72015-08-05 10:21:34 +01003689 template <typename T, typename U, typename V>
3690 T Compute(T x, U y, V max_shift_value) const {
3691 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3692 "V is not the unsigned integer type corresponding to T");
3693 return x << (y & max_shift_value);
3694 }
3695
3696 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3697 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003698 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003699 }
3700 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3701 // case is handled as `x << static_cast<int>(y)`.
3702 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3703 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003704 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003705 }
3706 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3707 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003708 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003709 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003710
3711 DECLARE_INSTRUCTION(Shl);
3712
3713 private:
3714 DISALLOW_COPY_AND_ASSIGN(HShl);
3715};
3716
3717class HShr : public HBinaryOperation {
3718 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003719 HShr(Primitive::Type result_type,
3720 HInstruction* left,
3721 HInstruction* right,
3722 uint32_t dex_pc = kNoDexPc)
3723 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003724
Roland Levillain9867bc72015-08-05 10:21:34 +01003725 template <typename T, typename U, typename V>
3726 T Compute(T x, U y, V max_shift_value) const {
3727 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3728 "V is not the unsigned integer type corresponding to T");
3729 return x >> (y & max_shift_value);
3730 }
3731
3732 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3733 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003734 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003735 }
3736 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3737 // case is handled as `x >> static_cast<int>(y)`.
3738 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3739 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003740 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003741 }
3742 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3743 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003744 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003745 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003746
3747 DECLARE_INSTRUCTION(Shr);
3748
3749 private:
3750 DISALLOW_COPY_AND_ASSIGN(HShr);
3751};
3752
3753class HUShr : public HBinaryOperation {
3754 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003755 HUShr(Primitive::Type result_type,
3756 HInstruction* left,
3757 HInstruction* right,
3758 uint32_t dex_pc = kNoDexPc)
3759 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003760
Roland Levillain9867bc72015-08-05 10:21:34 +01003761 template <typename T, typename U, typename V>
3762 T Compute(T x, U y, V max_shift_value) const {
3763 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3764 "V is not the unsigned integer type corresponding to T");
3765 V ux = static_cast<V>(x);
3766 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003767 }
3768
Roland Levillain9867bc72015-08-05 10:21:34 +01003769 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3770 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003771 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003772 }
3773 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3774 // case is handled as `x >>> static_cast<int>(y)`.
3775 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3776 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003777 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003778 }
3779 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3780 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003781 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003782 }
3783
3784 DECLARE_INSTRUCTION(UShr);
3785
3786 private:
3787 DISALLOW_COPY_AND_ASSIGN(HUShr);
3788};
3789
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003790class HAnd : public HBinaryOperation {
3791 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003792 HAnd(Primitive::Type result_type,
3793 HInstruction* left,
3794 HInstruction* right,
3795 uint32_t dex_pc = kNoDexPc)
3796 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003797
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003798 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003799
Roland Levillain9867bc72015-08-05 10:21:34 +01003800 template <typename T, typename U>
3801 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3802
3803 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003804 return GetBlock()->GetGraph()->GetIntConstant(
3805 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003806 }
3807 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003808 return GetBlock()->GetGraph()->GetLongConstant(
3809 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003810 }
3811 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003812 return GetBlock()->GetGraph()->GetLongConstant(
3813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003814 }
3815 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003816 return GetBlock()->GetGraph()->GetLongConstant(
3817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003818 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003819
3820 DECLARE_INSTRUCTION(And);
3821
3822 private:
3823 DISALLOW_COPY_AND_ASSIGN(HAnd);
3824};
3825
3826class HOr : public HBinaryOperation {
3827 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003828 HOr(Primitive::Type result_type,
3829 HInstruction* left,
3830 HInstruction* right,
3831 uint32_t dex_pc = kNoDexPc)
3832 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003833
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003834 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003835
Roland Levillain9867bc72015-08-05 10:21:34 +01003836 template <typename T, typename U>
3837 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3838
3839 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003840 return GetBlock()->GetGraph()->GetIntConstant(
3841 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003842 }
3843 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003844 return GetBlock()->GetGraph()->GetLongConstant(
3845 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003846 }
3847 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003848 return GetBlock()->GetGraph()->GetLongConstant(
3849 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003850 }
3851 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003852 return GetBlock()->GetGraph()->GetLongConstant(
3853 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003854 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003855
3856 DECLARE_INSTRUCTION(Or);
3857
3858 private:
3859 DISALLOW_COPY_AND_ASSIGN(HOr);
3860};
3861
3862class HXor : public HBinaryOperation {
3863 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003864 HXor(Primitive::Type result_type,
3865 HInstruction* left,
3866 HInstruction* right,
3867 uint32_t dex_pc = kNoDexPc)
3868 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003869
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003870 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003871
Roland Levillain9867bc72015-08-05 10:21:34 +01003872 template <typename T, typename U>
3873 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3874
3875 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003876 return GetBlock()->GetGraph()->GetIntConstant(
3877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003878 }
3879 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003880 return GetBlock()->GetGraph()->GetLongConstant(
3881 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003882 }
3883 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003884 return GetBlock()->GetGraph()->GetLongConstant(
3885 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003886 }
3887 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003888 return GetBlock()->GetGraph()->GetLongConstant(
3889 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003890 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003891
3892 DECLARE_INSTRUCTION(Xor);
3893
3894 private:
3895 DISALLOW_COPY_AND_ASSIGN(HXor);
3896};
3897
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003898// The value of a parameter in this method. Its location depends on
3899// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003900class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003901 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003902 HParameterValue(uint8_t index,
3903 Primitive::Type parameter_type,
3904 bool is_this = false)
3905 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003906 index_(index),
3907 is_this_(is_this),
3908 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003909
3910 uint8_t GetIndex() const { return index_; }
3911
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003912 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3913 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003914
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003915 bool IsThis() const { return is_this_; }
3916
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003917 DECLARE_INSTRUCTION(ParameterValue);
3918
3919 private:
3920 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003921 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003922 const uint8_t index_;
3923
Calin Juravle10e244f2015-01-26 18:54:32 +00003924 // Whether or not the parameter value corresponds to 'this' argument.
3925 const bool is_this_;
3926
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003927 bool can_be_null_;
3928
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003929 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3930};
3931
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003932class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003933 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003934 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3935 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003936
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003937 bool CanBeMoved() const OVERRIDE { return true; }
3938 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003939 UNUSED(other);
3940 return true;
3941 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003942
Roland Levillain9867bc72015-08-05 10:21:34 +01003943 template <typename T> T Compute(T x) const { return ~x; }
3944
3945 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003946 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003947 }
3948 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003949 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003950 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003951
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003952 DECLARE_INSTRUCTION(Not);
3953
3954 private:
3955 DISALLOW_COPY_AND_ASSIGN(HNot);
3956};
3957
David Brazdil66d126e2015-04-03 16:02:44 +01003958class HBooleanNot : public HUnaryOperation {
3959 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003960 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3961 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003962
3963 bool CanBeMoved() const OVERRIDE { return true; }
3964 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3965 UNUSED(other);
3966 return true;
3967 }
3968
Roland Levillain9867bc72015-08-05 10:21:34 +01003969 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003970 DCHECK(IsUint<1>(x));
3971 return !x;
3972 }
3973
Roland Levillain9867bc72015-08-05 10:21:34 +01003974 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003975 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003976 }
3977 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3978 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003979 UNREACHABLE();
3980 }
3981
3982 DECLARE_INSTRUCTION(BooleanNot);
3983
3984 private:
3985 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3986};
3987
Roland Levillaindff1f282014-11-05 14:15:05 +00003988class HTypeConversion : public HExpression<1> {
3989 public:
3990 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003991 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003992 : HExpression(result_type,
3993 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
3994 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003995 SetRawInputAt(0, input);
3996 DCHECK_NE(input->GetType(), result_type);
3997 }
3998
3999 HInstruction* GetInput() const { return InputAt(0); }
4000 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4001 Primitive::Type GetResultType() const { return GetType(); }
4002
Roland Levillain624279f2014-12-04 11:54:28 +00004003 // Required by the x86 and ARM code generators when producing calls
4004 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004005
Roland Levillaindff1f282014-11-05 14:15:05 +00004006 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004007 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004008
Mark Mendelle82549b2015-05-06 10:55:34 -04004009 // Try to statically evaluate the conversion and return a HConstant
4010 // containing the result. If the input cannot be converted, return nullptr.
4011 HConstant* TryStaticEvaluation() const;
4012
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004013 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4014 Primitive::Type result_type) {
4015 // Some architectures may not require the 'GC' side effects, but at this point
4016 // in the compilation process we do not know what architecture we will
4017 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004018 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4019 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004020 return SideEffects::CanTriggerGC();
4021 }
4022 return SideEffects::None();
4023 }
4024
Roland Levillaindff1f282014-11-05 14:15:05 +00004025 DECLARE_INSTRUCTION(TypeConversion);
4026
4027 private:
4028 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4029};
4030
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004031static constexpr uint32_t kNoRegNumber = -1;
4032
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004033class HPhi : public HInstruction {
4034 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004035 HPhi(ArenaAllocator* arena,
4036 uint32_t reg_number,
4037 size_t number_of_inputs,
4038 Primitive::Type type,
4039 uint32_t dex_pc = kNoDexPc)
4040 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004041 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004042 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004043 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004044 is_live_(false),
4045 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004046 }
4047
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004048 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4049 static Primitive::Type ToPhiType(Primitive::Type type) {
4050 switch (type) {
4051 case Primitive::kPrimBoolean:
4052 case Primitive::kPrimByte:
4053 case Primitive::kPrimShort:
4054 case Primitive::kPrimChar:
4055 return Primitive::kPrimInt;
4056 default:
4057 return type;
4058 }
4059 }
4060
David Brazdilffee3d32015-07-06 11:48:53 +01004061 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4062
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004063 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004064
4065 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004066 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004067
Calin Juravle10e244f2015-01-26 18:54:32 +00004068 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004069 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004070
Calin Juravle10e244f2015-01-26 18:54:32 +00004071 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4072 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4073
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004074 uint32_t GetRegNumber() const { return reg_number_; }
4075
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004076 void SetDead() { is_live_ = false; }
4077 void SetLive() { is_live_ = true; }
4078 bool IsDead() const { return !is_live_; }
4079 bool IsLive() const { return is_live_; }
4080
David Brazdil77a48ae2015-09-15 12:34:04 +00004081 bool IsVRegEquivalentOf(HInstruction* other) const {
4082 return other != nullptr
4083 && other->IsPhi()
4084 && other->AsPhi()->GetBlock() == GetBlock()
4085 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4086 }
4087
Calin Juravlea4f88312015-04-16 12:57:19 +01004088 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4089 // An equivalent phi is a phi having the same dex register and type.
4090 // It assumes that phis with the same dex register are adjacent.
4091 HPhi* GetNextEquivalentPhiWithSameType() {
4092 HInstruction* next = GetNext();
4093 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4094 if (next->GetType() == GetType()) {
4095 return next->AsPhi();
4096 }
4097 next = next->GetNext();
4098 }
4099 return nullptr;
4100 }
4101
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004102 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004103
David Brazdil1abb4192015-02-17 18:33:36 +00004104 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004105 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
4106 DCHECK_LE(index, InputCount());
4107 return inputs_[index];
4108 }
David Brazdil1abb4192015-02-17 18:33:36 +00004109
4110 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004111 DCHECK_LE(index, InputCount());
4112 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004113 }
4114
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004115 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004116 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004117 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004118 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004119 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004120 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004121
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004122 DISALLOW_COPY_AND_ASSIGN(HPhi);
4123};
4124
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004125class HNullCheck : public HExpression<1> {
4126 public:
4127 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004128 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004129 SetRawInputAt(0, value);
4130 }
4131
Calin Juravle10e244f2015-01-26 18:54:32 +00004132 bool CanBeMoved() const OVERRIDE { return true; }
4133 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004134 UNUSED(other);
4135 return true;
4136 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004137
Calin Juravle10e244f2015-01-26 18:54:32 +00004138 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004139
Calin Juravle10e244f2015-01-26 18:54:32 +00004140 bool CanThrow() const OVERRIDE { return true; }
4141
4142 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004143
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004144
4145 DECLARE_INSTRUCTION(NullCheck);
4146
4147 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004148 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4149};
4150
4151class FieldInfo : public ValueObject {
4152 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004153 FieldInfo(MemberOffset field_offset,
4154 Primitive::Type field_type,
4155 bool is_volatile,
4156 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004157 const DexFile& dex_file,
4158 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004159 : field_offset_(field_offset),
4160 field_type_(field_type),
4161 is_volatile_(is_volatile),
4162 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004163 dex_file_(dex_file),
4164 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004165
4166 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004167 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004168 uint32_t GetFieldIndex() const { return index_; }
4169 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004170 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004171 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004172
4173 private:
4174 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004175 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004176 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004177 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004178 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004179 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004180};
4181
4182class HInstanceFieldGet : public HExpression<1> {
4183 public:
4184 HInstanceFieldGet(HInstruction* value,
4185 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004186 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004187 bool is_volatile,
4188 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004189 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004190 Handle<mirror::DexCache> dex_cache,
4191 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004192 : HExpression(
4193 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004194 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004195 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004196 SetRawInputAt(0, value);
4197 }
4198
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004199 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004200
4201 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4202 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4203 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004204 }
4205
Calin Juravle641547a2015-04-21 22:08:51 +01004206 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4207 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004208 }
4209
4210 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004211 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4212 }
4213
Calin Juravle52c48962014-12-16 17:02:57 +00004214 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004215 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004216 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004217 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004218
4219 DECLARE_INSTRUCTION(InstanceFieldGet);
4220
4221 private:
4222 const FieldInfo field_info_;
4223
4224 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4225};
4226
4227class HInstanceFieldSet : public HTemplateInstruction<2> {
4228 public:
4229 HInstanceFieldSet(HInstruction* object,
4230 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004231 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004232 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004233 bool is_volatile,
4234 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004235 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004236 Handle<mirror::DexCache> dex_cache,
4237 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004238 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004239 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004240 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004241 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004242 SetRawInputAt(0, object);
4243 SetRawInputAt(1, value);
4244 }
4245
Calin Juravle641547a2015-04-21 22:08:51 +01004246 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4247 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004248 }
4249
Calin Juravle52c48962014-12-16 17:02:57 +00004250 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004251 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004252 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004253 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004254 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004255 bool GetValueCanBeNull() const { return value_can_be_null_; }
4256 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004257
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004258 DECLARE_INSTRUCTION(InstanceFieldSet);
4259
4260 private:
4261 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004262 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004263
4264 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4265};
4266
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004267class HArrayGet : public HExpression<2> {
4268 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004269 HArrayGet(HInstruction* array,
4270 HInstruction* index,
4271 Primitive::Type type,
4272 uint32_t dex_pc = kNoDexPc)
4273 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004274 SetRawInputAt(0, array);
4275 SetRawInputAt(1, index);
4276 }
4277
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004278 bool CanBeMoved() const OVERRIDE { return true; }
4279 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004280 UNUSED(other);
4281 return true;
4282 }
Calin Juravle641547a2015-04-21 22:08:51 +01004283 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4284 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004285 // TODO: We can be smarter here.
4286 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4287 // which generates the implicit null check. There are cases when these can be removed
4288 // to produce better code. If we ever add optimizations to do so we should allow an
4289 // implicit check here (as long as the address falls in the first page).
4290 return false;
4291 }
4292
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004293 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004294
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004295 HInstruction* GetArray() const { return InputAt(0); }
4296 HInstruction* GetIndex() const { return InputAt(1); }
4297
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004298 DECLARE_INSTRUCTION(ArrayGet);
4299
4300 private:
4301 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4302};
4303
4304class HArraySet : public HTemplateInstruction<3> {
4305 public:
4306 HArraySet(HInstruction* array,
4307 HInstruction* index,
4308 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004309 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004310 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004311 : HTemplateInstruction(
4312 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004313 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004314 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004315 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4316 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004317 SetRawInputAt(0, array);
4318 SetRawInputAt(1, index);
4319 SetRawInputAt(2, value);
4320 }
4321
Calin Juravle77520bc2015-01-12 18:45:46 +00004322 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004323 // We currently always call a runtime method to catch array store
4324 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004325 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004326 }
4327
Mingyao Yang81014cb2015-06-02 03:16:27 -07004328 // Can throw ArrayStoreException.
4329 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4330
Calin Juravle641547a2015-04-21 22:08:51 +01004331 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4332 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004333 // TODO: Same as for ArrayGet.
4334 return false;
4335 }
4336
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004337 void ClearNeedsTypeCheck() {
4338 needs_type_check_ = false;
4339 }
4340
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004341 void ClearValueCanBeNull() {
4342 value_can_be_null_ = false;
4343 }
4344
4345 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004346 bool NeedsTypeCheck() const { return needs_type_check_; }
4347
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004348 HInstruction* GetArray() const { return InputAt(0); }
4349 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004350 HInstruction* GetValue() const { return InputAt(2); }
4351
4352 Primitive::Type GetComponentType() const {
4353 // The Dex format does not type floating point index operations. Since the
4354 // `expected_component_type_` is set during building and can therefore not
4355 // be correct, we also check what is the value type. If it is a floating
4356 // point type, we must use that type.
4357 Primitive::Type value_type = GetValue()->GetType();
4358 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4359 ? value_type
4360 : expected_component_type_;
4361 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004362
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004363 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4364 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4365 }
4366
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004367 DECLARE_INSTRUCTION(ArraySet);
4368
4369 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004370 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004371 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004372 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004373
4374 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4375};
4376
4377class HArrayLength : public HExpression<1> {
4378 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004379 explicit HArrayLength(HInstruction* array, uint32_t dex_pc = kNoDexPc)
4380 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004381 // Note that arrays do not change length, so the instruction does not
4382 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004383 SetRawInputAt(0, array);
4384 }
4385
Calin Juravle77520bc2015-01-12 18:45:46 +00004386 bool CanBeMoved() const OVERRIDE { return true; }
4387 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004388 UNUSED(other);
4389 return true;
4390 }
Calin Juravle641547a2015-04-21 22:08:51 +01004391 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4392 return obj == InputAt(0);
4393 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004394
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004395 DECLARE_INSTRUCTION(ArrayLength);
4396
4397 private:
4398 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4399};
4400
4401class HBoundsCheck : public HExpression<2> {
4402 public:
4403 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004404 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004405 DCHECK(index->GetType() == Primitive::kPrimInt);
4406 SetRawInputAt(0, index);
4407 SetRawInputAt(1, length);
4408 }
4409
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004410 bool CanBeMoved() const OVERRIDE { return true; }
4411 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004412 UNUSED(other);
4413 return true;
4414 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004415
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004416 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004417
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004418 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004419
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004420
4421 DECLARE_INSTRUCTION(BoundsCheck);
4422
4423 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004424 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4425};
4426
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004427/**
4428 * Some DEX instructions are folded into multiple HInstructions that need
4429 * to stay live until the last HInstruction. This class
4430 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004431 * HInstruction stays live. `index` represents the stack location index of the
4432 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004433 */
4434class HTemporary : public HTemplateInstruction<0> {
4435 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004436 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4437 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004438
4439 size_t GetIndex() const { return index_; }
4440
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004441 Primitive::Type GetType() const OVERRIDE {
4442 // The previous instruction is the one that will be stored in the temporary location.
4443 DCHECK(GetPrevious() != nullptr);
4444 return GetPrevious()->GetType();
4445 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004446
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004447 DECLARE_INSTRUCTION(Temporary);
4448
4449 private:
4450 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004451 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4452};
4453
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004454class HSuspendCheck : public HTemplateInstruction<0> {
4455 public:
4456 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004457 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004458
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004459 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004460 return true;
4461 }
4462
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004463 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4464 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004465
4466 DECLARE_INSTRUCTION(SuspendCheck);
4467
4468 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004469 // Only used for code generation, in order to share the same slow path between back edges
4470 // of a same loop.
4471 SlowPathCode* slow_path_;
4472
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004473 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4474};
4475
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004476/**
4477 * Instruction to load a Class object.
4478 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004479class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004480 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004481 HLoadClass(HCurrentMethod* current_method,
4482 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004483 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004484 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004485 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004486 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004487 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004488 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004489 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004490 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004491 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004492 SetRawInputAt(0, current_method);
4493 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004494
4495 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004496
4497 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4498 return other->AsLoadClass()->type_index_ == type_index_;
4499 }
4500
4501 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4502
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004503 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004504 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004505 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004506
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004507 bool NeedsEnvironment() const OVERRIDE {
4508 // Will call runtime and load the class if the class is not loaded yet.
4509 // TODO: finer grain decision.
4510 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004511 }
4512
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004513 bool MustGenerateClinitCheck() const {
4514 return generate_clinit_check_;
4515 }
4516
Calin Juravle0ba218d2015-05-19 18:46:01 +01004517 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4518 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004519 }
4520
4521 bool CanCallRuntime() const {
4522 return MustGenerateClinitCheck() || !is_referrers_class_;
4523 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004524
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004525 bool CanThrow() const OVERRIDE {
4526 // May call runtime and and therefore can throw.
4527 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004528 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004529 }
4530
Calin Juravleacf735c2015-02-12 15:25:22 +00004531 ReferenceTypeInfo GetLoadedClassRTI() {
4532 return loaded_class_rti_;
4533 }
4534
4535 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4536 // Make sure we only set exact types (the loaded class should never be merged).
4537 DCHECK(rti.IsExact());
4538 loaded_class_rti_ = rti;
4539 }
4540
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004541 const DexFile& GetDexFile() { return dex_file_; }
4542
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004543 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4544
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004545 static SideEffects SideEffectsForArchRuntimeCalls() {
4546 return SideEffects::CanTriggerGC();
4547 }
4548
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004549 DECLARE_INSTRUCTION(LoadClass);
4550
4551 private:
4552 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004553 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004554 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004555 // Whether this instruction must generate the initialization check.
4556 // Used for code generation.
4557 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004558
Calin Juravleacf735c2015-02-12 15:25:22 +00004559 ReferenceTypeInfo loaded_class_rti_;
4560
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004561 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4562};
4563
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004564class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004565 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004566 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004567 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4568 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004569 SetRawInputAt(0, current_method);
4570 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004571
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004572 bool CanBeMoved() const OVERRIDE { return true; }
4573
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004574 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4575 return other->AsLoadString()->string_index_ == string_index_;
4576 }
4577
4578 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4579
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004580 uint32_t GetStringIndex() const { return string_index_; }
4581
4582 // TODO: Can we deopt or debug when we resolve a string?
4583 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004584 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004585 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004586
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004587 static SideEffects SideEffectsForArchRuntimeCalls() {
4588 return SideEffects::CanTriggerGC();
4589 }
4590
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004591 DECLARE_INSTRUCTION(LoadString);
4592
4593 private:
4594 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004595
4596 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4597};
4598
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004599/**
4600 * Performs an initialization check on its Class object input.
4601 */
4602class HClinitCheck : public HExpression<1> {
4603 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004604 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004605 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004606 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004607 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4608 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004609 SetRawInputAt(0, constant);
4610 }
4611
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004612 bool CanBeMoved() const OVERRIDE { return true; }
4613 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4614 UNUSED(other);
4615 return true;
4616 }
4617
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004618 bool NeedsEnvironment() const OVERRIDE {
4619 // May call runtime to initialize the class.
4620 return true;
4621 }
4622
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004623
4624 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4625
4626 DECLARE_INSTRUCTION(ClinitCheck);
4627
4628 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004629 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4630};
4631
4632class HStaticFieldGet : public HExpression<1> {
4633 public:
4634 HStaticFieldGet(HInstruction* cls,
4635 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004636 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004637 bool is_volatile,
4638 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004639 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004640 Handle<mirror::DexCache> dex_cache,
4641 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004642 : HExpression(
4643 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004644 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004645 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004646 SetRawInputAt(0, cls);
4647 }
4648
Calin Juravle52c48962014-12-16 17:02:57 +00004649
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004650 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004651
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004652 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004653 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4654 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004655 }
4656
4657 size_t ComputeHashCode() const OVERRIDE {
4658 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4659 }
4660
Calin Juravle52c48962014-12-16 17:02:57 +00004661 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004662 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4663 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004664 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004665
4666 DECLARE_INSTRUCTION(StaticFieldGet);
4667
4668 private:
4669 const FieldInfo field_info_;
4670
4671 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4672};
4673
4674class HStaticFieldSet : public HTemplateInstruction<2> {
4675 public:
4676 HStaticFieldSet(HInstruction* cls,
4677 HInstruction* value,
4678 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004679 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004680 bool is_volatile,
4681 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004682 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 Handle<mirror::DexCache> dex_cache,
4684 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004685 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004686 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004687 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004688 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004689 SetRawInputAt(0, cls);
4690 SetRawInputAt(1, value);
4691 }
4692
Calin Juravle52c48962014-12-16 17:02:57 +00004693 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004694 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4695 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004696 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004697
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004698 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004699 bool GetValueCanBeNull() const { return value_can_be_null_; }
4700 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004701
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004702 DECLARE_INSTRUCTION(StaticFieldSet);
4703
4704 private:
4705 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004706 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004707
4708 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4709};
4710
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004711// Implement the move-exception DEX instruction.
4712class HLoadException : public HExpression<0> {
4713 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004714 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4715 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004716
David Brazdilbbd733e2015-08-18 17:48:17 +01004717 bool CanBeNull() const OVERRIDE { return false; }
4718
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004719 DECLARE_INSTRUCTION(LoadException);
4720
4721 private:
4722 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4723};
4724
David Brazdilcb1c0552015-08-04 16:22:25 +01004725// Implicit part of move-exception which clears thread-local exception storage.
4726// Must not be removed because the runtime expects the TLS to get cleared.
4727class HClearException : public HTemplateInstruction<0> {
4728 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004729 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4730 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004731
4732 DECLARE_INSTRUCTION(ClearException);
4733
4734 private:
4735 DISALLOW_COPY_AND_ASSIGN(HClearException);
4736};
4737
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004738class HThrow : public HTemplateInstruction<1> {
4739 public:
4740 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004741 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004742 SetRawInputAt(0, exception);
4743 }
4744
4745 bool IsControlFlow() const OVERRIDE { return true; }
4746
4747 bool NeedsEnvironment() const OVERRIDE { return true; }
4748
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004749 bool CanThrow() const OVERRIDE { return true; }
4750
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004751
4752 DECLARE_INSTRUCTION(Throw);
4753
4754 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004755 DISALLOW_COPY_AND_ASSIGN(HThrow);
4756};
4757
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004758/**
4759 * Implementation strategies for the code generator of a HInstanceOf
4760 * or `HCheckCast`.
4761 */
4762enum class TypeCheckKind {
4763 kExactCheck, // Can do a single class compare.
4764 kClassHierarchyCheck, // Can just walk the super class chain.
4765 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
4766 kInterfaceCheck, // No optimization yet when checking against an interface.
4767 kArrayObjectCheck, // Can just check if the array is not primitive.
4768 kArrayCheck // No optimization yet when checking against a generic array.
4769};
4770
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004771class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004772 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004773 HInstanceOf(HInstruction* object,
4774 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004775 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004776 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004777 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004778 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004779 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004780 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004781 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004782 SetRawInputAt(0, object);
4783 SetRawInputAt(1, constant);
4784 }
4785
4786 bool CanBeMoved() const OVERRIDE { return true; }
4787
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004788 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004789 return true;
4790 }
4791
4792 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004793 return false;
4794 }
4795
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004796 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
4797
4798 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004799
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004800 // Used only in code generation.
4801 bool MustDoNullCheck() const { return must_do_null_check_; }
4802 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4803
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004804 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
4805 return (check_kind == TypeCheckKind::kExactCheck)
4806 ? SideEffects::None()
4807 // Mips currently does runtime calls for any other checks.
4808 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004809 }
4810
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004811 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004812
4813 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004814 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004815 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004816
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004817 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4818};
4819
Calin Juravleb1498f62015-02-16 13:13:29 +00004820class HBoundType : public HExpression<1> {
4821 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004822 // Constructs an HBoundType with the given upper_bound.
4823 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004824 HBoundType(HInstruction* input,
4825 ReferenceTypeInfo upper_bound,
4826 bool upper_can_be_null,
4827 uint32_t dex_pc = kNoDexPc)
4828 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004829 upper_bound_(upper_bound),
4830 upper_can_be_null_(upper_can_be_null),
4831 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004832 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004833 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004834 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004835 }
4836
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004837 // GetUpper* should only be used in reference type propagation.
4838 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4839 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004840
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004841 void SetCanBeNull(bool can_be_null) {
4842 DCHECK(upper_can_be_null_ || !can_be_null);
4843 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004844 }
4845
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004846 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4847
Calin Juravleb1498f62015-02-16 13:13:29 +00004848 DECLARE_INSTRUCTION(BoundType);
4849
4850 private:
4851 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004852 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4853 // It is used to bound the type in cases like:
4854 // if (x instanceof ClassX) {
4855 // // uper_bound_ will be ClassX
4856 // }
4857 const ReferenceTypeInfo upper_bound_;
4858 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4859 // is false then can_be_null_ cannot be true).
4860 const bool upper_can_be_null_;
4861 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004862
4863 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4864};
4865
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004866class HCheckCast : public HTemplateInstruction<2> {
4867 public:
4868 HCheckCast(HInstruction* object,
4869 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004870 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004871 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004872 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004873 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004874 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004875 SetRawInputAt(0, object);
4876 SetRawInputAt(1, constant);
4877 }
4878
4879 bool CanBeMoved() const OVERRIDE { return true; }
4880
4881 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4882 return true;
4883 }
4884
4885 bool NeedsEnvironment() const OVERRIDE {
4886 // Instruction may throw a CheckCastError.
4887 return true;
4888 }
4889
4890 bool CanThrow() const OVERRIDE { return true; }
4891
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004892 bool MustDoNullCheck() const { return must_do_null_check_; }
4893 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004894 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004895
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004896 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004897
4898 DECLARE_INSTRUCTION(CheckCast);
4899
4900 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004901 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004902 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004903
4904 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004905};
4906
Calin Juravle27df7582015-04-17 19:12:31 +01004907class HMemoryBarrier : public HTemplateInstruction<0> {
4908 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004909 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004910 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004911 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004912 barrier_kind_(barrier_kind) {}
4913
4914 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4915
4916 DECLARE_INSTRUCTION(MemoryBarrier);
4917
4918 private:
4919 const MemBarrierKind barrier_kind_;
4920
4921 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4922};
4923
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004924class HMonitorOperation : public HTemplateInstruction<1> {
4925 public:
4926 enum OperationKind {
4927 kEnter,
4928 kExit,
4929 };
4930
4931 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004932 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004933 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
4934 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004935 SetRawInputAt(0, object);
4936 }
4937
4938 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004939 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4940
4941 bool CanThrow() const OVERRIDE {
4942 // Verifier guarantees that monitor-exit cannot throw.
4943 // This is important because it allows the HGraphBuilder to remove
4944 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4945 return IsEnter();
4946 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004947
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004948
4949 bool IsEnter() const { return kind_ == kEnter; }
4950
4951 DECLARE_INSTRUCTION(MonitorOperation);
4952
Calin Juravle52c48962014-12-16 17:02:57 +00004953 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004954 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004955
4956 private:
4957 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4958};
4959
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004960/**
4961 * A HInstruction used as a marker for the replacement of new + <init>
4962 * of a String to a call to a StringFactory. Only baseline will see
4963 * the node at code generation, where it will be be treated as null.
4964 * When compiling non-baseline, `HFakeString` instructions are being removed
4965 * in the instruction simplifier.
4966 */
4967class HFakeString : public HTemplateInstruction<0> {
4968 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004969 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
4970 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004971
4972 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4973
4974 DECLARE_INSTRUCTION(FakeString);
4975
4976 private:
4977 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4978};
4979
Vladimir Markof9f64412015-09-02 14:05:49 +01004980class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004981 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004982 MoveOperands(Location source,
4983 Location destination,
4984 Primitive::Type type,
4985 HInstruction* instruction)
4986 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004987
4988 Location GetSource() const { return source_; }
4989 Location GetDestination() const { return destination_; }
4990
4991 void SetSource(Location value) { source_ = value; }
4992 void SetDestination(Location value) { destination_ = value; }
4993
4994 // The parallel move resolver marks moves as "in-progress" by clearing the
4995 // destination (but not the source).
4996 Location MarkPending() {
4997 DCHECK(!IsPending());
4998 Location dest = destination_;
4999 destination_ = Location::NoLocation();
5000 return dest;
5001 }
5002
5003 void ClearPending(Location dest) {
5004 DCHECK(IsPending());
5005 destination_ = dest;
5006 }
5007
5008 bool IsPending() const {
5009 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5010 return destination_.IsInvalid() && !source_.IsInvalid();
5011 }
5012
5013 // True if this blocks a move from the given location.
5014 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005015 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005016 }
5017
5018 // A move is redundant if it's been eliminated, if its source and
5019 // destination are the same, or if its destination is unneeded.
5020 bool IsRedundant() const {
5021 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5022 }
5023
5024 // We clear both operands to indicate move that's been eliminated.
5025 void Eliminate() {
5026 source_ = destination_ = Location::NoLocation();
5027 }
5028
5029 bool IsEliminated() const {
5030 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5031 return source_.IsInvalid();
5032 }
5033
Alexey Frunze4dda3372015-06-01 18:31:49 -07005034 Primitive::Type GetType() const { return type_; }
5035
Nicolas Geoffray90218252015-04-15 11:56:51 +01005036 bool Is64BitMove() const {
5037 return Primitive::Is64BitType(type_);
5038 }
5039
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005040 HInstruction* GetInstruction() const { return instruction_; }
5041
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005042 private:
5043 Location source_;
5044 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005045 // The type this move is for.
5046 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005047 // The instruction this move is assocatied with. Null when this move is
5048 // for moving an input in the expected locations of user (including a phi user).
5049 // This is only used in debug mode, to ensure we do not connect interval siblings
5050 // in the same parallel move.
5051 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005052};
5053
5054static constexpr size_t kDefaultNumberOfMoves = 4;
5055
5056class HParallelMove : public HTemplateInstruction<0> {
5057 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005058 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
5059 : HTemplateInstruction(SideEffects::None(), dex_pc), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005060
Nicolas Geoffray90218252015-04-15 11:56:51 +01005061 void AddMove(Location source,
5062 Location destination,
5063 Primitive::Type type,
5064 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005065 DCHECK(source.IsValid());
5066 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005067 if (kIsDebugBuild) {
5068 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005069 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005070 if (moves_.Get(i).GetInstruction() == instruction) {
5071 // Special case the situation where the move is for the spill slot
5072 // of the instruction.
5073 if ((GetPrevious() == instruction)
5074 || ((GetPrevious() == nullptr)
5075 && instruction->IsPhi()
5076 && instruction->GetBlock() == GetBlock())) {
5077 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
5078 << "Doing parallel moves for the same instruction.";
5079 } else {
5080 DCHECK(false) << "Doing parallel moves for the same instruction.";
5081 }
5082 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005083 }
5084 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005085 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08005086 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005087 << "Overlapped destination for two moves in a parallel move: "
5088 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
5089 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005090 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005091 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01005092 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005093 }
5094
5095 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005096 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005097 }
5098
5099 size_t NumMoves() const { return moves_.Size(); }
5100
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005101 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005102
5103 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005104 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005105
5106 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5107};
5108
Mark Mendell0616ae02015-04-17 12:49:27 -04005109} // namespace art
5110
5111#ifdef ART_ENABLE_CODEGEN_x86
5112#include "nodes_x86.h"
5113#endif
5114
5115namespace art {
5116
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005117class HGraphVisitor : public ValueObject {
5118 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005119 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5120 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005121
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005122 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005123 virtual void VisitBasicBlock(HBasicBlock* block);
5124
Roland Levillain633021e2014-10-01 14:12:25 +01005125 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005126 void VisitInsertionOrder();
5127
Roland Levillain633021e2014-10-01 14:12:25 +01005128 // Visit the graph following dominator tree reverse post-order.
5129 void VisitReversePostOrder();
5130
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005131 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005132
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005133 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005134#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005135 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5136
5137 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5138
5139#undef DECLARE_VISIT_INSTRUCTION
5140
5141 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005142 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005143
5144 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5145};
5146
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005147class HGraphDelegateVisitor : public HGraphVisitor {
5148 public:
5149 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5150 virtual ~HGraphDelegateVisitor() {}
5151
5152 // Visit functions that delegate to to super class.
5153#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005154 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005155
5156 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5157
5158#undef DECLARE_VISIT_INSTRUCTION
5159
5160 private:
5161 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5162};
5163
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005164class HInsertionOrderIterator : public ValueObject {
5165 public:
5166 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5167
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005168 bool Done() const { return index_ == graph_.GetBlocks().size(); }
5169 HBasicBlock* Current() const { return graph_.GetBlock(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005170 void Advance() { ++index_; }
5171
5172 private:
5173 const HGraph& graph_;
5174 size_t index_;
5175
5176 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5177};
5178
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005179class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005180 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005181 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5182 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005183 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005184 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005185
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005186 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5187 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005188 void Advance() { ++index_; }
5189
5190 private:
5191 const HGraph& graph_;
5192 size_t index_;
5193
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005194 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005195};
5196
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005197class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005198 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005199 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005200 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005201 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005202 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005203 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005204
5205 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005206 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005207 void Advance() { --index_; }
5208
5209 private:
5210 const HGraph& graph_;
5211 size_t index_;
5212
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005213 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005214};
5215
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005216class HLinearPostOrderIterator : public ValueObject {
5217 public:
5218 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005219 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005220
5221 bool Done() const { return index_ == 0; }
5222
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005223 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005224
5225 void Advance() {
5226 --index_;
5227 DCHECK_GE(index_, 0U);
5228 }
5229
5230 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005231 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005232 size_t index_;
5233
5234 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5235};
5236
5237class HLinearOrderIterator : public ValueObject {
5238 public:
5239 explicit HLinearOrderIterator(const HGraph& graph)
5240 : order_(graph.GetLinearOrder()), index_(0) {}
5241
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005242 bool Done() const { return index_ == order_.size(); }
5243 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005244 void Advance() { ++index_; }
5245
5246 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005247 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005248 size_t index_;
5249
5250 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5251};
5252
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005253// Iterator over the blocks that art part of the loop. Includes blocks part
5254// of an inner loop. The order in which the blocks are iterated is on their
5255// block id.
5256class HBlocksInLoopIterator : public ValueObject {
5257 public:
5258 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5259 : blocks_in_loop_(info.GetBlocks()),
5260 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5261 index_(0) {
5262 if (!blocks_in_loop_.IsBitSet(index_)) {
5263 Advance();
5264 }
5265 }
5266
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005267 bool Done() const { return index_ == blocks_.size(); }
5268 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005269 void Advance() {
5270 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005271 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005272 if (blocks_in_loop_.IsBitSet(index_)) {
5273 break;
5274 }
5275 }
5276 }
5277
5278 private:
5279 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005280 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005281 size_t index_;
5282
5283 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5284};
5285
Mingyao Yang3584bce2015-05-19 16:01:59 -07005286// Iterator over the blocks that art part of the loop. Includes blocks part
5287// of an inner loop. The order in which the blocks are iterated is reverse
5288// post order.
5289class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5290 public:
5291 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5292 : blocks_in_loop_(info.GetBlocks()),
5293 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5294 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005295 DCHECK(!blocks_.empty());
5296 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005297 Advance();
5298 }
5299 }
5300
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005301 bool Done() const { return index_ == blocks_.size(); }
5302 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005303 void Advance() {
5304 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005305 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5306 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005307 break;
5308 }
5309 }
5310 }
5311
5312 private:
5313 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005314 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005315 size_t index_;
5316
5317 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5318};
5319
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005320inline int64_t Int64FromConstant(HConstant* constant) {
5321 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5322 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5323 : constant->AsLongConstant()->GetValue();
5324}
5325
Vladimir Marko58155012015-08-19 12:49:41 +00005326inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5327 // For the purposes of the compiler, the dex files must actually be the same object
5328 // if we want to safely treat them as the same. This is especially important for JIT
5329 // as custom class loaders can open the same underlying file (or memory) multiple
5330 // times and provide different class resolution but no two class loaders should ever
5331 // use the same DexFile object - doing so is an unsupported hack that can lead to
5332 // all sorts of weird failures.
5333 return &lhs == &rhs;
5334}
5335
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005336} // namespace art
5337
5338#endif // ART_COMPILER_OPTIMIZING_NODES_H_