blob: 993cc37444ede0403f74c5246f3a0df7624db361 [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 Geoffraybe9a92a2014-02-25 14:22:56 +0000373 void VisitBlockForDominatorTree(HBasicBlock* block,
374 HBasicBlock* predecessor,
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100375 ArenaVector<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100376 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000377 void VisitBlockForBackEdges(HBasicBlock* block,
378 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100379 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000380 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100381 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000382
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000383 template <class InstructionType, typename ValueType>
384 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600385 ArenaSafeMap<ValueType, InstructionType*>* cache,
386 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000387 // Try to find an existing constant of the given value.
388 InstructionType* constant = nullptr;
389 auto cached_constant = cache->find(value);
390 if (cached_constant != cache->end()) {
391 constant = cached_constant->second;
392 }
393
394 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100395 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000396 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600397 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000398 cache->Overwrite(value, constant);
399 InsertConstant(constant);
400 }
401 return constant;
402 }
403
David Brazdil8d5b8b22015-03-24 10:51:52 +0000404 void InsertConstant(HConstant* instruction);
405
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000406 // Cache a float constant into the graph. This method should only be
407 // called by the SsaBuilder when creating "equivalent" instructions.
408 void CacheFloatConstant(HFloatConstant* constant);
409
410 // See CacheFloatConstant comment.
411 void CacheDoubleConstant(HDoubleConstant* constant);
412
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000414
415 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100416 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000417
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100418 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100419 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000420
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100421 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100422 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100423
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000424 HBasicBlock* entry_block_;
425 HBasicBlock* exit_block_;
426
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100427 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100428 uint16_t maximum_number_of_out_vregs_;
429
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100430 // The number of virtual registers in this method. Contains the parameters.
431 uint16_t number_of_vregs_;
432
433 // The number of virtual registers used by parameters of this method.
434 uint16_t number_of_in_vregs_;
435
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000436 // Number of vreg size slots that the temporaries use (used in baseline compiler).
437 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100438
Mark Mendell1152c922015-04-24 17:06:35 -0400439 // Has bounds checks. We can totally skip BCE if it's false.
440 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800441
David Brazdil77a48ae2015-09-15 12:34:04 +0000442 // Flag whether there are any try/catch blocks in the graph. We will skip
443 // try/catch-related passes if false.
444 bool has_try_catch_;
445
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000446 // Indicates whether the graph should be compiled in a way that
447 // ensures full debuggability. If false, we can apply more
448 // aggressive optimizations that may limit the level of debugging.
449 const bool debuggable_;
450
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000451 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000452 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000453
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100454 // The dex file from which the method is from.
455 const DexFile& dex_file_;
456
457 // The method index in the dex file.
458 const uint32_t method_idx_;
459
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100460 // If inlined, this encodes how the callee is being invoked.
461 const InvokeType invoke_type_;
462
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100463 // Whether the graph has been transformed to SSA form. Only used
464 // in debug mode to ensure we are not using properties only valid
465 // for non-SSA form (like the number of temporaries).
466 bool in_ssa_form_;
467
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100468 const bool should_generate_constructor_barrier_;
469
Mathieu Chartiere401d142015-04-22 13:56:20 -0700470 const InstructionSet instruction_set_;
471
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000472 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000473 HNullConstant* cached_null_constant_;
474 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000475 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000476 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000477 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000478
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100479 HCurrentMethod* cached_current_method_;
480
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000481 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100482 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000483 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000484 DISALLOW_COPY_AND_ASSIGN(HGraph);
485};
486
Vladimir Markof9f64412015-09-02 14:05:49 +0100487class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000488 public:
489 HLoopInformation(HBasicBlock* header, HGraph* graph)
490 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100491 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100492 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100493 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100494 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
495 back_edges_.reserve(kDefaultNumberOfBackEdges);
496 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100497
498 HBasicBlock* GetHeader() const {
499 return header_;
500 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000501
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000502 void SetHeader(HBasicBlock* block) {
503 header_ = block;
504 }
505
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100506 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
507 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
508 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
509
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000510 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100511 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000512 }
513
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100514 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100515 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100516 }
517
David Brazdil46e2a392015-03-16 17:31:52 +0000518 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100519 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100520 }
521
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000522 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100523 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000524 }
525
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100526 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100527
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100528 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100529 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100530 }
531
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100532 // Returns the lifetime position of the back edge that has the
533 // greatest lifetime position.
534 size_t GetLifetimeEnd() const;
535
536 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100537 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100538 }
539
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100540 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100541 // that is the header dominates the back edge.
542 bool Populate();
543
David Brazdila4b8c212015-05-07 09:59:30 +0100544 // Reanalyzes the loop by removing loop info from its blocks and re-running
545 // Populate(). If there are no back edges left, the loop info is completely
546 // removed as well as its SuspendCheck instruction. It must be run on nested
547 // inner loops first.
548 void Update();
549
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100550 // Returns whether this loop information contains `block`.
551 // Note that this loop information *must* be populated before entering this function.
552 bool Contains(const HBasicBlock& block) const;
553
554 // Returns whether this loop information is an inner loop of `other`.
555 // Note that `other` *must* be populated before entering this function.
556 bool IsIn(const HLoopInformation& other) const;
557
558 const ArenaBitVector& GetBlocks() const { return blocks_; }
559
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000560 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000561 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000562
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100564 // Internal recursive implementation of `Populate`.
565 void PopulateRecursive(HBasicBlock* block);
566
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100568 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100569 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100570 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000571
572 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
573};
574
David Brazdilec16f792015-08-19 15:04:01 +0100575// Stores try/catch information for basic blocks.
576// Note that HGraph is constructed so that catch blocks cannot simultaneously
577// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100578class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100579 public:
580 // Try block information constructor.
581 explicit TryCatchInformation(const HTryBoundary& try_entry)
582 : try_entry_(&try_entry),
583 catch_dex_file_(nullptr),
584 catch_type_index_(DexFile::kDexNoIndex16) {
585 DCHECK(try_entry_ != nullptr);
586 }
587
588 // Catch block information constructor.
589 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
590 : try_entry_(nullptr),
591 catch_dex_file_(&dex_file),
592 catch_type_index_(catch_type_index) {}
593
594 bool IsTryBlock() const { return try_entry_ != nullptr; }
595
596 const HTryBoundary& GetTryEntry() const {
597 DCHECK(IsTryBlock());
598 return *try_entry_;
599 }
600
601 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
602
603 bool IsCatchAllTypeIndex() const {
604 DCHECK(IsCatchBlock());
605 return catch_type_index_ == DexFile::kDexNoIndex16;
606 }
607
608 uint16_t GetCatchTypeIndex() const {
609 DCHECK(IsCatchBlock());
610 return catch_type_index_;
611 }
612
613 const DexFile& GetCatchDexFile() const {
614 DCHECK(IsCatchBlock());
615 return *catch_dex_file_;
616 }
617
618 private:
619 // One of possibly several TryBoundary instructions entering the block's try.
620 // Only set for try blocks.
621 const HTryBoundary* try_entry_;
622
623 // Exception type information. Only set for catch blocks.
624 const DexFile* catch_dex_file_;
625 const uint16_t catch_type_index_;
626};
627
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100628static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100629static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100630
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000631// A block in a method. Contains the list of instructions represented
632// as a double linked list. Each block knows its predecessors and
633// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100634
Vladimir Markof9f64412015-09-02 14:05:49 +0100635class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000636 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600637 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000639 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
640 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000641 loop_information_(nullptr),
642 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000643 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100644 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100645 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100646 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000647 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000648 try_catch_information_(nullptr) {
649 predecessors_.reserve(kDefaultNumberOfPredecessors);
650 successors_.reserve(kDefaultNumberOfSuccessors);
651 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
652 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000653
Vladimir Marko60584552015-09-03 13:35:12 +0000654 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100655 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656 }
657
Vladimir Marko60584552015-09-03 13:35:12 +0000658 HBasicBlock* GetPredecessor(size_t pred_idx) const {
659 DCHECK_LT(pred_idx, predecessors_.size());
660 return predecessors_[pred_idx];
661 }
662
663 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100664 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000665 }
666
Vladimir Marko60584552015-09-03 13:35:12 +0000667 HBasicBlock* GetSuccessor(size_t succ_idx) const {
668 DCHECK_LT(succ_idx, successors_.size());
669 return successors_[succ_idx];
670 }
671
672 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
673 return ContainsElement(successors_, block, start_from);
674 }
675
676 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100677 return dominated_blocks_;
678 }
679
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100680 bool IsEntryBlock() const {
681 return graph_->GetEntryBlock() == this;
682 }
683
684 bool IsExitBlock() const {
685 return graph_->GetExitBlock() == this;
686 }
687
David Brazdil46e2a392015-03-16 17:31:52 +0000688 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000689 bool IsSingleTryBoundary() const;
690
691 // Returns true if this block emits nothing but a jump.
692 bool IsSingleJump() const {
693 HLoopInformation* loop_info = GetLoopInformation();
694 return (IsSingleGoto() || IsSingleTryBoundary())
695 // Back edges generate a suspend check.
696 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
697 }
David Brazdil46e2a392015-03-16 17:31:52 +0000698
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000699 void AddBackEdge(HBasicBlock* back_edge) {
700 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000701 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000702 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100703 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000704 loop_information_->AddBackEdge(back_edge);
705 }
706
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000707 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000708 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000709
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100710 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000711 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600712 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000713
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000714 HBasicBlock* GetDominator() const { return dominator_; }
715 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000716 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
717
718 void RemoveDominatedBlock(HBasicBlock* block) {
719 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100720 }
Vladimir Marko60584552015-09-03 13:35:12 +0000721
722 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
723 ReplaceElement(dominated_blocks_, existing, new_block);
724 }
725
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100726 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000727
728 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100729 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000730 }
731
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100732 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
733 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100734 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100735 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100736 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
737 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000738
739 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000740 successors_.push_back(block);
741 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000742 }
743
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100744 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
745 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100746 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000747 new_block->predecessors_.push_back(this);
748 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000749 }
750
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000751 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
752 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000753 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000754 new_block->successors_.push_back(this);
755 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 }
757
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100758 // Insert `this` between `predecessor` and `successor. This method
759 // preserves the indicies, and will update the first edge found between
760 // `predecessor` and `successor`.
761 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
762 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100763 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000764 successor->predecessors_[predecessor_index] = this;
765 predecessor->successors_[successor_index] = this;
766 successors_.push_back(successor);
767 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100768 }
769
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100770 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000771 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100772 }
773
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000774 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000775 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000776 }
777
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000779 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780 }
781
782 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000783 predecessors_.push_back(block);
784 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 }
786
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100787 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 DCHECK_EQ(predecessors_.size(), 2u);
789 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100790 }
791
David Brazdil769c9e52015-04-27 13:54:09 +0100792 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000793 DCHECK_EQ(successors_.size(), 2u);
794 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100795 }
796
David Brazdilfc6a86a2015-06-26 10:33:45 +0000797 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000798 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100799 }
800
David Brazdilfc6a86a2015-06-26 10:33:45 +0000801 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000802 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100803 }
804
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000806 DCHECK_EQ(GetPredecessors().size(), 1u);
807 return GetPredecessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000808 }
809
810 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000811 DCHECK_EQ(GetSuccessors().size(), 1u);
812 return GetSuccessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 }
814
815 // Returns whether the first occurrence of `predecessor` in the list of
816 // predecessors is at index `idx`.
817 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000818 DCHECK_EQ(GetPredecessor(idx), predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000819 return GetPredecessorIndexOf(predecessor) == idx;
820 }
821
David Brazdilffee3d32015-07-06 11:48:53 +0100822 // Returns the number of non-exceptional successors. SsaChecker ensures that
823 // these are stored at the beginning of the successor list.
824 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000825 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100826 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000827
828 // 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
832 // loop 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 Brazdila4b8c212015-05-07 09:59:30 +0100943 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100944 bool Dominates(HBasicBlock* block) const;
945
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100946 size_t GetLifetimeStart() const { return lifetime_start_; }
947 size_t GetLifetimeEnd() const { return lifetime_end_; }
948
949 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
950 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
951
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100952
David Brazdil8d5b8b22015-03-24 10:51:52 +0000953 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000954 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100955 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000956 bool HasSinglePhi() const;
957
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000958 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000959 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000960 ArenaVector<HBasicBlock*> predecessors_;
961 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100962 HInstructionList instructions_;
963 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000964 HLoopInformation* loop_information_;
965 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000966 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100967 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100968 // The dex program counter of the first instruction of this block.
969 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100970 size_t lifetime_start_;
971 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100972 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100973
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000974 friend class HGraph;
975 friend class HInstruction;
976
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000977 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
978};
979
David Brazdilb2bd1c52015-03-25 11:17:37 +0000980// Iterates over the LoopInformation of all loops which contain 'block'
981// from the innermost to the outermost.
982class HLoopInformationOutwardIterator : public ValueObject {
983 public:
984 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
985 : current_(block.GetLoopInformation()) {}
986
987 bool Done() const { return current_ == nullptr; }
988
989 void Advance() {
990 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100991 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000992 }
993
994 HLoopInformation* Current() const {
995 DCHECK(!Done());
996 return current_;
997 }
998
999 private:
1000 HLoopInformation* current_;
1001
1002 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1003};
1004
Alexandre Ramesef20f712015-06-09 10:29:30 +01001005#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001006 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001007 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001008 M(ArrayGet, Instruction) \
1009 M(ArrayLength, Instruction) \
1010 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +01001011 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001012 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001013 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001014 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001015 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001016 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001017 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001018 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001019 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001020 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001021 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001022 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001023 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001024 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001025 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001026 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001027 M(FloatConstant, Constant) \
1028 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001029 M(GreaterThan, Condition) \
1030 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001031 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001032 M(InstanceFieldGet, Instruction) \
1033 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001034 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001035 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001036 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001037 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001039 M(LessThan, Condition) \
1040 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001041 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001042 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001043 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001044 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001045 M(Local, Instruction) \
1046 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001047 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001048 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001049 M(Mul, BinaryOperation) \
1050 M(Neg, UnaryOperation) \
1051 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001052 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001053 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001054 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001055 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001056 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001057 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001058 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001059 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001060 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001061 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001062 M(Return, Instruction) \
1063 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001064 M(Shl, BinaryOperation) \
1065 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001066 M(StaticFieldGet, Instruction) \
1067 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001068 M(StoreLocal, Instruction) \
1069 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001070 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001071 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001072 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001073 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001074 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001075 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001076 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001077
Alexandre Ramesef20f712015-06-09 10:29:30 +01001078#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1079
1080#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1081
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001082#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1083
Mark Mendell0616ae02015-04-17 12:49:27 -04001084#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1085 M(X86ComputeBaseMethodAddress, Instruction) \
1086 M(X86LoadFromConstantTable, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001087
1088#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1089
1090#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1091 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1092 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1093 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001094 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001095 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1096 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1097
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001098#define FOR_EACH_INSTRUCTION(M) \
1099 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1100 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001101 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001102 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001103 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001104
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001105#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001106FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1107#undef FORWARD_DECLARATION
1108
Roland Levillainccc07a92014-09-16 14:48:16 +01001109#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001110 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1111 const char* DebugName() const OVERRIDE { return #type; } \
1112 const H##type* As##type() const OVERRIDE { return this; } \
1113 H##type* As##type() OVERRIDE { return this; } \
1114 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001115 return other->Is##type(); \
1116 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001117 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001118
David Brazdiled596192015-01-23 10:39:45 +00001119template <typename T> class HUseList;
1120
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001121template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001122class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001123 public:
David Brazdiled596192015-01-23 10:39:45 +00001124 HUseListNode* GetPrevious() const { return prev_; }
1125 HUseListNode* GetNext() const { return next_; }
1126 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001127 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001128 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001129
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001130 private:
David Brazdiled596192015-01-23 10:39:45 +00001131 HUseListNode(T user, size_t index)
1132 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1133
1134 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001135 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001136 HUseListNode<T>* prev_;
1137 HUseListNode<T>* next_;
1138
1139 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001140
1141 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1142};
1143
David Brazdiled596192015-01-23 10:39:45 +00001144template <typename T>
1145class HUseList : public ValueObject {
1146 public:
1147 HUseList() : first_(nullptr) {}
1148
1149 void Clear() {
1150 first_ = nullptr;
1151 }
1152
1153 // Adds a new entry at the beginning of the use list and returns
1154 // the newly created node.
1155 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001156 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001157 if (IsEmpty()) {
1158 first_ = new_node;
1159 } else {
1160 first_->prev_ = new_node;
1161 new_node->next_ = first_;
1162 first_ = new_node;
1163 }
1164 return new_node;
1165 }
1166
1167 HUseListNode<T>* GetFirst() const {
1168 return first_;
1169 }
1170
1171 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001172 DCHECK(node != nullptr);
1173 DCHECK(Contains(node));
1174
David Brazdiled596192015-01-23 10:39:45 +00001175 if (node->prev_ != nullptr) {
1176 node->prev_->next_ = node->next_;
1177 }
1178 if (node->next_ != nullptr) {
1179 node->next_->prev_ = node->prev_;
1180 }
1181 if (node == first_) {
1182 first_ = node->next_;
1183 }
1184 }
1185
David Brazdil1abb4192015-02-17 18:33:36 +00001186 bool Contains(const HUseListNode<T>* node) const {
1187 if (node == nullptr) {
1188 return false;
1189 }
1190 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1191 if (current == node) {
1192 return true;
1193 }
1194 }
1195 return false;
1196 }
1197
David Brazdiled596192015-01-23 10:39:45 +00001198 bool IsEmpty() const {
1199 return first_ == nullptr;
1200 }
1201
1202 bool HasOnlyOneUse() const {
1203 return first_ != nullptr && first_->next_ == nullptr;
1204 }
1205
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001206 size_t SizeSlow() const {
1207 size_t count = 0;
1208 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1209 ++count;
1210 }
1211 return count;
1212 }
1213
David Brazdiled596192015-01-23 10:39:45 +00001214 private:
1215 HUseListNode<T>* first_;
1216};
1217
1218template<typename T>
1219class HUseIterator : public ValueObject {
1220 public:
1221 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1222
1223 bool Done() const { return current_ == nullptr; }
1224
1225 void Advance() {
1226 DCHECK(!Done());
1227 current_ = current_->GetNext();
1228 }
1229
1230 HUseListNode<T>* Current() const {
1231 DCHECK(!Done());
1232 return current_;
1233 }
1234
1235 private:
1236 HUseListNode<T>* current_;
1237
1238 friend class HValue;
1239};
1240
David Brazdil1abb4192015-02-17 18:33:36 +00001241// This class is used by HEnvironment and HInstruction classes to record the
1242// instructions they use and pointers to the corresponding HUseListNodes kept
1243// by the used instructions.
1244template <typename T>
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001245class HUserRecord {
David Brazdil1abb4192015-02-17 18:33:36 +00001246 public:
1247 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1248 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1249
1250 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1251 : instruction_(old_record.instruction_), use_node_(use_node) {
1252 DCHECK(instruction_ != nullptr);
1253 DCHECK(use_node_ != nullptr);
1254 DCHECK(old_record.use_node_ == nullptr);
1255 }
1256
1257 HInstruction* GetInstruction() const { return instruction_; }
1258 HUseListNode<T>* GetUseNode() const { return use_node_; }
1259
1260 private:
1261 // Instruction used by the user.
1262 HInstruction* instruction_;
1263
1264 // Corresponding entry in the use list kept by 'instruction_'.
1265 HUseListNode<T>* use_node_;
1266};
1267
Aart Bik854a02b2015-07-14 16:07:00 -07001268/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001269 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001270 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001271 * For write/read dependences on fields/arrays, the dependence analysis uses
1272 * type disambiguation (e.g. a float field write cannot modify the value of an
1273 * integer field read) and the access type (e.g. a reference array write cannot
1274 * modify the value of a reference field read [although it may modify the
1275 * reference fetch prior to reading the field, which is represented by its own
1276 * write/read dependence]). The analysis makes conservative points-to
1277 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1278 * the same, and any reference read depends on any reference read without
1279 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001280 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001281 * The internal representation uses 38-bit and is described in the table below.
1282 * The first line indicates the side effect, and for field/array accesses the
1283 * second line indicates the type of the access (in the order of the
1284 * Primitive::Type enum).
1285 * The two numbered lines below indicate the bit position in the bitfield (read
1286 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001287 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001288 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1289 * +-------------+---------+---------+--------------+---------+---------+
1290 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1291 * | 3 |333333322|222222221| 1 |111111110|000000000|
1292 * | 7 |654321098|765432109| 8 |765432109|876543210|
1293 *
1294 * Note that, to ease the implementation, 'changes' bits are least significant
1295 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001296 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001297class SideEffects : public ValueObject {
1298 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001299 SideEffects() : flags_(0) {}
1300
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001301 static SideEffects None() {
1302 return SideEffects(0);
1303 }
1304
1305 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001306 return SideEffects(kAllChangeBits | kAllDependOnBits);
1307 }
1308
1309 static SideEffects AllChanges() {
1310 return SideEffects(kAllChangeBits);
1311 }
1312
1313 static SideEffects AllDependencies() {
1314 return SideEffects(kAllDependOnBits);
1315 }
1316
1317 static SideEffects AllExceptGCDependency() {
1318 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1319 }
1320
1321 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001322 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001323 }
1324
Aart Bik34c3ba92015-07-20 14:08:59 -07001325 static SideEffects AllWrites() {
1326 return SideEffects(kAllWrites);
1327 }
1328
1329 static SideEffects AllReads() {
1330 return SideEffects(kAllReads);
1331 }
1332
1333 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1334 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001335 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001336 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001337 }
1338
Aart Bik854a02b2015-07-14 16:07:00 -07001339 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1340 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001341 }
1342
Aart Bik34c3ba92015-07-20 14:08:59 -07001343 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1344 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001345 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001346 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001347 }
1348
1349 static SideEffects ArrayReadOfType(Primitive::Type type) {
1350 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1351 }
1352
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001353 static SideEffects CanTriggerGC() {
1354 return SideEffects(1ULL << kCanTriggerGCBit);
1355 }
1356
1357 static SideEffects DependsOnGC() {
1358 return SideEffects(1ULL << kDependsOnGCBit);
1359 }
1360
Aart Bik854a02b2015-07-14 16:07:00 -07001361 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001362 SideEffects Union(SideEffects other) const {
1363 return SideEffects(flags_ | other.flags_);
1364 }
1365
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001366 SideEffects Exclusion(SideEffects other) const {
1367 return SideEffects(flags_ & ~other.flags_);
1368 }
1369
1370 bool Includes(SideEffects other) const {
1371 return (other.flags_ & flags_) == other.flags_;
1372 }
1373
1374 bool HasSideEffects() const {
1375 return (flags_ & kAllChangeBits);
1376 }
1377
1378 bool HasDependencies() const {
1379 return (flags_ & kAllDependOnBits);
1380 }
1381
1382 // Returns true if there are no side effects or dependencies.
1383 bool DoesNothing() const {
1384 return flags_ == 0;
1385 }
1386
Aart Bik854a02b2015-07-14 16:07:00 -07001387 // Returns true if something is written.
1388 bool DoesAnyWrite() const {
1389 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001390 }
1391
Aart Bik854a02b2015-07-14 16:07:00 -07001392 // Returns true if something is read.
1393 bool DoesAnyRead() const {
1394 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001395 }
1396
Aart Bik854a02b2015-07-14 16:07:00 -07001397 // Returns true if potentially everything is written and read
1398 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001399 bool DoesAllReadWrite() const {
1400 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1401 }
1402
Aart Bik854a02b2015-07-14 16:07:00 -07001403 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001404 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001405 }
1406
1407 // Returns true if this may read something written by other.
1408 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001409 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1410 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001411 }
1412
1413 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001414 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001415 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001416 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001417 for (int s = kLastBit; s >= 0; s--) {
1418 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1419 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1420 // This is a bit for the GC side effect.
1421 if (current_bit_is_set) {
1422 flags += "GC";
1423 }
Aart Bik854a02b2015-07-14 16:07:00 -07001424 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001425 } else {
1426 // This is a bit for the array/field analysis.
1427 // The underscore character stands for the 'can trigger GC' bit.
1428 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1429 if (current_bit_is_set) {
1430 flags += kDebug[s];
1431 }
1432 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1433 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1434 flags += "|";
1435 }
1436 }
Aart Bik854a02b2015-07-14 16:07:00 -07001437 }
1438 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001439 }
1440
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001441 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001442
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001443 private:
1444 static constexpr int kFieldArrayAnalysisBits = 9;
1445
1446 static constexpr int kFieldWriteOffset = 0;
1447 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1448 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1449 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1450
1451 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1452
1453 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1454 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1455 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1456 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1457
1458 static constexpr int kLastBit = kDependsOnGCBit;
1459 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1460
1461 // Aliases.
1462
1463 static_assert(kChangeBits == kDependOnBits,
1464 "the 'change' bits should match the 'depend on' bits.");
1465
1466 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1467 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1468 static constexpr uint64_t kAllWrites =
1469 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1470 static constexpr uint64_t kAllReads =
1471 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001472
Aart Bik854a02b2015-07-14 16:07:00 -07001473 // Work around the fact that HIR aliases I/F and J/D.
1474 // TODO: remove this interceptor once HIR types are clean
1475 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1476 switch (type) {
1477 case Primitive::kPrimInt:
1478 case Primitive::kPrimFloat:
1479 return TypeFlag(Primitive::kPrimInt, offset) |
1480 TypeFlag(Primitive::kPrimFloat, offset);
1481 case Primitive::kPrimLong:
1482 case Primitive::kPrimDouble:
1483 return TypeFlag(Primitive::kPrimLong, offset) |
1484 TypeFlag(Primitive::kPrimDouble, offset);
1485 default:
1486 return TypeFlag(type, offset);
1487 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001488 }
1489
Aart Bik854a02b2015-07-14 16:07:00 -07001490 // Translates type to bit flag.
1491 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1492 CHECK_NE(type, Primitive::kPrimVoid);
1493 const uint64_t one = 1;
1494 const int shift = type; // 0-based consecutive enum
1495 DCHECK_LE(kFieldWriteOffset, shift);
1496 DCHECK_LT(shift, kArrayWriteOffset);
1497 return one << (type + offset);
1498 }
1499
1500 // Private constructor on direct flags value.
1501 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1502
1503 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001504};
1505
David Brazdiled596192015-01-23 10:39:45 +00001506// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001507class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001508 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001509 HEnvironment(ArenaAllocator* arena,
1510 size_t number_of_vregs,
1511 const DexFile& dex_file,
1512 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001513 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001514 InvokeType invoke_type,
1515 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001516 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1517 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001518 parent_(nullptr),
1519 dex_file_(dex_file),
1520 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001521 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001522 invoke_type_(invoke_type),
1523 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001524 }
1525
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001526 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001527 : HEnvironment(arena,
1528 to_copy.Size(),
1529 to_copy.GetDexFile(),
1530 to_copy.GetMethodIdx(),
1531 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001532 to_copy.GetInvokeType(),
1533 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001534
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001535 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001536 if (parent_ != nullptr) {
1537 parent_->SetAndCopyParentChain(allocator, parent);
1538 } else {
1539 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1540 parent_->CopyFrom(parent);
1541 if (parent->GetParent() != nullptr) {
1542 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1543 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001544 }
David Brazdiled596192015-01-23 10:39:45 +00001545 }
1546
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001547 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1548 void CopyFrom(HEnvironment* environment);
1549
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001550 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1551 // input to the loop phi instead. This is for inserting instructions that
1552 // require an environment (like HDeoptimization) in the loop pre-header.
1553 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001554
1555 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001556 DCHECK_LT(index, Size());
1557 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001558 }
1559
1560 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001561 DCHECK_LT(index, Size());
1562 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001563 }
1564
David Brazdil1abb4192015-02-17 18:33:36 +00001565 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001566
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001567 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001568
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001569 HEnvironment* GetParent() const { return parent_; }
1570
1571 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001572 DCHECK_LT(index, Size());
1573 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001574 }
1575
1576 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001577 DCHECK_LT(index, Size());
1578 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001579 }
1580
1581 uint32_t GetDexPc() const {
1582 return dex_pc_;
1583 }
1584
1585 uint32_t GetMethodIdx() const {
1586 return method_idx_;
1587 }
1588
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001589 InvokeType GetInvokeType() const {
1590 return invoke_type_;
1591 }
1592
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001593 const DexFile& GetDexFile() const {
1594 return dex_file_;
1595 }
1596
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001597 HInstruction* GetHolder() const {
1598 return holder_;
1599 }
1600
David Brazdiled596192015-01-23 10:39:45 +00001601 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001602 // Record instructions' use entries of this environment for constant-time removal.
1603 // It should only be called by HInstruction when a new environment use is added.
1604 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1605 DCHECK(env_use->GetUser() == this);
1606 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001607 DCHECK_LT(index, Size());
1608 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001609 }
David Brazdiled596192015-01-23 10:39:45 +00001610
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001611 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1612 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001613 HEnvironment* parent_;
1614 const DexFile& dex_file_;
1615 const uint32_t method_idx_;
1616 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001617 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001618
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001619 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001620 HInstruction* const holder_;
1621
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001622 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001623
1624 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1625};
1626
Calin Juravleacf735c2015-02-12 15:25:22 +00001627class ReferenceTypeInfo : ValueObject {
1628 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001629 typedef Handle<mirror::Class> TypeHandle;
1630
Calin Juravle2e768302015-07-28 14:41:11 +00001631 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1632 // The constructor will check that the type_handle is valid.
1633 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001634 }
1635
Calin Juravle2e768302015-07-28 14:41:11 +00001636 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1637
1638 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1639 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001640 }
1641
Calin Juravle2e768302015-07-28 14:41:11 +00001642 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1643 return IsValidHandle(type_handle_);
1644 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001645 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001646
1647 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1648 DCHECK(IsValid());
1649 return GetTypeHandle()->IsObjectClass();
1650 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001651 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001652 DCHECK(IsValid());
1653 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001654 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001655
1656 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1657
Mathieu Chartier90443472015-07-16 20:32:27 -07001658 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001659 DCHECK(IsValid());
1660 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001661 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1662 }
1663
1664 // Returns true if the type information provide the same amount of details.
1665 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001666 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001667 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001668 if (!IsValid() && !rti.IsValid()) {
1669 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001670 return true;
1671 }
Calin Juravle2e768302015-07-28 14:41:11 +00001672 if (!IsValid() || !rti.IsValid()) {
1673 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001674 return false;
1675 }
Calin Juravle2e768302015-07-28 14:41:11 +00001676 return IsExact() == rti.IsExact()
1677 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001678 }
1679
1680 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001681 ReferenceTypeInfo();
1682 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001683
Calin Juravleacf735c2015-02-12 15:25:22 +00001684 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001685 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001686 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001687 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001688 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001689};
1690
1691std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1692
Vladimir Markof9f64412015-09-02 14:05:49 +01001693class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001694 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001695 HInstruction(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001696 : previous_(nullptr),
1697 next_(nullptr),
1698 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001699 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001700 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001701 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001702 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001703 locations_(nullptr),
1704 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001705 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001706 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001707 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001708
Dave Allison20dfc792014-06-16 20:44:29 -07001709 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001710
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001711#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001712 enum InstructionKind {
1713 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1714 };
1715#undef DECLARE_KIND
1716
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001717 HInstruction* GetNext() const { return next_; }
1718 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001719
Calin Juravle77520bc2015-01-12 18:45:46 +00001720 HInstruction* GetNextDisregardingMoves() const;
1721 HInstruction* GetPreviousDisregardingMoves() const;
1722
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001723 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001724 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001725 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001726 bool IsInBlock() const { return block_ != nullptr; }
1727 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001728 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001729
Roland Levillain6b879dd2014-09-22 17:13:44 +01001730 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001731 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001732
1733 virtual void Accept(HGraphVisitor* visitor) = 0;
1734 virtual const char* DebugName() const = 0;
1735
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001736 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001737 void SetRawInputAt(size_t index, HInstruction* input) {
1738 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1739 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001740
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001741 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001742
1743 uint32_t GetDexPc() const { return dex_pc_; }
1744
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001745 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001746
Roland Levillaine161a2a2014-10-03 12:45:18 +01001747 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001748 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001749
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001750 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001751 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001752
Calin Juravle10e244f2015-01-26 18:54:32 +00001753 // Does not apply for all instructions, but having this at top level greatly
1754 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001755 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001756 virtual bool CanBeNull() const {
1757 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1758 return true;
1759 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001760
Calin Juravle641547a2015-04-21 22:08:51 +01001761 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1762 UNUSED(obj);
1763 return false;
1764 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001765
Calin Juravle2e768302015-07-28 14:41:11 +00001766 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001767
Calin Juravle61d544b2015-02-23 16:46:57 +00001768 ReferenceTypeInfo GetReferenceTypeInfo() const {
1769 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1770 return reference_type_info_;
1771 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001772
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001773 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001774 DCHECK(user != nullptr);
1775 HUseListNode<HInstruction*>* use =
1776 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1777 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001778 }
1779
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001780 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001781 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001782 HUseListNode<HEnvironment*>* env_use =
1783 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1784 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001785 }
1786
David Brazdil1abb4192015-02-17 18:33:36 +00001787 void RemoveAsUserOfInput(size_t input) {
1788 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1789 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1790 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001791
David Brazdil1abb4192015-02-17 18:33:36 +00001792 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1793 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001794
David Brazdiled596192015-01-23 10:39:45 +00001795 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1796 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001797 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001798 bool HasOnlyOneNonEnvironmentUse() const {
1799 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1800 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001801
Roland Levillain6c82d402014-10-13 16:10:27 +01001802 // Does this instruction strictly dominate `other_instruction`?
1803 // Returns false if this instruction and `other_instruction` are the same.
1804 // Aborts if this instruction and `other_instruction` are both phis.
1805 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001806
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001807 int GetId() const { return id_; }
1808 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001809
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001810 int GetSsaIndex() const { return ssa_index_; }
1811 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1812 bool HasSsaIndex() const { return ssa_index_ != -1; }
1813
1814 bool HasEnvironment() const { return environment_ != nullptr; }
1815 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001816 // Set the `environment_` field. Raw because this method does not
1817 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001818 void SetRawEnvironment(HEnvironment* environment) {
1819 DCHECK(environment_ == nullptr);
1820 DCHECK_EQ(environment->GetHolder(), this);
1821 environment_ = environment;
1822 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001823
1824 // Set the environment of this instruction, copying it from `environment`. While
1825 // copying, the uses lists are being updated.
1826 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001827 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001828 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001829 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001830 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001831 if (environment->GetParent() != nullptr) {
1832 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1833 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001834 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001835
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001836 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1837 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001838 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001839 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001840 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001841 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001842 if (environment->GetParent() != nullptr) {
1843 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1844 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001845 }
1846
Nicolas Geoffray39468442014-09-02 15:17:15 +01001847 // Returns the number of entries in the environment. Typically, that is the
1848 // number of dex registers in a method. It could be more in case of inlining.
1849 size_t EnvironmentSize() const;
1850
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001851 LocationSummary* GetLocations() const { return locations_; }
1852 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001853
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001854 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001855 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001856
Alexandre Rames188d4312015-04-09 18:30:21 +01001857 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1858 // uses of this instruction by `other` are *not* updated.
1859 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1860 ReplaceWith(other);
1861 other->ReplaceInput(this, use_index);
1862 }
1863
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001864 // Move `this` instruction before `cursor`.
1865 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001866
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001867#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001868 bool Is##type() const { return (As##type() != nullptr); } \
1869 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001870 virtual H##type* As##type() { return nullptr; }
1871
1872 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1873#undef INSTRUCTION_TYPE_CHECK
1874
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001875 // Returns whether the instruction can be moved within the graph.
1876 virtual bool CanBeMoved() const { return false; }
1877
1878 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001879 virtual bool InstructionTypeEquals(HInstruction* other) const {
1880 UNUSED(other);
1881 return false;
1882 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001883
1884 // Returns whether any data encoded in the two instructions is equal.
1885 // This method does not look at the inputs. Both instructions must be
1886 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001887 virtual bool InstructionDataEquals(HInstruction* other) const {
1888 UNUSED(other);
1889 return false;
1890 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001891
1892 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001893 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001894 // 2) Their inputs are identical.
1895 bool Equals(HInstruction* other) const;
1896
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001897 virtual InstructionKind GetKind() const = 0;
1898
1899 virtual size_t ComputeHashCode() const {
1900 size_t result = GetKind();
1901 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1902 result = (result * 31) + InputAt(i)->GetId();
1903 }
1904 return result;
1905 }
1906
1907 SideEffects GetSideEffects() const { return side_effects_; }
1908
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001909 size_t GetLifetimePosition() const { return lifetime_position_; }
1910 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1911 LiveInterval* GetLiveInterval() const { return live_interval_; }
1912 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1913 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1914
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001915 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1916
1917 // Returns whether the code generation of the instruction will require to have access
1918 // to the current method. Such instructions are:
1919 // (1): Instructions that require an environment, as calling the runtime requires
1920 // to walk the stack and have the current method stored at a specific stack address.
1921 // (2): Object literals like classes and strings, that are loaded from the dex cache
1922 // fields of the current method.
1923 bool NeedsCurrentMethod() const {
1924 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1925 }
1926
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001927 virtual bool NeedsDexCache() const { return false; }
1928
Mark Mendellc4701932015-04-10 13:18:51 -04001929 // Does this instruction have any use in an environment before
1930 // control flow hits 'other'?
1931 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1932
1933 // Remove all references to environment uses of this instruction.
1934 // The caller must ensure that this is safe to do.
1935 void RemoveEnvironmentUsers();
1936
David Brazdil1abb4192015-02-17 18:33:36 +00001937 protected:
1938 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1939 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1940
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001941 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001942 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1943
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001944 HInstruction* previous_;
1945 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001946 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001947 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001948
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001949 // An instruction gets an id when it is added to the graph.
1950 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001951 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001952 int id_;
1953
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001954 // When doing liveness analysis, instructions that have uses get an SSA index.
1955 int ssa_index_;
1956
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001958 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959
1960 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001961 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001962
Nicolas Geoffray39468442014-09-02 15:17:15 +01001963 // The environment associated with this instruction. Not null if the instruction
1964 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001965 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001966
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001967 // Set by the code generator.
1968 LocationSummary* locations_;
1969
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001970 // Set by the liveness analysis.
1971 LiveInterval* live_interval_;
1972
1973 // Set by the liveness analysis, this is the position in a linear
1974 // order of blocks where this instruction's live interval start.
1975 size_t lifetime_position_;
1976
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001977 const SideEffects side_effects_;
1978
Calin Juravleacf735c2015-02-12 15:25:22 +00001979 // TODO: for primitive types this should be marked as invalid.
1980 ReferenceTypeInfo reference_type_info_;
1981
David Brazdil1abb4192015-02-17 18:33:36 +00001982 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001983 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001984 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001985 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001986 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001987
1988 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1989};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001990std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001991
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001992class HInputIterator : public ValueObject {
1993 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001994 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001995
1996 bool Done() const { return index_ == instruction_->InputCount(); }
1997 HInstruction* Current() const { return instruction_->InputAt(index_); }
1998 void Advance() { index_++; }
1999
2000 private:
2001 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002002 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002003
2004 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2005};
2006
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002007class HInstructionIterator : public ValueObject {
2008 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002009 explicit HInstructionIterator(const HInstructionList& instructions)
2010 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002011 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002012 }
2013
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002014 bool Done() const { return instruction_ == nullptr; }
2015 HInstruction* Current() const { return instruction_; }
2016 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002017 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002018 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002019 }
2020
2021 private:
2022 HInstruction* instruction_;
2023 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002024
2025 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002026};
2027
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002028class HBackwardInstructionIterator : public ValueObject {
2029 public:
2030 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2031 : instruction_(instructions.last_instruction_) {
2032 next_ = Done() ? nullptr : instruction_->GetPrevious();
2033 }
2034
2035 bool Done() const { return instruction_ == nullptr; }
2036 HInstruction* Current() const { return instruction_; }
2037 void Advance() {
2038 instruction_ = next_;
2039 next_ = Done() ? nullptr : instruction_->GetPrevious();
2040 }
2041
2042 private:
2043 HInstruction* instruction_;
2044 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002045
2046 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002047};
2048
Vladimir Markof9f64412015-09-02 14:05:49 +01002049template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002050class HTemplateInstruction: public HInstruction {
2051 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002052 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2053 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002054 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002056 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002057
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002058 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002059 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2060 DCHECK_LT(i, N);
2061 return inputs_[i];
2062 }
David Brazdil1abb4192015-02-17 18:33:36 +00002063
2064 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002065 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002066 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002067 }
2068
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002069 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002070 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002071
2072 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002073};
2074
Vladimir Markof9f64412015-09-02 14:05:49 +01002075// HTemplateInstruction specialization for N=0.
2076template<>
2077class HTemplateInstruction<0>: public HInstruction {
2078 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002079 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2080 : HInstruction(side_effects, dex_pc) {}
2081
Vladimir Markof9f64412015-09-02 14:05:49 +01002082 virtual ~HTemplateInstruction() {}
2083
2084 size_t InputCount() const OVERRIDE { return 0; }
2085
2086 protected:
2087 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2088 LOG(FATAL) << "Unreachable";
2089 UNREACHABLE();
2090 }
2091
2092 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2093 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2094 LOG(FATAL) << "Unreachable";
2095 UNREACHABLE();
2096 }
2097
2098 private:
2099 friend class SsaBuilder;
2100};
2101
Dave Allison20dfc792014-06-16 20:44:29 -07002102template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002103class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002104 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002105 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2106 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002107 virtual ~HExpression() {}
2108
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002109 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002110
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002111 protected:
2112 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002113};
2114
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002115// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2116// instruction that branches to the exit block.
2117class HReturnVoid : public HTemplateInstruction<0> {
2118 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002119 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2120 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002121
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002122 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002123
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002124 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002125
2126 private:
2127 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2128};
2129
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002130// Represents dex's RETURN opcodes. A HReturn is a control flow
2131// instruction that branches to the exit block.
2132class HReturn : public HTemplateInstruction<1> {
2133 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002134 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2135 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002136 SetRawInputAt(0, value);
2137 }
2138
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002139 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002140
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002141 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002142
2143 private:
2144 DISALLOW_COPY_AND_ASSIGN(HReturn);
2145};
2146
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002147// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002148// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002149// exit block.
2150class HExit : public HTemplateInstruction<0> {
2151 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002152 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002153
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002154 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002155
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002156 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157
2158 private:
2159 DISALLOW_COPY_AND_ASSIGN(HExit);
2160};
2161
2162// Jumps from one block to another.
2163class HGoto : public HTemplateInstruction<0> {
2164 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002165 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002166
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002167 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002168
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002169 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002170 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002171 }
2172
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002173 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002174
2175 private:
2176 DISALLOW_COPY_AND_ASSIGN(HGoto);
2177};
2178
Roland Levillain9867bc72015-08-05 10:21:34 +01002179class HConstant : public HExpression<0> {
2180 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002181 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2182 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002183
2184 bool CanBeMoved() const OVERRIDE { return true; }
2185
2186 virtual bool IsMinusOne() const { return false; }
2187 virtual bool IsZero() const { return false; }
2188 virtual bool IsOne() const { return false; }
2189
David Brazdil77a48ae2015-09-15 12:34:04 +00002190 virtual uint64_t GetValueAsUint64() const = 0;
2191
Roland Levillain9867bc72015-08-05 10:21:34 +01002192 DECLARE_INSTRUCTION(Constant);
2193
2194 private:
2195 DISALLOW_COPY_AND_ASSIGN(HConstant);
2196};
2197
2198class HNullConstant : public HConstant {
2199 public:
2200 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2201 return true;
2202 }
2203
David Brazdil77a48ae2015-09-15 12:34:04 +00002204 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2205
Roland Levillain9867bc72015-08-05 10:21:34 +01002206 size_t ComputeHashCode() const OVERRIDE { return 0; }
2207
2208 DECLARE_INSTRUCTION(NullConstant);
2209
2210 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002211 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002212
2213 friend class HGraph;
2214 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2215};
2216
2217// Constants of the type int. Those can be from Dex instructions, or
2218// synthesized (for example with the if-eqz instruction).
2219class HIntConstant : public HConstant {
2220 public:
2221 int32_t GetValue() const { return value_; }
2222
David Brazdil77a48ae2015-09-15 12:34:04 +00002223 uint64_t GetValueAsUint64() const OVERRIDE { return static_cast<uint64_t>(value_); }
2224
Roland Levillain9867bc72015-08-05 10:21:34 +01002225 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2226 DCHECK(other->IsIntConstant());
2227 return other->AsIntConstant()->value_ == value_;
2228 }
2229
2230 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2231
2232 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2233 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2234 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2235
2236 DECLARE_INSTRUCTION(IntConstant);
2237
2238 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002239 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2240 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2241 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2242 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002243
2244 const int32_t value_;
2245
2246 friend class HGraph;
2247 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2248 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2249 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2250};
2251
2252class HLongConstant : public HConstant {
2253 public:
2254 int64_t GetValue() const { return value_; }
2255
David Brazdil77a48ae2015-09-15 12:34:04 +00002256 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2257
Roland Levillain9867bc72015-08-05 10:21:34 +01002258 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2259 DCHECK(other->IsLongConstant());
2260 return other->AsLongConstant()->value_ == value_;
2261 }
2262
2263 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2264
2265 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2266 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2267 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2268
2269 DECLARE_INSTRUCTION(LongConstant);
2270
2271 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002272 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2273 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002274
2275 const int64_t value_;
2276
2277 friend class HGraph;
2278 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2279};
Dave Allison20dfc792014-06-16 20:44:29 -07002280
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002281// Conditional branch. A block ending with an HIf instruction must have
2282// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002283class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002284 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002285 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2286 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002287 SetRawInputAt(0, input);
2288 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002289
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002290 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002291
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002292 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002293 return GetBlock()->GetSuccessor(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002294 }
2295
2296 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002297 return GetBlock()->GetSuccessor(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002298 }
2299
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002300 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002301
2302 private:
2303 DISALLOW_COPY_AND_ASSIGN(HIf);
2304};
2305
David Brazdilfc6a86a2015-06-26 10:33:45 +00002306
2307// Abstract instruction which marks the beginning and/or end of a try block and
2308// links it to the respective exception handlers. Behaves the same as a Goto in
2309// non-exceptional control flow.
2310// Normal-flow successor is stored at index zero, exception handlers under
2311// higher indices in no particular order.
2312class HTryBoundary : public HTemplateInstruction<0> {
2313 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002314 enum BoundaryKind {
2315 kEntry,
2316 kExit,
2317 };
2318
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002319 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2320 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002321
2322 bool IsControlFlow() const OVERRIDE { return true; }
2323
2324 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko60584552015-09-03 13:35:12 +00002325 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessor(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002326
2327 // Returns whether `handler` is among its exception handlers (non-zero index
2328 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002329 bool HasExceptionHandler(const HBasicBlock& handler) const {
2330 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002331 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002332 }
2333
2334 // If not present already, adds `handler` to its block's list of exception
2335 // handlers.
2336 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002337 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002338 GetBlock()->AddSuccessor(handler);
2339 }
2340 }
2341
David Brazdil56e1acc2015-06-30 15:41:36 +01002342 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002343
David Brazdilffee3d32015-07-06 11:48:53 +01002344 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2345
David Brazdilfc6a86a2015-06-26 10:33:45 +00002346 DECLARE_INSTRUCTION(TryBoundary);
2347
2348 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002349 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002350
2351 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2352};
2353
David Brazdilffee3d32015-07-06 11:48:53 +01002354// Iterator over exception handlers of a given HTryBoundary, i.e. over
2355// exceptional successors of its basic block.
2356class HExceptionHandlerIterator : public ValueObject {
2357 public:
2358 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2359 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2360
Vladimir Marko60584552015-09-03 13:35:12 +00002361 bool Done() const { return index_ == block_.GetSuccessors().size(); }
2362 HBasicBlock* Current() const { return block_.GetSuccessor(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002363 size_t CurrentSuccessorIndex() const { return index_; }
2364 void Advance() { ++index_; }
2365
2366 private:
2367 const HBasicBlock& block_;
2368 size_t index_;
2369
2370 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2371};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002372
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002373// Deoptimize to interpreter, upon checking a condition.
2374class HDeoptimize : public HTemplateInstruction<1> {
2375 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002376 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2377 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002378 SetRawInputAt(0, cond);
2379 }
2380
2381 bool NeedsEnvironment() const OVERRIDE { return true; }
2382 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002383
2384 DECLARE_INSTRUCTION(Deoptimize);
2385
2386 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002387 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2388};
2389
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002390// Represents the ArtMethod that was passed as a first argument to
2391// the method. It is used by instructions that depend on it, like
2392// instructions that work with the dex cache.
2393class HCurrentMethod : public HExpression<0> {
2394 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002395 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2396 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002397
2398 DECLARE_INSTRUCTION(CurrentMethod);
2399
2400 private:
2401 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2402};
2403
Roland Levillain88cb1752014-10-20 16:36:47 +01002404class HUnaryOperation : public HExpression<1> {
2405 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002406 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2407 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002408 SetRawInputAt(0, input);
2409 }
2410
2411 HInstruction* GetInput() const { return InputAt(0); }
2412 Primitive::Type GetResultType() const { return GetType(); }
2413
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002414 bool CanBeMoved() const OVERRIDE { return true; }
2415 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002416 UNUSED(other);
2417 return true;
2418 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002419
Roland Levillain9240d6a2014-10-20 16:47:04 +01002420 // Try to statically evaluate `operation` and return a HConstant
2421 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002422 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002423 HConstant* TryStaticEvaluation() const;
2424
2425 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002426 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2427 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002428
Roland Levillain88cb1752014-10-20 16:36:47 +01002429 DECLARE_INSTRUCTION(UnaryOperation);
2430
2431 private:
2432 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2433};
2434
Dave Allison20dfc792014-06-16 20:44:29 -07002435class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002436 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002437 HBinaryOperation(Primitive::Type result_type,
2438 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002439 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002440 SideEffects side_effects = SideEffects::None(),
2441 uint32_t dex_pc = kNoDexPc)
2442 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002443 SetRawInputAt(0, left);
2444 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002445 }
2446
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002447 HInstruction* GetLeft() const { return InputAt(0); }
2448 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002449 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002450
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002451 virtual bool IsCommutative() const { return false; }
2452
2453 // Put constant on the right.
2454 // Returns whether order is changed.
2455 bool OrderInputsWithConstantOnTheRight() {
2456 HInstruction* left = InputAt(0);
2457 HInstruction* right = InputAt(1);
2458 if (left->IsConstant() && !right->IsConstant()) {
2459 ReplaceInput(right, 0);
2460 ReplaceInput(left, 1);
2461 return true;
2462 }
2463 return false;
2464 }
2465
2466 // Order inputs by instruction id, but favor constant on the right side.
2467 // This helps GVN for commutative ops.
2468 void OrderInputs() {
2469 DCHECK(IsCommutative());
2470 HInstruction* left = InputAt(0);
2471 HInstruction* right = InputAt(1);
2472 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2473 return;
2474 }
2475 if (OrderInputsWithConstantOnTheRight()) {
2476 return;
2477 }
2478 // Order according to instruction id.
2479 if (left->GetId() > right->GetId()) {
2480 ReplaceInput(right, 0);
2481 ReplaceInput(left, 1);
2482 }
2483 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002484
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002485 bool CanBeMoved() const OVERRIDE { return true; }
2486 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002487 UNUSED(other);
2488 return true;
2489 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002490
Roland Levillain9240d6a2014-10-20 16:47:04 +01002491 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002492 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002493 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002494 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002495
2496 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002497 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2498 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2499 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2500 HLongConstant* y ATTRIBUTE_UNUSED) const {
2501 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2502 return nullptr;
2503 }
2504 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2505 HIntConstant* y ATTRIBUTE_UNUSED) const {
2506 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2507 return nullptr;
2508 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002509
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002510 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002511 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002512 HConstant* GetConstantRight() const;
2513
2514 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002515 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002516 HInstruction* GetLeastConstantLeft() const;
2517
Roland Levillainccc07a92014-09-16 14:48:16 +01002518 DECLARE_INSTRUCTION(BinaryOperation);
2519
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002520 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002521 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2522};
2523
Mark Mendellc4701932015-04-10 13:18:51 -04002524// The comparison bias applies for floating point operations and indicates how NaN
2525// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002526enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002527 kNoBias, // bias is not applicable (i.e. for long operation)
2528 kGtBias, // return 1 for NaN comparisons
2529 kLtBias, // return -1 for NaN comparisons
2530};
2531
Dave Allison20dfc792014-06-16 20:44:29 -07002532class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002533 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002534 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2535 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002536 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002537 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002538
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002539 bool NeedsMaterialization() const { return needs_materialization_; }
2540 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002541
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002542 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002543 // `instruction`, and disregard moves in between.
2544 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002545
Dave Allison20dfc792014-06-16 20:44:29 -07002546 DECLARE_INSTRUCTION(Condition);
2547
2548 virtual IfCondition GetCondition() const = 0;
2549
Mark Mendellc4701932015-04-10 13:18:51 -04002550 virtual IfCondition GetOppositeCondition() const = 0;
2551
Roland Levillain4fa13f62015-07-06 18:11:54 +01002552 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002553
2554 void SetBias(ComparisonBias bias) { bias_ = bias; }
2555
2556 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2557 return bias_ == other->AsCondition()->bias_;
2558 }
2559
Roland Levillain4fa13f62015-07-06 18:11:54 +01002560 bool IsFPConditionTrueIfNaN() const {
2561 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2562 IfCondition if_cond = GetCondition();
2563 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2564 }
2565
2566 bool IsFPConditionFalseIfNaN() const {
2567 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2568 IfCondition if_cond = GetCondition();
2569 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2570 }
2571
Dave Allison20dfc792014-06-16 20:44:29 -07002572 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002573 // For register allocation purposes, returns whether this instruction needs to be
2574 // materialized (that is, not just be in the processor flags).
2575 bool needs_materialization_;
2576
Mark Mendellc4701932015-04-10 13:18:51 -04002577 // Needed if we merge a HCompare into a HCondition.
2578 ComparisonBias bias_;
2579
Dave Allison20dfc792014-06-16 20:44:29 -07002580 DISALLOW_COPY_AND_ASSIGN(HCondition);
2581};
2582
2583// Instruction to check if two inputs are equal to each other.
2584class HEqual : public HCondition {
2585 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002586 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2587 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002588
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002589 bool IsCommutative() const OVERRIDE { return true; }
2590
Roland Levillain9867bc72015-08-05 10:21:34 +01002591 template <typename T> bool Compute(T x, T y) const { return x == y; }
2592
2593 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002594 return GetBlock()->GetGraph()->GetIntConstant(
2595 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002596 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002597 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002598 return GetBlock()->GetGraph()->GetIntConstant(
2599 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002600 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002601
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002602 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002603
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002604 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002605 return kCondEQ;
2606 }
2607
Mark Mendellc4701932015-04-10 13:18:51 -04002608 IfCondition GetOppositeCondition() const OVERRIDE {
2609 return kCondNE;
2610 }
2611
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002612 private:
2613 DISALLOW_COPY_AND_ASSIGN(HEqual);
2614};
2615
Dave Allison20dfc792014-06-16 20:44:29 -07002616class HNotEqual : public HCondition {
2617 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002618 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2619 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002620
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002621 bool IsCommutative() const OVERRIDE { return true; }
2622
Roland Levillain9867bc72015-08-05 10:21:34 +01002623 template <typename T> bool Compute(T x, T y) const { return x != y; }
2624
2625 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002626 return GetBlock()->GetGraph()->GetIntConstant(
2627 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002628 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002629 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002630 return GetBlock()->GetGraph()->GetIntConstant(
2631 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002632 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002633
Dave Allison20dfc792014-06-16 20:44:29 -07002634 DECLARE_INSTRUCTION(NotEqual);
2635
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002636 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002637 return kCondNE;
2638 }
2639
Mark Mendellc4701932015-04-10 13:18:51 -04002640 IfCondition GetOppositeCondition() const OVERRIDE {
2641 return kCondEQ;
2642 }
2643
Dave Allison20dfc792014-06-16 20:44:29 -07002644 private:
2645 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2646};
2647
2648class HLessThan : public HCondition {
2649 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002650 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2651 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002652
Roland Levillain9867bc72015-08-05 10:21:34 +01002653 template <typename T> bool Compute(T x, T y) const { return x < y; }
2654
2655 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002656 return GetBlock()->GetGraph()->GetIntConstant(
2657 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002658 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002659 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002660 return GetBlock()->GetGraph()->GetIntConstant(
2661 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002662 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002663
Dave Allison20dfc792014-06-16 20:44:29 -07002664 DECLARE_INSTRUCTION(LessThan);
2665
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002666 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002667 return kCondLT;
2668 }
2669
Mark Mendellc4701932015-04-10 13:18:51 -04002670 IfCondition GetOppositeCondition() const OVERRIDE {
2671 return kCondGE;
2672 }
2673
Dave Allison20dfc792014-06-16 20:44:29 -07002674 private:
2675 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2676};
2677
2678class HLessThanOrEqual : public HCondition {
2679 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002680 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2681 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002682
Roland Levillain9867bc72015-08-05 10:21:34 +01002683 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2684
2685 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002686 return GetBlock()->GetGraph()->GetIntConstant(
2687 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002688 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002689 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002690 return GetBlock()->GetGraph()->GetIntConstant(
2691 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002692 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002693
Dave Allison20dfc792014-06-16 20:44:29 -07002694 DECLARE_INSTRUCTION(LessThanOrEqual);
2695
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002696 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002697 return kCondLE;
2698 }
2699
Mark Mendellc4701932015-04-10 13:18:51 -04002700 IfCondition GetOppositeCondition() const OVERRIDE {
2701 return kCondGT;
2702 }
2703
Dave Allison20dfc792014-06-16 20:44:29 -07002704 private:
2705 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2706};
2707
2708class HGreaterThan : public HCondition {
2709 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002710 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2711 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002712
Roland Levillain9867bc72015-08-05 10:21:34 +01002713 template <typename T> bool Compute(T x, T y) const { return x > y; }
2714
2715 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002716 return GetBlock()->GetGraph()->GetIntConstant(
2717 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002718 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002720 return GetBlock()->GetGraph()->GetIntConstant(
2721 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002722 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002723
Dave Allison20dfc792014-06-16 20:44:29 -07002724 DECLARE_INSTRUCTION(GreaterThan);
2725
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002726 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002727 return kCondGT;
2728 }
2729
Mark Mendellc4701932015-04-10 13:18:51 -04002730 IfCondition GetOppositeCondition() const OVERRIDE {
2731 return kCondLE;
2732 }
2733
Dave Allison20dfc792014-06-16 20:44:29 -07002734 private:
2735 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2736};
2737
2738class HGreaterThanOrEqual : public HCondition {
2739 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002740 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2741 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002742
Roland Levillain9867bc72015-08-05 10:21:34 +01002743 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2744
2745 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002746 return GetBlock()->GetGraph()->GetIntConstant(
2747 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002748 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002749 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002750 return GetBlock()->GetGraph()->GetIntConstant(
2751 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002752 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002753
Dave Allison20dfc792014-06-16 20:44:29 -07002754 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2755
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002756 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002757 return kCondGE;
2758 }
2759
Mark Mendellc4701932015-04-10 13:18:51 -04002760 IfCondition GetOppositeCondition() const OVERRIDE {
2761 return kCondLT;
2762 }
2763
Dave Allison20dfc792014-06-16 20:44:29 -07002764 private:
2765 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2766};
2767
2768
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002769// Instruction to check how two inputs compare to each other.
2770// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2771class HCompare : public HBinaryOperation {
2772 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002773 HCompare(Primitive::Type type,
2774 HInstruction* first,
2775 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002776 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002777 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002778 : HBinaryOperation(Primitive::kPrimInt,
2779 first,
2780 second,
2781 SideEffectsForArchRuntimeCalls(type),
2782 dex_pc),
2783 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002784 DCHECK_EQ(type, first->GetType());
2785 DCHECK_EQ(type, second->GetType());
2786 }
2787
Roland Levillain9867bc72015-08-05 10:21:34 +01002788 template <typename T>
2789 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002790
Roland Levillain9867bc72015-08-05 10:21:34 +01002791 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002792 return GetBlock()->GetGraph()->GetIntConstant(
2793 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002794 }
2795 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002796 return GetBlock()->GetGraph()->GetIntConstant(
2797 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002798 }
2799
Calin Juravleddb7df22014-11-25 20:56:51 +00002800 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2801 return bias_ == other->AsCompare()->bias_;
2802 }
2803
Mark Mendellc4701932015-04-10 13:18:51 -04002804 ComparisonBias GetBias() const { return bias_; }
2805
Roland Levillain4fa13f62015-07-06 18:11:54 +01002806 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002807
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002808
2809 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2810 // MIPS64 uses a runtime call for FP comparisons.
2811 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2812 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002813
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002814 DECLARE_INSTRUCTION(Compare);
2815
2816 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002817 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002818
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002819 DISALLOW_COPY_AND_ASSIGN(HCompare);
2820};
2821
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002822// A local in the graph. Corresponds to a Dex register.
2823class HLocal : public HTemplateInstruction<0> {
2824 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002825 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002826 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002827
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002828 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002829
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002830 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002831
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002832 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002833 // The Dex register number.
2834 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002835
2836 DISALLOW_COPY_AND_ASSIGN(HLocal);
2837};
2838
2839// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002840class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002841 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002842 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2843 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002844 SetRawInputAt(0, local);
2845 }
2846
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002847 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2848
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002849 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002850
2851 private:
2852 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2853};
2854
2855// Store a value in a given local. This instruction has two inputs: the value
2856// and the local.
2857class HStoreLocal : public HTemplateInstruction<2> {
2858 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002859 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2860 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002861 SetRawInputAt(0, local);
2862 SetRawInputAt(1, value);
2863 }
2864
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002865 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2866
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002867 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002868
2869 private:
2870 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2871};
2872
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002873class HFloatConstant : public HConstant {
2874 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002875 float GetValue() const { return value_; }
2876
David Brazdil77a48ae2015-09-15 12:34:04 +00002877 uint64_t GetValueAsUint64() const OVERRIDE {
2878 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2879 }
2880
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002881 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002882 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002883 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002884 }
2885
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002886 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002887
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002888 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002889 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002890 }
2891 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002892 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002893 }
2894 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002895 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2896 }
2897 bool IsNaN() const {
2898 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002899 }
2900
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002901 DECLARE_INSTRUCTION(FloatConstant);
2902
2903 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002904 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2905 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2906 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2907 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002908
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002909 const float value_;
2910
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002911 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002912 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002913 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002914 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2915};
2916
2917class HDoubleConstant : public HConstant {
2918 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002919 double GetValue() const { return value_; }
2920
David Brazdil77a48ae2015-09-15 12:34:04 +00002921 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2922
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002923 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002924 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002925 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002926 }
2927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002928 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002929
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002930 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002931 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002932 }
2933 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002934 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002935 }
2936 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002937 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2938 }
2939 bool IsNaN() const {
2940 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002941 }
2942
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002943 DECLARE_INSTRUCTION(DoubleConstant);
2944
2945 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002946 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2947 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2948 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2949 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002950
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002951 const double value_;
2952
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002953 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002954 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002955 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002956 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2957};
2958
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002959enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002960#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002961#include "intrinsics_list.h"
2962 kNone,
2963 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2964#undef INTRINSICS_LIST
2965#undef OPTIMIZING_INTRINSICS
2966};
2967std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2968
Agi Csaki05f20562015-08-19 14:58:14 -07002969enum IntrinsicNeedsEnvironmentOrCache {
2970 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
2971 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07002972};
2973
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002974class HInvoke : public HInstruction {
2975 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002976 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002977
2978 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2979 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07002980 bool NeedsEnvironment() const OVERRIDE {
2981 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
2982 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002983
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002984 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002985 SetRawInputAt(index, argument);
2986 }
2987
Roland Levillain3e3d7332015-04-28 11:00:54 +01002988 // Return the number of arguments. This number can be lower than
2989 // the number of inputs returned by InputCount(), as some invoke
2990 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2991 // inputs at the end of their list of inputs.
2992 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2993
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002994 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002995
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002996
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002997 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002998 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002999
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003000 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3001
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003002 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003003 return intrinsic_;
3004 }
3005
Agi Csaki05f20562015-08-19 14:58:14 -07003006 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003007 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07003008 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003009 }
3010
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003011 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003012 return GetEnvironment()->GetParent() != nullptr;
3013 }
3014
3015 bool CanThrow() const OVERRIDE { return true; }
3016
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003017 DECLARE_INSTRUCTION(Invoke);
3018
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003019 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003020 HInvoke(ArenaAllocator* arena,
3021 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003022 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003023 Primitive::Type return_type,
3024 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003025 uint32_t dex_method_index,
3026 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003027 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003028 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003029 number_of_arguments_(number_of_arguments),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003030 inputs_(number_of_arguments + number_of_other_inputs, arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003031 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003032 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003033 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003034 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07003035 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003036 }
3037
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003038 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
3039 DCHECK_LT(index, InputCount());
3040 return inputs_[index];
3041 }
3042
David Brazdil1abb4192015-02-17 18:33:36 +00003043 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003044 DCHECK_LT(index, InputCount());
3045 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003046 }
3047
Roland Levillain3e3d7332015-04-28 11:00:54 +01003048 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003049 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003050 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003051 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003052 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003053 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003054 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003055
3056 private:
3057 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3058};
3059
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003060class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003061 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003062 // Requirements of this method call regarding the class
3063 // initialization (clinit) check of its declaring class.
3064 enum class ClinitCheckRequirement {
3065 kNone, // Class already initialized.
3066 kExplicit, // Static call having explicit clinit check as last input.
3067 kImplicit, // Static call implicitly requiring a clinit check.
3068 };
3069
Vladimir Marko58155012015-08-19 12:49:41 +00003070 // Determines how to load the target ArtMethod*.
3071 enum class MethodLoadKind {
3072 // Use a String init ArtMethod* loaded from Thread entrypoints.
3073 kStringInit,
3074
3075 // Use the method's own ArtMethod* loaded by the register allocator.
3076 kRecursive,
3077
3078 // Use ArtMethod* at a known address, embed the direct address in the code.
3079 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3080 kDirectAddress,
3081
3082 // Use ArtMethod* at an address that will be known at link time, embed the direct
3083 // address in the code. If the image is relocatable, emit .patch_oat entry.
3084 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3085 // the image relocatable or not.
3086 kDirectAddressWithFixup,
3087
3088 // Load from resoved methods array in the dex cache using a PC-relative load.
3089 // Used when we need to use the dex cache, for example for invoke-static that
3090 // may cause class initialization (the entry may point to a resolution method),
3091 // and we know that we can access the dex cache arrays using a PC-relative load.
3092 kDexCachePcRelative,
3093
3094 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3095 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3096 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3097 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3098 kDexCacheViaMethod,
3099 };
3100
3101 // Determines the location of the code pointer.
3102 enum class CodePtrLocation {
3103 // Recursive call, use local PC-relative call instruction.
3104 kCallSelf,
3105
3106 // Use PC-relative call instruction patched at link time.
3107 // Used for calls within an oat file, boot->boot or app->app.
3108 kCallPCRelative,
3109
3110 // Call to a known target address, embed the direct address in code.
3111 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3112 kCallDirect,
3113
3114 // Call to a target address that will be known at link time, embed the direct
3115 // address in code. If the image is relocatable, emit .patch_oat entry.
3116 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3117 // the image relocatable or not.
3118 kCallDirectWithFixup,
3119
3120 // Use code pointer from the ArtMethod*.
3121 // Used when we don't know the target code. This is also the last-resort-kind used when
3122 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3123 kCallArtMethod,
3124 };
3125
3126 struct DispatchInfo {
3127 const MethodLoadKind method_load_kind;
3128 const CodePtrLocation code_ptr_location;
3129 // The method load data holds
3130 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3131 // Note that there are multiple string init methods, each having its own offset.
3132 // - the method address for kDirectAddress
3133 // - the dex cache arrays offset for kDexCachePcRel.
3134 const uint64_t method_load_data;
3135 const uint64_t direct_code_ptr;
3136 };
3137
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003138 HInvokeStaticOrDirect(ArenaAllocator* arena,
3139 uint32_t number_of_arguments,
3140 Primitive::Type return_type,
3141 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003142 uint32_t method_index,
3143 MethodReference target_method,
3144 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003145 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003146 InvokeType invoke_type,
3147 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003148 : HInvoke(arena,
3149 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003150 // There is one extra argument for the HCurrentMethod node, and
3151 // potentially one other if the clinit check is explicit, and one other
3152 // if the method is a string factory.
3153 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003154 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003155 return_type,
3156 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003157 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003158 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003159 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003160 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003161 target_method_(target_method),
3162 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003163
Calin Juravle641547a2015-04-21 22:08:51 +01003164 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3165 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003166 // We access the method via the dex cache so we can't do an implicit null check.
3167 // TODO: for intrinsics we can generate implicit null checks.
3168 return false;
3169 }
3170
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003171 bool CanBeNull() const OVERRIDE {
3172 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3173 }
3174
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003175 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003176 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3177 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3178 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003179 bool NeedsDexCache() const OVERRIDE {
3180 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3181 return !IsRecursive() && !IsStringInit();
3182 }
Vladimir Marko58155012015-08-19 12:49:41 +00003183 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003184 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003185 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3186 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3187 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3188 MethodReference GetTargetMethod() const { return target_method_; }
3189
3190 int32_t GetStringInitOffset() const {
3191 DCHECK(IsStringInit());
3192 return dispatch_info_.method_load_data;
3193 }
3194
3195 uint64_t GetMethodAddress() const {
3196 DCHECK(HasMethodAddress());
3197 return dispatch_info_.method_load_data;
3198 }
3199
3200 uint32_t GetDexCacheArrayOffset() const {
3201 DCHECK(HasPcRelDexCache());
3202 return dispatch_info_.method_load_data;
3203 }
3204
3205 uint64_t GetDirectCodePtr() const {
3206 DCHECK(HasDirectCodePtr());
3207 return dispatch_info_.direct_code_ptr;
3208 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003209
Calin Juravle68ad6492015-08-18 17:08:12 +01003210 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3211
Roland Levillain4c0eb422015-04-24 16:43:49 +01003212 // Is this instruction a call to a static method?
3213 bool IsStatic() const {
3214 return GetInvokeType() == kStatic;
3215 }
3216
Roland Levillain3e3d7332015-04-28 11:00:54 +01003217 // Remove the art::HLoadClass instruction set as last input by
3218 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3219 // the initial art::HClinitCheck instruction (only relevant for
3220 // static calls with explicit clinit check).
3221 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003222 DCHECK(IsStaticWithExplicitClinitCheck());
3223 size_t last_input_index = InputCount() - 1;
3224 HInstruction* last_input = InputAt(last_input_index);
3225 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003226 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003227 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003228 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003229 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3230 DCHECK(IsStaticWithImplicitClinitCheck());
3231 }
3232
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003233 bool IsStringFactoryFor(HFakeString* str) const {
3234 if (!IsStringInit()) return false;
3235 // +1 for the current method.
3236 if (InputCount() == (number_of_arguments_ + 1)) return false;
3237 return InputAt(InputCount() - 1)->AsFakeString() == str;
3238 }
3239
3240 void RemoveFakeStringArgumentAsLastInput() {
3241 DCHECK(IsStringInit());
3242 size_t last_input_index = InputCount() - 1;
3243 HInstruction* last_input = InputAt(last_input_index);
3244 DCHECK(last_input != nullptr);
3245 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3246 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003247 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003248 }
3249
Roland Levillain4c0eb422015-04-24 16:43:49 +01003250 // Is this a call to a static method whose declaring class has an
3251 // explicit intialization check in the graph?
3252 bool IsStaticWithExplicitClinitCheck() const {
3253 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3254 }
3255
3256 // Is this a call to a static method whose declaring class has an
3257 // implicit intialization check requirement?
3258 bool IsStaticWithImplicitClinitCheck() const {
3259 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3260 }
3261
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003262 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003263
Roland Levillain4c0eb422015-04-24 16:43:49 +01003264 protected:
3265 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3266 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3267 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3268 HInstruction* input = input_record.GetInstruction();
3269 // `input` is the last input of a static invoke marked as having
3270 // an explicit clinit check. It must either be:
3271 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3272 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3273 DCHECK(input != nullptr);
3274 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3275 }
3276 return input_record;
3277 }
3278
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003279 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003280 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003281 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003282 // The target method may refer to different dex file or method index than the original
3283 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3284 // in derived class to increase visibility.
3285 MethodReference target_method_;
3286 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003287
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003288 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003289};
3290
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003291class HInvokeVirtual : public HInvoke {
3292 public:
3293 HInvokeVirtual(ArenaAllocator* arena,
3294 uint32_t number_of_arguments,
3295 Primitive::Type return_type,
3296 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003297 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003298 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003299 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003300 vtable_index_(vtable_index) {}
3301
Calin Juravle641547a2015-04-21 22:08:51 +01003302 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003303 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003304 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003305 }
3306
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003307 uint32_t GetVTableIndex() const { return vtable_index_; }
3308
3309 DECLARE_INSTRUCTION(InvokeVirtual);
3310
3311 private:
3312 const uint32_t vtable_index_;
3313
3314 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3315};
3316
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003317class HInvokeInterface : public HInvoke {
3318 public:
3319 HInvokeInterface(ArenaAllocator* arena,
3320 uint32_t number_of_arguments,
3321 Primitive::Type return_type,
3322 uint32_t dex_pc,
3323 uint32_t dex_method_index,
3324 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003325 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003326 imt_index_(imt_index) {}
3327
Calin Juravle641547a2015-04-21 22:08:51 +01003328 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003329 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003330 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003331 }
3332
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003333 uint32_t GetImtIndex() const { return imt_index_; }
3334 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3335
3336 DECLARE_INSTRUCTION(InvokeInterface);
3337
3338 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003339 const uint32_t imt_index_;
3340
3341 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3342};
3343
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003344class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003345 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003346 HNewInstance(HCurrentMethod* current_method,
3347 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003348 uint16_t type_index,
3349 const DexFile& dex_file,
3350 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003351 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003352 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003353 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003354 entrypoint_(entrypoint) {
3355 SetRawInputAt(0, current_method);
3356 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003357
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003358 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003359 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003360
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003361 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003362 bool NeedsEnvironment() const OVERRIDE { return true; }
3363 // It may throw when called on:
3364 // - interfaces
3365 // - abstract/innaccessible/unknown classes
3366 // TODO: optimize when possible.
3367 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003368
Calin Juravle10e244f2015-01-26 18:54:32 +00003369 bool CanBeNull() const OVERRIDE { return false; }
3370
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003371 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3372
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003373 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003374
3375 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003376 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003377 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003378 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003379
3380 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3381};
3382
Roland Levillain88cb1752014-10-20 16:36:47 +01003383class HNeg : public HUnaryOperation {
3384 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003385 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3386 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003387
Roland Levillain9867bc72015-08-05 10:21:34 +01003388 template <typename T> T Compute(T x) const { return -x; }
3389
3390 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003391 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003392 }
3393 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003394 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003395 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003396
Roland Levillain88cb1752014-10-20 16:36:47 +01003397 DECLARE_INSTRUCTION(Neg);
3398
3399 private:
3400 DISALLOW_COPY_AND_ASSIGN(HNeg);
3401};
3402
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003403class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003404 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003405 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003406 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003407 uint32_t dex_pc,
3408 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003409 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003410 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003411 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003412 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003413 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003414 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003415 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003416 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003417 }
3418
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003419 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003420 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003421
3422 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003423 bool NeedsEnvironment() const OVERRIDE { return true; }
3424
Mingyao Yang0c365e62015-03-31 15:09:29 -07003425 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3426 bool CanThrow() const OVERRIDE { return true; }
3427
Calin Juravle10e244f2015-01-26 18:54:32 +00003428 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003429
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003430 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3431
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003432 DECLARE_INSTRUCTION(NewArray);
3433
3434 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003435 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003436 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003437 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003438
3439 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3440};
3441
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003442class HAdd : public HBinaryOperation {
3443 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003444 HAdd(Primitive::Type result_type,
3445 HInstruction* left,
3446 HInstruction* right,
3447 uint32_t dex_pc = kNoDexPc)
3448 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003449
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003450 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003451
Roland Levillain9867bc72015-08-05 10:21:34 +01003452 template <typename T> T Compute(T x, T y) const { return x + y; }
3453
3454 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003455 return GetBlock()->GetGraph()->GetIntConstant(
3456 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003457 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003458 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003459 return GetBlock()->GetGraph()->GetLongConstant(
3460 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003461 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003462
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003463 DECLARE_INSTRUCTION(Add);
3464
3465 private:
3466 DISALLOW_COPY_AND_ASSIGN(HAdd);
3467};
3468
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003469class HSub : public HBinaryOperation {
3470 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003471 HSub(Primitive::Type result_type,
3472 HInstruction* left,
3473 HInstruction* right,
3474 uint32_t dex_pc = kNoDexPc)
3475 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003476
Roland Levillain9867bc72015-08-05 10:21:34 +01003477 template <typename T> T Compute(T x, T y) const { return x - y; }
3478
3479 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003480 return GetBlock()->GetGraph()->GetIntConstant(
3481 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003482 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003483 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003484 return GetBlock()->GetGraph()->GetLongConstant(
3485 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003486 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003487
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003488 DECLARE_INSTRUCTION(Sub);
3489
3490 private:
3491 DISALLOW_COPY_AND_ASSIGN(HSub);
3492};
3493
Calin Juravle34bacdf2014-10-07 20:23:36 +01003494class HMul : public HBinaryOperation {
3495 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003496 HMul(Primitive::Type result_type,
3497 HInstruction* left,
3498 HInstruction* right,
3499 uint32_t dex_pc = kNoDexPc)
3500 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003501
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003502 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003503
Roland Levillain9867bc72015-08-05 10:21:34 +01003504 template <typename T> T Compute(T x, T y) const { return x * y; }
3505
3506 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003507 return GetBlock()->GetGraph()->GetIntConstant(
3508 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003509 }
3510 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003511 return GetBlock()->GetGraph()->GetLongConstant(
3512 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003513 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003514
3515 DECLARE_INSTRUCTION(Mul);
3516
3517 private:
3518 DISALLOW_COPY_AND_ASSIGN(HMul);
3519};
3520
Calin Juravle7c4954d2014-10-28 16:57:40 +00003521class HDiv : public HBinaryOperation {
3522 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003523 HDiv(Primitive::Type result_type,
3524 HInstruction* left,
3525 HInstruction* right,
3526 uint32_t dex_pc)
3527 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003528
Roland Levillain9867bc72015-08-05 10:21:34 +01003529 template <typename T>
3530 T Compute(T x, T y) const {
3531 // Our graph structure ensures we never have 0 for `y` during
3532 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003533 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003534 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003535 return (y == -1) ? -x : x / y;
3536 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003537
Roland Levillain9867bc72015-08-05 10:21:34 +01003538 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 Levillain9867bc72015-08-05 10:21:34 +01003541 }
3542 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());
Calin Juravlebacfec32014-11-14 15:54:36 +00003545 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003546
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003547 static SideEffects SideEffectsForArchRuntimeCalls() {
3548 // The generated code can use a runtime call.
3549 return SideEffects::CanTriggerGC();
3550 }
3551
Calin Juravle7c4954d2014-10-28 16:57:40 +00003552 DECLARE_INSTRUCTION(Div);
3553
3554 private:
3555 DISALLOW_COPY_AND_ASSIGN(HDiv);
3556};
3557
Calin Juravlebacfec32014-11-14 15:54:36 +00003558class HRem : public HBinaryOperation {
3559 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003560 HRem(Primitive::Type result_type,
3561 HInstruction* left,
3562 HInstruction* right,
3563 uint32_t dex_pc)
3564 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003565
Roland Levillain9867bc72015-08-05 10:21:34 +01003566 template <typename T>
3567 T Compute(T x, T y) const {
3568 // Our graph structure ensures we never have 0 for `y` during
3569 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003570 DCHECK_NE(y, 0);
3571 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3572 return (y == -1) ? 0 : x % y;
3573 }
3574
Roland Levillain9867bc72015-08-05 10:21:34 +01003575 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003576 return GetBlock()->GetGraph()->GetIntConstant(
3577 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003578 }
3579 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003580 return GetBlock()->GetGraph()->GetLongConstant(
3581 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003582 }
3583
Calin Juravlebacfec32014-11-14 15:54:36 +00003584
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003585 static SideEffects SideEffectsForArchRuntimeCalls() {
3586 return SideEffects::CanTriggerGC();
3587 }
3588
Calin Juravlebacfec32014-11-14 15:54:36 +00003589 DECLARE_INSTRUCTION(Rem);
3590
3591 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003592 DISALLOW_COPY_AND_ASSIGN(HRem);
3593};
3594
Calin Juravled0d48522014-11-04 16:40:20 +00003595class HDivZeroCheck : public HExpression<1> {
3596 public:
3597 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003598 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003599 SetRawInputAt(0, value);
3600 }
3601
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003602 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3603
Calin Juravled0d48522014-11-04 16:40:20 +00003604 bool CanBeMoved() const OVERRIDE { return true; }
3605
3606 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3607 UNUSED(other);
3608 return true;
3609 }
3610
3611 bool NeedsEnvironment() const OVERRIDE { return true; }
3612 bool CanThrow() const OVERRIDE { return true; }
3613
Calin Juravled0d48522014-11-04 16:40:20 +00003614 DECLARE_INSTRUCTION(DivZeroCheck);
3615
3616 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003617 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3618};
3619
Calin Juravle9aec02f2014-11-18 23:06:35 +00003620class HShl : public HBinaryOperation {
3621 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003622 HShl(Primitive::Type result_type,
3623 HInstruction* left,
3624 HInstruction* right,
3625 uint32_t dex_pc = kNoDexPc)
3626 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003627
Roland Levillain9867bc72015-08-05 10:21:34 +01003628 template <typename T, typename U, typename V>
3629 T Compute(T x, U y, V max_shift_value) const {
3630 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3631 "V is not the unsigned integer type corresponding to T");
3632 return x << (y & max_shift_value);
3633 }
3634
3635 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3636 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003637 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003638 }
3639 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3640 // case is handled as `x << static_cast<int>(y)`.
3641 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3642 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003643 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003644 }
3645 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3646 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003647 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003648 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003649
3650 DECLARE_INSTRUCTION(Shl);
3651
3652 private:
3653 DISALLOW_COPY_AND_ASSIGN(HShl);
3654};
3655
3656class HShr : public HBinaryOperation {
3657 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003658 HShr(Primitive::Type result_type,
3659 HInstruction* left,
3660 HInstruction* right,
3661 uint32_t dex_pc = kNoDexPc)
3662 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003663
Roland Levillain9867bc72015-08-05 10:21:34 +01003664 template <typename T, typename U, typename V>
3665 T Compute(T x, U y, V max_shift_value) const {
3666 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3667 "V is not the unsigned integer type corresponding to T");
3668 return x >> (y & max_shift_value);
3669 }
3670
3671 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3672 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003673 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003674 }
3675 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3676 // case is handled as `x >> static_cast<int>(y)`.
3677 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3678 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003679 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003680 }
3681 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3682 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003683 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003684 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003685
3686 DECLARE_INSTRUCTION(Shr);
3687
3688 private:
3689 DISALLOW_COPY_AND_ASSIGN(HShr);
3690};
3691
3692class HUShr : public HBinaryOperation {
3693 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003694 HUShr(Primitive::Type result_type,
3695 HInstruction* left,
3696 HInstruction* right,
3697 uint32_t dex_pc = kNoDexPc)
3698 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003699
Roland Levillain9867bc72015-08-05 10:21:34 +01003700 template <typename T, typename U, typename V>
3701 T Compute(T x, U y, V max_shift_value) const {
3702 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3703 "V is not the unsigned integer type corresponding to T");
3704 V ux = static_cast<V>(x);
3705 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003706 }
3707
Roland Levillain9867bc72015-08-05 10:21:34 +01003708 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3709 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003710 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003711 }
3712 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3713 // case is handled as `x >>> static_cast<int>(y)`.
3714 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3715 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003716 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003717 }
3718 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3719 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003720 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003721 }
3722
3723 DECLARE_INSTRUCTION(UShr);
3724
3725 private:
3726 DISALLOW_COPY_AND_ASSIGN(HUShr);
3727};
3728
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003729class HAnd : public HBinaryOperation {
3730 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003731 HAnd(Primitive::Type result_type,
3732 HInstruction* left,
3733 HInstruction* right,
3734 uint32_t dex_pc = kNoDexPc)
3735 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003736
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003737 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003738
Roland Levillain9867bc72015-08-05 10:21:34 +01003739 template <typename T, typename U>
3740 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3741
3742 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003743 return GetBlock()->GetGraph()->GetIntConstant(
3744 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003745 }
3746 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003747 return GetBlock()->GetGraph()->GetLongConstant(
3748 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003749 }
3750 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003751 return GetBlock()->GetGraph()->GetLongConstant(
3752 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003753 }
3754 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003755 return GetBlock()->GetGraph()->GetLongConstant(
3756 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003757 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003758
3759 DECLARE_INSTRUCTION(And);
3760
3761 private:
3762 DISALLOW_COPY_AND_ASSIGN(HAnd);
3763};
3764
3765class HOr : public HBinaryOperation {
3766 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003767 HOr(Primitive::Type result_type,
3768 HInstruction* left,
3769 HInstruction* right,
3770 uint32_t dex_pc = kNoDexPc)
3771 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003772
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003773 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003774
Roland Levillain9867bc72015-08-05 10:21:34 +01003775 template <typename T, typename U>
3776 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3777
3778 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003779 return GetBlock()->GetGraph()->GetIntConstant(
3780 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003781 }
3782 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003783 return GetBlock()->GetGraph()->GetLongConstant(
3784 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003785 }
3786 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003787 return GetBlock()->GetGraph()->GetLongConstant(
3788 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003789 }
3790 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003791 return GetBlock()->GetGraph()->GetLongConstant(
3792 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003793 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003794
3795 DECLARE_INSTRUCTION(Or);
3796
3797 private:
3798 DISALLOW_COPY_AND_ASSIGN(HOr);
3799};
3800
3801class HXor : public HBinaryOperation {
3802 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003803 HXor(Primitive::Type result_type,
3804 HInstruction* left,
3805 HInstruction* right,
3806 uint32_t dex_pc = kNoDexPc)
3807 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003808
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003809 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003810
Roland Levillain9867bc72015-08-05 10:21:34 +01003811 template <typename T, typename U>
3812 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3813
3814 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003815 return GetBlock()->GetGraph()->GetIntConstant(
3816 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003817 }
3818 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003819 return GetBlock()->GetGraph()->GetLongConstant(
3820 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003821 }
3822 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003823 return GetBlock()->GetGraph()->GetLongConstant(
3824 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003825 }
3826 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003827 return GetBlock()->GetGraph()->GetLongConstant(
3828 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003829 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003830
3831 DECLARE_INSTRUCTION(Xor);
3832
3833 private:
3834 DISALLOW_COPY_AND_ASSIGN(HXor);
3835};
3836
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003837// The value of a parameter in this method. Its location depends on
3838// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003839class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003840 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003841 HParameterValue(uint8_t index,
3842 Primitive::Type parameter_type,
3843 bool is_this = false)
3844 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003845 index_(index),
3846 is_this_(is_this),
3847 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003848
3849 uint8_t GetIndex() const { return index_; }
3850
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003851 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3852 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003853
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003854 bool IsThis() const { return is_this_; }
3855
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003856 DECLARE_INSTRUCTION(ParameterValue);
3857
3858 private:
3859 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003860 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003861 const uint8_t index_;
3862
Calin Juravle10e244f2015-01-26 18:54:32 +00003863 // Whether or not the parameter value corresponds to 'this' argument.
3864 const bool is_this_;
3865
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003866 bool can_be_null_;
3867
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003868 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3869};
3870
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003871class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003872 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003873 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3874 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003876 bool CanBeMoved() const OVERRIDE { return true; }
3877 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003878 UNUSED(other);
3879 return true;
3880 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003881
Roland Levillain9867bc72015-08-05 10:21:34 +01003882 template <typename T> T Compute(T x) const { return ~x; }
3883
3884 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003885 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003886 }
3887 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003888 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003889 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003890
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003891 DECLARE_INSTRUCTION(Not);
3892
3893 private:
3894 DISALLOW_COPY_AND_ASSIGN(HNot);
3895};
3896
David Brazdil66d126e2015-04-03 16:02:44 +01003897class HBooleanNot : public HUnaryOperation {
3898 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003899 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3900 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003901
3902 bool CanBeMoved() const OVERRIDE { return true; }
3903 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3904 UNUSED(other);
3905 return true;
3906 }
3907
Roland Levillain9867bc72015-08-05 10:21:34 +01003908 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003909 DCHECK(IsUint<1>(x));
3910 return !x;
3911 }
3912
Roland Levillain9867bc72015-08-05 10:21:34 +01003913 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003914 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003915 }
3916 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3917 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003918 UNREACHABLE();
3919 }
3920
3921 DECLARE_INSTRUCTION(BooleanNot);
3922
3923 private:
3924 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3925};
3926
Roland Levillaindff1f282014-11-05 14:15:05 +00003927class HTypeConversion : public HExpression<1> {
3928 public:
3929 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003930 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003931 : HExpression(result_type,
3932 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
3933 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003934 SetRawInputAt(0, input);
3935 DCHECK_NE(input->GetType(), result_type);
3936 }
3937
3938 HInstruction* GetInput() const { return InputAt(0); }
3939 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3940 Primitive::Type GetResultType() const { return GetType(); }
3941
Roland Levillain624279f2014-12-04 11:54:28 +00003942 // Required by the x86 and ARM code generators when producing calls
3943 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003944
Roland Levillaindff1f282014-11-05 14:15:05 +00003945 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003946 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003947
Mark Mendelle82549b2015-05-06 10:55:34 -04003948 // Try to statically evaluate the conversion and return a HConstant
3949 // containing the result. If the input cannot be converted, return nullptr.
3950 HConstant* TryStaticEvaluation() const;
3951
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003952 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3953 Primitive::Type result_type) {
3954 // Some architectures may not require the 'GC' side effects, but at this point
3955 // in the compilation process we do not know what architecture we will
3956 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003957 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3958 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003959 return SideEffects::CanTriggerGC();
3960 }
3961 return SideEffects::None();
3962 }
3963
Roland Levillaindff1f282014-11-05 14:15:05 +00003964 DECLARE_INSTRUCTION(TypeConversion);
3965
3966 private:
3967 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3968};
3969
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003970static constexpr uint32_t kNoRegNumber = -1;
3971
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003972class HPhi : public HInstruction {
3973 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003974 HPhi(ArenaAllocator* arena,
3975 uint32_t reg_number,
3976 size_t number_of_inputs,
3977 Primitive::Type type,
3978 uint32_t dex_pc = kNoDexPc)
3979 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003980 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003981 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003982 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003983 is_live_(false),
3984 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003985 }
3986
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003987 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3988 static Primitive::Type ToPhiType(Primitive::Type type) {
3989 switch (type) {
3990 case Primitive::kPrimBoolean:
3991 case Primitive::kPrimByte:
3992 case Primitive::kPrimShort:
3993 case Primitive::kPrimChar:
3994 return Primitive::kPrimInt;
3995 default:
3996 return type;
3997 }
3998 }
3999
David Brazdilffee3d32015-07-06 11:48:53 +01004000 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4001
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004002 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004003
4004 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004005 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004006
Calin Juravle10e244f2015-01-26 18:54:32 +00004007 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004008 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004009
Calin Juravle10e244f2015-01-26 18:54:32 +00004010 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4011 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4012
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004013 uint32_t GetRegNumber() const { return reg_number_; }
4014
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004015 void SetDead() { is_live_ = false; }
4016 void SetLive() { is_live_ = true; }
4017 bool IsDead() const { return !is_live_; }
4018 bool IsLive() const { return is_live_; }
4019
David Brazdil77a48ae2015-09-15 12:34:04 +00004020 bool IsVRegEquivalentOf(HInstruction* other) const {
4021 return other != nullptr
4022 && other->IsPhi()
4023 && other->AsPhi()->GetBlock() == GetBlock()
4024 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4025 }
4026
Calin Juravlea4f88312015-04-16 12:57:19 +01004027 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4028 // An equivalent phi is a phi having the same dex register and type.
4029 // It assumes that phis with the same dex register are adjacent.
4030 HPhi* GetNextEquivalentPhiWithSameType() {
4031 HInstruction* next = GetNext();
4032 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4033 if (next->GetType() == GetType()) {
4034 return next->AsPhi();
4035 }
4036 next = next->GetNext();
4037 }
4038 return nullptr;
4039 }
4040
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004041 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004042
David Brazdil1abb4192015-02-17 18:33:36 +00004043 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004044 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
4045 DCHECK_LE(index, InputCount());
4046 return inputs_[index];
4047 }
David Brazdil1abb4192015-02-17 18:33:36 +00004048
4049 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004050 DCHECK_LE(index, InputCount());
4051 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004052 }
4053
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004054 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004055 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004056 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004057 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004058 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004059 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004060
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004061 DISALLOW_COPY_AND_ASSIGN(HPhi);
4062};
4063
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004064class HNullCheck : public HExpression<1> {
4065 public:
4066 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004067 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004068 SetRawInputAt(0, value);
4069 }
4070
Calin Juravle10e244f2015-01-26 18:54:32 +00004071 bool CanBeMoved() const OVERRIDE { return true; }
4072 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004073 UNUSED(other);
4074 return true;
4075 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004076
Calin Juravle10e244f2015-01-26 18:54:32 +00004077 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004078
Calin Juravle10e244f2015-01-26 18:54:32 +00004079 bool CanThrow() const OVERRIDE { return true; }
4080
4081 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004082
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004083
4084 DECLARE_INSTRUCTION(NullCheck);
4085
4086 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004087 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4088};
4089
4090class FieldInfo : public ValueObject {
4091 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004092 FieldInfo(MemberOffset field_offset,
4093 Primitive::Type field_type,
4094 bool is_volatile,
4095 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004096 const DexFile& dex_file,
4097 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004098 : field_offset_(field_offset),
4099 field_type_(field_type),
4100 is_volatile_(is_volatile),
4101 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004102 dex_file_(dex_file),
4103 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004104
4105 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004106 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004107 uint32_t GetFieldIndex() const { return index_; }
4108 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004109 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004110 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004111
4112 private:
4113 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004114 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004115 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004116 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004117 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004118 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004119};
4120
4121class HInstanceFieldGet : public HExpression<1> {
4122 public:
4123 HInstanceFieldGet(HInstruction* value,
4124 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004125 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004126 bool is_volatile,
4127 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004128 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004129 Handle<mirror::DexCache> dex_cache,
4130 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004131 : HExpression(
4132 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004133 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004134 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004135 SetRawInputAt(0, value);
4136 }
4137
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004138 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004139
4140 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4141 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4142 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004143 }
4144
Calin Juravle641547a2015-04-21 22:08:51 +01004145 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4146 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004147 }
4148
4149 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004150 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4151 }
4152
Calin Juravle52c48962014-12-16 17:02:57 +00004153 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004154 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004155 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004156 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004157
4158 DECLARE_INSTRUCTION(InstanceFieldGet);
4159
4160 private:
4161 const FieldInfo field_info_;
4162
4163 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4164};
4165
4166class HInstanceFieldSet : public HTemplateInstruction<2> {
4167 public:
4168 HInstanceFieldSet(HInstruction* object,
4169 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004170 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004171 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004172 bool is_volatile,
4173 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004174 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004175 Handle<mirror::DexCache> dex_cache,
4176 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004177 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004178 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004179 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004180 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004181 SetRawInputAt(0, object);
4182 SetRawInputAt(1, value);
4183 }
4184
Calin Juravle641547a2015-04-21 22:08:51 +01004185 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4186 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004187 }
4188
Calin Juravle52c48962014-12-16 17:02:57 +00004189 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004190 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004191 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004192 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004193 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004194 bool GetValueCanBeNull() const { return value_can_be_null_; }
4195 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004196
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004197 DECLARE_INSTRUCTION(InstanceFieldSet);
4198
4199 private:
4200 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004201 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004202
4203 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4204};
4205
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004206class HArrayGet : public HExpression<2> {
4207 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004208 HArrayGet(HInstruction* array,
4209 HInstruction* index,
4210 Primitive::Type type,
4211 uint32_t dex_pc = kNoDexPc)
4212 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004213 SetRawInputAt(0, array);
4214 SetRawInputAt(1, index);
4215 }
4216
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004217 bool CanBeMoved() const OVERRIDE { return true; }
4218 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004219 UNUSED(other);
4220 return true;
4221 }
Calin Juravle641547a2015-04-21 22:08:51 +01004222 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4223 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004224 // TODO: We can be smarter here.
4225 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4226 // which generates the implicit null check. There are cases when these can be removed
4227 // to produce better code. If we ever add optimizations to do so we should allow an
4228 // implicit check here (as long as the address falls in the first page).
4229 return false;
4230 }
4231
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004232 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004233
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004234 HInstruction* GetArray() const { return InputAt(0); }
4235 HInstruction* GetIndex() const { return InputAt(1); }
4236
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004237 DECLARE_INSTRUCTION(ArrayGet);
4238
4239 private:
4240 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4241};
4242
4243class HArraySet : public HTemplateInstruction<3> {
4244 public:
4245 HArraySet(HInstruction* array,
4246 HInstruction* index,
4247 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004248 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004249 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004250 : HTemplateInstruction(
4251 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004252 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004253 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004254 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4255 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004256 SetRawInputAt(0, array);
4257 SetRawInputAt(1, index);
4258 SetRawInputAt(2, value);
4259 }
4260
Calin Juravle77520bc2015-01-12 18:45:46 +00004261 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004262 // We currently always call a runtime method to catch array store
4263 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004264 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004265 }
4266
Mingyao Yang81014cb2015-06-02 03:16:27 -07004267 // Can throw ArrayStoreException.
4268 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4269
Calin Juravle641547a2015-04-21 22:08:51 +01004270 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4271 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004272 // TODO: Same as for ArrayGet.
4273 return false;
4274 }
4275
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004276 void ClearNeedsTypeCheck() {
4277 needs_type_check_ = false;
4278 }
4279
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004280 void ClearValueCanBeNull() {
4281 value_can_be_null_ = false;
4282 }
4283
4284 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004285 bool NeedsTypeCheck() const { return needs_type_check_; }
4286
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004287 HInstruction* GetArray() const { return InputAt(0); }
4288 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004289 HInstruction* GetValue() const { return InputAt(2); }
4290
4291 Primitive::Type GetComponentType() const {
4292 // The Dex format does not type floating point index operations. Since the
4293 // `expected_component_type_` is set during building and can therefore not
4294 // be correct, we also check what is the value type. If it is a floating
4295 // point type, we must use that type.
4296 Primitive::Type value_type = GetValue()->GetType();
4297 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4298 ? value_type
4299 : expected_component_type_;
4300 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004301
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004302 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4303 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4304 }
4305
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004306 DECLARE_INSTRUCTION(ArraySet);
4307
4308 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004309 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004310 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004311 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004312
4313 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4314};
4315
4316class HArrayLength : public HExpression<1> {
4317 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004318 explicit HArrayLength(HInstruction* array, uint32_t dex_pc = kNoDexPc)
4319 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004320 // Note that arrays do not change length, so the instruction does not
4321 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004322 SetRawInputAt(0, array);
4323 }
4324
Calin Juravle77520bc2015-01-12 18:45:46 +00004325 bool CanBeMoved() const OVERRIDE { return true; }
4326 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004327 UNUSED(other);
4328 return true;
4329 }
Calin Juravle641547a2015-04-21 22:08:51 +01004330 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4331 return obj == InputAt(0);
4332 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004333
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004334 DECLARE_INSTRUCTION(ArrayLength);
4335
4336 private:
4337 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4338};
4339
4340class HBoundsCheck : public HExpression<2> {
4341 public:
4342 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004343 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004344 DCHECK(index->GetType() == Primitive::kPrimInt);
4345 SetRawInputAt(0, index);
4346 SetRawInputAt(1, length);
4347 }
4348
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004349 bool CanBeMoved() const OVERRIDE { return true; }
4350 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004351 UNUSED(other);
4352 return true;
4353 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004354
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004355 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004356
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004357 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004358
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004359
4360 DECLARE_INSTRUCTION(BoundsCheck);
4361
4362 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004363 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4364};
4365
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004366/**
4367 * Some DEX instructions are folded into multiple HInstructions that need
4368 * to stay live until the last HInstruction. This class
4369 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004370 * HInstruction stays live. `index` represents the stack location index of the
4371 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004372 */
4373class HTemporary : public HTemplateInstruction<0> {
4374 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004375 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4376 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004377
4378 size_t GetIndex() const { return index_; }
4379
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004380 Primitive::Type GetType() const OVERRIDE {
4381 // The previous instruction is the one that will be stored in the temporary location.
4382 DCHECK(GetPrevious() != nullptr);
4383 return GetPrevious()->GetType();
4384 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004385
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004386 DECLARE_INSTRUCTION(Temporary);
4387
4388 private:
4389 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004390 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4391};
4392
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004393class HSuspendCheck : public HTemplateInstruction<0> {
4394 public:
4395 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004396 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004397
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004398 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004399 return true;
4400 }
4401
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004402 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4403 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004404
4405 DECLARE_INSTRUCTION(SuspendCheck);
4406
4407 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004408 // Only used for code generation, in order to share the same slow path between back edges
4409 // of a same loop.
4410 SlowPathCode* slow_path_;
4411
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004412 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4413};
4414
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004415/**
4416 * Instruction to load a Class object.
4417 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004418class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004419 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004420 HLoadClass(HCurrentMethod* current_method,
4421 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004422 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004423 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004424 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004425 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004426 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004427 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004428 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004429 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004430 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004431 SetRawInputAt(0, current_method);
4432 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004433
4434 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004435
4436 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4437 return other->AsLoadClass()->type_index_ == type_index_;
4438 }
4439
4440 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4441
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004442 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004443 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004444 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004445
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004446 bool NeedsEnvironment() const OVERRIDE {
4447 // Will call runtime and load the class if the class is not loaded yet.
4448 // TODO: finer grain decision.
4449 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004450 }
4451
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004452 bool MustGenerateClinitCheck() const {
4453 return generate_clinit_check_;
4454 }
4455
Calin Juravle0ba218d2015-05-19 18:46:01 +01004456 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4457 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004458 }
4459
4460 bool CanCallRuntime() const {
4461 return MustGenerateClinitCheck() || !is_referrers_class_;
4462 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004463
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004464 bool CanThrow() const OVERRIDE {
4465 // May call runtime and and therefore can throw.
4466 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004467 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004468 }
4469
Calin Juravleacf735c2015-02-12 15:25:22 +00004470 ReferenceTypeInfo GetLoadedClassRTI() {
4471 return loaded_class_rti_;
4472 }
4473
4474 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4475 // Make sure we only set exact types (the loaded class should never be merged).
4476 DCHECK(rti.IsExact());
4477 loaded_class_rti_ = rti;
4478 }
4479
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004480 const DexFile& GetDexFile() { return dex_file_; }
4481
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004482 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4483
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004484 static SideEffects SideEffectsForArchRuntimeCalls() {
4485 return SideEffects::CanTriggerGC();
4486 }
4487
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004488 DECLARE_INSTRUCTION(LoadClass);
4489
4490 private:
4491 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004492 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004493 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004494 // Whether this instruction must generate the initialization check.
4495 // Used for code generation.
4496 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004497
Calin Juravleacf735c2015-02-12 15:25:22 +00004498 ReferenceTypeInfo loaded_class_rti_;
4499
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004500 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4501};
4502
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004503class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004504 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004505 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004506 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4507 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004508 SetRawInputAt(0, current_method);
4509 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004510
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004511 bool CanBeMoved() const OVERRIDE { return true; }
4512
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004513 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4514 return other->AsLoadString()->string_index_ == string_index_;
4515 }
4516
4517 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4518
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004519 uint32_t GetStringIndex() const { return string_index_; }
4520
4521 // TODO: Can we deopt or debug when we resolve a string?
4522 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004523 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004524 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004525
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004526 static SideEffects SideEffectsForArchRuntimeCalls() {
4527 return SideEffects::CanTriggerGC();
4528 }
4529
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004530 DECLARE_INSTRUCTION(LoadString);
4531
4532 private:
4533 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004534
4535 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4536};
4537
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004538/**
4539 * Performs an initialization check on its Class object input.
4540 */
4541class HClinitCheck : public HExpression<1> {
4542 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004543 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004544 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004545 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004546 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4547 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004548 SetRawInputAt(0, constant);
4549 }
4550
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004551 bool CanBeMoved() const OVERRIDE { return true; }
4552 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4553 UNUSED(other);
4554 return true;
4555 }
4556
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004557 bool NeedsEnvironment() const OVERRIDE {
4558 // May call runtime to initialize the class.
4559 return true;
4560 }
4561
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004562
4563 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4564
4565 DECLARE_INSTRUCTION(ClinitCheck);
4566
4567 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004568 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4569};
4570
4571class HStaticFieldGet : public HExpression<1> {
4572 public:
4573 HStaticFieldGet(HInstruction* cls,
4574 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004575 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004576 bool is_volatile,
4577 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004578 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004579 Handle<mirror::DexCache> dex_cache,
4580 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004581 : HExpression(
4582 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004583 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004584 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004585 SetRawInputAt(0, cls);
4586 }
4587
Calin Juravle52c48962014-12-16 17:02:57 +00004588
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004589 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004590
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004591 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004592 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4593 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004594 }
4595
4596 size_t ComputeHashCode() const OVERRIDE {
4597 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4598 }
4599
Calin Juravle52c48962014-12-16 17:02:57 +00004600 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004601 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4602 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004603 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004604
4605 DECLARE_INSTRUCTION(StaticFieldGet);
4606
4607 private:
4608 const FieldInfo field_info_;
4609
4610 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4611};
4612
4613class HStaticFieldSet : public HTemplateInstruction<2> {
4614 public:
4615 HStaticFieldSet(HInstruction* cls,
4616 HInstruction* value,
4617 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004618 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004619 bool is_volatile,
4620 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004621 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004622 Handle<mirror::DexCache> dex_cache,
4623 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004624 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004625 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004626 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004627 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004628 SetRawInputAt(0, cls);
4629 SetRawInputAt(1, value);
4630 }
4631
Calin Juravle52c48962014-12-16 17:02:57 +00004632 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004633 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4634 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004635 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004636
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004637 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004638 bool GetValueCanBeNull() const { return value_can_be_null_; }
4639 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004640
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004641 DECLARE_INSTRUCTION(StaticFieldSet);
4642
4643 private:
4644 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004645 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004646
4647 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4648};
4649
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004650// Implement the move-exception DEX instruction.
4651class HLoadException : public HExpression<0> {
4652 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004653 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4654 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004655
David Brazdilbbd733e2015-08-18 17:48:17 +01004656 bool CanBeNull() const OVERRIDE { return false; }
4657
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004658 DECLARE_INSTRUCTION(LoadException);
4659
4660 private:
4661 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4662};
4663
David Brazdilcb1c0552015-08-04 16:22:25 +01004664// Implicit part of move-exception which clears thread-local exception storage.
4665// Must not be removed because the runtime expects the TLS to get cleared.
4666class HClearException : public HTemplateInstruction<0> {
4667 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004668 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4669 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004670
4671 DECLARE_INSTRUCTION(ClearException);
4672
4673 private:
4674 DISALLOW_COPY_AND_ASSIGN(HClearException);
4675};
4676
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004677class HThrow : public HTemplateInstruction<1> {
4678 public:
4679 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004681 SetRawInputAt(0, exception);
4682 }
4683
4684 bool IsControlFlow() const OVERRIDE { return true; }
4685
4686 bool NeedsEnvironment() const OVERRIDE { return true; }
4687
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004688 bool CanThrow() const OVERRIDE { return true; }
4689
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004690
4691 DECLARE_INSTRUCTION(Throw);
4692
4693 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004694 DISALLOW_COPY_AND_ASSIGN(HThrow);
4695};
4696
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004697class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004698 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004699 HInstanceOf(HInstruction* object,
4700 HLoadClass* constant,
4701 bool class_is_final,
4702 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004703 : HExpression(Primitive::kPrimBoolean,
4704 SideEffectsForArchRuntimeCalls(class_is_final),
4705 dex_pc),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004706 class_is_final_(class_is_final),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004707 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004708 SetRawInputAt(0, object);
4709 SetRawInputAt(1, constant);
4710 }
4711
4712 bool CanBeMoved() const OVERRIDE { return true; }
4713
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004714 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004715 return true;
4716 }
4717
4718 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004719 return false;
4720 }
4721
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004722 bool IsClassFinal() const { return class_is_final_; }
4723
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004724 // Used only in code generation.
4725 bool MustDoNullCheck() const { return must_do_null_check_; }
4726 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4727
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004728 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4729 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4730 }
4731
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004732 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004733
4734 private:
4735 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004736 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004737
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004738 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4739};
4740
Calin Juravleb1498f62015-02-16 13:13:29 +00004741class HBoundType : public HExpression<1> {
4742 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004743 // Constructs an HBoundType with the given upper_bound.
4744 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004745 HBoundType(HInstruction* input,
4746 ReferenceTypeInfo upper_bound,
4747 bool upper_can_be_null,
4748 uint32_t dex_pc = kNoDexPc)
4749 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004750 upper_bound_(upper_bound),
4751 upper_can_be_null_(upper_can_be_null),
4752 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004753 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004754 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004755 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004756 }
4757
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004758 // GetUpper* should only be used in reference type propagation.
4759 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4760 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004761
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004762 void SetCanBeNull(bool can_be_null) {
4763 DCHECK(upper_can_be_null_ || !can_be_null);
4764 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004765 }
4766
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004767 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4768
Calin Juravleb1498f62015-02-16 13:13:29 +00004769 DECLARE_INSTRUCTION(BoundType);
4770
4771 private:
4772 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004773 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4774 // It is used to bound the type in cases like:
4775 // if (x instanceof ClassX) {
4776 // // uper_bound_ will be ClassX
4777 // }
4778 const ReferenceTypeInfo upper_bound_;
4779 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4780 // is false then can_be_null_ cannot be true).
4781 const bool upper_can_be_null_;
4782 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004783
4784 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4785};
4786
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004787class HCheckCast : public HTemplateInstruction<2> {
4788 public:
4789 HCheckCast(HInstruction* object,
4790 HLoadClass* constant,
4791 bool class_is_final,
4792 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004793 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004794 class_is_final_(class_is_final),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004795 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004796 SetRawInputAt(0, object);
4797 SetRawInputAt(1, constant);
4798 }
4799
4800 bool CanBeMoved() const OVERRIDE { return true; }
4801
4802 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4803 return true;
4804 }
4805
4806 bool NeedsEnvironment() const OVERRIDE {
4807 // Instruction may throw a CheckCastError.
4808 return true;
4809 }
4810
4811 bool CanThrow() const OVERRIDE { return true; }
4812
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004813 bool MustDoNullCheck() const { return must_do_null_check_; }
4814 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4815
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004816
4817 bool IsClassFinal() const { return class_is_final_; }
4818
4819 DECLARE_INSTRUCTION(CheckCast);
4820
4821 private:
4822 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004823 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004824
4825 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004826};
4827
Calin Juravle27df7582015-04-17 19:12:31 +01004828class HMemoryBarrier : public HTemplateInstruction<0> {
4829 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004830 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004831 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004832 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004833 barrier_kind_(barrier_kind) {}
4834
4835 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4836
4837 DECLARE_INSTRUCTION(MemoryBarrier);
4838
4839 private:
4840 const MemBarrierKind barrier_kind_;
4841
4842 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4843};
4844
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004845class HMonitorOperation : public HTemplateInstruction<1> {
4846 public:
4847 enum OperationKind {
4848 kEnter,
4849 kExit,
4850 };
4851
4852 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004853 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004854 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
4855 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004856 SetRawInputAt(0, object);
4857 }
4858
4859 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004860 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4861
4862 bool CanThrow() const OVERRIDE {
4863 // Verifier guarantees that monitor-exit cannot throw.
4864 // This is important because it allows the HGraphBuilder to remove
4865 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4866 return IsEnter();
4867 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004868
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004869
4870 bool IsEnter() const { return kind_ == kEnter; }
4871
4872 DECLARE_INSTRUCTION(MonitorOperation);
4873
Calin Juravle52c48962014-12-16 17:02:57 +00004874 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004875 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004876
4877 private:
4878 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4879};
4880
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004881/**
4882 * A HInstruction used as a marker for the replacement of new + <init>
4883 * of a String to a call to a StringFactory. Only baseline will see
4884 * the node at code generation, where it will be be treated as null.
4885 * When compiling non-baseline, `HFakeString` instructions are being removed
4886 * in the instruction simplifier.
4887 */
4888class HFakeString : public HTemplateInstruction<0> {
4889 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004890 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
4891 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004892
4893 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4894
4895 DECLARE_INSTRUCTION(FakeString);
4896
4897 private:
4898 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4899};
4900
Vladimir Markof9f64412015-09-02 14:05:49 +01004901class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004902 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004903 MoveOperands(Location source,
4904 Location destination,
4905 Primitive::Type type,
4906 HInstruction* instruction)
4907 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004908
4909 Location GetSource() const { return source_; }
4910 Location GetDestination() const { return destination_; }
4911
4912 void SetSource(Location value) { source_ = value; }
4913 void SetDestination(Location value) { destination_ = value; }
4914
4915 // The parallel move resolver marks moves as "in-progress" by clearing the
4916 // destination (but not the source).
4917 Location MarkPending() {
4918 DCHECK(!IsPending());
4919 Location dest = destination_;
4920 destination_ = Location::NoLocation();
4921 return dest;
4922 }
4923
4924 void ClearPending(Location dest) {
4925 DCHECK(IsPending());
4926 destination_ = dest;
4927 }
4928
4929 bool IsPending() const {
4930 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4931 return destination_.IsInvalid() && !source_.IsInvalid();
4932 }
4933
4934 // True if this blocks a move from the given location.
4935 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004936 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004937 }
4938
4939 // A move is redundant if it's been eliminated, if its source and
4940 // destination are the same, or if its destination is unneeded.
4941 bool IsRedundant() const {
4942 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4943 }
4944
4945 // We clear both operands to indicate move that's been eliminated.
4946 void Eliminate() {
4947 source_ = destination_ = Location::NoLocation();
4948 }
4949
4950 bool IsEliminated() const {
4951 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4952 return source_.IsInvalid();
4953 }
4954
Alexey Frunze4dda3372015-06-01 18:31:49 -07004955 Primitive::Type GetType() const { return type_; }
4956
Nicolas Geoffray90218252015-04-15 11:56:51 +01004957 bool Is64BitMove() const {
4958 return Primitive::Is64BitType(type_);
4959 }
4960
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004961 HInstruction* GetInstruction() const { return instruction_; }
4962
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004963 private:
4964 Location source_;
4965 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004966 // The type this move is for.
4967 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004968 // The instruction this move is assocatied with. Null when this move is
4969 // for moving an input in the expected locations of user (including a phi user).
4970 // This is only used in debug mode, to ensure we do not connect interval siblings
4971 // in the same parallel move.
4972 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004973};
4974
4975static constexpr size_t kDefaultNumberOfMoves = 4;
4976
4977class HParallelMove : public HTemplateInstruction<0> {
4978 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004979 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
4980 : HTemplateInstruction(SideEffects::None(), dex_pc), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004981
Nicolas Geoffray90218252015-04-15 11:56:51 +01004982 void AddMove(Location source,
4983 Location destination,
4984 Primitive::Type type,
4985 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004986 DCHECK(source.IsValid());
4987 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004988 if (kIsDebugBuild) {
4989 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004990 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004991 if (moves_.Get(i).GetInstruction() == instruction) {
4992 // Special case the situation where the move is for the spill slot
4993 // of the instruction.
4994 if ((GetPrevious() == instruction)
4995 || ((GetPrevious() == nullptr)
4996 && instruction->IsPhi()
4997 && instruction->GetBlock() == GetBlock())) {
4998 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4999 << "Doing parallel moves for the same instruction.";
5000 } else {
5001 DCHECK(false) << "Doing parallel moves for the same instruction.";
5002 }
5003 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005004 }
5005 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005006 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08005007 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005008 << "Overlapped destination for two moves in a parallel move: "
5009 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
5010 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005011 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005012 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01005013 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005014 }
5015
5016 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005017 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005018 }
5019
5020 size_t NumMoves() const { return moves_.Size(); }
5021
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005022 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005023
5024 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005025 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005026
5027 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5028};
5029
Mark Mendell0616ae02015-04-17 12:49:27 -04005030} // namespace art
5031
5032#ifdef ART_ENABLE_CODEGEN_x86
5033#include "nodes_x86.h"
5034#endif
5035
5036namespace art {
5037
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005038class HGraphVisitor : public ValueObject {
5039 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005040 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5041 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005042
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005043 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005044 virtual void VisitBasicBlock(HBasicBlock* block);
5045
Roland Levillain633021e2014-10-01 14:12:25 +01005046 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005047 void VisitInsertionOrder();
5048
Roland Levillain633021e2014-10-01 14:12:25 +01005049 // Visit the graph following dominator tree reverse post-order.
5050 void VisitReversePostOrder();
5051
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005052 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005053
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005054 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005055#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005056 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5057
5058 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5059
5060#undef DECLARE_VISIT_INSTRUCTION
5061
5062 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005063 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005064
5065 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5066};
5067
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005068class HGraphDelegateVisitor : public HGraphVisitor {
5069 public:
5070 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5071 virtual ~HGraphDelegateVisitor() {}
5072
5073 // Visit functions that delegate to to super class.
5074#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005075 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005076
5077 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5078
5079#undef DECLARE_VISIT_INSTRUCTION
5080
5081 private:
5082 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5083};
5084
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005085class HInsertionOrderIterator : public ValueObject {
5086 public:
5087 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5088
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005089 bool Done() const { return index_ == graph_.GetBlocks().size(); }
5090 HBasicBlock* Current() const { return graph_.GetBlock(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005091 void Advance() { ++index_; }
5092
5093 private:
5094 const HGraph& graph_;
5095 size_t index_;
5096
5097 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5098};
5099
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005100class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005101 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005102 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5103 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005104 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005105 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005106
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005107 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5108 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005109 void Advance() { ++index_; }
5110
5111 private:
5112 const HGraph& graph_;
5113 size_t index_;
5114
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005115 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005116};
5117
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005118class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005119 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005120 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005121 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005122 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005123 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005124 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005125
5126 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005127 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005128 void Advance() { --index_; }
5129
5130 private:
5131 const HGraph& graph_;
5132 size_t index_;
5133
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005134 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005135};
5136
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005137class HLinearPostOrderIterator : public ValueObject {
5138 public:
5139 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005140 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005141
5142 bool Done() const { return index_ == 0; }
5143
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005144 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005145
5146 void Advance() {
5147 --index_;
5148 DCHECK_GE(index_, 0U);
5149 }
5150
5151 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005152 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005153 size_t index_;
5154
5155 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5156};
5157
5158class HLinearOrderIterator : public ValueObject {
5159 public:
5160 explicit HLinearOrderIterator(const HGraph& graph)
5161 : order_(graph.GetLinearOrder()), index_(0) {}
5162
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005163 bool Done() const { return index_ == order_.size(); }
5164 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005165 void Advance() { ++index_; }
5166
5167 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005168 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005169 size_t index_;
5170
5171 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5172};
5173
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005174// Iterator over the blocks that art part of the loop. Includes blocks part
5175// of an inner loop. The order in which the blocks are iterated is on their
5176// block id.
5177class HBlocksInLoopIterator : public ValueObject {
5178 public:
5179 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5180 : blocks_in_loop_(info.GetBlocks()),
5181 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5182 index_(0) {
5183 if (!blocks_in_loop_.IsBitSet(index_)) {
5184 Advance();
5185 }
5186 }
5187
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005188 bool Done() const { return index_ == blocks_.size(); }
5189 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005190 void Advance() {
5191 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005192 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005193 if (blocks_in_loop_.IsBitSet(index_)) {
5194 break;
5195 }
5196 }
5197 }
5198
5199 private:
5200 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005201 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005202 size_t index_;
5203
5204 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5205};
5206
Mingyao Yang3584bce2015-05-19 16:01:59 -07005207// Iterator over the blocks that art part of the loop. Includes blocks part
5208// of an inner loop. The order in which the blocks are iterated is reverse
5209// post order.
5210class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5211 public:
5212 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5213 : blocks_in_loop_(info.GetBlocks()),
5214 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5215 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005216 DCHECK(!blocks_.empty());
5217 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005218 Advance();
5219 }
5220 }
5221
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005222 bool Done() const { return index_ == blocks_.size(); }
5223 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005224 void Advance() {
5225 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005226 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5227 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005228 break;
5229 }
5230 }
5231 }
5232
5233 private:
5234 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005235 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005236 size_t index_;
5237
5238 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5239};
5240
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005241inline int64_t Int64FromConstant(HConstant* constant) {
5242 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5243 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5244 : constant->AsLongConstant()->GetValue();
5245}
5246
Vladimir Marko58155012015-08-19 12:49:41 +00005247inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5248 // For the purposes of the compiler, the dex files must actually be the same object
5249 // if we want to safely treat them as the same. This is especially important for JIT
5250 // as custom class loaders can open the same underlying file (or memory) multiple
5251 // times and provide different class resolution but no two class loaders should ever
5252 // use the same DexFile object - doing so is an unsupported hack that can lead to
5253 // all sorts of weird failures.
5254 return &lhs == &rhs;
5255}
5256
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005257} // namespace art
5258
5259#endif // ART_COMPILER_OPTIMIZING_NODES_H_