blob: 6b0ccf8690040f33c45e568379ffc9e6dfcf952d [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:
Vladimir Marko211c2112015-09-24 16:52:33 +01002412 HPackedSwitch(int32_t start_value, uint32_t num_entries, HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002413 uint32_t dex_pc = kNoDexPc)
2414 : HTemplateInstruction(SideEffects::None(), dex_pc),
2415 start_value_(start_value),
2416 num_entries_(num_entries) {
2417 SetRawInputAt(0, input);
2418 }
2419
2420 bool IsControlFlow() const OVERRIDE { return true; }
2421
2422 int32_t GetStartValue() const { return start_value_; }
2423
Vladimir Marko211c2112015-09-24 16:52:33 +01002424 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002425
2426 HBasicBlock* GetDefaultBlock() const {
2427 // Last entry is the default block.
2428 return GetBlock()->GetSuccessor(num_entries_);
2429 }
2430 DECLARE_INSTRUCTION(PackedSwitch);
2431
2432 private:
2433 int32_t start_value_;
Vladimir Marko211c2112015-09-24 16:52:33 +01002434 uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002435
2436 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2437};
2438
Roland Levillain88cb1752014-10-20 16:36:47 +01002439class HUnaryOperation : public HExpression<1> {
2440 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002441 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2442 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002443 SetRawInputAt(0, input);
2444 }
2445
2446 HInstruction* GetInput() const { return InputAt(0); }
2447 Primitive::Type GetResultType() const { return GetType(); }
2448
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002449 bool CanBeMoved() const OVERRIDE { return true; }
2450 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002451 UNUSED(other);
2452 return true;
2453 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002454
Roland Levillain9240d6a2014-10-20 16:47:04 +01002455 // Try to statically evaluate `operation` and return a HConstant
2456 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002457 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002458 HConstant* TryStaticEvaluation() const;
2459
2460 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002461 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2462 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002463
Roland Levillain88cb1752014-10-20 16:36:47 +01002464 DECLARE_INSTRUCTION(UnaryOperation);
2465
2466 private:
2467 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2468};
2469
Dave Allison20dfc792014-06-16 20:44:29 -07002470class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002471 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002472 HBinaryOperation(Primitive::Type result_type,
2473 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002474 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002475 SideEffects side_effects = SideEffects::None(),
2476 uint32_t dex_pc = kNoDexPc)
2477 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002478 SetRawInputAt(0, left);
2479 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002480 }
2481
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002482 HInstruction* GetLeft() const { return InputAt(0); }
2483 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002484 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002485
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002486 virtual bool IsCommutative() const { return false; }
2487
2488 // Put constant on the right.
2489 // Returns whether order is changed.
2490 bool OrderInputsWithConstantOnTheRight() {
2491 HInstruction* left = InputAt(0);
2492 HInstruction* right = InputAt(1);
2493 if (left->IsConstant() && !right->IsConstant()) {
2494 ReplaceInput(right, 0);
2495 ReplaceInput(left, 1);
2496 return true;
2497 }
2498 return false;
2499 }
2500
2501 // Order inputs by instruction id, but favor constant on the right side.
2502 // This helps GVN for commutative ops.
2503 void OrderInputs() {
2504 DCHECK(IsCommutative());
2505 HInstruction* left = InputAt(0);
2506 HInstruction* right = InputAt(1);
2507 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2508 return;
2509 }
2510 if (OrderInputsWithConstantOnTheRight()) {
2511 return;
2512 }
2513 // Order according to instruction id.
2514 if (left->GetId() > right->GetId()) {
2515 ReplaceInput(right, 0);
2516 ReplaceInput(left, 1);
2517 }
2518 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002519
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002520 bool CanBeMoved() const OVERRIDE { return true; }
2521 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002522 UNUSED(other);
2523 return true;
2524 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002525
Roland Levillain9240d6a2014-10-20 16:47:04 +01002526 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002527 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002528 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002529 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002530
2531 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002532 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2533 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2534 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2535 HLongConstant* y ATTRIBUTE_UNUSED) const {
2536 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2537 return nullptr;
2538 }
2539 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2540 HIntConstant* y ATTRIBUTE_UNUSED) const {
2541 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2542 return nullptr;
2543 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002544
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002545 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002546 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002547 HConstant* GetConstantRight() const;
2548
2549 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002550 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002551 HInstruction* GetLeastConstantLeft() const;
2552
Roland Levillainccc07a92014-09-16 14:48:16 +01002553 DECLARE_INSTRUCTION(BinaryOperation);
2554
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002555 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002556 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2557};
2558
Mark Mendellc4701932015-04-10 13:18:51 -04002559// The comparison bias applies for floating point operations and indicates how NaN
2560// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002561enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002562 kNoBias, // bias is not applicable (i.e. for long operation)
2563 kGtBias, // return 1 for NaN comparisons
2564 kLtBias, // return -1 for NaN comparisons
2565};
2566
Dave Allison20dfc792014-06-16 20:44:29 -07002567class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002568 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002569 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2570 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002571 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002572 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002573
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002574 bool NeedsMaterialization() const { return needs_materialization_; }
2575 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002576
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002577 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002578 // `instruction`, and disregard moves in between.
2579 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002580
Dave Allison20dfc792014-06-16 20:44:29 -07002581 DECLARE_INSTRUCTION(Condition);
2582
2583 virtual IfCondition GetCondition() const = 0;
2584
Mark Mendellc4701932015-04-10 13:18:51 -04002585 virtual IfCondition GetOppositeCondition() const = 0;
2586
Roland Levillain4fa13f62015-07-06 18:11:54 +01002587 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002588
2589 void SetBias(ComparisonBias bias) { bias_ = bias; }
2590
2591 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2592 return bias_ == other->AsCondition()->bias_;
2593 }
2594
Roland Levillain4fa13f62015-07-06 18:11:54 +01002595 bool IsFPConditionTrueIfNaN() const {
2596 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2597 IfCondition if_cond = GetCondition();
2598 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2599 }
2600
2601 bool IsFPConditionFalseIfNaN() const {
2602 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2603 IfCondition if_cond = GetCondition();
2604 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2605 }
2606
Dave Allison20dfc792014-06-16 20:44:29 -07002607 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002608 // For register allocation purposes, returns whether this instruction needs to be
2609 // materialized (that is, not just be in the processor flags).
2610 bool needs_materialization_;
2611
Mark Mendellc4701932015-04-10 13:18:51 -04002612 // Needed if we merge a HCompare into a HCondition.
2613 ComparisonBias bias_;
2614
Dave Allison20dfc792014-06-16 20:44:29 -07002615 DISALLOW_COPY_AND_ASSIGN(HCondition);
2616};
2617
2618// Instruction to check if two inputs are equal to each other.
2619class HEqual : public HCondition {
2620 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002621 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2622 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002623
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002624 bool IsCommutative() const OVERRIDE { return true; }
2625
Roland Levillain9867bc72015-08-05 10:21:34 +01002626 template <typename T> bool Compute(T x, T y) const { return x == y; }
2627
2628 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002629 return GetBlock()->GetGraph()->GetIntConstant(
2630 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002631 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002632 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002633 return GetBlock()->GetGraph()->GetIntConstant(
2634 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002635 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002636
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002637 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002638
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002639 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002640 return kCondEQ;
2641 }
2642
Mark Mendellc4701932015-04-10 13:18:51 -04002643 IfCondition GetOppositeCondition() const OVERRIDE {
2644 return kCondNE;
2645 }
2646
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002647 private:
2648 DISALLOW_COPY_AND_ASSIGN(HEqual);
2649};
2650
Dave Allison20dfc792014-06-16 20:44:29 -07002651class HNotEqual : public HCondition {
2652 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002653 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2654 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002655
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002656 bool IsCommutative() const OVERRIDE { return true; }
2657
Roland Levillain9867bc72015-08-05 10:21:34 +01002658 template <typename T> bool Compute(T x, T y) const { return x != y; }
2659
2660 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002661 return GetBlock()->GetGraph()->GetIntConstant(
2662 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002663 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002664 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002665 return GetBlock()->GetGraph()->GetIntConstant(
2666 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002667 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002668
Dave Allison20dfc792014-06-16 20:44:29 -07002669 DECLARE_INSTRUCTION(NotEqual);
2670
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002671 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002672 return kCondNE;
2673 }
2674
Mark Mendellc4701932015-04-10 13:18:51 -04002675 IfCondition GetOppositeCondition() const OVERRIDE {
2676 return kCondEQ;
2677 }
2678
Dave Allison20dfc792014-06-16 20:44:29 -07002679 private:
2680 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2681};
2682
2683class HLessThan : public HCondition {
2684 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002685 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2686 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002687
Roland Levillain9867bc72015-08-05 10:21:34 +01002688 template <typename T> bool Compute(T x, T y) const { return x < y; }
2689
2690 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002691 return GetBlock()->GetGraph()->GetIntConstant(
2692 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002693 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002694 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002695 return GetBlock()->GetGraph()->GetIntConstant(
2696 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002697 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002698
Dave Allison20dfc792014-06-16 20:44:29 -07002699 DECLARE_INSTRUCTION(LessThan);
2700
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002701 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002702 return kCondLT;
2703 }
2704
Mark Mendellc4701932015-04-10 13:18:51 -04002705 IfCondition GetOppositeCondition() const OVERRIDE {
2706 return kCondGE;
2707 }
2708
Dave Allison20dfc792014-06-16 20:44:29 -07002709 private:
2710 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2711};
2712
2713class HLessThanOrEqual : public HCondition {
2714 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002715 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2716 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002717
Roland Levillain9867bc72015-08-05 10:21:34 +01002718 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2719
2720 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721 return GetBlock()->GetGraph()->GetIntConstant(
2722 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002723 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002724 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002725 return GetBlock()->GetGraph()->GetIntConstant(
2726 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002727 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002728
Dave Allison20dfc792014-06-16 20:44:29 -07002729 DECLARE_INSTRUCTION(LessThanOrEqual);
2730
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002731 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002732 return kCondLE;
2733 }
2734
Mark Mendellc4701932015-04-10 13:18:51 -04002735 IfCondition GetOppositeCondition() const OVERRIDE {
2736 return kCondGT;
2737 }
2738
Dave Allison20dfc792014-06-16 20:44:29 -07002739 private:
2740 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2741};
2742
2743class HGreaterThan : public HCondition {
2744 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002745 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2746 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002747
Roland Levillain9867bc72015-08-05 10:21:34 +01002748 template <typename T> bool Compute(T x, T y) const { return x > y; }
2749
2750 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002751 return GetBlock()->GetGraph()->GetIntConstant(
2752 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002753 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002754 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002755 return GetBlock()->GetGraph()->GetIntConstant(
2756 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002757 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002758
Dave Allison20dfc792014-06-16 20:44:29 -07002759 DECLARE_INSTRUCTION(GreaterThan);
2760
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002761 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002762 return kCondGT;
2763 }
2764
Mark Mendellc4701932015-04-10 13:18:51 -04002765 IfCondition GetOppositeCondition() const OVERRIDE {
2766 return kCondLE;
2767 }
2768
Dave Allison20dfc792014-06-16 20:44:29 -07002769 private:
2770 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2771};
2772
2773class HGreaterThanOrEqual : public HCondition {
2774 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002775 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2776 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002777
Roland Levillain9867bc72015-08-05 10:21:34 +01002778 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2779
2780 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002781 return GetBlock()->GetGraph()->GetIntConstant(
2782 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002783 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002784 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002785 return GetBlock()->GetGraph()->GetIntConstant(
2786 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002787 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002788
Dave Allison20dfc792014-06-16 20:44:29 -07002789 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2790
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002791 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002792 return kCondGE;
2793 }
2794
Mark Mendellc4701932015-04-10 13:18:51 -04002795 IfCondition GetOppositeCondition() const OVERRIDE {
2796 return kCondLT;
2797 }
2798
Dave Allison20dfc792014-06-16 20:44:29 -07002799 private:
2800 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2801};
2802
2803
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002804// Instruction to check how two inputs compare to each other.
2805// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2806class HCompare : public HBinaryOperation {
2807 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002808 HCompare(Primitive::Type type,
2809 HInstruction* first,
2810 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002811 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002812 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002813 : HBinaryOperation(Primitive::kPrimInt,
2814 first,
2815 second,
2816 SideEffectsForArchRuntimeCalls(type),
2817 dex_pc),
2818 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002819 DCHECK_EQ(type, first->GetType());
2820 DCHECK_EQ(type, second->GetType());
2821 }
2822
Roland Levillain9867bc72015-08-05 10:21:34 +01002823 template <typename T>
2824 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002825
Roland Levillain9867bc72015-08-05 10:21:34 +01002826 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002827 return GetBlock()->GetGraph()->GetIntConstant(
2828 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002829 }
2830 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002831 return GetBlock()->GetGraph()->GetIntConstant(
2832 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002833 }
2834
Calin Juravleddb7df22014-11-25 20:56:51 +00002835 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2836 return bias_ == other->AsCompare()->bias_;
2837 }
2838
Mark Mendellc4701932015-04-10 13:18:51 -04002839 ComparisonBias GetBias() const { return bias_; }
2840
Roland Levillain4fa13f62015-07-06 18:11:54 +01002841 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002842
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002843
2844 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2845 // MIPS64 uses a runtime call for FP comparisons.
2846 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2847 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002848
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002849 DECLARE_INSTRUCTION(Compare);
2850
2851 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002852 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002853
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002854 DISALLOW_COPY_AND_ASSIGN(HCompare);
2855};
2856
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002857// A local in the graph. Corresponds to a Dex register.
2858class HLocal : public HTemplateInstruction<0> {
2859 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002860 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002861 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002862
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002863 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002864
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002865 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002866
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002867 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002868 // The Dex register number.
2869 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002870
2871 DISALLOW_COPY_AND_ASSIGN(HLocal);
2872};
2873
2874// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002875class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002876 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002877 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2878 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002879 SetRawInputAt(0, local);
2880 }
2881
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002882 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2883
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002884 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002885
2886 private:
2887 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2888};
2889
2890// Store a value in a given local. This instruction has two inputs: the value
2891// and the local.
2892class HStoreLocal : public HTemplateInstruction<2> {
2893 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002894 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2895 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002896 SetRawInputAt(0, local);
2897 SetRawInputAt(1, value);
2898 }
2899
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002900 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2901
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002902 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002903
2904 private:
2905 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2906};
2907
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002908class HFloatConstant : public HConstant {
2909 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002910 float GetValue() const { return value_; }
2911
David Brazdil77a48ae2015-09-15 12:34:04 +00002912 uint64_t GetValueAsUint64() const OVERRIDE {
2913 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2914 }
2915
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002916 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002917 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002918 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002919 }
2920
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002921 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002922
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002923 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002924 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002925 }
2926 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002927 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002928 }
2929 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002930 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2931 }
2932 bool IsNaN() const {
2933 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002934 }
2935
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002936 DECLARE_INSTRUCTION(FloatConstant);
2937
2938 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002939 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2940 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2941 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2942 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002943
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002944 const float value_;
2945
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002946 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002947 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002948 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002949 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2950};
2951
2952class HDoubleConstant : public HConstant {
2953 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002954 double GetValue() const { return value_; }
2955
David Brazdil77a48ae2015-09-15 12:34:04 +00002956 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2957
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002958 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002959 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002960 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002961 }
2962
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002963 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002964
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002965 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002966 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002967 }
2968 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002969 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002970 }
2971 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002972 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2973 }
2974 bool IsNaN() const {
2975 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002976 }
2977
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002978 DECLARE_INSTRUCTION(DoubleConstant);
2979
2980 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002981 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2982 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2983 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2984 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002985
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002986 const double value_;
2987
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002988 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002989 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002990 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002991 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2992};
2993
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002994enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002995#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002996#include "intrinsics_list.h"
2997 kNone,
2998 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2999#undef INTRINSICS_LIST
3000#undef OPTIMIZING_INTRINSICS
3001};
3002std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3003
Agi Csaki05f20562015-08-19 14:58:14 -07003004enum IntrinsicNeedsEnvironmentOrCache {
3005 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3006 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003007};
3008
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003009class HInvoke : public HInstruction {
3010 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003011 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003012
3013 // Runtime needs to walk the stack, so Dex -> Dex calls need to
3014 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07003015 bool NeedsEnvironment() const OVERRIDE {
3016 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
3017 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003018
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003019 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003020 SetRawInputAt(index, argument);
3021 }
3022
Roland Levillain3e3d7332015-04-28 11:00:54 +01003023 // Return the number of arguments. This number can be lower than
3024 // the number of inputs returned by InputCount(), as some invoke
3025 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3026 // inputs at the end of their list of inputs.
3027 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3028
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003029 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003030
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003031
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003032 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003033 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003034
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003035 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3036
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003037 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003038 return intrinsic_;
3039 }
3040
Agi Csaki05f20562015-08-19 14:58:14 -07003041 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003042 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07003043 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003044 }
3045
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003046 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003047 return GetEnvironment()->GetParent() != nullptr;
3048 }
3049
3050 bool CanThrow() const OVERRIDE { return true; }
3051
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003052 DECLARE_INSTRUCTION(Invoke);
3053
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003054 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003055 HInvoke(ArenaAllocator* arena,
3056 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003057 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003058 Primitive::Type return_type,
3059 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003060 uint32_t dex_method_index,
3061 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003062 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003063 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003064 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003065 inputs_(number_of_arguments + number_of_other_inputs,
3066 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003067 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003068 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003069 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003070 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07003071 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003072 }
3073
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003074 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
3075 DCHECK_LT(index, InputCount());
3076 return inputs_[index];
3077 }
3078
David Brazdil1abb4192015-02-17 18:33:36 +00003079 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003080 DCHECK_LT(index, InputCount());
3081 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003082 }
3083
Roland Levillain3e3d7332015-04-28 11:00:54 +01003084 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003085 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003086 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003087 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003088 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003089 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003090 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003091
3092 private:
3093 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3094};
3095
Calin Juravle175dc732015-08-25 15:42:32 +01003096class HInvokeUnresolved : public HInvoke {
3097 public:
3098 HInvokeUnresolved(ArenaAllocator* arena,
3099 uint32_t number_of_arguments,
3100 Primitive::Type return_type,
3101 uint32_t dex_pc,
3102 uint32_t dex_method_index,
3103 InvokeType invoke_type)
3104 : HInvoke(arena,
3105 number_of_arguments,
3106 0u /* number_of_other_inputs */,
3107 return_type,
3108 dex_pc,
3109 dex_method_index,
3110 invoke_type) {
3111 }
3112
3113 DECLARE_INSTRUCTION(InvokeUnresolved);
3114
3115 private:
3116 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3117};
3118
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003119class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003120 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003121 // Requirements of this method call regarding the class
3122 // initialization (clinit) check of its declaring class.
3123 enum class ClinitCheckRequirement {
3124 kNone, // Class already initialized.
3125 kExplicit, // Static call having explicit clinit check as last input.
3126 kImplicit, // Static call implicitly requiring a clinit check.
3127 };
3128
Vladimir Marko58155012015-08-19 12:49:41 +00003129 // Determines how to load the target ArtMethod*.
3130 enum class MethodLoadKind {
3131 // Use a String init ArtMethod* loaded from Thread entrypoints.
3132 kStringInit,
3133
3134 // Use the method's own ArtMethod* loaded by the register allocator.
3135 kRecursive,
3136
3137 // Use ArtMethod* at a known address, embed the direct address in the code.
3138 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3139 kDirectAddress,
3140
3141 // Use ArtMethod* at an address that will be known at link time, embed the direct
3142 // address in the code. If the image is relocatable, emit .patch_oat entry.
3143 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3144 // the image relocatable or not.
3145 kDirectAddressWithFixup,
3146
3147 // Load from resoved methods array in the dex cache using a PC-relative load.
3148 // Used when we need to use the dex cache, for example for invoke-static that
3149 // may cause class initialization (the entry may point to a resolution method),
3150 // and we know that we can access the dex cache arrays using a PC-relative load.
3151 kDexCachePcRelative,
3152
3153 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3154 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3155 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3156 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3157 kDexCacheViaMethod,
3158 };
3159
3160 // Determines the location of the code pointer.
3161 enum class CodePtrLocation {
3162 // Recursive call, use local PC-relative call instruction.
3163 kCallSelf,
3164
3165 // Use PC-relative call instruction patched at link time.
3166 // Used for calls within an oat file, boot->boot or app->app.
3167 kCallPCRelative,
3168
3169 // Call to a known target address, embed the direct address in code.
3170 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3171 kCallDirect,
3172
3173 // Call to a target address that will be known at link time, embed the direct
3174 // address in code. If the image is relocatable, emit .patch_oat entry.
3175 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3176 // the image relocatable or not.
3177 kCallDirectWithFixup,
3178
3179 // Use code pointer from the ArtMethod*.
3180 // Used when we don't know the target code. This is also the last-resort-kind used when
3181 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3182 kCallArtMethod,
3183 };
3184
3185 struct DispatchInfo {
3186 const MethodLoadKind method_load_kind;
3187 const CodePtrLocation code_ptr_location;
3188 // The method load data holds
3189 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3190 // Note that there are multiple string init methods, each having its own offset.
3191 // - the method address for kDirectAddress
3192 // - the dex cache arrays offset for kDexCachePcRel.
3193 const uint64_t method_load_data;
3194 const uint64_t direct_code_ptr;
3195 };
3196
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003197 HInvokeStaticOrDirect(ArenaAllocator* arena,
3198 uint32_t number_of_arguments,
3199 Primitive::Type return_type,
3200 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003201 uint32_t method_index,
3202 MethodReference target_method,
3203 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003204 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003205 InvokeType invoke_type,
3206 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003207 : HInvoke(arena,
3208 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003209 // There is one extra argument for the HCurrentMethod node, and
3210 // potentially one other if the clinit check is explicit, and one other
3211 // if the method is a string factory.
3212 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003213 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003214 return_type,
3215 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003216 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003217 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003218 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003219 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003220 target_method_(target_method),
3221 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003222
Calin Juravle641547a2015-04-21 22:08:51 +01003223 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3224 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003225 // We access the method via the dex cache so we can't do an implicit null check.
3226 // TODO: for intrinsics we can generate implicit null checks.
3227 return false;
3228 }
3229
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003230 bool CanBeNull() const OVERRIDE {
3231 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3232 }
3233
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003234 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003235 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3236 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3237 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003238 bool NeedsDexCache() const OVERRIDE {
3239 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3240 return !IsRecursive() && !IsStringInit();
3241 }
Vladimir Marko58155012015-08-19 12:49:41 +00003242 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003243 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003244 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3245 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3246 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3247 MethodReference GetTargetMethod() const { return target_method_; }
3248
3249 int32_t GetStringInitOffset() const {
3250 DCHECK(IsStringInit());
3251 return dispatch_info_.method_load_data;
3252 }
3253
3254 uint64_t GetMethodAddress() const {
3255 DCHECK(HasMethodAddress());
3256 return dispatch_info_.method_load_data;
3257 }
3258
3259 uint32_t GetDexCacheArrayOffset() const {
3260 DCHECK(HasPcRelDexCache());
3261 return dispatch_info_.method_load_data;
3262 }
3263
3264 uint64_t GetDirectCodePtr() const {
3265 DCHECK(HasDirectCodePtr());
3266 return dispatch_info_.direct_code_ptr;
3267 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003268
Calin Juravle68ad6492015-08-18 17:08:12 +01003269 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3270
Roland Levillain4c0eb422015-04-24 16:43:49 +01003271 // Is this instruction a call to a static method?
3272 bool IsStatic() const {
3273 return GetInvokeType() == kStatic;
3274 }
3275
Roland Levillain3e3d7332015-04-28 11:00:54 +01003276 // Remove the art::HLoadClass instruction set as last input by
3277 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3278 // the initial art::HClinitCheck instruction (only relevant for
3279 // static calls with explicit clinit check).
3280 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003281 DCHECK(IsStaticWithExplicitClinitCheck());
3282 size_t last_input_index = InputCount() - 1;
3283 HInstruction* last_input = InputAt(last_input_index);
3284 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003285 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003286 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003287 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003288 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3289 DCHECK(IsStaticWithImplicitClinitCheck());
3290 }
3291
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003292 bool IsStringFactoryFor(HFakeString* str) const {
3293 if (!IsStringInit()) return false;
3294 // +1 for the current method.
3295 if (InputCount() == (number_of_arguments_ + 1)) return false;
3296 return InputAt(InputCount() - 1)->AsFakeString() == str;
3297 }
3298
3299 void RemoveFakeStringArgumentAsLastInput() {
3300 DCHECK(IsStringInit());
3301 size_t last_input_index = InputCount() - 1;
3302 HInstruction* last_input = InputAt(last_input_index);
3303 DCHECK(last_input != nullptr);
3304 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3305 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003306 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003307 }
3308
Roland Levillain4c0eb422015-04-24 16:43:49 +01003309 // Is this a call to a static method whose declaring class has an
3310 // explicit intialization check in the graph?
3311 bool IsStaticWithExplicitClinitCheck() const {
3312 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3313 }
3314
3315 // Is this a call to a static method whose declaring class has an
3316 // implicit intialization check requirement?
3317 bool IsStaticWithImplicitClinitCheck() const {
3318 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3319 }
3320
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003321 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003322
Roland Levillain4c0eb422015-04-24 16:43:49 +01003323 protected:
3324 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3325 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3326 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3327 HInstruction* input = input_record.GetInstruction();
3328 // `input` is the last input of a static invoke marked as having
3329 // an explicit clinit check. It must either be:
3330 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3331 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3332 DCHECK(input != nullptr);
3333 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3334 }
3335 return input_record;
3336 }
3337
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003338 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003339 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003340 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003341 // The target method may refer to different dex file or method index than the original
3342 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3343 // in derived class to increase visibility.
3344 MethodReference target_method_;
3345 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003346
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003347 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003348};
3349
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003350class HInvokeVirtual : public HInvoke {
3351 public:
3352 HInvokeVirtual(ArenaAllocator* arena,
3353 uint32_t number_of_arguments,
3354 Primitive::Type return_type,
3355 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003356 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003357 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003358 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003359 vtable_index_(vtable_index) {}
3360
Calin Juravle641547a2015-04-21 22:08:51 +01003361 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003362 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003363 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003364 }
3365
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003366 uint32_t GetVTableIndex() const { return vtable_index_; }
3367
3368 DECLARE_INSTRUCTION(InvokeVirtual);
3369
3370 private:
3371 const uint32_t vtable_index_;
3372
3373 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3374};
3375
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003376class HInvokeInterface : public HInvoke {
3377 public:
3378 HInvokeInterface(ArenaAllocator* arena,
3379 uint32_t number_of_arguments,
3380 Primitive::Type return_type,
3381 uint32_t dex_pc,
3382 uint32_t dex_method_index,
3383 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003384 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003385 imt_index_(imt_index) {}
3386
Calin Juravle641547a2015-04-21 22:08:51 +01003387 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003388 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003389 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003390 }
3391
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003392 uint32_t GetImtIndex() const { return imt_index_; }
3393 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3394
3395 DECLARE_INSTRUCTION(InvokeInterface);
3396
3397 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003398 const uint32_t imt_index_;
3399
3400 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3401};
3402
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003403class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003404 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003405 HNewInstance(HCurrentMethod* current_method,
3406 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003407 uint16_t type_index,
3408 const DexFile& dex_file,
3409 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003410 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003411 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003412 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003413 entrypoint_(entrypoint) {
3414 SetRawInputAt(0, current_method);
3415 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003416
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003417 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003418 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003419
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003420 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003421 bool NeedsEnvironment() const OVERRIDE { return true; }
3422 // It may throw when called on:
3423 // - interfaces
3424 // - abstract/innaccessible/unknown classes
3425 // TODO: optimize when possible.
3426 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003427
Calin Juravle10e244f2015-01-26 18:54:32 +00003428 bool CanBeNull() const OVERRIDE { return false; }
3429
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003430 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3431
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003432 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003433
3434 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003435 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003436 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003437 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003438
3439 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3440};
3441
Roland Levillain88cb1752014-10-20 16:36:47 +01003442class HNeg : public HUnaryOperation {
3443 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003444 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3445 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003446
Roland Levillain9867bc72015-08-05 10:21:34 +01003447 template <typename T> T Compute(T x) const { return -x; }
3448
3449 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003450 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003451 }
3452 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003453 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003454 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003455
Roland Levillain88cb1752014-10-20 16:36:47 +01003456 DECLARE_INSTRUCTION(Neg);
3457
3458 private:
3459 DISALLOW_COPY_AND_ASSIGN(HNeg);
3460};
3461
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003462class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003463 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003464 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003465 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003466 uint32_t dex_pc,
3467 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003468 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003469 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003470 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003471 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003472 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003473 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003474 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003475 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003476 }
3477
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003478 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003479 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003480
3481 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003482 bool NeedsEnvironment() const OVERRIDE { return true; }
3483
Mingyao Yang0c365e62015-03-31 15:09:29 -07003484 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3485 bool CanThrow() const OVERRIDE { return true; }
3486
Calin Juravle10e244f2015-01-26 18:54:32 +00003487 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003488
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003489 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3490
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003491 DECLARE_INSTRUCTION(NewArray);
3492
3493 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003494 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003495 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003496 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003497
3498 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3499};
3500
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003501class HAdd : public HBinaryOperation {
3502 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003503 HAdd(Primitive::Type result_type,
3504 HInstruction* left,
3505 HInstruction* right,
3506 uint32_t dex_pc = kNoDexPc)
3507 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003508
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003509 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003510
Roland Levillain9867bc72015-08-05 10:21:34 +01003511 template <typename T> T Compute(T x, T y) const { return x + y; }
3512
3513 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003514 return GetBlock()->GetGraph()->GetIntConstant(
3515 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003516 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003517 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003518 return GetBlock()->GetGraph()->GetLongConstant(
3519 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003520 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003521
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003522 DECLARE_INSTRUCTION(Add);
3523
3524 private:
3525 DISALLOW_COPY_AND_ASSIGN(HAdd);
3526};
3527
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003528class HSub : public HBinaryOperation {
3529 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003530 HSub(Primitive::Type result_type,
3531 HInstruction* left,
3532 HInstruction* right,
3533 uint32_t dex_pc = kNoDexPc)
3534 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003535
Roland Levillain9867bc72015-08-05 10:21:34 +01003536 template <typename T> T Compute(T x, T y) const { return x - y; }
3537
3538 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003539 return GetBlock()->GetGraph()->GetIntConstant(
3540 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003541 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003542 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003543 return GetBlock()->GetGraph()->GetLongConstant(
3544 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003545 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003546
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003547 DECLARE_INSTRUCTION(Sub);
3548
3549 private:
3550 DISALLOW_COPY_AND_ASSIGN(HSub);
3551};
3552
Calin Juravle34bacdf2014-10-07 20:23:36 +01003553class HMul : public HBinaryOperation {
3554 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003555 HMul(Primitive::Type result_type,
3556 HInstruction* left,
3557 HInstruction* right,
3558 uint32_t dex_pc = kNoDexPc)
3559 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003560
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003561 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003562
Roland Levillain9867bc72015-08-05 10:21:34 +01003563 template <typename T> T Compute(T x, T y) const { return x * y; }
3564
3565 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003566 return GetBlock()->GetGraph()->GetIntConstant(
3567 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003568 }
3569 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003570 return GetBlock()->GetGraph()->GetLongConstant(
3571 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003572 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003573
3574 DECLARE_INSTRUCTION(Mul);
3575
3576 private:
3577 DISALLOW_COPY_AND_ASSIGN(HMul);
3578};
3579
Calin Juravle7c4954d2014-10-28 16:57:40 +00003580class HDiv : public HBinaryOperation {
3581 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003582 HDiv(Primitive::Type result_type,
3583 HInstruction* left,
3584 HInstruction* right,
3585 uint32_t dex_pc)
3586 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003587
Roland Levillain9867bc72015-08-05 10:21:34 +01003588 template <typename T>
3589 T Compute(T x, T y) const {
3590 // Our graph structure ensures we never have 0 for `y` during
3591 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003592 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003593 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003594 return (y == -1) ? -x : x / y;
3595 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003596
Roland Levillain9867bc72015-08-05 10:21:34 +01003597 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003598 return GetBlock()->GetGraph()->GetIntConstant(
3599 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003600 }
3601 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003602 return GetBlock()->GetGraph()->GetLongConstant(
3603 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003604 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003605
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003606 static SideEffects SideEffectsForArchRuntimeCalls() {
3607 // The generated code can use a runtime call.
3608 return SideEffects::CanTriggerGC();
3609 }
3610
Calin Juravle7c4954d2014-10-28 16:57:40 +00003611 DECLARE_INSTRUCTION(Div);
3612
3613 private:
3614 DISALLOW_COPY_AND_ASSIGN(HDiv);
3615};
3616
Calin Juravlebacfec32014-11-14 15:54:36 +00003617class HRem : public HBinaryOperation {
3618 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003619 HRem(Primitive::Type result_type,
3620 HInstruction* left,
3621 HInstruction* right,
3622 uint32_t dex_pc)
3623 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003624
Roland Levillain9867bc72015-08-05 10:21:34 +01003625 template <typename T>
3626 T Compute(T x, T y) const {
3627 // Our graph structure ensures we never have 0 for `y` during
3628 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003629 DCHECK_NE(y, 0);
3630 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3631 return (y == -1) ? 0 : x % y;
3632 }
3633
Roland Levillain9867bc72015-08-05 10:21:34 +01003634 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003635 return GetBlock()->GetGraph()->GetIntConstant(
3636 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003637 }
3638 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003639 return GetBlock()->GetGraph()->GetLongConstant(
3640 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003641 }
3642
Calin Juravlebacfec32014-11-14 15:54:36 +00003643
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003644 static SideEffects SideEffectsForArchRuntimeCalls() {
3645 return SideEffects::CanTriggerGC();
3646 }
3647
Calin Juravlebacfec32014-11-14 15:54:36 +00003648 DECLARE_INSTRUCTION(Rem);
3649
3650 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003651 DISALLOW_COPY_AND_ASSIGN(HRem);
3652};
3653
Calin Juravled0d48522014-11-04 16:40:20 +00003654class HDivZeroCheck : public HExpression<1> {
3655 public:
3656 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003657 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003658 SetRawInputAt(0, value);
3659 }
3660
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003661 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3662
Calin Juravled0d48522014-11-04 16:40:20 +00003663 bool CanBeMoved() const OVERRIDE { return true; }
3664
3665 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3666 UNUSED(other);
3667 return true;
3668 }
3669
3670 bool NeedsEnvironment() const OVERRIDE { return true; }
3671 bool CanThrow() const OVERRIDE { return true; }
3672
Calin Juravled0d48522014-11-04 16:40:20 +00003673 DECLARE_INSTRUCTION(DivZeroCheck);
3674
3675 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003676 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3677};
3678
Calin Juravle9aec02f2014-11-18 23:06:35 +00003679class HShl : public HBinaryOperation {
3680 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003681 HShl(Primitive::Type result_type,
3682 HInstruction* left,
3683 HInstruction* right,
3684 uint32_t dex_pc = kNoDexPc)
3685 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003686
Roland Levillain9867bc72015-08-05 10:21:34 +01003687 template <typename T, typename U, typename V>
3688 T Compute(T x, U y, V max_shift_value) const {
3689 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3690 "V is not the unsigned integer type corresponding to T");
3691 return x << (y & max_shift_value);
3692 }
3693
3694 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3695 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003696 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003697 }
3698 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3699 // case is handled as `x << static_cast<int>(y)`.
3700 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3701 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003702 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003703 }
3704 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3705 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003706 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003707 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003708
3709 DECLARE_INSTRUCTION(Shl);
3710
3711 private:
3712 DISALLOW_COPY_AND_ASSIGN(HShl);
3713};
3714
3715class HShr : public HBinaryOperation {
3716 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003717 HShr(Primitive::Type result_type,
3718 HInstruction* left,
3719 HInstruction* right,
3720 uint32_t dex_pc = kNoDexPc)
3721 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003722
Roland Levillain9867bc72015-08-05 10:21:34 +01003723 template <typename T, typename U, typename V>
3724 T Compute(T x, U y, V max_shift_value) const {
3725 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3726 "V is not the unsigned integer type corresponding to T");
3727 return x >> (y & max_shift_value);
3728 }
3729
3730 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3731 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003732 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003733 }
3734 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3735 // case is handled as `x >> static_cast<int>(y)`.
3736 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3737 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003738 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003739 }
3740 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3741 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003742 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003743 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003744
3745 DECLARE_INSTRUCTION(Shr);
3746
3747 private:
3748 DISALLOW_COPY_AND_ASSIGN(HShr);
3749};
3750
3751class HUShr : public HBinaryOperation {
3752 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003753 HUShr(Primitive::Type result_type,
3754 HInstruction* left,
3755 HInstruction* right,
3756 uint32_t dex_pc = kNoDexPc)
3757 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003758
Roland Levillain9867bc72015-08-05 10:21:34 +01003759 template <typename T, typename U, typename V>
3760 T Compute(T x, U y, V max_shift_value) const {
3761 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3762 "V is not the unsigned integer type corresponding to T");
3763 V ux = static_cast<V>(x);
3764 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003765 }
3766
Roland Levillain9867bc72015-08-05 10:21:34 +01003767 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3768 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003769 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003770 }
3771 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3772 // case is handled as `x >>> static_cast<int>(y)`.
3773 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3774 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003775 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003776 }
3777 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3778 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003779 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003780 }
3781
3782 DECLARE_INSTRUCTION(UShr);
3783
3784 private:
3785 DISALLOW_COPY_AND_ASSIGN(HUShr);
3786};
3787
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003788class HAnd : public HBinaryOperation {
3789 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003790 HAnd(Primitive::Type result_type,
3791 HInstruction* left,
3792 HInstruction* right,
3793 uint32_t dex_pc = kNoDexPc)
3794 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003795
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003796 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003797
Roland Levillain9867bc72015-08-05 10:21:34 +01003798 template <typename T, typename U>
3799 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3800
3801 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003802 return GetBlock()->GetGraph()->GetIntConstant(
3803 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003804 }
3805 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003806 return GetBlock()->GetGraph()->GetLongConstant(
3807 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003808 }
3809 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003810 return GetBlock()->GetGraph()->GetLongConstant(
3811 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003812 }
3813 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003814 return GetBlock()->GetGraph()->GetLongConstant(
3815 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003816 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003817
3818 DECLARE_INSTRUCTION(And);
3819
3820 private:
3821 DISALLOW_COPY_AND_ASSIGN(HAnd);
3822};
3823
3824class HOr : public HBinaryOperation {
3825 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003826 HOr(Primitive::Type result_type,
3827 HInstruction* left,
3828 HInstruction* right,
3829 uint32_t dex_pc = kNoDexPc)
3830 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003831
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003832 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003833
Roland Levillain9867bc72015-08-05 10:21:34 +01003834 template <typename T, typename U>
3835 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3836
3837 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003838 return GetBlock()->GetGraph()->GetIntConstant(
3839 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003840 }
3841 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003842 return GetBlock()->GetGraph()->GetLongConstant(
3843 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003844 }
3845 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003846 return GetBlock()->GetGraph()->GetLongConstant(
3847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003848 }
3849 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003850 return GetBlock()->GetGraph()->GetLongConstant(
3851 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003852 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003853
3854 DECLARE_INSTRUCTION(Or);
3855
3856 private:
3857 DISALLOW_COPY_AND_ASSIGN(HOr);
3858};
3859
3860class HXor : public HBinaryOperation {
3861 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003862 HXor(Primitive::Type result_type,
3863 HInstruction* left,
3864 HInstruction* right,
3865 uint32_t dex_pc = kNoDexPc)
3866 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003867
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003868 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003869
Roland Levillain9867bc72015-08-05 10:21:34 +01003870 template <typename T, typename U>
3871 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3872
3873 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003874 return GetBlock()->GetGraph()->GetIntConstant(
3875 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003876 }
3877 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003878 return GetBlock()->GetGraph()->GetLongConstant(
3879 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003880 }
3881 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003882 return GetBlock()->GetGraph()->GetLongConstant(
3883 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003884 }
3885 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003886 return GetBlock()->GetGraph()->GetLongConstant(
3887 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003888 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003889
3890 DECLARE_INSTRUCTION(Xor);
3891
3892 private:
3893 DISALLOW_COPY_AND_ASSIGN(HXor);
3894};
3895
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003896// The value of a parameter in this method. Its location depends on
3897// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003898class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003899 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003900 HParameterValue(uint8_t index,
3901 Primitive::Type parameter_type,
3902 bool is_this = false)
3903 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003904 index_(index),
3905 is_this_(is_this),
3906 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003907
3908 uint8_t GetIndex() const { return index_; }
3909
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003910 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3911 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003912
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003913 bool IsThis() const { return is_this_; }
3914
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003915 DECLARE_INSTRUCTION(ParameterValue);
3916
3917 private:
3918 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003919 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003920 const uint8_t index_;
3921
Calin Juravle10e244f2015-01-26 18:54:32 +00003922 // Whether or not the parameter value corresponds to 'this' argument.
3923 const bool is_this_;
3924
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003925 bool can_be_null_;
3926
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003927 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3928};
3929
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003930class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003931 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003932 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3933 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003934
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003935 bool CanBeMoved() const OVERRIDE { return true; }
3936 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003937 UNUSED(other);
3938 return true;
3939 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003940
Roland Levillain9867bc72015-08-05 10:21:34 +01003941 template <typename T> T Compute(T x) const { return ~x; }
3942
3943 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003944 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003945 }
3946 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003947 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003948 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003949
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003950 DECLARE_INSTRUCTION(Not);
3951
3952 private:
3953 DISALLOW_COPY_AND_ASSIGN(HNot);
3954};
3955
David Brazdil66d126e2015-04-03 16:02:44 +01003956class HBooleanNot : public HUnaryOperation {
3957 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003958 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3959 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003960
3961 bool CanBeMoved() const OVERRIDE { return true; }
3962 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3963 UNUSED(other);
3964 return true;
3965 }
3966
Roland Levillain9867bc72015-08-05 10:21:34 +01003967 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003968 DCHECK(IsUint<1>(x));
3969 return !x;
3970 }
3971
Roland Levillain9867bc72015-08-05 10:21:34 +01003972 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003973 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003974 }
3975 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3976 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003977 UNREACHABLE();
3978 }
3979
3980 DECLARE_INSTRUCTION(BooleanNot);
3981
3982 private:
3983 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3984};
3985
Roland Levillaindff1f282014-11-05 14:15:05 +00003986class HTypeConversion : public HExpression<1> {
3987 public:
3988 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003989 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003990 : HExpression(result_type,
3991 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
3992 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003993 SetRawInputAt(0, input);
3994 DCHECK_NE(input->GetType(), result_type);
3995 }
3996
3997 HInstruction* GetInput() const { return InputAt(0); }
3998 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3999 Primitive::Type GetResultType() const { return GetType(); }
4000
Roland Levillain624279f2014-12-04 11:54:28 +00004001 // Required by the x86 and ARM code generators when producing calls
4002 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004003
Roland Levillaindff1f282014-11-05 14:15:05 +00004004 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004005 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004006
Mark Mendelle82549b2015-05-06 10:55:34 -04004007 // Try to statically evaluate the conversion and return a HConstant
4008 // containing the result. If the input cannot be converted, return nullptr.
4009 HConstant* TryStaticEvaluation() const;
4010
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004011 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4012 Primitive::Type result_type) {
4013 // Some architectures may not require the 'GC' side effects, but at this point
4014 // in the compilation process we do not know what architecture we will
4015 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004016 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4017 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004018 return SideEffects::CanTriggerGC();
4019 }
4020 return SideEffects::None();
4021 }
4022
Roland Levillaindff1f282014-11-05 14:15:05 +00004023 DECLARE_INSTRUCTION(TypeConversion);
4024
4025 private:
4026 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4027};
4028
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004029static constexpr uint32_t kNoRegNumber = -1;
4030
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004031class HPhi : public HInstruction {
4032 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004033 HPhi(ArenaAllocator* arena,
4034 uint32_t reg_number,
4035 size_t number_of_inputs,
4036 Primitive::Type type,
4037 uint32_t dex_pc = kNoDexPc)
4038 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004039 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004040 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004041 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004042 is_live_(false),
4043 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004044 }
4045
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004046 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4047 static Primitive::Type ToPhiType(Primitive::Type type) {
4048 switch (type) {
4049 case Primitive::kPrimBoolean:
4050 case Primitive::kPrimByte:
4051 case Primitive::kPrimShort:
4052 case Primitive::kPrimChar:
4053 return Primitive::kPrimInt;
4054 default:
4055 return type;
4056 }
4057 }
4058
David Brazdilffee3d32015-07-06 11:48:53 +01004059 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4060
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004061 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004062
4063 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004064 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004065
Calin Juravle10e244f2015-01-26 18:54:32 +00004066 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004067 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004068
Calin Juravle10e244f2015-01-26 18:54:32 +00004069 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4070 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4071
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004072 uint32_t GetRegNumber() const { return reg_number_; }
4073
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004074 void SetDead() { is_live_ = false; }
4075 void SetLive() { is_live_ = true; }
4076 bool IsDead() const { return !is_live_; }
4077 bool IsLive() const { return is_live_; }
4078
David Brazdil77a48ae2015-09-15 12:34:04 +00004079 bool IsVRegEquivalentOf(HInstruction* other) const {
4080 return other != nullptr
4081 && other->IsPhi()
4082 && other->AsPhi()->GetBlock() == GetBlock()
4083 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4084 }
4085
Calin Juravlea4f88312015-04-16 12:57:19 +01004086 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4087 // An equivalent phi is a phi having the same dex register and type.
4088 // It assumes that phis with the same dex register are adjacent.
4089 HPhi* GetNextEquivalentPhiWithSameType() {
4090 HInstruction* next = GetNext();
4091 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4092 if (next->GetType() == GetType()) {
4093 return next->AsPhi();
4094 }
4095 next = next->GetNext();
4096 }
4097 return nullptr;
4098 }
4099
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004100 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004101
David Brazdil1abb4192015-02-17 18:33:36 +00004102 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004103 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
4104 DCHECK_LE(index, InputCount());
4105 return inputs_[index];
4106 }
David Brazdil1abb4192015-02-17 18:33:36 +00004107
4108 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004109 DCHECK_LE(index, InputCount());
4110 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004111 }
4112
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004113 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004114 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004115 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004116 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004117 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004118 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004119
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004120 DISALLOW_COPY_AND_ASSIGN(HPhi);
4121};
4122
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004123class HNullCheck : public HExpression<1> {
4124 public:
4125 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004126 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004127 SetRawInputAt(0, value);
4128 }
4129
Calin Juravle10e244f2015-01-26 18:54:32 +00004130 bool CanBeMoved() const OVERRIDE { return true; }
4131 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004132 UNUSED(other);
4133 return true;
4134 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004135
Calin Juravle10e244f2015-01-26 18:54:32 +00004136 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004137
Calin Juravle10e244f2015-01-26 18:54:32 +00004138 bool CanThrow() const OVERRIDE { return true; }
4139
4140 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004141
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004142
4143 DECLARE_INSTRUCTION(NullCheck);
4144
4145 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004146 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4147};
4148
4149class FieldInfo : public ValueObject {
4150 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004151 FieldInfo(MemberOffset field_offset,
4152 Primitive::Type field_type,
4153 bool is_volatile,
4154 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004155 const DexFile& dex_file,
4156 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004157 : field_offset_(field_offset),
4158 field_type_(field_type),
4159 is_volatile_(is_volatile),
4160 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004161 dex_file_(dex_file),
4162 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004163
4164 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004165 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004166 uint32_t GetFieldIndex() const { return index_; }
4167 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004168 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004169 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004170
4171 private:
4172 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004173 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004174 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004175 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004176 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004177 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004178};
4179
4180class HInstanceFieldGet : public HExpression<1> {
4181 public:
4182 HInstanceFieldGet(HInstruction* value,
4183 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004184 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004185 bool is_volatile,
4186 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004187 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004188 Handle<mirror::DexCache> dex_cache,
4189 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004190 : HExpression(
4191 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004192 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004193 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004194 SetRawInputAt(0, value);
4195 }
4196
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004197 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004198
4199 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4200 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4201 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004202 }
4203
Calin Juravle641547a2015-04-21 22:08:51 +01004204 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4205 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004206 }
4207
4208 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004209 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4210 }
4211
Calin Juravle52c48962014-12-16 17:02:57 +00004212 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004213 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004214 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004215 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004216
4217 DECLARE_INSTRUCTION(InstanceFieldGet);
4218
4219 private:
4220 const FieldInfo field_info_;
4221
4222 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4223};
4224
4225class HInstanceFieldSet : public HTemplateInstruction<2> {
4226 public:
4227 HInstanceFieldSet(HInstruction* object,
4228 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004229 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004230 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004231 bool is_volatile,
4232 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004233 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004234 Handle<mirror::DexCache> dex_cache,
4235 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004236 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004237 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004238 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004239 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004240 SetRawInputAt(0, object);
4241 SetRawInputAt(1, value);
4242 }
4243
Calin Juravle641547a2015-04-21 22:08:51 +01004244 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4245 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004246 }
4247
Calin Juravle52c48962014-12-16 17:02:57 +00004248 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004249 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004250 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004251 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004252 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004253 bool GetValueCanBeNull() const { return value_can_be_null_; }
4254 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004255
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004256 DECLARE_INSTRUCTION(InstanceFieldSet);
4257
4258 private:
4259 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004260 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004261
4262 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4263};
4264
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004265class HArrayGet : public HExpression<2> {
4266 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004267 HArrayGet(HInstruction* array,
4268 HInstruction* index,
4269 Primitive::Type type,
4270 uint32_t dex_pc = kNoDexPc)
4271 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004272 SetRawInputAt(0, array);
4273 SetRawInputAt(1, index);
4274 }
4275
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004276 bool CanBeMoved() const OVERRIDE { return true; }
4277 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004278 UNUSED(other);
4279 return true;
4280 }
Calin Juravle641547a2015-04-21 22:08:51 +01004281 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4282 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004283 // TODO: We can be smarter here.
4284 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4285 // which generates the implicit null check. There are cases when these can be removed
4286 // to produce better code. If we ever add optimizations to do so we should allow an
4287 // implicit check here (as long as the address falls in the first page).
4288 return false;
4289 }
4290
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004291 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004292
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004293 HInstruction* GetArray() const { return InputAt(0); }
4294 HInstruction* GetIndex() const { return InputAt(1); }
4295
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004296 DECLARE_INSTRUCTION(ArrayGet);
4297
4298 private:
4299 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4300};
4301
4302class HArraySet : public HTemplateInstruction<3> {
4303 public:
4304 HArraySet(HInstruction* array,
4305 HInstruction* index,
4306 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004307 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004308 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004309 : HTemplateInstruction(
4310 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004312 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004313 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4314 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004315 SetRawInputAt(0, array);
4316 SetRawInputAt(1, index);
4317 SetRawInputAt(2, value);
4318 }
4319
Calin Juravle77520bc2015-01-12 18:45:46 +00004320 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004321 // We currently always call a runtime method to catch array store
4322 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004323 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004324 }
4325
Mingyao Yang81014cb2015-06-02 03:16:27 -07004326 // Can throw ArrayStoreException.
4327 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4328
Calin Juravle641547a2015-04-21 22:08:51 +01004329 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4330 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004331 // TODO: Same as for ArrayGet.
4332 return false;
4333 }
4334
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004335 void ClearNeedsTypeCheck() {
4336 needs_type_check_ = false;
4337 }
4338
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004339 void ClearValueCanBeNull() {
4340 value_can_be_null_ = false;
4341 }
4342
4343 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004344 bool NeedsTypeCheck() const { return needs_type_check_; }
4345
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004346 HInstruction* GetArray() const { return InputAt(0); }
4347 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004348 HInstruction* GetValue() const { return InputAt(2); }
4349
4350 Primitive::Type GetComponentType() const {
4351 // The Dex format does not type floating point index operations. Since the
4352 // `expected_component_type_` is set during building and can therefore not
4353 // be correct, we also check what is the value type. If it is a floating
4354 // point type, we must use that type.
4355 Primitive::Type value_type = GetValue()->GetType();
4356 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4357 ? value_type
4358 : expected_component_type_;
4359 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004360
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004361 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4362 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4363 }
4364
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004365 DECLARE_INSTRUCTION(ArraySet);
4366
4367 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004368 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004369 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004370 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004371
4372 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4373};
4374
4375class HArrayLength : public HExpression<1> {
4376 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004377 explicit HArrayLength(HInstruction* array, uint32_t dex_pc = kNoDexPc)
4378 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004379 // Note that arrays do not change length, so the instruction does not
4380 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004381 SetRawInputAt(0, array);
4382 }
4383
Calin Juravle77520bc2015-01-12 18:45:46 +00004384 bool CanBeMoved() const OVERRIDE { return true; }
4385 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004386 UNUSED(other);
4387 return true;
4388 }
Calin Juravle641547a2015-04-21 22:08:51 +01004389 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4390 return obj == InputAt(0);
4391 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004392
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004393 DECLARE_INSTRUCTION(ArrayLength);
4394
4395 private:
4396 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4397};
4398
4399class HBoundsCheck : public HExpression<2> {
4400 public:
4401 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004402 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004403 DCHECK(index->GetType() == Primitive::kPrimInt);
4404 SetRawInputAt(0, index);
4405 SetRawInputAt(1, length);
4406 }
4407
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004408 bool CanBeMoved() const OVERRIDE { return true; }
4409 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004410 UNUSED(other);
4411 return true;
4412 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004413
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004414 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004415
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004416 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004417
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004418
4419 DECLARE_INSTRUCTION(BoundsCheck);
4420
4421 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004422 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4423};
4424
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004425/**
4426 * Some DEX instructions are folded into multiple HInstructions that need
4427 * to stay live until the last HInstruction. This class
4428 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004429 * HInstruction stays live. `index` represents the stack location index of the
4430 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004431 */
4432class HTemporary : public HTemplateInstruction<0> {
4433 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004434 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4435 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004436
4437 size_t GetIndex() const { return index_; }
4438
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004439 Primitive::Type GetType() const OVERRIDE {
4440 // The previous instruction is the one that will be stored in the temporary location.
4441 DCHECK(GetPrevious() != nullptr);
4442 return GetPrevious()->GetType();
4443 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004444
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004445 DECLARE_INSTRUCTION(Temporary);
4446
4447 private:
4448 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004449 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4450};
4451
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004452class HSuspendCheck : public HTemplateInstruction<0> {
4453 public:
4454 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004455 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004456
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004457 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004458 return true;
4459 }
4460
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004461 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4462 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004463
4464 DECLARE_INSTRUCTION(SuspendCheck);
4465
4466 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004467 // Only used for code generation, in order to share the same slow path between back edges
4468 // of a same loop.
4469 SlowPathCode* slow_path_;
4470
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004471 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4472};
4473
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004474/**
4475 * Instruction to load a Class object.
4476 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004477class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004478 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004479 HLoadClass(HCurrentMethod* current_method,
4480 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004481 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004482 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004483 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004484 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004485 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004486 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004487 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004488 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004489 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004490 SetRawInputAt(0, current_method);
4491 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004492
4493 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004494
4495 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4496 return other->AsLoadClass()->type_index_ == type_index_;
4497 }
4498
4499 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4500
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004501 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004502 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004503 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004504
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004505 bool NeedsEnvironment() const OVERRIDE {
4506 // Will call runtime and load the class if the class is not loaded yet.
4507 // TODO: finer grain decision.
4508 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004509 }
4510
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004511 bool MustGenerateClinitCheck() const {
4512 return generate_clinit_check_;
4513 }
4514
Calin Juravle0ba218d2015-05-19 18:46:01 +01004515 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4516 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004517 }
4518
4519 bool CanCallRuntime() const {
4520 return MustGenerateClinitCheck() || !is_referrers_class_;
4521 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004522
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004523 bool CanThrow() const OVERRIDE {
4524 // May call runtime and and therefore can throw.
4525 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004526 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004527 }
4528
Calin Juravleacf735c2015-02-12 15:25:22 +00004529 ReferenceTypeInfo GetLoadedClassRTI() {
4530 return loaded_class_rti_;
4531 }
4532
4533 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4534 // Make sure we only set exact types (the loaded class should never be merged).
4535 DCHECK(rti.IsExact());
4536 loaded_class_rti_ = rti;
4537 }
4538
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004539 const DexFile& GetDexFile() { return dex_file_; }
4540
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004541 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4542
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004543 static SideEffects SideEffectsForArchRuntimeCalls() {
4544 return SideEffects::CanTriggerGC();
4545 }
4546
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004547 DECLARE_INSTRUCTION(LoadClass);
4548
4549 private:
4550 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004551 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004552 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004553 // Whether this instruction must generate the initialization check.
4554 // Used for code generation.
4555 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004556
Calin Juravleacf735c2015-02-12 15:25:22 +00004557 ReferenceTypeInfo loaded_class_rti_;
4558
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004559 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4560};
4561
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004562class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004563 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004564 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004565 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4566 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004567 SetRawInputAt(0, current_method);
4568 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004569
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004570 bool CanBeMoved() const OVERRIDE { return true; }
4571
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004572 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4573 return other->AsLoadString()->string_index_ == string_index_;
4574 }
4575
4576 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4577
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004578 uint32_t GetStringIndex() const { return string_index_; }
4579
4580 // TODO: Can we deopt or debug when we resolve a string?
4581 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004582 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004583 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004584
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004585 static SideEffects SideEffectsForArchRuntimeCalls() {
4586 return SideEffects::CanTriggerGC();
4587 }
4588
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004589 DECLARE_INSTRUCTION(LoadString);
4590
4591 private:
4592 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004593
4594 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4595};
4596
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004597/**
4598 * Performs an initialization check on its Class object input.
4599 */
4600class HClinitCheck : public HExpression<1> {
4601 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004602 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004603 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004604 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004605 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4606 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004607 SetRawInputAt(0, constant);
4608 }
4609
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004610 bool CanBeMoved() const OVERRIDE { return true; }
4611 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4612 UNUSED(other);
4613 return true;
4614 }
4615
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004616 bool NeedsEnvironment() const OVERRIDE {
4617 // May call runtime to initialize the class.
4618 return true;
4619 }
4620
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004621
4622 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4623
4624 DECLARE_INSTRUCTION(ClinitCheck);
4625
4626 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004627 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4628};
4629
4630class HStaticFieldGet : public HExpression<1> {
4631 public:
4632 HStaticFieldGet(HInstruction* cls,
4633 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004634 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004635 bool is_volatile,
4636 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004637 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004638 Handle<mirror::DexCache> dex_cache,
4639 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004640 : HExpression(
4641 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004642 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004643 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004644 SetRawInputAt(0, cls);
4645 }
4646
Calin Juravle52c48962014-12-16 17:02:57 +00004647
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004648 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004649
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004650 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004651 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4652 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004653 }
4654
4655 size_t ComputeHashCode() const OVERRIDE {
4656 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4657 }
4658
Calin Juravle52c48962014-12-16 17:02:57 +00004659 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004660 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4661 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004662 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004663
4664 DECLARE_INSTRUCTION(StaticFieldGet);
4665
4666 private:
4667 const FieldInfo field_info_;
4668
4669 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4670};
4671
4672class HStaticFieldSet : public HTemplateInstruction<2> {
4673 public:
4674 HStaticFieldSet(HInstruction* cls,
4675 HInstruction* value,
4676 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004677 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004678 bool is_volatile,
4679 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004680 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004681 Handle<mirror::DexCache> dex_cache,
4682 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004683 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004684 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004685 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004686 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004687 SetRawInputAt(0, cls);
4688 SetRawInputAt(1, value);
4689 }
4690
Calin Juravle52c48962014-12-16 17:02:57 +00004691 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004692 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4693 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004694 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004695
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004696 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004697 bool GetValueCanBeNull() const { return value_can_be_null_; }
4698 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004699
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004700 DECLARE_INSTRUCTION(StaticFieldSet);
4701
4702 private:
4703 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004704 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004705
4706 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4707};
4708
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004709// Implement the move-exception DEX instruction.
4710class HLoadException : public HExpression<0> {
4711 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004712 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4713 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004714
David Brazdilbbd733e2015-08-18 17:48:17 +01004715 bool CanBeNull() const OVERRIDE { return false; }
4716
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004717 DECLARE_INSTRUCTION(LoadException);
4718
4719 private:
4720 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4721};
4722
David Brazdilcb1c0552015-08-04 16:22:25 +01004723// Implicit part of move-exception which clears thread-local exception storage.
4724// Must not be removed because the runtime expects the TLS to get cleared.
4725class HClearException : public HTemplateInstruction<0> {
4726 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004727 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4728 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004729
4730 DECLARE_INSTRUCTION(ClearException);
4731
4732 private:
4733 DISALLOW_COPY_AND_ASSIGN(HClearException);
4734};
4735
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004736class HThrow : public HTemplateInstruction<1> {
4737 public:
4738 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004739 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004740 SetRawInputAt(0, exception);
4741 }
4742
4743 bool IsControlFlow() const OVERRIDE { return true; }
4744
4745 bool NeedsEnvironment() const OVERRIDE { return true; }
4746
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004747 bool CanThrow() const OVERRIDE { return true; }
4748
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004749
4750 DECLARE_INSTRUCTION(Throw);
4751
4752 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004753 DISALLOW_COPY_AND_ASSIGN(HThrow);
4754};
4755
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004756/**
4757 * Implementation strategies for the code generator of a HInstanceOf
4758 * or `HCheckCast`.
4759 */
4760enum class TypeCheckKind {
4761 kExactCheck, // Can do a single class compare.
4762 kClassHierarchyCheck, // Can just walk the super class chain.
4763 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
4764 kInterfaceCheck, // No optimization yet when checking against an interface.
4765 kArrayObjectCheck, // Can just check if the array is not primitive.
4766 kArrayCheck // No optimization yet when checking against a generic array.
4767};
4768
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004769class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004770 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004771 HInstanceOf(HInstruction* object,
4772 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004773 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004774 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004775 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004776 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004777 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004778 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004779 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004780 SetRawInputAt(0, object);
4781 SetRawInputAt(1, constant);
4782 }
4783
4784 bool CanBeMoved() const OVERRIDE { return true; }
4785
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004786 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004787 return true;
4788 }
4789
4790 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004791 return false;
4792 }
4793
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004794 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
4795
4796 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004797
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004798 // Used only in code generation.
4799 bool MustDoNullCheck() const { return must_do_null_check_; }
4800 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4801
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004802 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
4803 return (check_kind == TypeCheckKind::kExactCheck)
4804 ? SideEffects::None()
4805 // Mips currently does runtime calls for any other checks.
4806 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004807 }
4808
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004809 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004810
4811 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004812 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004813 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004814
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004815 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4816};
4817
Calin Juravleb1498f62015-02-16 13:13:29 +00004818class HBoundType : public HExpression<1> {
4819 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004820 // Constructs an HBoundType with the given upper_bound.
4821 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004822 HBoundType(HInstruction* input,
4823 ReferenceTypeInfo upper_bound,
4824 bool upper_can_be_null,
4825 uint32_t dex_pc = kNoDexPc)
4826 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004827 upper_bound_(upper_bound),
4828 upper_can_be_null_(upper_can_be_null),
4829 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004830 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004831 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004832 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004833 }
4834
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004835 // GetUpper* should only be used in reference type propagation.
4836 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4837 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004838
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004839 void SetCanBeNull(bool can_be_null) {
4840 DCHECK(upper_can_be_null_ || !can_be_null);
4841 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004842 }
4843
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004844 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4845
Calin Juravleb1498f62015-02-16 13:13:29 +00004846 DECLARE_INSTRUCTION(BoundType);
4847
4848 private:
4849 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004850 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4851 // It is used to bound the type in cases like:
4852 // if (x instanceof ClassX) {
4853 // // uper_bound_ will be ClassX
4854 // }
4855 const ReferenceTypeInfo upper_bound_;
4856 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4857 // is false then can_be_null_ cannot be true).
4858 const bool upper_can_be_null_;
4859 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004860
4861 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4862};
4863
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004864class HCheckCast : public HTemplateInstruction<2> {
4865 public:
4866 HCheckCast(HInstruction* object,
4867 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004868 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004869 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004870 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004871 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004872 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004873 SetRawInputAt(0, object);
4874 SetRawInputAt(1, constant);
4875 }
4876
4877 bool CanBeMoved() const OVERRIDE { return true; }
4878
4879 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4880 return true;
4881 }
4882
4883 bool NeedsEnvironment() const OVERRIDE {
4884 // Instruction may throw a CheckCastError.
4885 return true;
4886 }
4887
4888 bool CanThrow() const OVERRIDE { return true; }
4889
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004890 bool MustDoNullCheck() const { return must_do_null_check_; }
4891 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004892 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004893
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004894 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004895
4896 DECLARE_INSTRUCTION(CheckCast);
4897
4898 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004899 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004900 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004901
4902 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004903};
4904
Calin Juravle27df7582015-04-17 19:12:31 +01004905class HMemoryBarrier : public HTemplateInstruction<0> {
4906 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004907 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004908 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004909 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004910 barrier_kind_(barrier_kind) {}
4911
4912 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4913
4914 DECLARE_INSTRUCTION(MemoryBarrier);
4915
4916 private:
4917 const MemBarrierKind barrier_kind_;
4918
4919 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4920};
4921
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004922class HMonitorOperation : public HTemplateInstruction<1> {
4923 public:
4924 enum OperationKind {
4925 kEnter,
4926 kExit,
4927 };
4928
4929 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004930 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004931 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
4932 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004933 SetRawInputAt(0, object);
4934 }
4935
4936 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004937 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4938
4939 bool CanThrow() const OVERRIDE {
4940 // Verifier guarantees that monitor-exit cannot throw.
4941 // This is important because it allows the HGraphBuilder to remove
4942 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4943 return IsEnter();
4944 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004945
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004946
4947 bool IsEnter() const { return kind_ == kEnter; }
4948
4949 DECLARE_INSTRUCTION(MonitorOperation);
4950
Calin Juravle52c48962014-12-16 17:02:57 +00004951 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004952 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004953
4954 private:
4955 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4956};
4957
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004958/**
4959 * A HInstruction used as a marker for the replacement of new + <init>
4960 * of a String to a call to a StringFactory. Only baseline will see
4961 * the node at code generation, where it will be be treated as null.
4962 * When compiling non-baseline, `HFakeString` instructions are being removed
4963 * in the instruction simplifier.
4964 */
4965class HFakeString : public HTemplateInstruction<0> {
4966 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004967 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
4968 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004969
4970 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4971
4972 DECLARE_INSTRUCTION(FakeString);
4973
4974 private:
4975 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4976};
4977
Vladimir Markof9f64412015-09-02 14:05:49 +01004978class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004979 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004980 MoveOperands(Location source,
4981 Location destination,
4982 Primitive::Type type,
4983 HInstruction* instruction)
4984 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004985
4986 Location GetSource() const { return source_; }
4987 Location GetDestination() const { return destination_; }
4988
4989 void SetSource(Location value) { source_ = value; }
4990 void SetDestination(Location value) { destination_ = value; }
4991
4992 // The parallel move resolver marks moves as "in-progress" by clearing the
4993 // destination (but not the source).
4994 Location MarkPending() {
4995 DCHECK(!IsPending());
4996 Location dest = destination_;
4997 destination_ = Location::NoLocation();
4998 return dest;
4999 }
5000
5001 void ClearPending(Location dest) {
5002 DCHECK(IsPending());
5003 destination_ = dest;
5004 }
5005
5006 bool IsPending() const {
5007 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5008 return destination_.IsInvalid() && !source_.IsInvalid();
5009 }
5010
5011 // True if this blocks a move from the given location.
5012 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005013 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005014 }
5015
5016 // A move is redundant if it's been eliminated, if its source and
5017 // destination are the same, or if its destination is unneeded.
5018 bool IsRedundant() const {
5019 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5020 }
5021
5022 // We clear both operands to indicate move that's been eliminated.
5023 void Eliminate() {
5024 source_ = destination_ = Location::NoLocation();
5025 }
5026
5027 bool IsEliminated() const {
5028 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5029 return source_.IsInvalid();
5030 }
5031
Alexey Frunze4dda3372015-06-01 18:31:49 -07005032 Primitive::Type GetType() const { return type_; }
5033
Nicolas Geoffray90218252015-04-15 11:56:51 +01005034 bool Is64BitMove() const {
5035 return Primitive::Is64BitType(type_);
5036 }
5037
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005038 HInstruction* GetInstruction() const { return instruction_; }
5039
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005040 private:
5041 Location source_;
5042 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005043 // The type this move is for.
5044 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005045 // The instruction this move is assocatied with. Null when this move is
5046 // for moving an input in the expected locations of user (including a phi user).
5047 // This is only used in debug mode, to ensure we do not connect interval siblings
5048 // in the same parallel move.
5049 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005050};
5051
5052static constexpr size_t kDefaultNumberOfMoves = 4;
5053
5054class HParallelMove : public HTemplateInstruction<0> {
5055 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005056 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
5057 : HTemplateInstruction(SideEffects::None(), dex_pc), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005058
Nicolas Geoffray90218252015-04-15 11:56:51 +01005059 void AddMove(Location source,
5060 Location destination,
5061 Primitive::Type type,
5062 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005063 DCHECK(source.IsValid());
5064 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005065 if (kIsDebugBuild) {
5066 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005067 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005068 if (moves_.Get(i).GetInstruction() == instruction) {
5069 // Special case the situation where the move is for the spill slot
5070 // of the instruction.
5071 if ((GetPrevious() == instruction)
5072 || ((GetPrevious() == nullptr)
5073 && instruction->IsPhi()
5074 && instruction->GetBlock() == GetBlock())) {
5075 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
5076 << "Doing parallel moves for the same instruction.";
5077 } else {
5078 DCHECK(false) << "Doing parallel moves for the same instruction.";
5079 }
5080 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005081 }
5082 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005083 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08005084 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005085 << "Overlapped destination for two moves in a parallel move: "
5086 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
5087 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005088 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005089 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01005090 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005091 }
5092
5093 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005094 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005095 }
5096
5097 size_t NumMoves() const { return moves_.Size(); }
5098
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005099 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005100
5101 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005102 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005103
5104 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5105};
5106
Mark Mendell0616ae02015-04-17 12:49:27 -04005107} // namespace art
5108
5109#ifdef ART_ENABLE_CODEGEN_x86
5110#include "nodes_x86.h"
5111#endif
5112
5113namespace art {
5114
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005115class HGraphVisitor : public ValueObject {
5116 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005117 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5118 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005119
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005120 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005121 virtual void VisitBasicBlock(HBasicBlock* block);
5122
Roland Levillain633021e2014-10-01 14:12:25 +01005123 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005124 void VisitInsertionOrder();
5125
Roland Levillain633021e2014-10-01 14:12:25 +01005126 // Visit the graph following dominator tree reverse post-order.
5127 void VisitReversePostOrder();
5128
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005129 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005130
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005131 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005132#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005133 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5134
5135 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5136
5137#undef DECLARE_VISIT_INSTRUCTION
5138
5139 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005140 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005141
5142 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5143};
5144
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005145class HGraphDelegateVisitor : public HGraphVisitor {
5146 public:
5147 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5148 virtual ~HGraphDelegateVisitor() {}
5149
5150 // Visit functions that delegate to to super class.
5151#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005152 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005153
5154 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5155
5156#undef DECLARE_VISIT_INSTRUCTION
5157
5158 private:
5159 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5160};
5161
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005162class HInsertionOrderIterator : public ValueObject {
5163 public:
5164 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5165
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005166 bool Done() const { return index_ == graph_.GetBlocks().size(); }
5167 HBasicBlock* Current() const { return graph_.GetBlock(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005168 void Advance() { ++index_; }
5169
5170 private:
5171 const HGraph& graph_;
5172 size_t index_;
5173
5174 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5175};
5176
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005177class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005178 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005179 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5180 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005181 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005182 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005183
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005184 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5185 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005186 void Advance() { ++index_; }
5187
5188 private:
5189 const HGraph& graph_;
5190 size_t index_;
5191
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005192 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005193};
5194
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005195class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005196 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005197 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005198 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005199 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005200 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005201 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005202
5203 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005204 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005205 void Advance() { --index_; }
5206
5207 private:
5208 const HGraph& graph_;
5209 size_t index_;
5210
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005211 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005212};
5213
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005214class HLinearPostOrderIterator : public ValueObject {
5215 public:
5216 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005217 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005218
5219 bool Done() const { return index_ == 0; }
5220
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005221 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005222
5223 void Advance() {
5224 --index_;
5225 DCHECK_GE(index_, 0U);
5226 }
5227
5228 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005229 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005230 size_t index_;
5231
5232 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5233};
5234
5235class HLinearOrderIterator : public ValueObject {
5236 public:
5237 explicit HLinearOrderIterator(const HGraph& graph)
5238 : order_(graph.GetLinearOrder()), index_(0) {}
5239
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005240 bool Done() const { return index_ == order_.size(); }
5241 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005242 void Advance() { ++index_; }
5243
5244 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005245 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005246 size_t index_;
5247
5248 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5249};
5250
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005251// Iterator over the blocks that art part of the loop. Includes blocks part
5252// of an inner loop. The order in which the blocks are iterated is on their
5253// block id.
5254class HBlocksInLoopIterator : public ValueObject {
5255 public:
5256 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5257 : blocks_in_loop_(info.GetBlocks()),
5258 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5259 index_(0) {
5260 if (!blocks_in_loop_.IsBitSet(index_)) {
5261 Advance();
5262 }
5263 }
5264
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005265 bool Done() const { return index_ == blocks_.size(); }
5266 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005267 void Advance() {
5268 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005269 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005270 if (blocks_in_loop_.IsBitSet(index_)) {
5271 break;
5272 }
5273 }
5274 }
5275
5276 private:
5277 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005278 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005279 size_t index_;
5280
5281 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5282};
5283
Mingyao Yang3584bce2015-05-19 16:01:59 -07005284// Iterator over the blocks that art part of the loop. Includes blocks part
5285// of an inner loop. The order in which the blocks are iterated is reverse
5286// post order.
5287class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5288 public:
5289 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5290 : blocks_in_loop_(info.GetBlocks()),
5291 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5292 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005293 DCHECK(!blocks_.empty());
5294 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005295 Advance();
5296 }
5297 }
5298
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005299 bool Done() const { return index_ == blocks_.size(); }
5300 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005301 void Advance() {
5302 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005303 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5304 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005305 break;
5306 }
5307 }
5308 }
5309
5310 private:
5311 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005312 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005313 size_t index_;
5314
5315 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5316};
5317
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005318inline int64_t Int64FromConstant(HConstant* constant) {
5319 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5320 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5321 : constant->AsLongConstant()->GetValue();
5322}
5323
Vladimir Marko58155012015-08-19 12:49:41 +00005324inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5325 // For the purposes of the compiler, the dex files must actually be the same object
5326 // if we want to safely treat them as the same. This is especially important for JIT
5327 // as custom class loaders can open the same underlying file (or memory) multiple
5328 // times and provide different class resolution but no two class loaders should ever
5329 // use the same DexFile object - doing so is an unsupported hack that can lead to
5330 // all sorts of weird failures.
5331 return &lhs == &rhs;
5332}
5333
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005334} // namespace art
5335
5336#endif // ART_COMPILER_OPTIMIZING_NODES_H_