blob: 33c0d88f3dad1d4957b2b6836910067206ed5c76 [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) \
Calin Juravle175dc732015-08-25 15:42:32 +01001036 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001037 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001038 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001039 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001040 M(LessThan, Condition) \
1041 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001042 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001043 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001044 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001045 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001046 M(Local, Instruction) \
1047 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001048 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001049 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001050 M(Mul, BinaryOperation) \
1051 M(Neg, UnaryOperation) \
1052 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001054 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001055 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001056 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001057 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001058 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001059 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001060 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001061 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001062 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001063 M(Return, Instruction) \
1064 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001065 M(Shl, BinaryOperation) \
1066 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001067 M(StaticFieldGet, Instruction) \
1068 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001069 M(StoreLocal, Instruction) \
1070 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001071 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001072 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001073 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001074 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001075 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001076 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001077 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001078
Alexandre Ramesef20f712015-06-09 10:29:30 +01001079#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1080
1081#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1082
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001083#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1084
Mark Mendell0616ae02015-04-17 12:49:27 -04001085#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1086 M(X86ComputeBaseMethodAddress, Instruction) \
1087 M(X86LoadFromConstantTable, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001088
1089#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1090
1091#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1092 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1093 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1094 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001095 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001096 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1097 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1098
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001099#define FOR_EACH_INSTRUCTION(M) \
1100 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1101 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001102 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001103 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001104 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001105
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001106#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001107FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1108#undef FORWARD_DECLARATION
1109
Roland Levillainccc07a92014-09-16 14:48:16 +01001110#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001111 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1112 const char* DebugName() const OVERRIDE { return #type; } \
1113 const H##type* As##type() const OVERRIDE { return this; } \
1114 H##type* As##type() OVERRIDE { return this; } \
1115 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001116 return other->Is##type(); \
1117 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001118 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001119
David Brazdiled596192015-01-23 10:39:45 +00001120template <typename T> class HUseList;
1121
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001122template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001123class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001124 public:
David Brazdiled596192015-01-23 10:39:45 +00001125 HUseListNode* GetPrevious() const { return prev_; }
1126 HUseListNode* GetNext() const { return next_; }
1127 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001128 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001129 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001130
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001131 private:
David Brazdiled596192015-01-23 10:39:45 +00001132 HUseListNode(T user, size_t index)
1133 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1134
1135 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001136 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001137 HUseListNode<T>* prev_;
1138 HUseListNode<T>* next_;
1139
1140 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001141
1142 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1143};
1144
David Brazdiled596192015-01-23 10:39:45 +00001145template <typename T>
1146class HUseList : public ValueObject {
1147 public:
1148 HUseList() : first_(nullptr) {}
1149
1150 void Clear() {
1151 first_ = nullptr;
1152 }
1153
1154 // Adds a new entry at the beginning of the use list and returns
1155 // the newly created node.
1156 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001157 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001158 if (IsEmpty()) {
1159 first_ = new_node;
1160 } else {
1161 first_->prev_ = new_node;
1162 new_node->next_ = first_;
1163 first_ = new_node;
1164 }
1165 return new_node;
1166 }
1167
1168 HUseListNode<T>* GetFirst() const {
1169 return first_;
1170 }
1171
1172 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001173 DCHECK(node != nullptr);
1174 DCHECK(Contains(node));
1175
David Brazdiled596192015-01-23 10:39:45 +00001176 if (node->prev_ != nullptr) {
1177 node->prev_->next_ = node->next_;
1178 }
1179 if (node->next_ != nullptr) {
1180 node->next_->prev_ = node->prev_;
1181 }
1182 if (node == first_) {
1183 first_ = node->next_;
1184 }
1185 }
1186
David Brazdil1abb4192015-02-17 18:33:36 +00001187 bool Contains(const HUseListNode<T>* node) const {
1188 if (node == nullptr) {
1189 return false;
1190 }
1191 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1192 if (current == node) {
1193 return true;
1194 }
1195 }
1196 return false;
1197 }
1198
David Brazdiled596192015-01-23 10:39:45 +00001199 bool IsEmpty() const {
1200 return first_ == nullptr;
1201 }
1202
1203 bool HasOnlyOneUse() const {
1204 return first_ != nullptr && first_->next_ == nullptr;
1205 }
1206
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001207 size_t SizeSlow() const {
1208 size_t count = 0;
1209 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1210 ++count;
1211 }
1212 return count;
1213 }
1214
David Brazdiled596192015-01-23 10:39:45 +00001215 private:
1216 HUseListNode<T>* first_;
1217};
1218
1219template<typename T>
1220class HUseIterator : public ValueObject {
1221 public:
1222 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1223
1224 bool Done() const { return current_ == nullptr; }
1225
1226 void Advance() {
1227 DCHECK(!Done());
1228 current_ = current_->GetNext();
1229 }
1230
1231 HUseListNode<T>* Current() const {
1232 DCHECK(!Done());
1233 return current_;
1234 }
1235
1236 private:
1237 HUseListNode<T>* current_;
1238
1239 friend class HValue;
1240};
1241
David Brazdil1abb4192015-02-17 18:33:36 +00001242// This class is used by HEnvironment and HInstruction classes to record the
1243// instructions they use and pointers to the corresponding HUseListNodes kept
1244// by the used instructions.
1245template <typename T>
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001246class HUserRecord {
David Brazdil1abb4192015-02-17 18:33:36 +00001247 public:
1248 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1249 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1250
1251 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1252 : instruction_(old_record.instruction_), use_node_(use_node) {
1253 DCHECK(instruction_ != nullptr);
1254 DCHECK(use_node_ != nullptr);
1255 DCHECK(old_record.use_node_ == nullptr);
1256 }
1257
1258 HInstruction* GetInstruction() const { return instruction_; }
1259 HUseListNode<T>* GetUseNode() const { return use_node_; }
1260
1261 private:
1262 // Instruction used by the user.
1263 HInstruction* instruction_;
1264
1265 // Corresponding entry in the use list kept by 'instruction_'.
1266 HUseListNode<T>* use_node_;
1267};
1268
Aart Bik854a02b2015-07-14 16:07:00 -07001269/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001270 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001271 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001272 * For write/read dependences on fields/arrays, the dependence analysis uses
1273 * type disambiguation (e.g. a float field write cannot modify the value of an
1274 * integer field read) and the access type (e.g. a reference array write cannot
1275 * modify the value of a reference field read [although it may modify the
1276 * reference fetch prior to reading the field, which is represented by its own
1277 * write/read dependence]). The analysis makes conservative points-to
1278 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1279 * the same, and any reference read depends on any reference read without
1280 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001281 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001282 * The internal representation uses 38-bit and is described in the table below.
1283 * The first line indicates the side effect, and for field/array accesses the
1284 * second line indicates the type of the access (in the order of the
1285 * Primitive::Type enum).
1286 * The two numbered lines below indicate the bit position in the bitfield (read
1287 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001288 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001289 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1290 * +-------------+---------+---------+--------------+---------+---------+
1291 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1292 * | 3 |333333322|222222221| 1 |111111110|000000000|
1293 * | 7 |654321098|765432109| 8 |765432109|876543210|
1294 *
1295 * Note that, to ease the implementation, 'changes' bits are least significant
1296 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001297 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001298class SideEffects : public ValueObject {
1299 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001300 SideEffects() : flags_(0) {}
1301
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001302 static SideEffects None() {
1303 return SideEffects(0);
1304 }
1305
1306 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001307 return SideEffects(kAllChangeBits | kAllDependOnBits);
1308 }
1309
1310 static SideEffects AllChanges() {
1311 return SideEffects(kAllChangeBits);
1312 }
1313
1314 static SideEffects AllDependencies() {
1315 return SideEffects(kAllDependOnBits);
1316 }
1317
1318 static SideEffects AllExceptGCDependency() {
1319 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1320 }
1321
1322 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001323 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001324 }
1325
Aart Bik34c3ba92015-07-20 14:08:59 -07001326 static SideEffects AllWrites() {
1327 return SideEffects(kAllWrites);
1328 }
1329
1330 static SideEffects AllReads() {
1331 return SideEffects(kAllReads);
1332 }
1333
1334 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1335 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001336 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001337 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001338 }
1339
Aart Bik854a02b2015-07-14 16:07:00 -07001340 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1341 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001342 }
1343
Aart Bik34c3ba92015-07-20 14:08:59 -07001344 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1345 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001346 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001347 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001348 }
1349
1350 static SideEffects ArrayReadOfType(Primitive::Type type) {
1351 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1352 }
1353
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001354 static SideEffects CanTriggerGC() {
1355 return SideEffects(1ULL << kCanTriggerGCBit);
1356 }
1357
1358 static SideEffects DependsOnGC() {
1359 return SideEffects(1ULL << kDependsOnGCBit);
1360 }
1361
Aart Bik854a02b2015-07-14 16:07:00 -07001362 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001363 SideEffects Union(SideEffects other) const {
1364 return SideEffects(flags_ | other.flags_);
1365 }
1366
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001367 SideEffects Exclusion(SideEffects other) const {
1368 return SideEffects(flags_ & ~other.flags_);
1369 }
1370
1371 bool Includes(SideEffects other) const {
1372 return (other.flags_ & flags_) == other.flags_;
1373 }
1374
1375 bool HasSideEffects() const {
1376 return (flags_ & kAllChangeBits);
1377 }
1378
1379 bool HasDependencies() const {
1380 return (flags_ & kAllDependOnBits);
1381 }
1382
1383 // Returns true if there are no side effects or dependencies.
1384 bool DoesNothing() const {
1385 return flags_ == 0;
1386 }
1387
Aart Bik854a02b2015-07-14 16:07:00 -07001388 // Returns true if something is written.
1389 bool DoesAnyWrite() const {
1390 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001391 }
1392
Aart Bik854a02b2015-07-14 16:07:00 -07001393 // Returns true if something is read.
1394 bool DoesAnyRead() const {
1395 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001396 }
1397
Aart Bik854a02b2015-07-14 16:07:00 -07001398 // Returns true if potentially everything is written and read
1399 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001400 bool DoesAllReadWrite() const {
1401 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1402 }
1403
Aart Bik854a02b2015-07-14 16:07:00 -07001404 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001406 }
1407
1408 // Returns true if this may read something written by other.
1409 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001410 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1411 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001412 }
1413
1414 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001415 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001416 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001417 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001418 for (int s = kLastBit; s >= 0; s--) {
1419 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1420 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1421 // This is a bit for the GC side effect.
1422 if (current_bit_is_set) {
1423 flags += "GC";
1424 }
Aart Bik854a02b2015-07-14 16:07:00 -07001425 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001426 } else {
1427 // This is a bit for the array/field analysis.
1428 // The underscore character stands for the 'can trigger GC' bit.
1429 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1430 if (current_bit_is_set) {
1431 flags += kDebug[s];
1432 }
1433 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1434 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1435 flags += "|";
1436 }
1437 }
Aart Bik854a02b2015-07-14 16:07:00 -07001438 }
1439 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001440 }
1441
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001442 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001443
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001444 private:
1445 static constexpr int kFieldArrayAnalysisBits = 9;
1446
1447 static constexpr int kFieldWriteOffset = 0;
1448 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1449 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1450 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1451
1452 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1453
1454 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1455 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1456 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1457 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1458
1459 static constexpr int kLastBit = kDependsOnGCBit;
1460 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1461
1462 // Aliases.
1463
1464 static_assert(kChangeBits == kDependOnBits,
1465 "the 'change' bits should match the 'depend on' bits.");
1466
1467 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1468 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1469 static constexpr uint64_t kAllWrites =
1470 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1471 static constexpr uint64_t kAllReads =
1472 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001473
Aart Bik854a02b2015-07-14 16:07:00 -07001474 // Work around the fact that HIR aliases I/F and J/D.
1475 // TODO: remove this interceptor once HIR types are clean
1476 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1477 switch (type) {
1478 case Primitive::kPrimInt:
1479 case Primitive::kPrimFloat:
1480 return TypeFlag(Primitive::kPrimInt, offset) |
1481 TypeFlag(Primitive::kPrimFloat, offset);
1482 case Primitive::kPrimLong:
1483 case Primitive::kPrimDouble:
1484 return TypeFlag(Primitive::kPrimLong, offset) |
1485 TypeFlag(Primitive::kPrimDouble, offset);
1486 default:
1487 return TypeFlag(type, offset);
1488 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001489 }
1490
Aart Bik854a02b2015-07-14 16:07:00 -07001491 // Translates type to bit flag.
1492 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1493 CHECK_NE(type, Primitive::kPrimVoid);
1494 const uint64_t one = 1;
1495 const int shift = type; // 0-based consecutive enum
1496 DCHECK_LE(kFieldWriteOffset, shift);
1497 DCHECK_LT(shift, kArrayWriteOffset);
1498 return one << (type + offset);
1499 }
1500
1501 // Private constructor on direct flags value.
1502 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1503
1504 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001505};
1506
David Brazdiled596192015-01-23 10:39:45 +00001507// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001508class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001509 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001510 HEnvironment(ArenaAllocator* arena,
1511 size_t number_of_vregs,
1512 const DexFile& dex_file,
1513 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001514 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001515 InvokeType invoke_type,
1516 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001517 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1518 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001519 parent_(nullptr),
1520 dex_file_(dex_file),
1521 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001522 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001523 invoke_type_(invoke_type),
1524 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001525 }
1526
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001527 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001528 : HEnvironment(arena,
1529 to_copy.Size(),
1530 to_copy.GetDexFile(),
1531 to_copy.GetMethodIdx(),
1532 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001533 to_copy.GetInvokeType(),
1534 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001535
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001536 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001537 if (parent_ != nullptr) {
1538 parent_->SetAndCopyParentChain(allocator, parent);
1539 } else {
1540 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1541 parent_->CopyFrom(parent);
1542 if (parent->GetParent() != nullptr) {
1543 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1544 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001545 }
David Brazdiled596192015-01-23 10:39:45 +00001546 }
1547
Vladimir Marko71bf8092015-09-15 15:33:14 +01001548 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001549 void CopyFrom(HEnvironment* environment);
1550
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001551 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1552 // input to the loop phi instead. This is for inserting instructions that
1553 // require an environment (like HDeoptimization) in the loop pre-header.
1554 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001555
1556 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001557 DCHECK_LT(index, Size());
1558 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001559 }
1560
1561 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001562 DCHECK_LT(index, Size());
1563 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001564 }
1565
David Brazdil1abb4192015-02-17 18:33:36 +00001566 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001567
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001568 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001569
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001570 HEnvironment* GetParent() const { return parent_; }
1571
1572 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001573 DCHECK_LT(index, Size());
1574 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001575 }
1576
1577 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001578 DCHECK_LT(index, Size());
1579 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001580 }
1581
1582 uint32_t GetDexPc() const {
1583 return dex_pc_;
1584 }
1585
1586 uint32_t GetMethodIdx() const {
1587 return method_idx_;
1588 }
1589
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001590 InvokeType GetInvokeType() const {
1591 return invoke_type_;
1592 }
1593
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001594 const DexFile& GetDexFile() const {
1595 return dex_file_;
1596 }
1597
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001598 HInstruction* GetHolder() const {
1599 return holder_;
1600 }
1601
David Brazdiled596192015-01-23 10:39:45 +00001602 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001603 // Record instructions' use entries of this environment for constant-time removal.
1604 // It should only be called by HInstruction when a new environment use is added.
1605 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1606 DCHECK(env_use->GetUser() == this);
1607 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001608 DCHECK_LT(index, Size());
1609 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001610 }
David Brazdiled596192015-01-23 10:39:45 +00001611
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001612 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1613 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001614 HEnvironment* parent_;
1615 const DexFile& dex_file_;
1616 const uint32_t method_idx_;
1617 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001618 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001619
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001620 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001621 HInstruction* const holder_;
1622
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001623 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001624
1625 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1626};
1627
Calin Juravleacf735c2015-02-12 15:25:22 +00001628class ReferenceTypeInfo : ValueObject {
1629 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001630 typedef Handle<mirror::Class> TypeHandle;
1631
Calin Juravle2e768302015-07-28 14:41:11 +00001632 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1633 // The constructor will check that the type_handle is valid.
1634 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001635 }
1636
Calin Juravle2e768302015-07-28 14:41:11 +00001637 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1638
1639 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1640 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001641 }
1642
Calin Juravle2e768302015-07-28 14:41:11 +00001643 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1644 return IsValidHandle(type_handle_);
1645 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001646 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001647
1648 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1649 DCHECK(IsValid());
1650 return GetTypeHandle()->IsObjectClass();
1651 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001652 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001653 DCHECK(IsValid());
1654 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001655 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001656
1657 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1658
Mathieu Chartier90443472015-07-16 20:32:27 -07001659 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001660 DCHECK(IsValid());
1661 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001662 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1663 }
1664
1665 // Returns true if the type information provide the same amount of details.
1666 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001667 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001668 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001669 if (!IsValid() && !rti.IsValid()) {
1670 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001671 return true;
1672 }
Calin Juravle2e768302015-07-28 14:41:11 +00001673 if (!IsValid() || !rti.IsValid()) {
1674 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001675 return false;
1676 }
Calin Juravle2e768302015-07-28 14:41:11 +00001677 return IsExact() == rti.IsExact()
1678 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001679 }
1680
1681 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001682 ReferenceTypeInfo();
1683 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001684
Calin Juravleacf735c2015-02-12 15:25:22 +00001685 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001686 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001687 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001688 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001689 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001690};
1691
1692std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1693
Vladimir Markof9f64412015-09-02 14:05:49 +01001694class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001695 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001696 HInstruction(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001697 : previous_(nullptr),
1698 next_(nullptr),
1699 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001700 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001701 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001702 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001703 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001704 locations_(nullptr),
1705 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001706 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001707 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001708 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001709
Dave Allison20dfc792014-06-16 20:44:29 -07001710 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001711
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001712#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001713 enum InstructionKind {
1714 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1715 };
1716#undef DECLARE_KIND
1717
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001718 HInstruction* GetNext() const { return next_; }
1719 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001720
Calin Juravle77520bc2015-01-12 18:45:46 +00001721 HInstruction* GetNextDisregardingMoves() const;
1722 HInstruction* GetPreviousDisregardingMoves() const;
1723
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001724 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001725 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001726 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001727 bool IsInBlock() const { return block_ != nullptr; }
1728 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001729 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001730
Roland Levillain6b879dd2014-09-22 17:13:44 +01001731 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001732 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001733
1734 virtual void Accept(HGraphVisitor* visitor) = 0;
1735 virtual const char* DebugName() const = 0;
1736
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001737 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001738 void SetRawInputAt(size_t index, HInstruction* input) {
1739 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1740 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001741
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001742 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001743
1744 uint32_t GetDexPc() const { return dex_pc_; }
1745
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001746 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001747
Roland Levillaine161a2a2014-10-03 12:45:18 +01001748 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001749 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001750
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001751 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001752 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001753
Calin Juravle10e244f2015-01-26 18:54:32 +00001754 // Does not apply for all instructions, but having this at top level greatly
1755 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001756 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001757 virtual bool CanBeNull() const {
1758 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1759 return true;
1760 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001761
Calin Juravle641547a2015-04-21 22:08:51 +01001762 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1763 UNUSED(obj);
1764 return false;
1765 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001766
Calin Juravle2e768302015-07-28 14:41:11 +00001767 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001768
Calin Juravle61d544b2015-02-23 16:46:57 +00001769 ReferenceTypeInfo GetReferenceTypeInfo() const {
1770 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1771 return reference_type_info_;
1772 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001773
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001774 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001775 DCHECK(user != nullptr);
1776 HUseListNode<HInstruction*>* use =
1777 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1778 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001779 }
1780
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001781 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001782 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001783 HUseListNode<HEnvironment*>* env_use =
1784 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1785 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001786 }
1787
David Brazdil1abb4192015-02-17 18:33:36 +00001788 void RemoveAsUserOfInput(size_t input) {
1789 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1790 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1791 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001792
David Brazdil1abb4192015-02-17 18:33:36 +00001793 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1794 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001795
David Brazdiled596192015-01-23 10:39:45 +00001796 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1797 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001798 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001799 bool HasOnlyOneNonEnvironmentUse() const {
1800 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1801 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001802
Roland Levillain6c82d402014-10-13 16:10:27 +01001803 // Does this instruction strictly dominate `other_instruction`?
1804 // Returns false if this instruction and `other_instruction` are the same.
1805 // Aborts if this instruction and `other_instruction` are both phis.
1806 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001807
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001808 int GetId() const { return id_; }
1809 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001810
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001811 int GetSsaIndex() const { return ssa_index_; }
1812 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1813 bool HasSsaIndex() const { return ssa_index_ != -1; }
1814
1815 bool HasEnvironment() const { return environment_ != nullptr; }
1816 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001817 // Set the `environment_` field. Raw because this method does not
1818 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001819 void SetRawEnvironment(HEnvironment* environment) {
1820 DCHECK(environment_ == nullptr);
1821 DCHECK_EQ(environment->GetHolder(), this);
1822 environment_ = environment;
1823 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001824
1825 // Set the environment of this instruction, copying it from `environment`. While
1826 // copying, the uses lists are being updated.
1827 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001828 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001829 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001830 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001831 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001832 if (environment->GetParent() != nullptr) {
1833 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1834 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001835 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001836
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001837 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1838 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001839 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001840 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001841 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001842 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001843 if (environment->GetParent() != nullptr) {
1844 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1845 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001846 }
1847
Nicolas Geoffray39468442014-09-02 15:17:15 +01001848 // Returns the number of entries in the environment. Typically, that is the
1849 // number of dex registers in a method. It could be more in case of inlining.
1850 size_t EnvironmentSize() const;
1851
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001852 LocationSummary* GetLocations() const { return locations_; }
1853 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001854
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001855 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001856 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001857
Alexandre Rames188d4312015-04-09 18:30:21 +01001858 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1859 // uses of this instruction by `other` are *not* updated.
1860 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1861 ReplaceWith(other);
1862 other->ReplaceInput(this, use_index);
1863 }
1864
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001865 // Move `this` instruction before `cursor`.
1866 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001867
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001868#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001869 bool Is##type() const { return (As##type() != nullptr); } \
1870 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001871 virtual H##type* As##type() { return nullptr; }
1872
1873 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1874#undef INSTRUCTION_TYPE_CHECK
1875
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001876 // Returns whether the instruction can be moved within the graph.
1877 virtual bool CanBeMoved() const { return false; }
1878
1879 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001880 virtual bool InstructionTypeEquals(HInstruction* other) const {
1881 UNUSED(other);
1882 return false;
1883 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001884
1885 // Returns whether any data encoded in the two instructions is equal.
1886 // This method does not look at the inputs. Both instructions must be
1887 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001888 virtual bool InstructionDataEquals(HInstruction* other) const {
1889 UNUSED(other);
1890 return false;
1891 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001892
1893 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001894 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001895 // 2) Their inputs are identical.
1896 bool Equals(HInstruction* other) const;
1897
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001898 virtual InstructionKind GetKind() const = 0;
1899
1900 virtual size_t ComputeHashCode() const {
1901 size_t result = GetKind();
1902 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1903 result = (result * 31) + InputAt(i)->GetId();
1904 }
1905 return result;
1906 }
1907
1908 SideEffects GetSideEffects() const { return side_effects_; }
1909
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001910 size_t GetLifetimePosition() const { return lifetime_position_; }
1911 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1912 LiveInterval* GetLiveInterval() const { return live_interval_; }
1913 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1914 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1915
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001916 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1917
1918 // Returns whether the code generation of the instruction will require to have access
1919 // to the current method. Such instructions are:
1920 // (1): Instructions that require an environment, as calling the runtime requires
1921 // to walk the stack and have the current method stored at a specific stack address.
1922 // (2): Object literals like classes and strings, that are loaded from the dex cache
1923 // fields of the current method.
1924 bool NeedsCurrentMethod() const {
1925 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1926 }
1927
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001928 virtual bool NeedsDexCache() const { return false; }
1929
Mark Mendellc4701932015-04-10 13:18:51 -04001930 // Does this instruction have any use in an environment before
1931 // control flow hits 'other'?
1932 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1933
1934 // Remove all references to environment uses of this instruction.
1935 // The caller must ensure that this is safe to do.
1936 void RemoveEnvironmentUsers();
1937
David Brazdil1abb4192015-02-17 18:33:36 +00001938 protected:
1939 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1940 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1941
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001942 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001943 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1944
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001945 HInstruction* previous_;
1946 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001947 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001948 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001949
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001950 // An instruction gets an id when it is added to the graph.
1951 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001952 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001953 int id_;
1954
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001955 // When doing liveness analysis, instructions that have uses get an SSA index.
1956 int ssa_index_;
1957
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001958 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001959 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960
1961 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001962 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001963
Nicolas Geoffray39468442014-09-02 15:17:15 +01001964 // The environment associated with this instruction. Not null if the instruction
1965 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001966 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001967
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001968 // Set by the code generator.
1969 LocationSummary* locations_;
1970
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001971 // Set by the liveness analysis.
1972 LiveInterval* live_interval_;
1973
1974 // Set by the liveness analysis, this is the position in a linear
1975 // order of blocks where this instruction's live interval start.
1976 size_t lifetime_position_;
1977
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001978 const SideEffects side_effects_;
1979
Calin Juravleacf735c2015-02-12 15:25:22 +00001980 // TODO: for primitive types this should be marked as invalid.
1981 ReferenceTypeInfo reference_type_info_;
1982
David Brazdil1abb4192015-02-17 18:33:36 +00001983 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001984 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001985 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001986 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001987 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001988
1989 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1990};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001991std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001992
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001993class HInputIterator : public ValueObject {
1994 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001995 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001996
1997 bool Done() const { return index_ == instruction_->InputCount(); }
1998 HInstruction* Current() const { return instruction_->InputAt(index_); }
1999 void Advance() { index_++; }
2000
2001 private:
2002 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002003 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002004
2005 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2006};
2007
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002008class HInstructionIterator : public ValueObject {
2009 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002010 explicit HInstructionIterator(const HInstructionList& instructions)
2011 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002012 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002013 }
2014
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002015 bool Done() const { return instruction_ == nullptr; }
2016 HInstruction* Current() const { return instruction_; }
2017 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002019 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002020 }
2021
2022 private:
2023 HInstruction* instruction_;
2024 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002025
2026 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002027};
2028
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002029class HBackwardInstructionIterator : public ValueObject {
2030 public:
2031 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2032 : instruction_(instructions.last_instruction_) {
2033 next_ = Done() ? nullptr : instruction_->GetPrevious();
2034 }
2035
2036 bool Done() const { return instruction_ == nullptr; }
2037 HInstruction* Current() const { return instruction_; }
2038 void Advance() {
2039 instruction_ = next_;
2040 next_ = Done() ? nullptr : instruction_->GetPrevious();
2041 }
2042
2043 private:
2044 HInstruction* instruction_;
2045 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002046
2047 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002048};
2049
Vladimir Markof9f64412015-09-02 14:05:49 +01002050template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002051class HTemplateInstruction: public HInstruction {
2052 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002053 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2054 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002055 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002056
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002057 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002058
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002059 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002060 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2061 DCHECK_LT(i, N);
2062 return inputs_[i];
2063 }
David Brazdil1abb4192015-02-17 18:33:36 +00002064
2065 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002066 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002067 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002068 }
2069
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002070 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002071 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002072
2073 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002074};
2075
Vladimir Markof9f64412015-09-02 14:05:49 +01002076// HTemplateInstruction specialization for N=0.
2077template<>
2078class HTemplateInstruction<0>: public HInstruction {
2079 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002080 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2081 : HInstruction(side_effects, dex_pc) {}
2082
Vladimir Markof9f64412015-09-02 14:05:49 +01002083 virtual ~HTemplateInstruction() {}
2084
2085 size_t InputCount() const OVERRIDE { return 0; }
2086
2087 protected:
2088 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2089 LOG(FATAL) << "Unreachable";
2090 UNREACHABLE();
2091 }
2092
2093 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2094 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2095 LOG(FATAL) << "Unreachable";
2096 UNREACHABLE();
2097 }
2098
2099 private:
2100 friend class SsaBuilder;
2101};
2102
Dave Allison20dfc792014-06-16 20:44:29 -07002103template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002104class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002105 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002106 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2107 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002108 virtual ~HExpression() {}
2109
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002110 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002111
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002112 protected:
2113 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002114};
2115
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002116// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2117// instruction that branches to the exit block.
2118class HReturnVoid : public HTemplateInstruction<0> {
2119 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002120 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2121 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002122
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002123 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002124
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002125 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002126
2127 private:
2128 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2129};
2130
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002131// Represents dex's RETURN opcodes. A HReturn is a control flow
2132// instruction that branches to the exit block.
2133class HReturn : public HTemplateInstruction<1> {
2134 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002135 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2136 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002137 SetRawInputAt(0, value);
2138 }
2139
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002140 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002141
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002142 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002143
2144 private:
2145 DISALLOW_COPY_AND_ASSIGN(HReturn);
2146};
2147
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002148// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002149// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002150// exit block.
2151class HExit : public HTemplateInstruction<0> {
2152 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002153 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002154
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002155 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002157 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002158
2159 private:
2160 DISALLOW_COPY_AND_ASSIGN(HExit);
2161};
2162
2163// Jumps from one block to another.
2164class HGoto : public HTemplateInstruction<0> {
2165 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002166 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002167
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002168 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002169
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002170 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002171 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002172 }
2173
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002174 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175
2176 private:
2177 DISALLOW_COPY_AND_ASSIGN(HGoto);
2178};
2179
Roland Levillain9867bc72015-08-05 10:21:34 +01002180class HConstant : public HExpression<0> {
2181 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002182 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2183 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002184
2185 bool CanBeMoved() const OVERRIDE { return true; }
2186
2187 virtual bool IsMinusOne() const { return false; }
2188 virtual bool IsZero() const { return false; }
2189 virtual bool IsOne() const { return false; }
2190
David Brazdil77a48ae2015-09-15 12:34:04 +00002191 virtual uint64_t GetValueAsUint64() const = 0;
2192
Roland Levillain9867bc72015-08-05 10:21:34 +01002193 DECLARE_INSTRUCTION(Constant);
2194
2195 private:
2196 DISALLOW_COPY_AND_ASSIGN(HConstant);
2197};
2198
2199class HNullConstant : public HConstant {
2200 public:
2201 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2202 return true;
2203 }
2204
David Brazdil77a48ae2015-09-15 12:34:04 +00002205 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2206
Roland Levillain9867bc72015-08-05 10:21:34 +01002207 size_t ComputeHashCode() const OVERRIDE { return 0; }
2208
2209 DECLARE_INSTRUCTION(NullConstant);
2210
2211 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002212 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002213
2214 friend class HGraph;
2215 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2216};
2217
2218// Constants of the type int. Those can be from Dex instructions, or
2219// synthesized (for example with the if-eqz instruction).
2220class HIntConstant : public HConstant {
2221 public:
2222 int32_t GetValue() const { return value_; }
2223
David Brazdil77a48ae2015-09-15 12:34:04 +00002224 uint64_t GetValueAsUint64() const OVERRIDE { return static_cast<uint64_t>(value_); }
2225
Roland Levillain9867bc72015-08-05 10:21:34 +01002226 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2227 DCHECK(other->IsIntConstant());
2228 return other->AsIntConstant()->value_ == value_;
2229 }
2230
2231 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2232
2233 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2234 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2235 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2236
2237 DECLARE_INSTRUCTION(IntConstant);
2238
2239 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002240 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2241 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2242 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2243 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002244
2245 const int32_t value_;
2246
2247 friend class HGraph;
2248 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2249 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2250 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2251};
2252
2253class HLongConstant : public HConstant {
2254 public:
2255 int64_t GetValue() const { return value_; }
2256
David Brazdil77a48ae2015-09-15 12:34:04 +00002257 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2258
Roland Levillain9867bc72015-08-05 10:21:34 +01002259 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2260 DCHECK(other->IsLongConstant());
2261 return other->AsLongConstant()->value_ == value_;
2262 }
2263
2264 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2265
2266 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2267 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2268 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2269
2270 DECLARE_INSTRUCTION(LongConstant);
2271
2272 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002273 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2274 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002275
2276 const int64_t value_;
2277
2278 friend class HGraph;
2279 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2280};
Dave Allison20dfc792014-06-16 20:44:29 -07002281
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002282// Conditional branch. A block ending with an HIf instruction must have
2283// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002284class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002285 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002286 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2287 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002288 SetRawInputAt(0, input);
2289 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002290
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002291 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002292
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002293 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002294 return GetBlock()->GetSuccessor(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002295 }
2296
2297 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002298 return GetBlock()->GetSuccessor(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002299 }
2300
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002301 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002302
2303 private:
2304 DISALLOW_COPY_AND_ASSIGN(HIf);
2305};
2306
David Brazdilfc6a86a2015-06-26 10:33:45 +00002307
2308// Abstract instruction which marks the beginning and/or end of a try block and
2309// links it to the respective exception handlers. Behaves the same as a Goto in
2310// non-exceptional control flow.
2311// Normal-flow successor is stored at index zero, exception handlers under
2312// higher indices in no particular order.
2313class HTryBoundary : public HTemplateInstruction<0> {
2314 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002315 enum BoundaryKind {
2316 kEntry,
2317 kExit,
2318 };
2319
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002320 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2321 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002322
2323 bool IsControlFlow() const OVERRIDE { return true; }
2324
2325 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko60584552015-09-03 13:35:12 +00002326 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessor(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002327
2328 // Returns whether `handler` is among its exception handlers (non-zero index
2329 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002330 bool HasExceptionHandler(const HBasicBlock& handler) const {
2331 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002332 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002333 }
2334
2335 // If not present already, adds `handler` to its block's list of exception
2336 // handlers.
2337 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002338 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002339 GetBlock()->AddSuccessor(handler);
2340 }
2341 }
2342
David Brazdil56e1acc2015-06-30 15:41:36 +01002343 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002344
David Brazdilffee3d32015-07-06 11:48:53 +01002345 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2346
David Brazdilfc6a86a2015-06-26 10:33:45 +00002347 DECLARE_INSTRUCTION(TryBoundary);
2348
2349 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002350 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002351
2352 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2353};
2354
David Brazdilffee3d32015-07-06 11:48:53 +01002355// Iterator over exception handlers of a given HTryBoundary, i.e. over
2356// exceptional successors of its basic block.
2357class HExceptionHandlerIterator : public ValueObject {
2358 public:
2359 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2360 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2361
Vladimir Marko60584552015-09-03 13:35:12 +00002362 bool Done() const { return index_ == block_.GetSuccessors().size(); }
2363 HBasicBlock* Current() const { return block_.GetSuccessor(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002364 size_t CurrentSuccessorIndex() const { return index_; }
2365 void Advance() { ++index_; }
2366
2367 private:
2368 const HBasicBlock& block_;
2369 size_t index_;
2370
2371 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2372};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002373
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002374// Deoptimize to interpreter, upon checking a condition.
2375class HDeoptimize : public HTemplateInstruction<1> {
2376 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002377 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2378 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002379 SetRawInputAt(0, cond);
2380 }
2381
2382 bool NeedsEnvironment() const OVERRIDE { return true; }
2383 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002384
2385 DECLARE_INSTRUCTION(Deoptimize);
2386
2387 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002388 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2389};
2390
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002391// Represents the ArtMethod that was passed as a first argument to
2392// the method. It is used by instructions that depend on it, like
2393// instructions that work with the dex cache.
2394class HCurrentMethod : public HExpression<0> {
2395 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002396 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2397 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002398
2399 DECLARE_INSTRUCTION(CurrentMethod);
2400
2401 private:
2402 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2403};
2404
Roland Levillain88cb1752014-10-20 16:36:47 +01002405class HUnaryOperation : public HExpression<1> {
2406 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002407 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2408 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002409 SetRawInputAt(0, input);
2410 }
2411
2412 HInstruction* GetInput() const { return InputAt(0); }
2413 Primitive::Type GetResultType() const { return GetType(); }
2414
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002415 bool CanBeMoved() const OVERRIDE { return true; }
2416 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002417 UNUSED(other);
2418 return true;
2419 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002420
Roland Levillain9240d6a2014-10-20 16:47:04 +01002421 // Try to statically evaluate `operation` and return a HConstant
2422 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002423 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002424 HConstant* TryStaticEvaluation() const;
2425
2426 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002427 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2428 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002429
Roland Levillain88cb1752014-10-20 16:36:47 +01002430 DECLARE_INSTRUCTION(UnaryOperation);
2431
2432 private:
2433 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2434};
2435
Dave Allison20dfc792014-06-16 20:44:29 -07002436class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002437 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002438 HBinaryOperation(Primitive::Type result_type,
2439 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002440 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002441 SideEffects side_effects = SideEffects::None(),
2442 uint32_t dex_pc = kNoDexPc)
2443 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002444 SetRawInputAt(0, left);
2445 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002446 }
2447
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002448 HInstruction* GetLeft() const { return InputAt(0); }
2449 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002450 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002451
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002452 virtual bool IsCommutative() const { return false; }
2453
2454 // Put constant on the right.
2455 // Returns whether order is changed.
2456 bool OrderInputsWithConstantOnTheRight() {
2457 HInstruction* left = InputAt(0);
2458 HInstruction* right = InputAt(1);
2459 if (left->IsConstant() && !right->IsConstant()) {
2460 ReplaceInput(right, 0);
2461 ReplaceInput(left, 1);
2462 return true;
2463 }
2464 return false;
2465 }
2466
2467 // Order inputs by instruction id, but favor constant on the right side.
2468 // This helps GVN for commutative ops.
2469 void OrderInputs() {
2470 DCHECK(IsCommutative());
2471 HInstruction* left = InputAt(0);
2472 HInstruction* right = InputAt(1);
2473 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2474 return;
2475 }
2476 if (OrderInputsWithConstantOnTheRight()) {
2477 return;
2478 }
2479 // Order according to instruction id.
2480 if (left->GetId() > right->GetId()) {
2481 ReplaceInput(right, 0);
2482 ReplaceInput(left, 1);
2483 }
2484 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002485
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002486 bool CanBeMoved() const OVERRIDE { return true; }
2487 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002488 UNUSED(other);
2489 return true;
2490 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002491
Roland Levillain9240d6a2014-10-20 16:47:04 +01002492 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002493 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002494 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002495 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002496
2497 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002498 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2499 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2500 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2501 HLongConstant* y ATTRIBUTE_UNUSED) const {
2502 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2503 return nullptr;
2504 }
2505 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2506 HIntConstant* y ATTRIBUTE_UNUSED) const {
2507 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2508 return nullptr;
2509 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002510
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002511 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002512 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002513 HConstant* GetConstantRight() const;
2514
2515 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002516 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002517 HInstruction* GetLeastConstantLeft() const;
2518
Roland Levillainccc07a92014-09-16 14:48:16 +01002519 DECLARE_INSTRUCTION(BinaryOperation);
2520
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002521 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002522 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2523};
2524
Mark Mendellc4701932015-04-10 13:18:51 -04002525// The comparison bias applies for floating point operations and indicates how NaN
2526// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002527enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002528 kNoBias, // bias is not applicable (i.e. for long operation)
2529 kGtBias, // return 1 for NaN comparisons
2530 kLtBias, // return -1 for NaN comparisons
2531};
2532
Dave Allison20dfc792014-06-16 20:44:29 -07002533class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002534 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002535 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2536 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002537 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002538 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002539
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002540 bool NeedsMaterialization() const { return needs_materialization_; }
2541 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002542
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002543 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002544 // `instruction`, and disregard moves in between.
2545 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002546
Dave Allison20dfc792014-06-16 20:44:29 -07002547 DECLARE_INSTRUCTION(Condition);
2548
2549 virtual IfCondition GetCondition() const = 0;
2550
Mark Mendellc4701932015-04-10 13:18:51 -04002551 virtual IfCondition GetOppositeCondition() const = 0;
2552
Roland Levillain4fa13f62015-07-06 18:11:54 +01002553 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002554
2555 void SetBias(ComparisonBias bias) { bias_ = bias; }
2556
2557 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2558 return bias_ == other->AsCondition()->bias_;
2559 }
2560
Roland Levillain4fa13f62015-07-06 18:11:54 +01002561 bool IsFPConditionTrueIfNaN() const {
2562 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2563 IfCondition if_cond = GetCondition();
2564 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2565 }
2566
2567 bool IsFPConditionFalseIfNaN() const {
2568 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2569 IfCondition if_cond = GetCondition();
2570 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2571 }
2572
Dave Allison20dfc792014-06-16 20:44:29 -07002573 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002574 // For register allocation purposes, returns whether this instruction needs to be
2575 // materialized (that is, not just be in the processor flags).
2576 bool needs_materialization_;
2577
Mark Mendellc4701932015-04-10 13:18:51 -04002578 // Needed if we merge a HCompare into a HCondition.
2579 ComparisonBias bias_;
2580
Dave Allison20dfc792014-06-16 20:44:29 -07002581 DISALLOW_COPY_AND_ASSIGN(HCondition);
2582};
2583
2584// Instruction to check if two inputs are equal to each other.
2585class HEqual : public HCondition {
2586 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002587 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2588 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002589
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002590 bool IsCommutative() const OVERRIDE { return true; }
2591
Roland Levillain9867bc72015-08-05 10:21:34 +01002592 template <typename T> bool Compute(T x, T y) const { return x == y; }
2593
2594 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002595 return GetBlock()->GetGraph()->GetIntConstant(
2596 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002597 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002598 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002599 return GetBlock()->GetGraph()->GetIntConstant(
2600 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002601 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002602
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002603 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002604
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002605 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002606 return kCondEQ;
2607 }
2608
Mark Mendellc4701932015-04-10 13:18:51 -04002609 IfCondition GetOppositeCondition() const OVERRIDE {
2610 return kCondNE;
2611 }
2612
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002613 private:
2614 DISALLOW_COPY_AND_ASSIGN(HEqual);
2615};
2616
Dave Allison20dfc792014-06-16 20:44:29 -07002617class HNotEqual : public HCondition {
2618 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002619 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2620 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002621
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002622 bool IsCommutative() const OVERRIDE { return true; }
2623
Roland Levillain9867bc72015-08-05 10:21:34 +01002624 template <typename T> bool Compute(T x, T y) const { return x != y; }
2625
2626 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002627 return GetBlock()->GetGraph()->GetIntConstant(
2628 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002629 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002630 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002631 return GetBlock()->GetGraph()->GetIntConstant(
2632 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002633 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002634
Dave Allison20dfc792014-06-16 20:44:29 -07002635 DECLARE_INSTRUCTION(NotEqual);
2636
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002637 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002638 return kCondNE;
2639 }
2640
Mark Mendellc4701932015-04-10 13:18:51 -04002641 IfCondition GetOppositeCondition() const OVERRIDE {
2642 return kCondEQ;
2643 }
2644
Dave Allison20dfc792014-06-16 20:44:29 -07002645 private:
2646 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2647};
2648
2649class HLessThan : public HCondition {
2650 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002651 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2652 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002653
Roland Levillain9867bc72015-08-05 10:21:34 +01002654 template <typename T> bool Compute(T x, T y) const { return x < y; }
2655
2656 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002657 return GetBlock()->GetGraph()->GetIntConstant(
2658 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002659 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002660 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002661 return GetBlock()->GetGraph()->GetIntConstant(
2662 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002663 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002664
Dave Allison20dfc792014-06-16 20:44:29 -07002665 DECLARE_INSTRUCTION(LessThan);
2666
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002667 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002668 return kCondLT;
2669 }
2670
Mark Mendellc4701932015-04-10 13:18:51 -04002671 IfCondition GetOppositeCondition() const OVERRIDE {
2672 return kCondGE;
2673 }
2674
Dave Allison20dfc792014-06-16 20:44:29 -07002675 private:
2676 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2677};
2678
2679class HLessThanOrEqual : public HCondition {
2680 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002681 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2682 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002683
Roland Levillain9867bc72015-08-05 10:21:34 +01002684 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2685
2686 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002687 return GetBlock()->GetGraph()->GetIntConstant(
2688 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002689 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002690 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002691 return GetBlock()->GetGraph()->GetIntConstant(
2692 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002693 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002694
Dave Allison20dfc792014-06-16 20:44:29 -07002695 DECLARE_INSTRUCTION(LessThanOrEqual);
2696
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002697 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002698 return kCondLE;
2699 }
2700
Mark Mendellc4701932015-04-10 13:18:51 -04002701 IfCondition GetOppositeCondition() const OVERRIDE {
2702 return kCondGT;
2703 }
2704
Dave Allison20dfc792014-06-16 20:44:29 -07002705 private:
2706 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2707};
2708
2709class HGreaterThan : public HCondition {
2710 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002711 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2712 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002713
Roland Levillain9867bc72015-08-05 10:21:34 +01002714 template <typename T> bool Compute(T x, T y) const { return x > y; }
2715
2716 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002717 return GetBlock()->GetGraph()->GetIntConstant(
2718 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002719 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002720 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721 return GetBlock()->GetGraph()->GetIntConstant(
2722 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002723 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002724
Dave Allison20dfc792014-06-16 20:44:29 -07002725 DECLARE_INSTRUCTION(GreaterThan);
2726
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002727 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002728 return kCondGT;
2729 }
2730
Mark Mendellc4701932015-04-10 13:18:51 -04002731 IfCondition GetOppositeCondition() const OVERRIDE {
2732 return kCondLE;
2733 }
2734
Dave Allison20dfc792014-06-16 20:44:29 -07002735 private:
2736 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2737};
2738
2739class HGreaterThanOrEqual : public HCondition {
2740 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002741 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2742 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002743
Roland Levillain9867bc72015-08-05 10:21:34 +01002744 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2745
2746 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002747 return GetBlock()->GetGraph()->GetIntConstant(
2748 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002749 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002750 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002751 return GetBlock()->GetGraph()->GetIntConstant(
2752 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002753 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002754
Dave Allison20dfc792014-06-16 20:44:29 -07002755 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2756
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002757 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002758 return kCondGE;
2759 }
2760
Mark Mendellc4701932015-04-10 13:18:51 -04002761 IfCondition GetOppositeCondition() const OVERRIDE {
2762 return kCondLT;
2763 }
2764
Dave Allison20dfc792014-06-16 20:44:29 -07002765 private:
2766 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2767};
2768
2769
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002770// Instruction to check how two inputs compare to each other.
2771// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2772class HCompare : public HBinaryOperation {
2773 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002774 HCompare(Primitive::Type type,
2775 HInstruction* first,
2776 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002777 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002778 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002779 : HBinaryOperation(Primitive::kPrimInt,
2780 first,
2781 second,
2782 SideEffectsForArchRuntimeCalls(type),
2783 dex_pc),
2784 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002785 DCHECK_EQ(type, first->GetType());
2786 DCHECK_EQ(type, second->GetType());
2787 }
2788
Roland Levillain9867bc72015-08-05 10:21:34 +01002789 template <typename T>
2790 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002791
Roland Levillain9867bc72015-08-05 10:21:34 +01002792 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002793 return GetBlock()->GetGraph()->GetIntConstant(
2794 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002795 }
2796 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002797 return GetBlock()->GetGraph()->GetIntConstant(
2798 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002799 }
2800
Calin Juravleddb7df22014-11-25 20:56:51 +00002801 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2802 return bias_ == other->AsCompare()->bias_;
2803 }
2804
Mark Mendellc4701932015-04-10 13:18:51 -04002805 ComparisonBias GetBias() const { return bias_; }
2806
Roland Levillain4fa13f62015-07-06 18:11:54 +01002807 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002808
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002809
2810 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2811 // MIPS64 uses a runtime call for FP comparisons.
2812 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2813 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002814
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002815 DECLARE_INSTRUCTION(Compare);
2816
2817 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002818 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002819
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002820 DISALLOW_COPY_AND_ASSIGN(HCompare);
2821};
2822
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002823// A local in the graph. Corresponds to a Dex register.
2824class HLocal : public HTemplateInstruction<0> {
2825 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002826 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002827 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002828
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002829 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002830
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002831 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002832
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002833 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002834 // The Dex register number.
2835 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002836
2837 DISALLOW_COPY_AND_ASSIGN(HLocal);
2838};
2839
2840// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002841class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002842 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002843 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2844 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002845 SetRawInputAt(0, local);
2846 }
2847
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002848 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2849
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002850 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002851
2852 private:
2853 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2854};
2855
2856// Store a value in a given local. This instruction has two inputs: the value
2857// and the local.
2858class HStoreLocal : public HTemplateInstruction<2> {
2859 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002860 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2861 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002862 SetRawInputAt(0, local);
2863 SetRawInputAt(1, value);
2864 }
2865
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002866 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2867
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002868 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002869
2870 private:
2871 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2872};
2873
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002874class HFloatConstant : public HConstant {
2875 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002876 float GetValue() const { return value_; }
2877
David Brazdil77a48ae2015-09-15 12:34:04 +00002878 uint64_t GetValueAsUint64() const OVERRIDE {
2879 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2880 }
2881
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002882 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002883 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002884 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002885 }
2886
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002887 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002888
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002889 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002890 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002891 }
2892 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002893 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002894 }
2895 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002896 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2897 }
2898 bool IsNaN() const {
2899 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002900 }
2901
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002902 DECLARE_INSTRUCTION(FloatConstant);
2903
2904 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002905 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2906 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2907 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2908 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002909
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002910 const float value_;
2911
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002912 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002913 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002914 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002915 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2916};
2917
2918class HDoubleConstant : public HConstant {
2919 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002920 double GetValue() const { return value_; }
2921
David Brazdil77a48ae2015-09-15 12:34:04 +00002922 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2923
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002924 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002925 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002926 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002927 }
2928
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002929 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002930
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002931 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002932 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002933 }
2934 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002935 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002936 }
2937 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002938 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2939 }
2940 bool IsNaN() const {
2941 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002942 }
2943
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002944 DECLARE_INSTRUCTION(DoubleConstant);
2945
2946 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002947 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2948 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2949 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2950 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002951
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002952 const double value_;
2953
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002954 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002955 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002956 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002957 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2958};
2959
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002960enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002961#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002962#include "intrinsics_list.h"
2963 kNone,
2964 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2965#undef INTRINSICS_LIST
2966#undef OPTIMIZING_INTRINSICS
2967};
2968std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2969
Agi Csaki05f20562015-08-19 14:58:14 -07002970enum IntrinsicNeedsEnvironmentOrCache {
2971 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
2972 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07002973};
2974
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002975class HInvoke : public HInstruction {
2976 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002977 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002978
2979 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2980 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07002981 bool NeedsEnvironment() const OVERRIDE {
2982 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
2983 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002984
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002985 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002986 SetRawInputAt(index, argument);
2987 }
2988
Roland Levillain3e3d7332015-04-28 11:00:54 +01002989 // Return the number of arguments. This number can be lower than
2990 // the number of inputs returned by InputCount(), as some invoke
2991 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2992 // inputs at the end of their list of inputs.
2993 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2994
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002995 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002996
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002997
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002998 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002999 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003000
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003001 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3002
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003003 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003004 return intrinsic_;
3005 }
3006
Agi Csaki05f20562015-08-19 14:58:14 -07003007 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003008 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07003009 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003010 }
3011
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003012 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003013 return GetEnvironment()->GetParent() != nullptr;
3014 }
3015
3016 bool CanThrow() const OVERRIDE { return true; }
3017
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003018 DECLARE_INSTRUCTION(Invoke);
3019
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003020 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003021 HInvoke(ArenaAllocator* arena,
3022 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003023 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003024 Primitive::Type return_type,
3025 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003026 uint32_t dex_method_index,
3027 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003028 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003029 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003030 number_of_arguments_(number_of_arguments),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003031 inputs_(number_of_arguments + number_of_other_inputs, arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003032 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003033 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003034 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003035 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07003036 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003037 }
3038
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003039 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
3040 DCHECK_LT(index, InputCount());
3041 return inputs_[index];
3042 }
3043
David Brazdil1abb4192015-02-17 18:33:36 +00003044 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003045 DCHECK_LT(index, InputCount());
3046 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003047 }
3048
Roland Levillain3e3d7332015-04-28 11:00:54 +01003049 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003050 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003051 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003052 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003053 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003054 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003055 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003056
3057 private:
3058 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3059};
3060
Calin Juravle175dc732015-08-25 15:42:32 +01003061class HInvokeUnresolved : public HInvoke {
3062 public:
3063 HInvokeUnresolved(ArenaAllocator* arena,
3064 uint32_t number_of_arguments,
3065 Primitive::Type return_type,
3066 uint32_t dex_pc,
3067 uint32_t dex_method_index,
3068 InvokeType invoke_type)
3069 : HInvoke(arena,
3070 number_of_arguments,
3071 0u /* number_of_other_inputs */,
3072 return_type,
3073 dex_pc,
3074 dex_method_index,
3075 invoke_type) {
3076 }
3077
3078 DECLARE_INSTRUCTION(InvokeUnresolved);
3079
3080 private:
3081 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3082};
3083
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003084class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003085 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003086 // Requirements of this method call regarding the class
3087 // initialization (clinit) check of its declaring class.
3088 enum class ClinitCheckRequirement {
3089 kNone, // Class already initialized.
3090 kExplicit, // Static call having explicit clinit check as last input.
3091 kImplicit, // Static call implicitly requiring a clinit check.
3092 };
3093
Vladimir Marko58155012015-08-19 12:49:41 +00003094 // Determines how to load the target ArtMethod*.
3095 enum class MethodLoadKind {
3096 // Use a String init ArtMethod* loaded from Thread entrypoints.
3097 kStringInit,
3098
3099 // Use the method's own ArtMethod* loaded by the register allocator.
3100 kRecursive,
3101
3102 // Use ArtMethod* at a known address, embed the direct address in the code.
3103 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3104 kDirectAddress,
3105
3106 // Use ArtMethod* at an address that will be known at link time, embed the direct
3107 // address in the code. If the image is relocatable, emit .patch_oat entry.
3108 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3109 // the image relocatable or not.
3110 kDirectAddressWithFixup,
3111
3112 // Load from resoved methods array in the dex cache using a PC-relative load.
3113 // Used when we need to use the dex cache, for example for invoke-static that
3114 // may cause class initialization (the entry may point to a resolution method),
3115 // and we know that we can access the dex cache arrays using a PC-relative load.
3116 kDexCachePcRelative,
3117
3118 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3119 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3120 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3121 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3122 kDexCacheViaMethod,
3123 };
3124
3125 // Determines the location of the code pointer.
3126 enum class CodePtrLocation {
3127 // Recursive call, use local PC-relative call instruction.
3128 kCallSelf,
3129
3130 // Use PC-relative call instruction patched at link time.
3131 // Used for calls within an oat file, boot->boot or app->app.
3132 kCallPCRelative,
3133
3134 // Call to a known target address, embed the direct address in code.
3135 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3136 kCallDirect,
3137
3138 // Call to a target address that will be known at link time, embed the direct
3139 // address in code. If the image is relocatable, emit .patch_oat entry.
3140 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3141 // the image relocatable or not.
3142 kCallDirectWithFixup,
3143
3144 // Use code pointer from the ArtMethod*.
3145 // Used when we don't know the target code. This is also the last-resort-kind used when
3146 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3147 kCallArtMethod,
3148 };
3149
3150 struct DispatchInfo {
3151 const MethodLoadKind method_load_kind;
3152 const CodePtrLocation code_ptr_location;
3153 // The method load data holds
3154 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3155 // Note that there are multiple string init methods, each having its own offset.
3156 // - the method address for kDirectAddress
3157 // - the dex cache arrays offset for kDexCachePcRel.
3158 const uint64_t method_load_data;
3159 const uint64_t direct_code_ptr;
3160 };
3161
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003162 HInvokeStaticOrDirect(ArenaAllocator* arena,
3163 uint32_t number_of_arguments,
3164 Primitive::Type return_type,
3165 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003166 uint32_t method_index,
3167 MethodReference target_method,
3168 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003169 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003170 InvokeType invoke_type,
3171 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003172 : HInvoke(arena,
3173 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003174 // There is one extra argument for the HCurrentMethod node, and
3175 // potentially one other if the clinit check is explicit, and one other
3176 // if the method is a string factory.
3177 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003178 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003179 return_type,
3180 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003181 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003182 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003183 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003184 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003185 target_method_(target_method),
3186 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003187
Calin Juravle641547a2015-04-21 22:08:51 +01003188 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3189 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003190 // We access the method via the dex cache so we can't do an implicit null check.
3191 // TODO: for intrinsics we can generate implicit null checks.
3192 return false;
3193 }
3194
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003195 bool CanBeNull() const OVERRIDE {
3196 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3197 }
3198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003199 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003200 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3201 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3202 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003203 bool NeedsDexCache() const OVERRIDE {
3204 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3205 return !IsRecursive() && !IsStringInit();
3206 }
Vladimir Marko58155012015-08-19 12:49:41 +00003207 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003208 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003209 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3210 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3211 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3212 MethodReference GetTargetMethod() const { return target_method_; }
3213
3214 int32_t GetStringInitOffset() const {
3215 DCHECK(IsStringInit());
3216 return dispatch_info_.method_load_data;
3217 }
3218
3219 uint64_t GetMethodAddress() const {
3220 DCHECK(HasMethodAddress());
3221 return dispatch_info_.method_load_data;
3222 }
3223
3224 uint32_t GetDexCacheArrayOffset() const {
3225 DCHECK(HasPcRelDexCache());
3226 return dispatch_info_.method_load_data;
3227 }
3228
3229 uint64_t GetDirectCodePtr() const {
3230 DCHECK(HasDirectCodePtr());
3231 return dispatch_info_.direct_code_ptr;
3232 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003233
Calin Juravle68ad6492015-08-18 17:08:12 +01003234 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3235
Roland Levillain4c0eb422015-04-24 16:43:49 +01003236 // Is this instruction a call to a static method?
3237 bool IsStatic() const {
3238 return GetInvokeType() == kStatic;
3239 }
3240
Roland Levillain3e3d7332015-04-28 11:00:54 +01003241 // Remove the art::HLoadClass instruction set as last input by
3242 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3243 // the initial art::HClinitCheck instruction (only relevant for
3244 // static calls with explicit clinit check).
3245 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003246 DCHECK(IsStaticWithExplicitClinitCheck());
3247 size_t last_input_index = InputCount() - 1;
3248 HInstruction* last_input = InputAt(last_input_index);
3249 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003250 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003251 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003252 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003253 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3254 DCHECK(IsStaticWithImplicitClinitCheck());
3255 }
3256
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003257 bool IsStringFactoryFor(HFakeString* str) const {
3258 if (!IsStringInit()) return false;
3259 // +1 for the current method.
3260 if (InputCount() == (number_of_arguments_ + 1)) return false;
3261 return InputAt(InputCount() - 1)->AsFakeString() == str;
3262 }
3263
3264 void RemoveFakeStringArgumentAsLastInput() {
3265 DCHECK(IsStringInit());
3266 size_t last_input_index = InputCount() - 1;
3267 HInstruction* last_input = InputAt(last_input_index);
3268 DCHECK(last_input != nullptr);
3269 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3270 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003271 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003272 }
3273
Roland Levillain4c0eb422015-04-24 16:43:49 +01003274 // Is this a call to a static method whose declaring class has an
3275 // explicit intialization check in the graph?
3276 bool IsStaticWithExplicitClinitCheck() const {
3277 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3278 }
3279
3280 // Is this a call to a static method whose declaring class has an
3281 // implicit intialization check requirement?
3282 bool IsStaticWithImplicitClinitCheck() const {
3283 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3284 }
3285
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003286 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003287
Roland Levillain4c0eb422015-04-24 16:43:49 +01003288 protected:
3289 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3290 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3291 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3292 HInstruction* input = input_record.GetInstruction();
3293 // `input` is the last input of a static invoke marked as having
3294 // an explicit clinit check. It must either be:
3295 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3296 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3297 DCHECK(input != nullptr);
3298 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3299 }
3300 return input_record;
3301 }
3302
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003303 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003304 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003305 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003306 // The target method may refer to different dex file or method index than the original
3307 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3308 // in derived class to increase visibility.
3309 MethodReference target_method_;
3310 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003311
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003312 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003313};
3314
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003315class HInvokeVirtual : public HInvoke {
3316 public:
3317 HInvokeVirtual(ArenaAllocator* arena,
3318 uint32_t number_of_arguments,
3319 Primitive::Type return_type,
3320 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003321 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003322 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003323 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003324 vtable_index_(vtable_index) {}
3325
Calin Juravle641547a2015-04-21 22:08:51 +01003326 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003327 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003328 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003329 }
3330
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003331 uint32_t GetVTableIndex() const { return vtable_index_; }
3332
3333 DECLARE_INSTRUCTION(InvokeVirtual);
3334
3335 private:
3336 const uint32_t vtable_index_;
3337
3338 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3339};
3340
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003341class HInvokeInterface : public HInvoke {
3342 public:
3343 HInvokeInterface(ArenaAllocator* arena,
3344 uint32_t number_of_arguments,
3345 Primitive::Type return_type,
3346 uint32_t dex_pc,
3347 uint32_t dex_method_index,
3348 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003349 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003350 imt_index_(imt_index) {}
3351
Calin Juravle641547a2015-04-21 22:08:51 +01003352 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003353 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003354 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003355 }
3356
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003357 uint32_t GetImtIndex() const { return imt_index_; }
3358 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3359
3360 DECLARE_INSTRUCTION(InvokeInterface);
3361
3362 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003363 const uint32_t imt_index_;
3364
3365 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3366};
3367
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003368class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003369 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003370 HNewInstance(HCurrentMethod* current_method,
3371 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003372 uint16_t type_index,
3373 const DexFile& dex_file,
3374 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003375 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003376 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003377 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003378 entrypoint_(entrypoint) {
3379 SetRawInputAt(0, current_method);
3380 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003381
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003382 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003383 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003384
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003385 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003386 bool NeedsEnvironment() const OVERRIDE { return true; }
3387 // It may throw when called on:
3388 // - interfaces
3389 // - abstract/innaccessible/unknown classes
3390 // TODO: optimize when possible.
3391 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003392
Calin Juravle10e244f2015-01-26 18:54:32 +00003393 bool CanBeNull() const OVERRIDE { return false; }
3394
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003395 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3396
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003397 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003398
3399 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003400 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003401 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003402 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003403
3404 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3405};
3406
Roland Levillain88cb1752014-10-20 16:36:47 +01003407class HNeg : public HUnaryOperation {
3408 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003409 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3410 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003411
Roland Levillain9867bc72015-08-05 10:21:34 +01003412 template <typename T> T Compute(T x) const { return -x; }
3413
3414 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003415 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003416 }
3417 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003418 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003419 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003420
Roland Levillain88cb1752014-10-20 16:36:47 +01003421 DECLARE_INSTRUCTION(Neg);
3422
3423 private:
3424 DISALLOW_COPY_AND_ASSIGN(HNeg);
3425};
3426
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003427class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003428 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003429 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003430 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003431 uint32_t dex_pc,
3432 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003433 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003434 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003435 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003436 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003437 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003438 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003439 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003440 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003441 }
3442
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003443 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003444 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003445
3446 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003447 bool NeedsEnvironment() const OVERRIDE { return true; }
3448
Mingyao Yang0c365e62015-03-31 15:09:29 -07003449 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3450 bool CanThrow() const OVERRIDE { return true; }
3451
Calin Juravle10e244f2015-01-26 18:54:32 +00003452 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003453
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003454 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3455
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003456 DECLARE_INSTRUCTION(NewArray);
3457
3458 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003459 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003460 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003461 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003462
3463 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3464};
3465
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003466class HAdd : public HBinaryOperation {
3467 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003468 HAdd(Primitive::Type result_type,
3469 HInstruction* left,
3470 HInstruction* right,
3471 uint32_t dex_pc = kNoDexPc)
3472 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003473
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003474 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003475
Roland Levillain9867bc72015-08-05 10:21:34 +01003476 template <typename T> T Compute(T x, T y) const { return x + y; }
3477
3478 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003479 return GetBlock()->GetGraph()->GetIntConstant(
3480 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003481 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003482 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003483 return GetBlock()->GetGraph()->GetLongConstant(
3484 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003485 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003486
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003487 DECLARE_INSTRUCTION(Add);
3488
3489 private:
3490 DISALLOW_COPY_AND_ASSIGN(HAdd);
3491};
3492
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003493class HSub : public HBinaryOperation {
3494 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003495 HSub(Primitive::Type result_type,
3496 HInstruction* left,
3497 HInstruction* right,
3498 uint32_t dex_pc = kNoDexPc)
3499 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003500
Roland Levillain9867bc72015-08-05 10:21:34 +01003501 template <typename T> T Compute(T x, T y) const { return x - y; }
3502
3503 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003504 return GetBlock()->GetGraph()->GetIntConstant(
3505 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003506 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003507 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003508 return GetBlock()->GetGraph()->GetLongConstant(
3509 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003510 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003511
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003512 DECLARE_INSTRUCTION(Sub);
3513
3514 private:
3515 DISALLOW_COPY_AND_ASSIGN(HSub);
3516};
3517
Calin Juravle34bacdf2014-10-07 20:23:36 +01003518class HMul : public HBinaryOperation {
3519 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003520 HMul(Primitive::Type result_type,
3521 HInstruction* left,
3522 HInstruction* right,
3523 uint32_t dex_pc = kNoDexPc)
3524 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003525
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003526 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003527
Roland Levillain9867bc72015-08-05 10:21:34 +01003528 template <typename T> T Compute(T x, T y) const { return x * y; }
3529
3530 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003531 return GetBlock()->GetGraph()->GetIntConstant(
3532 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003533 }
3534 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003535 return GetBlock()->GetGraph()->GetLongConstant(
3536 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003537 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003538
3539 DECLARE_INSTRUCTION(Mul);
3540
3541 private:
3542 DISALLOW_COPY_AND_ASSIGN(HMul);
3543};
3544
Calin Juravle7c4954d2014-10-28 16:57:40 +00003545class HDiv : public HBinaryOperation {
3546 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003547 HDiv(Primitive::Type result_type,
3548 HInstruction* left,
3549 HInstruction* right,
3550 uint32_t dex_pc)
3551 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003552
Roland Levillain9867bc72015-08-05 10:21:34 +01003553 template <typename T>
3554 T Compute(T x, T y) const {
3555 // Our graph structure ensures we never have 0 for `y` during
3556 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003557 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003558 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003559 return (y == -1) ? -x : x / y;
3560 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003561
Roland Levillain9867bc72015-08-05 10:21:34 +01003562 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003563 return GetBlock()->GetGraph()->GetIntConstant(
3564 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003565 }
3566 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003567 return GetBlock()->GetGraph()->GetLongConstant(
3568 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003569 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003570
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003571 static SideEffects SideEffectsForArchRuntimeCalls() {
3572 // The generated code can use a runtime call.
3573 return SideEffects::CanTriggerGC();
3574 }
3575
Calin Juravle7c4954d2014-10-28 16:57:40 +00003576 DECLARE_INSTRUCTION(Div);
3577
3578 private:
3579 DISALLOW_COPY_AND_ASSIGN(HDiv);
3580};
3581
Calin Juravlebacfec32014-11-14 15:54:36 +00003582class HRem : public HBinaryOperation {
3583 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003584 HRem(Primitive::Type result_type,
3585 HInstruction* left,
3586 HInstruction* right,
3587 uint32_t dex_pc)
3588 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003589
Roland Levillain9867bc72015-08-05 10:21:34 +01003590 template <typename T>
3591 T Compute(T x, T y) const {
3592 // Our graph structure ensures we never have 0 for `y` during
3593 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003594 DCHECK_NE(y, 0);
3595 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3596 return (y == -1) ? 0 : x % y;
3597 }
3598
Roland Levillain9867bc72015-08-05 10:21:34 +01003599 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003600 return GetBlock()->GetGraph()->GetIntConstant(
3601 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003602 }
3603 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003604 return GetBlock()->GetGraph()->GetLongConstant(
3605 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003606 }
3607
Calin Juravlebacfec32014-11-14 15:54:36 +00003608
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003609 static SideEffects SideEffectsForArchRuntimeCalls() {
3610 return SideEffects::CanTriggerGC();
3611 }
3612
Calin Juravlebacfec32014-11-14 15:54:36 +00003613 DECLARE_INSTRUCTION(Rem);
3614
3615 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003616 DISALLOW_COPY_AND_ASSIGN(HRem);
3617};
3618
Calin Juravled0d48522014-11-04 16:40:20 +00003619class HDivZeroCheck : public HExpression<1> {
3620 public:
3621 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003622 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003623 SetRawInputAt(0, value);
3624 }
3625
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003626 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3627
Calin Juravled0d48522014-11-04 16:40:20 +00003628 bool CanBeMoved() const OVERRIDE { return true; }
3629
3630 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3631 UNUSED(other);
3632 return true;
3633 }
3634
3635 bool NeedsEnvironment() const OVERRIDE { return true; }
3636 bool CanThrow() const OVERRIDE { return true; }
3637
Calin Juravled0d48522014-11-04 16:40:20 +00003638 DECLARE_INSTRUCTION(DivZeroCheck);
3639
3640 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003641 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3642};
3643
Calin Juravle9aec02f2014-11-18 23:06:35 +00003644class HShl : public HBinaryOperation {
3645 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003646 HShl(Primitive::Type result_type,
3647 HInstruction* left,
3648 HInstruction* right,
3649 uint32_t dex_pc = kNoDexPc)
3650 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003651
Roland Levillain9867bc72015-08-05 10:21:34 +01003652 template <typename T, typename U, typename V>
3653 T Compute(T x, U y, V max_shift_value) const {
3654 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3655 "V is not the unsigned integer type corresponding to T");
3656 return x << (y & max_shift_value);
3657 }
3658
3659 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3660 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003661 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003662 }
3663 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3664 // case is handled as `x << static_cast<int>(y)`.
3665 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3666 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003667 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003668 }
3669 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3670 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003671 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003672 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003673
3674 DECLARE_INSTRUCTION(Shl);
3675
3676 private:
3677 DISALLOW_COPY_AND_ASSIGN(HShl);
3678};
3679
3680class HShr : public HBinaryOperation {
3681 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003682 HShr(Primitive::Type result_type,
3683 HInstruction* left,
3684 HInstruction* right,
3685 uint32_t dex_pc = kNoDexPc)
3686 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003687
Roland Levillain9867bc72015-08-05 10:21:34 +01003688 template <typename T, typename U, typename V>
3689 T Compute(T x, U y, V max_shift_value) const {
3690 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3691 "V is not the unsigned integer type corresponding to T");
3692 return x >> (y & max_shift_value);
3693 }
3694
3695 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3696 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003697 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003698 }
3699 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3700 // case is handled as `x >> static_cast<int>(y)`.
3701 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3702 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003703 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003704 }
3705 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3706 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003707 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003708 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003709
3710 DECLARE_INSTRUCTION(Shr);
3711
3712 private:
3713 DISALLOW_COPY_AND_ASSIGN(HShr);
3714};
3715
3716class HUShr : public HBinaryOperation {
3717 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003718 HUShr(Primitive::Type result_type,
3719 HInstruction* left,
3720 HInstruction* right,
3721 uint32_t dex_pc = kNoDexPc)
3722 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003723
Roland Levillain9867bc72015-08-05 10:21:34 +01003724 template <typename T, typename U, typename V>
3725 T Compute(T x, U y, V max_shift_value) const {
3726 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3727 "V is not the unsigned integer type corresponding to T");
3728 V ux = static_cast<V>(x);
3729 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003730 }
3731
Roland Levillain9867bc72015-08-05 10:21:34 +01003732 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3733 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003734 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003735 }
3736 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3737 // case is handled as `x >>> static_cast<int>(y)`.
3738 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3739 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003740 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003741 }
3742 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3743 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003744 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003745 }
3746
3747 DECLARE_INSTRUCTION(UShr);
3748
3749 private:
3750 DISALLOW_COPY_AND_ASSIGN(HUShr);
3751};
3752
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003753class HAnd : public HBinaryOperation {
3754 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003755 HAnd(Primitive::Type result_type,
3756 HInstruction* left,
3757 HInstruction* right,
3758 uint32_t dex_pc = kNoDexPc)
3759 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003760
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003761 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003762
Roland Levillain9867bc72015-08-05 10:21:34 +01003763 template <typename T, typename U>
3764 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3765
3766 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003767 return GetBlock()->GetGraph()->GetIntConstant(
3768 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003769 }
3770 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003771 return GetBlock()->GetGraph()->GetLongConstant(
3772 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003773 }
3774 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003775 return GetBlock()->GetGraph()->GetLongConstant(
3776 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003777 }
3778 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003779 return GetBlock()->GetGraph()->GetLongConstant(
3780 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003781 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003782
3783 DECLARE_INSTRUCTION(And);
3784
3785 private:
3786 DISALLOW_COPY_AND_ASSIGN(HAnd);
3787};
3788
3789class HOr : public HBinaryOperation {
3790 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003791 HOr(Primitive::Type result_type,
3792 HInstruction* left,
3793 HInstruction* right,
3794 uint32_t dex_pc = kNoDexPc)
3795 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003796
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003797 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003798
Roland Levillain9867bc72015-08-05 10:21:34 +01003799 template <typename T, typename U>
3800 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3801
3802 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003803 return GetBlock()->GetGraph()->GetIntConstant(
3804 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003805 }
3806 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003807 return GetBlock()->GetGraph()->GetLongConstant(
3808 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003809 }
3810 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003811 return GetBlock()->GetGraph()->GetLongConstant(
3812 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003813 }
3814 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003815 return GetBlock()->GetGraph()->GetLongConstant(
3816 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003817 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003818
3819 DECLARE_INSTRUCTION(Or);
3820
3821 private:
3822 DISALLOW_COPY_AND_ASSIGN(HOr);
3823};
3824
3825class HXor : public HBinaryOperation {
3826 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003827 HXor(Primitive::Type result_type,
3828 HInstruction* left,
3829 HInstruction* right,
3830 uint32_t dex_pc = kNoDexPc)
3831 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003832
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003833 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003834
Roland Levillain9867bc72015-08-05 10:21:34 +01003835 template <typename T, typename U>
3836 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3837
3838 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003839 return GetBlock()->GetGraph()->GetIntConstant(
3840 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003841 }
3842 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003843 return GetBlock()->GetGraph()->GetLongConstant(
3844 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003845 }
3846 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003847 return GetBlock()->GetGraph()->GetLongConstant(
3848 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003849 }
3850 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003851 return GetBlock()->GetGraph()->GetLongConstant(
3852 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003853 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003854
3855 DECLARE_INSTRUCTION(Xor);
3856
3857 private:
3858 DISALLOW_COPY_AND_ASSIGN(HXor);
3859};
3860
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003861// The value of a parameter in this method. Its location depends on
3862// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003863class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003864 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003865 HParameterValue(uint8_t index,
3866 Primitive::Type parameter_type,
3867 bool is_this = false)
3868 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003869 index_(index),
3870 is_this_(is_this),
3871 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003872
3873 uint8_t GetIndex() const { return index_; }
3874
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003875 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3876 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003877
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003878 bool IsThis() const { return is_this_; }
3879
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003880 DECLARE_INSTRUCTION(ParameterValue);
3881
3882 private:
3883 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003884 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003885 const uint8_t index_;
3886
Calin Juravle10e244f2015-01-26 18:54:32 +00003887 // Whether or not the parameter value corresponds to 'this' argument.
3888 const bool is_this_;
3889
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003890 bool can_be_null_;
3891
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003892 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3893};
3894
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003895class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003896 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003897 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3898 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003899
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003900 bool CanBeMoved() const OVERRIDE { return true; }
3901 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003902 UNUSED(other);
3903 return true;
3904 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003905
Roland Levillain9867bc72015-08-05 10:21:34 +01003906 template <typename T> T Compute(T x) const { return ~x; }
3907
3908 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003909 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003910 }
3911 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003912 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003913 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003914
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003915 DECLARE_INSTRUCTION(Not);
3916
3917 private:
3918 DISALLOW_COPY_AND_ASSIGN(HNot);
3919};
3920
David Brazdil66d126e2015-04-03 16:02:44 +01003921class HBooleanNot : public HUnaryOperation {
3922 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003923 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3924 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003925
3926 bool CanBeMoved() const OVERRIDE { return true; }
3927 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3928 UNUSED(other);
3929 return true;
3930 }
3931
Roland Levillain9867bc72015-08-05 10:21:34 +01003932 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003933 DCHECK(IsUint<1>(x));
3934 return !x;
3935 }
3936
Roland Levillain9867bc72015-08-05 10:21:34 +01003937 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003938 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003939 }
3940 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3941 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003942 UNREACHABLE();
3943 }
3944
3945 DECLARE_INSTRUCTION(BooleanNot);
3946
3947 private:
3948 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3949};
3950
Roland Levillaindff1f282014-11-05 14:15:05 +00003951class HTypeConversion : public HExpression<1> {
3952 public:
3953 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003954 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003955 : HExpression(result_type,
3956 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
3957 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003958 SetRawInputAt(0, input);
3959 DCHECK_NE(input->GetType(), result_type);
3960 }
3961
3962 HInstruction* GetInput() const { return InputAt(0); }
3963 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3964 Primitive::Type GetResultType() const { return GetType(); }
3965
Roland Levillain624279f2014-12-04 11:54:28 +00003966 // Required by the x86 and ARM code generators when producing calls
3967 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003968
Roland Levillaindff1f282014-11-05 14:15:05 +00003969 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003970 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003971
Mark Mendelle82549b2015-05-06 10:55:34 -04003972 // Try to statically evaluate the conversion and return a HConstant
3973 // containing the result. If the input cannot be converted, return nullptr.
3974 HConstant* TryStaticEvaluation() const;
3975
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003976 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3977 Primitive::Type result_type) {
3978 // Some architectures may not require the 'GC' side effects, but at this point
3979 // in the compilation process we do not know what architecture we will
3980 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003981 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3982 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003983 return SideEffects::CanTriggerGC();
3984 }
3985 return SideEffects::None();
3986 }
3987
Roland Levillaindff1f282014-11-05 14:15:05 +00003988 DECLARE_INSTRUCTION(TypeConversion);
3989
3990 private:
3991 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3992};
3993
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003994static constexpr uint32_t kNoRegNumber = -1;
3995
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003996class HPhi : public HInstruction {
3997 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003998 HPhi(ArenaAllocator* arena,
3999 uint32_t reg_number,
4000 size_t number_of_inputs,
4001 Primitive::Type type,
4002 uint32_t dex_pc = kNoDexPc)
4003 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004004 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004005 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004006 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004007 is_live_(false),
4008 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004009 }
4010
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004011 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4012 static Primitive::Type ToPhiType(Primitive::Type type) {
4013 switch (type) {
4014 case Primitive::kPrimBoolean:
4015 case Primitive::kPrimByte:
4016 case Primitive::kPrimShort:
4017 case Primitive::kPrimChar:
4018 return Primitive::kPrimInt;
4019 default:
4020 return type;
4021 }
4022 }
4023
David Brazdilffee3d32015-07-06 11:48:53 +01004024 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4025
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004026 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004027
4028 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004029 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004030
Calin Juravle10e244f2015-01-26 18:54:32 +00004031 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004032 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004033
Calin Juravle10e244f2015-01-26 18:54:32 +00004034 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4035 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4036
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004037 uint32_t GetRegNumber() const { return reg_number_; }
4038
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004039 void SetDead() { is_live_ = false; }
4040 void SetLive() { is_live_ = true; }
4041 bool IsDead() const { return !is_live_; }
4042 bool IsLive() const { return is_live_; }
4043
David Brazdil77a48ae2015-09-15 12:34:04 +00004044 bool IsVRegEquivalentOf(HInstruction* other) const {
4045 return other != nullptr
4046 && other->IsPhi()
4047 && other->AsPhi()->GetBlock() == GetBlock()
4048 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4049 }
4050
Calin Juravlea4f88312015-04-16 12:57:19 +01004051 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4052 // An equivalent phi is a phi having the same dex register and type.
4053 // It assumes that phis with the same dex register are adjacent.
4054 HPhi* GetNextEquivalentPhiWithSameType() {
4055 HInstruction* next = GetNext();
4056 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4057 if (next->GetType() == GetType()) {
4058 return next->AsPhi();
4059 }
4060 next = next->GetNext();
4061 }
4062 return nullptr;
4063 }
4064
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004065 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004066
David Brazdil1abb4192015-02-17 18:33:36 +00004067 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004068 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
4069 DCHECK_LE(index, InputCount());
4070 return inputs_[index];
4071 }
David Brazdil1abb4192015-02-17 18:33:36 +00004072
4073 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004074 DCHECK_LE(index, InputCount());
4075 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004076 }
4077
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004078 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004079 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004080 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004081 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004082 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004083 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004084
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004085 DISALLOW_COPY_AND_ASSIGN(HPhi);
4086};
4087
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004088class HNullCheck : public HExpression<1> {
4089 public:
4090 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004091 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004092 SetRawInputAt(0, value);
4093 }
4094
Calin Juravle10e244f2015-01-26 18:54:32 +00004095 bool CanBeMoved() const OVERRIDE { return true; }
4096 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004097 UNUSED(other);
4098 return true;
4099 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004100
Calin Juravle10e244f2015-01-26 18:54:32 +00004101 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004102
Calin Juravle10e244f2015-01-26 18:54:32 +00004103 bool CanThrow() const OVERRIDE { return true; }
4104
4105 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004106
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004107
4108 DECLARE_INSTRUCTION(NullCheck);
4109
4110 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004111 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4112};
4113
4114class FieldInfo : public ValueObject {
4115 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004116 FieldInfo(MemberOffset field_offset,
4117 Primitive::Type field_type,
4118 bool is_volatile,
4119 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004120 const DexFile& dex_file,
4121 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004122 : field_offset_(field_offset),
4123 field_type_(field_type),
4124 is_volatile_(is_volatile),
4125 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004126 dex_file_(dex_file),
4127 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004128
4129 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004130 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004131 uint32_t GetFieldIndex() const { return index_; }
4132 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004133 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004134 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004135
4136 private:
4137 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004138 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004139 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004140 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004141 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004142 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004143};
4144
4145class HInstanceFieldGet : public HExpression<1> {
4146 public:
4147 HInstanceFieldGet(HInstruction* value,
4148 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004149 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004150 bool is_volatile,
4151 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004152 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004153 Handle<mirror::DexCache> dex_cache,
4154 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004155 : HExpression(
4156 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004157 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004158 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004159 SetRawInputAt(0, value);
4160 }
4161
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004162 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004163
4164 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4165 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4166 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004167 }
4168
Calin Juravle641547a2015-04-21 22:08:51 +01004169 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4170 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004171 }
4172
4173 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004174 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4175 }
4176
Calin Juravle52c48962014-12-16 17:02:57 +00004177 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004178 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004179 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004180 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004181
4182 DECLARE_INSTRUCTION(InstanceFieldGet);
4183
4184 private:
4185 const FieldInfo field_info_;
4186
4187 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4188};
4189
4190class HInstanceFieldSet : public HTemplateInstruction<2> {
4191 public:
4192 HInstanceFieldSet(HInstruction* object,
4193 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004194 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004195 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004196 bool is_volatile,
4197 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004198 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004199 Handle<mirror::DexCache> dex_cache,
4200 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004201 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004202 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004203 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004204 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004205 SetRawInputAt(0, object);
4206 SetRawInputAt(1, value);
4207 }
4208
Calin Juravle641547a2015-04-21 22:08:51 +01004209 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4210 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004211 }
4212
Calin Juravle52c48962014-12-16 17:02:57 +00004213 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004214 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004215 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004216 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004217 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004218 bool GetValueCanBeNull() const { return value_can_be_null_; }
4219 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004220
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004221 DECLARE_INSTRUCTION(InstanceFieldSet);
4222
4223 private:
4224 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004225 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004226
4227 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4228};
4229
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004230class HArrayGet : public HExpression<2> {
4231 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004232 HArrayGet(HInstruction* array,
4233 HInstruction* index,
4234 Primitive::Type type,
4235 uint32_t dex_pc = kNoDexPc)
4236 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004237 SetRawInputAt(0, array);
4238 SetRawInputAt(1, index);
4239 }
4240
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004241 bool CanBeMoved() const OVERRIDE { return true; }
4242 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004243 UNUSED(other);
4244 return true;
4245 }
Calin Juravle641547a2015-04-21 22:08:51 +01004246 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4247 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004248 // TODO: We can be smarter here.
4249 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4250 // which generates the implicit null check. There are cases when these can be removed
4251 // to produce better code. If we ever add optimizations to do so we should allow an
4252 // implicit check here (as long as the address falls in the first page).
4253 return false;
4254 }
4255
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004256 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004257
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004258 HInstruction* GetArray() const { return InputAt(0); }
4259 HInstruction* GetIndex() const { return InputAt(1); }
4260
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004261 DECLARE_INSTRUCTION(ArrayGet);
4262
4263 private:
4264 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4265};
4266
4267class HArraySet : public HTemplateInstruction<3> {
4268 public:
4269 HArraySet(HInstruction* array,
4270 HInstruction* index,
4271 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004272 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004273 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004274 : HTemplateInstruction(
4275 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004276 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004277 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004278 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4279 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004280 SetRawInputAt(0, array);
4281 SetRawInputAt(1, index);
4282 SetRawInputAt(2, value);
4283 }
4284
Calin Juravle77520bc2015-01-12 18:45:46 +00004285 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004286 // We currently always call a runtime method to catch array store
4287 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004288 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004289 }
4290
Mingyao Yang81014cb2015-06-02 03:16:27 -07004291 // Can throw ArrayStoreException.
4292 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4293
Calin Juravle641547a2015-04-21 22:08:51 +01004294 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4295 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004296 // TODO: Same as for ArrayGet.
4297 return false;
4298 }
4299
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004300 void ClearNeedsTypeCheck() {
4301 needs_type_check_ = false;
4302 }
4303
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004304 void ClearValueCanBeNull() {
4305 value_can_be_null_ = false;
4306 }
4307
4308 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004309 bool NeedsTypeCheck() const { return needs_type_check_; }
4310
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004311 HInstruction* GetArray() const { return InputAt(0); }
4312 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004313 HInstruction* GetValue() const { return InputAt(2); }
4314
4315 Primitive::Type GetComponentType() const {
4316 // The Dex format does not type floating point index operations. Since the
4317 // `expected_component_type_` is set during building and can therefore not
4318 // be correct, we also check what is the value type. If it is a floating
4319 // point type, we must use that type.
4320 Primitive::Type value_type = GetValue()->GetType();
4321 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4322 ? value_type
4323 : expected_component_type_;
4324 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004325
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004326 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4327 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4328 }
4329
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004330 DECLARE_INSTRUCTION(ArraySet);
4331
4332 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004333 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004334 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004335 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004336
4337 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4338};
4339
4340class HArrayLength : public HExpression<1> {
4341 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004342 explicit HArrayLength(HInstruction* array, uint32_t dex_pc = kNoDexPc)
4343 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004344 // Note that arrays do not change length, so the instruction does not
4345 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004346 SetRawInputAt(0, array);
4347 }
4348
Calin Juravle77520bc2015-01-12 18:45:46 +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 }
Calin Juravle641547a2015-04-21 22:08:51 +01004354 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4355 return obj == InputAt(0);
4356 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004357
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004358 DECLARE_INSTRUCTION(ArrayLength);
4359
4360 private:
4361 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4362};
4363
4364class HBoundsCheck : public HExpression<2> {
4365 public:
4366 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004367 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004368 DCHECK(index->GetType() == Primitive::kPrimInt);
4369 SetRawInputAt(0, index);
4370 SetRawInputAt(1, length);
4371 }
4372
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004373 bool CanBeMoved() const OVERRIDE { return true; }
4374 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004375 UNUSED(other);
4376 return true;
4377 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004378
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004379 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004380
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004381 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004382
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004383
4384 DECLARE_INSTRUCTION(BoundsCheck);
4385
4386 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004387 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4388};
4389
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004390/**
4391 * Some DEX instructions are folded into multiple HInstructions that need
4392 * to stay live until the last HInstruction. This class
4393 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004394 * HInstruction stays live. `index` represents the stack location index of the
4395 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004396 */
4397class HTemporary : public HTemplateInstruction<0> {
4398 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004399 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4400 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004401
4402 size_t GetIndex() const { return index_; }
4403
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004404 Primitive::Type GetType() const OVERRIDE {
4405 // The previous instruction is the one that will be stored in the temporary location.
4406 DCHECK(GetPrevious() != nullptr);
4407 return GetPrevious()->GetType();
4408 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004409
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004410 DECLARE_INSTRUCTION(Temporary);
4411
4412 private:
4413 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004414 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4415};
4416
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004417class HSuspendCheck : public HTemplateInstruction<0> {
4418 public:
4419 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004420 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004421
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004422 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004423 return true;
4424 }
4425
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004426 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4427 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004428
4429 DECLARE_INSTRUCTION(SuspendCheck);
4430
4431 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004432 // Only used for code generation, in order to share the same slow path between back edges
4433 // of a same loop.
4434 SlowPathCode* slow_path_;
4435
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004436 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4437};
4438
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004439/**
4440 * Instruction to load a Class object.
4441 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004442class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004443 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004444 HLoadClass(HCurrentMethod* current_method,
4445 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004446 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004447 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004448 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004449 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004450 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004451 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004452 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004453 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004454 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004455 SetRawInputAt(0, current_method);
4456 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004457
4458 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004459
4460 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4461 return other->AsLoadClass()->type_index_ == type_index_;
4462 }
4463
4464 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4465
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004466 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004467 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004468 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004469
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004470 bool NeedsEnvironment() const OVERRIDE {
4471 // Will call runtime and load the class if the class is not loaded yet.
4472 // TODO: finer grain decision.
4473 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004474 }
4475
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004476 bool MustGenerateClinitCheck() const {
4477 return generate_clinit_check_;
4478 }
4479
Calin Juravle0ba218d2015-05-19 18:46:01 +01004480 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4481 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004482 }
4483
4484 bool CanCallRuntime() const {
4485 return MustGenerateClinitCheck() || !is_referrers_class_;
4486 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004487
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004488 bool CanThrow() const OVERRIDE {
4489 // May call runtime and and therefore can throw.
4490 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004491 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004492 }
4493
Calin Juravleacf735c2015-02-12 15:25:22 +00004494 ReferenceTypeInfo GetLoadedClassRTI() {
4495 return loaded_class_rti_;
4496 }
4497
4498 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4499 // Make sure we only set exact types (the loaded class should never be merged).
4500 DCHECK(rti.IsExact());
4501 loaded_class_rti_ = rti;
4502 }
4503
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004504 const DexFile& GetDexFile() { return dex_file_; }
4505
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004506 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4507
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004508 static SideEffects SideEffectsForArchRuntimeCalls() {
4509 return SideEffects::CanTriggerGC();
4510 }
4511
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004512 DECLARE_INSTRUCTION(LoadClass);
4513
4514 private:
4515 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004516 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004517 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004518 // Whether this instruction must generate the initialization check.
4519 // Used for code generation.
4520 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004521
Calin Juravleacf735c2015-02-12 15:25:22 +00004522 ReferenceTypeInfo loaded_class_rti_;
4523
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004524 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4525};
4526
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004527class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004528 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004529 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004530 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4531 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004532 SetRawInputAt(0, current_method);
4533 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004534
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004535 bool CanBeMoved() const OVERRIDE { return true; }
4536
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004537 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4538 return other->AsLoadString()->string_index_ == string_index_;
4539 }
4540
4541 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4542
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004543 uint32_t GetStringIndex() const { return string_index_; }
4544
4545 // TODO: Can we deopt or debug when we resolve a string?
4546 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004547 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004548 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004549
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004550 static SideEffects SideEffectsForArchRuntimeCalls() {
4551 return SideEffects::CanTriggerGC();
4552 }
4553
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004554 DECLARE_INSTRUCTION(LoadString);
4555
4556 private:
4557 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004558
4559 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4560};
4561
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004562/**
4563 * Performs an initialization check on its Class object input.
4564 */
4565class HClinitCheck : public HExpression<1> {
4566 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004567 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004568 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004569 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004570 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4571 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004572 SetRawInputAt(0, constant);
4573 }
4574
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004575 bool CanBeMoved() const OVERRIDE { return true; }
4576 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4577 UNUSED(other);
4578 return true;
4579 }
4580
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004581 bool NeedsEnvironment() const OVERRIDE {
4582 // May call runtime to initialize the class.
4583 return true;
4584 }
4585
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004586
4587 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4588
4589 DECLARE_INSTRUCTION(ClinitCheck);
4590
4591 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004592 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4593};
4594
4595class HStaticFieldGet : public HExpression<1> {
4596 public:
4597 HStaticFieldGet(HInstruction* cls,
4598 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004599 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004600 bool is_volatile,
4601 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004602 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004603 Handle<mirror::DexCache> dex_cache,
4604 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004605 : HExpression(
4606 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004607 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004608 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004609 SetRawInputAt(0, cls);
4610 }
4611
Calin Juravle52c48962014-12-16 17:02:57 +00004612
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004613 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004614
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004615 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004616 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4617 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004618 }
4619
4620 size_t ComputeHashCode() const OVERRIDE {
4621 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4622 }
4623
Calin Juravle52c48962014-12-16 17:02:57 +00004624 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004625 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4626 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004627 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004628
4629 DECLARE_INSTRUCTION(StaticFieldGet);
4630
4631 private:
4632 const FieldInfo field_info_;
4633
4634 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4635};
4636
4637class HStaticFieldSet : public HTemplateInstruction<2> {
4638 public:
4639 HStaticFieldSet(HInstruction* cls,
4640 HInstruction* value,
4641 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004642 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004643 bool is_volatile,
4644 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004645 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004646 Handle<mirror::DexCache> dex_cache,
4647 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004648 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004649 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004650 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004651 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004652 SetRawInputAt(0, cls);
4653 SetRawInputAt(1, value);
4654 }
4655
Calin Juravle52c48962014-12-16 17:02:57 +00004656 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004657 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4658 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004659 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004660
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004661 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004662 bool GetValueCanBeNull() const { return value_can_be_null_; }
4663 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004664
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004665 DECLARE_INSTRUCTION(StaticFieldSet);
4666
4667 private:
4668 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004669 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004670
4671 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4672};
4673
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004674// Implement the move-exception DEX instruction.
4675class HLoadException : public HExpression<0> {
4676 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004677 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4678 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004679
David Brazdilbbd733e2015-08-18 17:48:17 +01004680 bool CanBeNull() const OVERRIDE { return false; }
4681
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004682 DECLARE_INSTRUCTION(LoadException);
4683
4684 private:
4685 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4686};
4687
David Brazdilcb1c0552015-08-04 16:22:25 +01004688// Implicit part of move-exception which clears thread-local exception storage.
4689// Must not be removed because the runtime expects the TLS to get cleared.
4690class HClearException : public HTemplateInstruction<0> {
4691 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004692 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4693 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004694
4695 DECLARE_INSTRUCTION(ClearException);
4696
4697 private:
4698 DISALLOW_COPY_AND_ASSIGN(HClearException);
4699};
4700
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004701class HThrow : public HTemplateInstruction<1> {
4702 public:
4703 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004704 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004705 SetRawInputAt(0, exception);
4706 }
4707
4708 bool IsControlFlow() const OVERRIDE { return true; }
4709
4710 bool NeedsEnvironment() const OVERRIDE { return true; }
4711
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004712 bool CanThrow() const OVERRIDE { return true; }
4713
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004714
4715 DECLARE_INSTRUCTION(Throw);
4716
4717 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004718 DISALLOW_COPY_AND_ASSIGN(HThrow);
4719};
4720
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004721/**
4722 * Implementation strategies for the code generator of a HInstanceOf
4723 * or `HCheckCast`.
4724 */
4725enum class TypeCheckKind {
4726 kExactCheck, // Can do a single class compare.
4727 kClassHierarchyCheck, // Can just walk the super class chain.
4728 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
4729 kInterfaceCheck, // No optimization yet when checking against an interface.
4730 kArrayObjectCheck, // Can just check if the array is not primitive.
4731 kArrayCheck // No optimization yet when checking against a generic array.
4732};
4733
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004734class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004735 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004736 HInstanceOf(HInstruction* object,
4737 HLoadClass* constant,
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004738 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004739 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004740 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004741 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004742 dex_pc),
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004743 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004744 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004745 SetRawInputAt(0, object);
4746 SetRawInputAt(1, constant);
4747 }
4748
4749 bool CanBeMoved() const OVERRIDE { return true; }
4750
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004751 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004752 return true;
4753 }
4754
4755 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004756 return false;
4757 }
4758
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004759 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
4760
4761 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004762
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004763 // Used only in code generation.
4764 bool MustDoNullCheck() const { return must_do_null_check_; }
4765 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4766
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004767 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
4768 return (check_kind == TypeCheckKind::kExactCheck)
4769 ? SideEffects::None()
4770 // Mips currently does runtime calls for any other checks.
4771 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004772 }
4773
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004774 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004775
4776 private:
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004777 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004778 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004779
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004780 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4781};
4782
Calin Juravleb1498f62015-02-16 13:13:29 +00004783class HBoundType : public HExpression<1> {
4784 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004785 // Constructs an HBoundType with the given upper_bound.
4786 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004787 HBoundType(HInstruction* input,
4788 ReferenceTypeInfo upper_bound,
4789 bool upper_can_be_null,
4790 uint32_t dex_pc = kNoDexPc)
4791 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004792 upper_bound_(upper_bound),
4793 upper_can_be_null_(upper_can_be_null),
4794 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004795 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004796 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004797 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004798 }
4799
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004800 // GetUpper* should only be used in reference type propagation.
4801 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4802 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004803
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004804 void SetCanBeNull(bool can_be_null) {
4805 DCHECK(upper_can_be_null_ || !can_be_null);
4806 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004807 }
4808
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004809 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4810
Calin Juravleb1498f62015-02-16 13:13:29 +00004811 DECLARE_INSTRUCTION(BoundType);
4812
4813 private:
4814 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004815 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4816 // It is used to bound the type in cases like:
4817 // if (x instanceof ClassX) {
4818 // // uper_bound_ will be ClassX
4819 // }
4820 const ReferenceTypeInfo upper_bound_;
4821 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4822 // is false then can_be_null_ cannot be true).
4823 const bool upper_can_be_null_;
4824 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004825
4826 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4827};
4828
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004829class HCheckCast : public HTemplateInstruction<2> {
4830 public:
4831 HCheckCast(HInstruction* object,
4832 HLoadClass* constant,
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004833 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004834 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004836 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004837 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004838 SetRawInputAt(0, object);
4839 SetRawInputAt(1, constant);
4840 }
4841
4842 bool CanBeMoved() const OVERRIDE { return true; }
4843
4844 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4845 return true;
4846 }
4847
4848 bool NeedsEnvironment() const OVERRIDE {
4849 // Instruction may throw a CheckCastError.
4850 return true;
4851 }
4852
4853 bool CanThrow() const OVERRIDE { return true; }
4854
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004855 bool MustDoNullCheck() const { return must_do_null_check_; }
4856 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004857 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004858
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004859 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004860
4861 DECLARE_INSTRUCTION(CheckCast);
4862
4863 private:
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004864 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004865 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004866
4867 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004868};
4869
Calin Juravle27df7582015-04-17 19:12:31 +01004870class HMemoryBarrier : public HTemplateInstruction<0> {
4871 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004872 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004873 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004874 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004875 barrier_kind_(barrier_kind) {}
4876
4877 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4878
4879 DECLARE_INSTRUCTION(MemoryBarrier);
4880
4881 private:
4882 const MemBarrierKind barrier_kind_;
4883
4884 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4885};
4886
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004887class HMonitorOperation : public HTemplateInstruction<1> {
4888 public:
4889 enum OperationKind {
4890 kEnter,
4891 kExit,
4892 };
4893
4894 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004895 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004896 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
4897 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004898 SetRawInputAt(0, object);
4899 }
4900
4901 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004902 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4903
4904 bool CanThrow() const OVERRIDE {
4905 // Verifier guarantees that monitor-exit cannot throw.
4906 // This is important because it allows the HGraphBuilder to remove
4907 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4908 return IsEnter();
4909 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004910
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004911
4912 bool IsEnter() const { return kind_ == kEnter; }
4913
4914 DECLARE_INSTRUCTION(MonitorOperation);
4915
Calin Juravle52c48962014-12-16 17:02:57 +00004916 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004917 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004918
4919 private:
4920 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4921};
4922
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004923/**
4924 * A HInstruction used as a marker for the replacement of new + <init>
4925 * of a String to a call to a StringFactory. Only baseline will see
4926 * the node at code generation, where it will be be treated as null.
4927 * When compiling non-baseline, `HFakeString` instructions are being removed
4928 * in the instruction simplifier.
4929 */
4930class HFakeString : public HTemplateInstruction<0> {
4931 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004932 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
4933 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004934
4935 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4936
4937 DECLARE_INSTRUCTION(FakeString);
4938
4939 private:
4940 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4941};
4942
Vladimir Markof9f64412015-09-02 14:05:49 +01004943class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004944 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004945 MoveOperands(Location source,
4946 Location destination,
4947 Primitive::Type type,
4948 HInstruction* instruction)
4949 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004950
4951 Location GetSource() const { return source_; }
4952 Location GetDestination() const { return destination_; }
4953
4954 void SetSource(Location value) { source_ = value; }
4955 void SetDestination(Location value) { destination_ = value; }
4956
4957 // The parallel move resolver marks moves as "in-progress" by clearing the
4958 // destination (but not the source).
4959 Location MarkPending() {
4960 DCHECK(!IsPending());
4961 Location dest = destination_;
4962 destination_ = Location::NoLocation();
4963 return dest;
4964 }
4965
4966 void ClearPending(Location dest) {
4967 DCHECK(IsPending());
4968 destination_ = dest;
4969 }
4970
4971 bool IsPending() const {
4972 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4973 return destination_.IsInvalid() && !source_.IsInvalid();
4974 }
4975
4976 // True if this blocks a move from the given location.
4977 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004978 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004979 }
4980
4981 // A move is redundant if it's been eliminated, if its source and
4982 // destination are the same, or if its destination is unneeded.
4983 bool IsRedundant() const {
4984 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4985 }
4986
4987 // We clear both operands to indicate move that's been eliminated.
4988 void Eliminate() {
4989 source_ = destination_ = Location::NoLocation();
4990 }
4991
4992 bool IsEliminated() const {
4993 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4994 return source_.IsInvalid();
4995 }
4996
Alexey Frunze4dda3372015-06-01 18:31:49 -07004997 Primitive::Type GetType() const { return type_; }
4998
Nicolas Geoffray90218252015-04-15 11:56:51 +01004999 bool Is64BitMove() const {
5000 return Primitive::Is64BitType(type_);
5001 }
5002
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005003 HInstruction* GetInstruction() const { return instruction_; }
5004
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005005 private:
5006 Location source_;
5007 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005008 // The type this move is for.
5009 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005010 // The instruction this move is assocatied with. Null when this move is
5011 // for moving an input in the expected locations of user (including a phi user).
5012 // This is only used in debug mode, to ensure we do not connect interval siblings
5013 // in the same parallel move.
5014 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005015};
5016
5017static constexpr size_t kDefaultNumberOfMoves = 4;
5018
5019class HParallelMove : public HTemplateInstruction<0> {
5020 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005021 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
5022 : HTemplateInstruction(SideEffects::None(), dex_pc), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005023
Nicolas Geoffray90218252015-04-15 11:56:51 +01005024 void AddMove(Location source,
5025 Location destination,
5026 Primitive::Type type,
5027 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005028 DCHECK(source.IsValid());
5029 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005030 if (kIsDebugBuild) {
5031 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005032 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005033 if (moves_.Get(i).GetInstruction() == instruction) {
5034 // Special case the situation where the move is for the spill slot
5035 // of the instruction.
5036 if ((GetPrevious() == instruction)
5037 || ((GetPrevious() == nullptr)
5038 && instruction->IsPhi()
5039 && instruction->GetBlock() == GetBlock())) {
5040 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
5041 << "Doing parallel moves for the same instruction.";
5042 } else {
5043 DCHECK(false) << "Doing parallel moves for the same instruction.";
5044 }
5045 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005046 }
5047 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005048 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08005049 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005050 << "Overlapped destination for two moves in a parallel move: "
5051 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
5052 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005053 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005054 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01005055 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005056 }
5057
5058 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005059 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005060 }
5061
5062 size_t NumMoves() const { return moves_.Size(); }
5063
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005064 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005065
5066 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005067 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005068
5069 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5070};
5071
Mark Mendell0616ae02015-04-17 12:49:27 -04005072} // namespace art
5073
5074#ifdef ART_ENABLE_CODEGEN_x86
5075#include "nodes_x86.h"
5076#endif
5077
5078namespace art {
5079
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005080class HGraphVisitor : public ValueObject {
5081 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005082 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5083 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005084
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005085 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005086 virtual void VisitBasicBlock(HBasicBlock* block);
5087
Roland Levillain633021e2014-10-01 14:12:25 +01005088 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005089 void VisitInsertionOrder();
5090
Roland Levillain633021e2014-10-01 14:12:25 +01005091 // Visit the graph following dominator tree reverse post-order.
5092 void VisitReversePostOrder();
5093
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005094 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005095
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005096 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005097#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005098 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5099
5100 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5101
5102#undef DECLARE_VISIT_INSTRUCTION
5103
5104 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005105 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005106
5107 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5108};
5109
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005110class HGraphDelegateVisitor : public HGraphVisitor {
5111 public:
5112 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5113 virtual ~HGraphDelegateVisitor() {}
5114
5115 // Visit functions that delegate to to super class.
5116#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005117 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005118
5119 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5120
5121#undef DECLARE_VISIT_INSTRUCTION
5122
5123 private:
5124 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5125};
5126
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005127class HInsertionOrderIterator : public ValueObject {
5128 public:
5129 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5130
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005131 bool Done() const { return index_ == graph_.GetBlocks().size(); }
5132 HBasicBlock* Current() const { return graph_.GetBlock(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005133 void Advance() { ++index_; }
5134
5135 private:
5136 const HGraph& graph_;
5137 size_t index_;
5138
5139 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5140};
5141
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005142class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005143 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005144 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5145 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005146 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005147 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005148
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005149 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5150 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005151 void Advance() { ++index_; }
5152
5153 private:
5154 const HGraph& graph_;
5155 size_t index_;
5156
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005157 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005158};
5159
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005160class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005161 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005162 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005163 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005164 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005165 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005166 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005167
5168 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005169 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005170 void Advance() { --index_; }
5171
5172 private:
5173 const HGraph& graph_;
5174 size_t index_;
5175
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005176 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005177};
5178
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005179class HLinearPostOrderIterator : public ValueObject {
5180 public:
5181 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005182 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005183
5184 bool Done() const { return index_ == 0; }
5185
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005186 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005187
5188 void Advance() {
5189 --index_;
5190 DCHECK_GE(index_, 0U);
5191 }
5192
5193 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005194 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005195 size_t index_;
5196
5197 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5198};
5199
5200class HLinearOrderIterator : public ValueObject {
5201 public:
5202 explicit HLinearOrderIterator(const HGraph& graph)
5203 : order_(graph.GetLinearOrder()), index_(0) {}
5204
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005205 bool Done() const { return index_ == order_.size(); }
5206 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005207 void Advance() { ++index_; }
5208
5209 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005210 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005211 size_t index_;
5212
5213 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5214};
5215
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005216// Iterator over the blocks that art part of the loop. Includes blocks part
5217// of an inner loop. The order in which the blocks are iterated is on their
5218// block id.
5219class HBlocksInLoopIterator : public ValueObject {
5220 public:
5221 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5222 : blocks_in_loop_(info.GetBlocks()),
5223 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5224 index_(0) {
5225 if (!blocks_in_loop_.IsBitSet(index_)) {
5226 Advance();
5227 }
5228 }
5229
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005230 bool Done() const { return index_ == blocks_.size(); }
5231 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005232 void Advance() {
5233 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005234 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005235 if (blocks_in_loop_.IsBitSet(index_)) {
5236 break;
5237 }
5238 }
5239 }
5240
5241 private:
5242 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005243 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005244 size_t index_;
5245
5246 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5247};
5248
Mingyao Yang3584bce2015-05-19 16:01:59 -07005249// Iterator over the blocks that art part of the loop. Includes blocks part
5250// of an inner loop. The order in which the blocks are iterated is reverse
5251// post order.
5252class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5253 public:
5254 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5255 : blocks_in_loop_(info.GetBlocks()),
5256 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5257 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005258 DCHECK(!blocks_.empty());
5259 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005260 Advance();
5261 }
5262 }
5263
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005264 bool Done() const { return index_ == blocks_.size(); }
5265 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005266 void Advance() {
5267 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005268 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5269 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005270 break;
5271 }
5272 }
5273 }
5274
5275 private:
5276 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005277 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005278 size_t index_;
5279
5280 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5281};
5282
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005283inline int64_t Int64FromConstant(HConstant* constant) {
5284 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5285 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5286 : constant->AsLongConstant()->GetValue();
5287}
5288
Vladimir Marko58155012015-08-19 12:49:41 +00005289inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5290 // For the purposes of the compiler, the dex files must actually be the same object
5291 // if we want to safely treat them as the same. This is especially important for JIT
5292 // as custom class loaders can open the same underlying file (or memory) multiple
5293 // times and provide different class resolution but no two class loaders should ever
5294 // use the same DexFile object - doing so is an unsupported hack that can lead to
5295 // all sorts of weird failures.
5296 return &lhs == &rhs;
5297}
5298
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005299} // namespace art
5300
5301#endif // ART_COMPILER_OPTIMIZING_NODES_H_