blob: 27bbff010462f0c1b85606ef30503fcfd7e0b9dd [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) \
Calin Juravle23a8e352015-09-08 19:56:31 +01001069 M(UnresolvedInstanceFieldGet, Instruction) \
1070 M(UnresolvedInstanceFieldSet, Instruction) \
1071 M(UnresolvedStaticFieldGet, Instruction) \
1072 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001073 M(StoreLocal, Instruction) \
1074 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001075 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001076 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001077 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001078 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001079 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001080 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001081 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001082
Alexandre Ramesef20f712015-06-09 10:29:30 +01001083#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1084
1085#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1086
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001087#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1088
Mark Mendell0616ae02015-04-17 12:49:27 -04001089#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1090 M(X86ComputeBaseMethodAddress, Instruction) \
1091 M(X86LoadFromConstantTable, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001092
1093#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1094
1095#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1096 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1097 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1098 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001099 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001100 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1101 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1102
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001103#define FOR_EACH_INSTRUCTION(M) \
1104 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1105 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001106 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001107 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001108 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001109
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001110#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001111FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1112#undef FORWARD_DECLARATION
1113
Roland Levillainccc07a92014-09-16 14:48:16 +01001114#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001115 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1116 const char* DebugName() const OVERRIDE { return #type; } \
1117 const H##type* As##type() const OVERRIDE { return this; } \
1118 H##type* As##type() OVERRIDE { return this; } \
1119 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001120 return other->Is##type(); \
1121 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001122 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001123
David Brazdiled596192015-01-23 10:39:45 +00001124template <typename T> class HUseList;
1125
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001126template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001127class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001128 public:
David Brazdiled596192015-01-23 10:39:45 +00001129 HUseListNode* GetPrevious() const { return prev_; }
1130 HUseListNode* GetNext() const { return next_; }
1131 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001132 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001133 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001134
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001135 private:
David Brazdiled596192015-01-23 10:39:45 +00001136 HUseListNode(T user, size_t index)
1137 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1138
1139 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001140 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001141 HUseListNode<T>* prev_;
1142 HUseListNode<T>* next_;
1143
1144 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001145
1146 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1147};
1148
David Brazdiled596192015-01-23 10:39:45 +00001149template <typename T>
1150class HUseList : public ValueObject {
1151 public:
1152 HUseList() : first_(nullptr) {}
1153
1154 void Clear() {
1155 first_ = nullptr;
1156 }
1157
1158 // Adds a new entry at the beginning of the use list and returns
1159 // the newly created node.
1160 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001161 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001162 if (IsEmpty()) {
1163 first_ = new_node;
1164 } else {
1165 first_->prev_ = new_node;
1166 new_node->next_ = first_;
1167 first_ = new_node;
1168 }
1169 return new_node;
1170 }
1171
1172 HUseListNode<T>* GetFirst() const {
1173 return first_;
1174 }
1175
1176 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001177 DCHECK(node != nullptr);
1178 DCHECK(Contains(node));
1179
David Brazdiled596192015-01-23 10:39:45 +00001180 if (node->prev_ != nullptr) {
1181 node->prev_->next_ = node->next_;
1182 }
1183 if (node->next_ != nullptr) {
1184 node->next_->prev_ = node->prev_;
1185 }
1186 if (node == first_) {
1187 first_ = node->next_;
1188 }
1189 }
1190
David Brazdil1abb4192015-02-17 18:33:36 +00001191 bool Contains(const HUseListNode<T>* node) const {
1192 if (node == nullptr) {
1193 return false;
1194 }
1195 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1196 if (current == node) {
1197 return true;
1198 }
1199 }
1200 return false;
1201 }
1202
David Brazdiled596192015-01-23 10:39:45 +00001203 bool IsEmpty() const {
1204 return first_ == nullptr;
1205 }
1206
1207 bool HasOnlyOneUse() const {
1208 return first_ != nullptr && first_->next_ == nullptr;
1209 }
1210
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001211 size_t SizeSlow() const {
1212 size_t count = 0;
1213 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1214 ++count;
1215 }
1216 return count;
1217 }
1218
David Brazdiled596192015-01-23 10:39:45 +00001219 private:
1220 HUseListNode<T>* first_;
1221};
1222
1223template<typename T>
1224class HUseIterator : public ValueObject {
1225 public:
1226 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1227
1228 bool Done() const { return current_ == nullptr; }
1229
1230 void Advance() {
1231 DCHECK(!Done());
1232 current_ = current_->GetNext();
1233 }
1234
1235 HUseListNode<T>* Current() const {
1236 DCHECK(!Done());
1237 return current_;
1238 }
1239
1240 private:
1241 HUseListNode<T>* current_;
1242
1243 friend class HValue;
1244};
1245
David Brazdil1abb4192015-02-17 18:33:36 +00001246// This class is used by HEnvironment and HInstruction classes to record the
1247// instructions they use and pointers to the corresponding HUseListNodes kept
1248// by the used instructions.
1249template <typename T>
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001250class HUserRecord {
David Brazdil1abb4192015-02-17 18:33:36 +00001251 public:
1252 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1253 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1254
1255 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1256 : instruction_(old_record.instruction_), use_node_(use_node) {
1257 DCHECK(instruction_ != nullptr);
1258 DCHECK(use_node_ != nullptr);
1259 DCHECK(old_record.use_node_ == nullptr);
1260 }
1261
1262 HInstruction* GetInstruction() const { return instruction_; }
1263 HUseListNode<T>* GetUseNode() const { return use_node_; }
1264
1265 private:
1266 // Instruction used by the user.
1267 HInstruction* instruction_;
1268
1269 // Corresponding entry in the use list kept by 'instruction_'.
1270 HUseListNode<T>* use_node_;
1271};
1272
Aart Bik854a02b2015-07-14 16:07:00 -07001273/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001274 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001275 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001276 * For write/read dependences on fields/arrays, the dependence analysis uses
1277 * type disambiguation (e.g. a float field write cannot modify the value of an
1278 * integer field read) and the access type (e.g. a reference array write cannot
1279 * modify the value of a reference field read [although it may modify the
1280 * reference fetch prior to reading the field, which is represented by its own
1281 * write/read dependence]). The analysis makes conservative points-to
1282 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1283 * the same, and any reference read depends on any reference read without
1284 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001285 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001286 * The internal representation uses 38-bit and is described in the table below.
1287 * The first line indicates the side effect, and for field/array accesses the
1288 * second line indicates the type of the access (in the order of the
1289 * Primitive::Type enum).
1290 * The two numbered lines below indicate the bit position in the bitfield (read
1291 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001292 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001293 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1294 * +-------------+---------+---------+--------------+---------+---------+
1295 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1296 * | 3 |333333322|222222221| 1 |111111110|000000000|
1297 * | 7 |654321098|765432109| 8 |765432109|876543210|
1298 *
1299 * Note that, to ease the implementation, 'changes' bits are least significant
1300 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001301 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001302class SideEffects : public ValueObject {
1303 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001304 SideEffects() : flags_(0) {}
1305
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001306 static SideEffects None() {
1307 return SideEffects(0);
1308 }
1309
1310 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001311 return SideEffects(kAllChangeBits | kAllDependOnBits);
1312 }
1313
1314 static SideEffects AllChanges() {
1315 return SideEffects(kAllChangeBits);
1316 }
1317
1318 static SideEffects AllDependencies() {
1319 return SideEffects(kAllDependOnBits);
1320 }
1321
1322 static SideEffects AllExceptGCDependency() {
1323 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1324 }
1325
1326 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001327 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001328 }
1329
Aart Bik34c3ba92015-07-20 14:08:59 -07001330 static SideEffects AllWrites() {
1331 return SideEffects(kAllWrites);
1332 }
1333
1334 static SideEffects AllReads() {
1335 return SideEffects(kAllReads);
1336 }
1337
1338 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1339 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001340 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001341 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001342 }
1343
Aart Bik854a02b2015-07-14 16:07:00 -07001344 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1345 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001346 }
1347
Aart Bik34c3ba92015-07-20 14:08:59 -07001348 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1349 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001350 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001351 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001352 }
1353
1354 static SideEffects ArrayReadOfType(Primitive::Type type) {
1355 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1356 }
1357
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001358 static SideEffects CanTriggerGC() {
1359 return SideEffects(1ULL << kCanTriggerGCBit);
1360 }
1361
1362 static SideEffects DependsOnGC() {
1363 return SideEffects(1ULL << kDependsOnGCBit);
1364 }
1365
Aart Bik854a02b2015-07-14 16:07:00 -07001366 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001367 SideEffects Union(SideEffects other) const {
1368 return SideEffects(flags_ | other.flags_);
1369 }
1370
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001371 SideEffects Exclusion(SideEffects other) const {
1372 return SideEffects(flags_ & ~other.flags_);
1373 }
1374
1375 bool Includes(SideEffects other) const {
1376 return (other.flags_ & flags_) == other.flags_;
1377 }
1378
1379 bool HasSideEffects() const {
1380 return (flags_ & kAllChangeBits);
1381 }
1382
1383 bool HasDependencies() const {
1384 return (flags_ & kAllDependOnBits);
1385 }
1386
1387 // Returns true if there are no side effects or dependencies.
1388 bool DoesNothing() const {
1389 return flags_ == 0;
1390 }
1391
Aart Bik854a02b2015-07-14 16:07:00 -07001392 // Returns true if something is written.
1393 bool DoesAnyWrite() const {
1394 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001395 }
1396
Aart Bik854a02b2015-07-14 16:07:00 -07001397 // Returns true if something is read.
1398 bool DoesAnyRead() const {
1399 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001400 }
1401
Aart Bik854a02b2015-07-14 16:07:00 -07001402 // Returns true if potentially everything is written and read
1403 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001404 bool DoesAllReadWrite() const {
1405 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1406 }
1407
Aart Bik854a02b2015-07-14 16:07:00 -07001408 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001409 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001410 }
1411
1412 // Returns true if this may read something written by other.
1413 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001414 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1415 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001416 }
1417
1418 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001419 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001420 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001421 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001422 for (int s = kLastBit; s >= 0; s--) {
1423 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1424 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1425 // This is a bit for the GC side effect.
1426 if (current_bit_is_set) {
1427 flags += "GC";
1428 }
Aart Bik854a02b2015-07-14 16:07:00 -07001429 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001430 } else {
1431 // This is a bit for the array/field analysis.
1432 // The underscore character stands for the 'can trigger GC' bit.
1433 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1434 if (current_bit_is_set) {
1435 flags += kDebug[s];
1436 }
1437 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1438 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1439 flags += "|";
1440 }
1441 }
Aart Bik854a02b2015-07-14 16:07:00 -07001442 }
1443 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001444 }
1445
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001446 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001447
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001448 private:
1449 static constexpr int kFieldArrayAnalysisBits = 9;
1450
1451 static constexpr int kFieldWriteOffset = 0;
1452 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1453 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1454 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1455
1456 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1457
1458 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1459 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1460 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1461 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1462
1463 static constexpr int kLastBit = kDependsOnGCBit;
1464 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1465
1466 // Aliases.
1467
1468 static_assert(kChangeBits == kDependOnBits,
1469 "the 'change' bits should match the 'depend on' bits.");
1470
1471 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1472 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1473 static constexpr uint64_t kAllWrites =
1474 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1475 static constexpr uint64_t kAllReads =
1476 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001477
Aart Bik854a02b2015-07-14 16:07:00 -07001478 // Work around the fact that HIR aliases I/F and J/D.
1479 // TODO: remove this interceptor once HIR types are clean
1480 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1481 switch (type) {
1482 case Primitive::kPrimInt:
1483 case Primitive::kPrimFloat:
1484 return TypeFlag(Primitive::kPrimInt, offset) |
1485 TypeFlag(Primitive::kPrimFloat, offset);
1486 case Primitive::kPrimLong:
1487 case Primitive::kPrimDouble:
1488 return TypeFlag(Primitive::kPrimLong, offset) |
1489 TypeFlag(Primitive::kPrimDouble, offset);
1490 default:
1491 return TypeFlag(type, offset);
1492 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001493 }
1494
Aart Bik854a02b2015-07-14 16:07:00 -07001495 // Translates type to bit flag.
1496 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1497 CHECK_NE(type, Primitive::kPrimVoid);
1498 const uint64_t one = 1;
1499 const int shift = type; // 0-based consecutive enum
1500 DCHECK_LE(kFieldWriteOffset, shift);
1501 DCHECK_LT(shift, kArrayWriteOffset);
1502 return one << (type + offset);
1503 }
1504
1505 // Private constructor on direct flags value.
1506 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1507
1508 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001509};
1510
David Brazdiled596192015-01-23 10:39:45 +00001511// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001512class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001513 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001514 HEnvironment(ArenaAllocator* arena,
1515 size_t number_of_vregs,
1516 const DexFile& dex_file,
1517 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001518 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001519 InvokeType invoke_type,
1520 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001521 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1522 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001523 parent_(nullptr),
1524 dex_file_(dex_file),
1525 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001526 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001527 invoke_type_(invoke_type),
1528 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001529 }
1530
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001531 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001532 : HEnvironment(arena,
1533 to_copy.Size(),
1534 to_copy.GetDexFile(),
1535 to_copy.GetMethodIdx(),
1536 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001537 to_copy.GetInvokeType(),
1538 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001539
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001540 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001541 if (parent_ != nullptr) {
1542 parent_->SetAndCopyParentChain(allocator, parent);
1543 } else {
1544 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1545 parent_->CopyFrom(parent);
1546 if (parent->GetParent() != nullptr) {
1547 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1548 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001549 }
David Brazdiled596192015-01-23 10:39:45 +00001550 }
1551
Vladimir Marko71bf8092015-09-15 15:33:14 +01001552 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001553 void CopyFrom(HEnvironment* environment);
1554
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001555 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1556 // input to the loop phi instead. This is for inserting instructions that
1557 // require an environment (like HDeoptimization) in the loop pre-header.
1558 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001559
1560 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001561 DCHECK_LT(index, Size());
1562 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001563 }
1564
1565 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001566 DCHECK_LT(index, Size());
1567 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001568 }
1569
David Brazdil1abb4192015-02-17 18:33:36 +00001570 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001571
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001572 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001573
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001574 HEnvironment* GetParent() const { return parent_; }
1575
1576 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001577 DCHECK_LT(index, Size());
1578 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001579 }
1580
1581 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001582 DCHECK_LT(index, Size());
1583 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001584 }
1585
1586 uint32_t GetDexPc() const {
1587 return dex_pc_;
1588 }
1589
1590 uint32_t GetMethodIdx() const {
1591 return method_idx_;
1592 }
1593
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001594 InvokeType GetInvokeType() const {
1595 return invoke_type_;
1596 }
1597
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001598 const DexFile& GetDexFile() const {
1599 return dex_file_;
1600 }
1601
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001602 HInstruction* GetHolder() const {
1603 return holder_;
1604 }
1605
David Brazdiled596192015-01-23 10:39:45 +00001606 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001607 // Record instructions' use entries of this environment for constant-time removal.
1608 // It should only be called by HInstruction when a new environment use is added.
1609 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1610 DCHECK(env_use->GetUser() == this);
1611 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001612 DCHECK_LT(index, Size());
1613 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001614 }
David Brazdiled596192015-01-23 10:39:45 +00001615
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001616 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1617 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001618 HEnvironment* parent_;
1619 const DexFile& dex_file_;
1620 const uint32_t method_idx_;
1621 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001622 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001623
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001624 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001625 HInstruction* const holder_;
1626
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001627 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001628
1629 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1630};
1631
Calin Juravleacf735c2015-02-12 15:25:22 +00001632class ReferenceTypeInfo : ValueObject {
1633 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001634 typedef Handle<mirror::Class> TypeHandle;
1635
Calin Juravle2e768302015-07-28 14:41:11 +00001636 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1637 // The constructor will check that the type_handle is valid.
1638 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001639 }
1640
Calin Juravle2e768302015-07-28 14:41:11 +00001641 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1642
1643 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1644 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001645 }
1646
Calin Juravle2e768302015-07-28 14:41:11 +00001647 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1648 return IsValidHandle(type_handle_);
1649 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001650 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001651
1652 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1653 DCHECK(IsValid());
1654 return GetTypeHandle()->IsObjectClass();
1655 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001656 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001657 DCHECK(IsValid());
1658 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001659 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001660
1661 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1662
Mathieu Chartier90443472015-07-16 20:32:27 -07001663 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001664 DCHECK(IsValid());
1665 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001666 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1667 }
1668
1669 // Returns true if the type information provide the same amount of details.
1670 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001671 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001672 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001673 if (!IsValid() && !rti.IsValid()) {
1674 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001675 return true;
1676 }
Calin Juravle2e768302015-07-28 14:41:11 +00001677 if (!IsValid() || !rti.IsValid()) {
1678 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001679 return false;
1680 }
Calin Juravle2e768302015-07-28 14:41:11 +00001681 return IsExact() == rti.IsExact()
1682 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001683 }
1684
1685 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001686 ReferenceTypeInfo();
1687 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001688
Calin Juravleacf735c2015-02-12 15:25:22 +00001689 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001690 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001691 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001692 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001693 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001694};
1695
1696std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1697
Vladimir Markof9f64412015-09-02 14:05:49 +01001698class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001699 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001700 HInstruction(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001701 : previous_(nullptr),
1702 next_(nullptr),
1703 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001704 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001705 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001706 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001707 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001708 locations_(nullptr),
1709 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001710 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001711 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001712 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001713
Dave Allison20dfc792014-06-16 20:44:29 -07001714 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001715
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001716#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001717 enum InstructionKind {
1718 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1719 };
1720#undef DECLARE_KIND
1721
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001722 HInstruction* GetNext() const { return next_; }
1723 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001724
Calin Juravle77520bc2015-01-12 18:45:46 +00001725 HInstruction* GetNextDisregardingMoves() const;
1726 HInstruction* GetPreviousDisregardingMoves() const;
1727
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001728 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001729 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001730 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001731 bool IsInBlock() const { return block_ != nullptr; }
1732 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001733 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001734
Roland Levillain6b879dd2014-09-22 17:13:44 +01001735 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001736 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001737
1738 virtual void Accept(HGraphVisitor* visitor) = 0;
1739 virtual const char* DebugName() const = 0;
1740
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001741 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001742 void SetRawInputAt(size_t index, HInstruction* input) {
1743 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1744 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001745
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001746 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001747
1748 uint32_t GetDexPc() const { return dex_pc_; }
1749
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001750 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001751
Roland Levillaine161a2a2014-10-03 12:45:18 +01001752 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001753 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001754
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001755 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001756 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001757
Calin Juravle10e244f2015-01-26 18:54:32 +00001758 // Does not apply for all instructions, but having this at top level greatly
1759 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001760 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001761 virtual bool CanBeNull() const {
1762 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1763 return true;
1764 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001765
Calin Juravle641547a2015-04-21 22:08:51 +01001766 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1767 UNUSED(obj);
1768 return false;
1769 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001770
Calin Juravle2e768302015-07-28 14:41:11 +00001771 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001772
Calin Juravle61d544b2015-02-23 16:46:57 +00001773 ReferenceTypeInfo GetReferenceTypeInfo() const {
1774 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1775 return reference_type_info_;
1776 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001777
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001778 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001779 DCHECK(user != nullptr);
1780 HUseListNode<HInstruction*>* use =
1781 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1782 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001783 }
1784
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001785 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001786 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001787 HUseListNode<HEnvironment*>* env_use =
1788 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1789 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001790 }
1791
David Brazdil1abb4192015-02-17 18:33:36 +00001792 void RemoveAsUserOfInput(size_t input) {
1793 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1794 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1795 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001796
David Brazdil1abb4192015-02-17 18:33:36 +00001797 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1798 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001799
David Brazdiled596192015-01-23 10:39:45 +00001800 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1801 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001802 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001803 bool HasOnlyOneNonEnvironmentUse() const {
1804 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1805 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001806
Roland Levillain6c82d402014-10-13 16:10:27 +01001807 // Does this instruction strictly dominate `other_instruction`?
1808 // Returns false if this instruction and `other_instruction` are the same.
1809 // Aborts if this instruction and `other_instruction` are both phis.
1810 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001811
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001812 int GetId() const { return id_; }
1813 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001814
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001815 int GetSsaIndex() const { return ssa_index_; }
1816 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1817 bool HasSsaIndex() const { return ssa_index_ != -1; }
1818
1819 bool HasEnvironment() const { return environment_ != nullptr; }
1820 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001821 // Set the `environment_` field. Raw because this method does not
1822 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001823 void SetRawEnvironment(HEnvironment* environment) {
1824 DCHECK(environment_ == nullptr);
1825 DCHECK_EQ(environment->GetHolder(), this);
1826 environment_ = environment;
1827 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001828
1829 // Set the environment of this instruction, copying it from `environment`. While
1830 // copying, the uses lists are being updated.
1831 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001832 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001833 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001834 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001835 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001836 if (environment->GetParent() != nullptr) {
1837 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1838 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001839 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001840
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001841 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1842 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001843 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001844 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001845 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001846 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001847 if (environment->GetParent() != nullptr) {
1848 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1849 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001850 }
1851
Nicolas Geoffray39468442014-09-02 15:17:15 +01001852 // Returns the number of entries in the environment. Typically, that is the
1853 // number of dex registers in a method. It could be more in case of inlining.
1854 size_t EnvironmentSize() const;
1855
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001856 LocationSummary* GetLocations() const { return locations_; }
1857 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001858
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001859 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001860 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001861
Alexandre Rames188d4312015-04-09 18:30:21 +01001862 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1863 // uses of this instruction by `other` are *not* updated.
1864 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1865 ReplaceWith(other);
1866 other->ReplaceInput(this, use_index);
1867 }
1868
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001869 // Move `this` instruction before `cursor`.
1870 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001871
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001872#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001873 bool Is##type() const { return (As##type() != nullptr); } \
1874 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001875 virtual H##type* As##type() { return nullptr; }
1876
1877 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1878#undef INSTRUCTION_TYPE_CHECK
1879
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001880 // Returns whether the instruction can be moved within the graph.
1881 virtual bool CanBeMoved() const { return false; }
1882
1883 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001884 virtual bool InstructionTypeEquals(HInstruction* other) const {
1885 UNUSED(other);
1886 return false;
1887 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001888
1889 // Returns whether any data encoded in the two instructions is equal.
1890 // This method does not look at the inputs. Both instructions must be
1891 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001892 virtual bool InstructionDataEquals(HInstruction* other) const {
1893 UNUSED(other);
1894 return false;
1895 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001896
1897 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001898 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001899 // 2) Their inputs are identical.
1900 bool Equals(HInstruction* other) const;
1901
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001902 virtual InstructionKind GetKind() const = 0;
1903
1904 virtual size_t ComputeHashCode() const {
1905 size_t result = GetKind();
1906 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1907 result = (result * 31) + InputAt(i)->GetId();
1908 }
1909 return result;
1910 }
1911
1912 SideEffects GetSideEffects() const { return side_effects_; }
1913
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001914 size_t GetLifetimePosition() const { return lifetime_position_; }
1915 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1916 LiveInterval* GetLiveInterval() const { return live_interval_; }
1917 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1918 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1919
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001920 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1921
1922 // Returns whether the code generation of the instruction will require to have access
1923 // to the current method. Such instructions are:
1924 // (1): Instructions that require an environment, as calling the runtime requires
1925 // to walk the stack and have the current method stored at a specific stack address.
1926 // (2): Object literals like classes and strings, that are loaded from the dex cache
1927 // fields of the current method.
1928 bool NeedsCurrentMethod() const {
1929 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1930 }
1931
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001932 virtual bool NeedsDexCache() const { return false; }
1933
Mark Mendellc4701932015-04-10 13:18:51 -04001934 // Does this instruction have any use in an environment before
1935 // control flow hits 'other'?
1936 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1937
1938 // Remove all references to environment uses of this instruction.
1939 // The caller must ensure that this is safe to do.
1940 void RemoveEnvironmentUsers();
1941
David Brazdil1abb4192015-02-17 18:33:36 +00001942 protected:
1943 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1944 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1945
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001946 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001947 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1948
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001949 HInstruction* previous_;
1950 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001951 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001952 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001953
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954 // An instruction gets an id when it is added to the graph.
1955 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001956 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001957 int id_;
1958
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001959 // When doing liveness analysis, instructions that have uses get an SSA index.
1960 int ssa_index_;
1961
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001962 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001963 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001964
1965 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001966 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001967
Nicolas Geoffray39468442014-09-02 15:17:15 +01001968 // The environment associated with this instruction. Not null if the instruction
1969 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001970 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001971
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001972 // Set by the code generator.
1973 LocationSummary* locations_;
1974
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001975 // Set by the liveness analysis.
1976 LiveInterval* live_interval_;
1977
1978 // Set by the liveness analysis, this is the position in a linear
1979 // order of blocks where this instruction's live interval start.
1980 size_t lifetime_position_;
1981
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001982 const SideEffects side_effects_;
1983
Calin Juravleacf735c2015-02-12 15:25:22 +00001984 // TODO: for primitive types this should be marked as invalid.
1985 ReferenceTypeInfo reference_type_info_;
1986
David Brazdil1abb4192015-02-17 18:33:36 +00001987 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001988 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001989 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001990 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001991 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001992
1993 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1994};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001995std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001996
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001997class HInputIterator : public ValueObject {
1998 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001999 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002000
2001 bool Done() const { return index_ == instruction_->InputCount(); }
2002 HInstruction* Current() const { return instruction_->InputAt(index_); }
2003 void Advance() { index_++; }
2004
2005 private:
2006 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002007 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002008
2009 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2010};
2011
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002012class HInstructionIterator : public ValueObject {
2013 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002014 explicit HInstructionIterator(const HInstructionList& instructions)
2015 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002016 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002017 }
2018
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002019 bool Done() const { return instruction_ == nullptr; }
2020 HInstruction* Current() const { return instruction_; }
2021 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002022 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002023 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002024 }
2025
2026 private:
2027 HInstruction* instruction_;
2028 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002029
2030 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002031};
2032
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002033class HBackwardInstructionIterator : public ValueObject {
2034 public:
2035 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2036 : instruction_(instructions.last_instruction_) {
2037 next_ = Done() ? nullptr : instruction_->GetPrevious();
2038 }
2039
2040 bool Done() const { return instruction_ == nullptr; }
2041 HInstruction* Current() const { return instruction_; }
2042 void Advance() {
2043 instruction_ = next_;
2044 next_ = Done() ? nullptr : instruction_->GetPrevious();
2045 }
2046
2047 private:
2048 HInstruction* instruction_;
2049 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002050
2051 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002052};
2053
Vladimir Markof9f64412015-09-02 14:05:49 +01002054template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055class HTemplateInstruction: public HInstruction {
2056 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002057 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2058 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002059 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002060
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002061 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002062
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002063 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002064 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2065 DCHECK_LT(i, N);
2066 return inputs_[i];
2067 }
David Brazdil1abb4192015-02-17 18:33:36 +00002068
2069 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002070 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002071 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002072 }
2073
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002074 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002075 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002076
2077 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002078};
2079
Vladimir Markof9f64412015-09-02 14:05:49 +01002080// HTemplateInstruction specialization for N=0.
2081template<>
2082class HTemplateInstruction<0>: public HInstruction {
2083 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002084 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2085 : HInstruction(side_effects, dex_pc) {}
2086
Vladimir Markof9f64412015-09-02 14:05:49 +01002087 virtual ~HTemplateInstruction() {}
2088
2089 size_t InputCount() const OVERRIDE { return 0; }
2090
2091 protected:
2092 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2093 LOG(FATAL) << "Unreachable";
2094 UNREACHABLE();
2095 }
2096
2097 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2098 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2099 LOG(FATAL) << "Unreachable";
2100 UNREACHABLE();
2101 }
2102
2103 private:
2104 friend class SsaBuilder;
2105};
2106
Dave Allison20dfc792014-06-16 20:44:29 -07002107template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002108class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002109 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002110 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2111 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002112 virtual ~HExpression() {}
2113
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002114 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002115
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002116 protected:
2117 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002118};
2119
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002120// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2121// instruction that branches to the exit block.
2122class HReturnVoid : public HTemplateInstruction<0> {
2123 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002124 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2125 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002126
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002127 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002128
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002129 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002130
2131 private:
2132 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2133};
2134
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002135// Represents dex's RETURN opcodes. A HReturn is a control flow
2136// instruction that branches to the exit block.
2137class HReturn : public HTemplateInstruction<1> {
2138 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002139 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2140 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002141 SetRawInputAt(0, value);
2142 }
2143
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002144 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002145
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002146 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002147
2148 private:
2149 DISALLOW_COPY_AND_ASSIGN(HReturn);
2150};
2151
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002153// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002154// exit block.
2155class HExit : public HTemplateInstruction<0> {
2156 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002157 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002158
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002159 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002160
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002161 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002162
2163 private:
2164 DISALLOW_COPY_AND_ASSIGN(HExit);
2165};
2166
2167// Jumps from one block to another.
2168class HGoto : public HTemplateInstruction<0> {
2169 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002170 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002171
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002172 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002173
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002174 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002175 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002176 }
2177
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002178 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002179
2180 private:
2181 DISALLOW_COPY_AND_ASSIGN(HGoto);
2182};
2183
Roland Levillain9867bc72015-08-05 10:21:34 +01002184class HConstant : public HExpression<0> {
2185 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002186 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2187 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002188
2189 bool CanBeMoved() const OVERRIDE { return true; }
2190
2191 virtual bool IsMinusOne() const { return false; }
2192 virtual bool IsZero() const { return false; }
2193 virtual bool IsOne() const { return false; }
2194
David Brazdil77a48ae2015-09-15 12:34:04 +00002195 virtual uint64_t GetValueAsUint64() const = 0;
2196
Roland Levillain9867bc72015-08-05 10:21:34 +01002197 DECLARE_INSTRUCTION(Constant);
2198
2199 private:
2200 DISALLOW_COPY_AND_ASSIGN(HConstant);
2201};
2202
2203class HNullConstant : public HConstant {
2204 public:
2205 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2206 return true;
2207 }
2208
David Brazdil77a48ae2015-09-15 12:34:04 +00002209 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2210
Roland Levillain9867bc72015-08-05 10:21:34 +01002211 size_t ComputeHashCode() const OVERRIDE { return 0; }
2212
2213 DECLARE_INSTRUCTION(NullConstant);
2214
2215 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002216 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002217
2218 friend class HGraph;
2219 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2220};
2221
2222// Constants of the type int. Those can be from Dex instructions, or
2223// synthesized (for example with the if-eqz instruction).
2224class HIntConstant : public HConstant {
2225 public:
2226 int32_t GetValue() const { return value_; }
2227
David Brazdil77a48ae2015-09-15 12:34:04 +00002228 uint64_t GetValueAsUint64() const OVERRIDE { return static_cast<uint64_t>(value_); }
2229
Roland Levillain9867bc72015-08-05 10:21:34 +01002230 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2231 DCHECK(other->IsIntConstant());
2232 return other->AsIntConstant()->value_ == value_;
2233 }
2234
2235 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2236
2237 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2238 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2239 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2240
2241 DECLARE_INSTRUCTION(IntConstant);
2242
2243 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002244 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2245 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2246 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2247 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002248
2249 const int32_t value_;
2250
2251 friend class HGraph;
2252 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2253 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2254 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2255};
2256
2257class HLongConstant : public HConstant {
2258 public:
2259 int64_t GetValue() const { return value_; }
2260
David Brazdil77a48ae2015-09-15 12:34:04 +00002261 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2262
Roland Levillain9867bc72015-08-05 10:21:34 +01002263 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2264 DCHECK(other->IsLongConstant());
2265 return other->AsLongConstant()->value_ == value_;
2266 }
2267
2268 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2269
2270 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2271 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2272 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2273
2274 DECLARE_INSTRUCTION(LongConstant);
2275
2276 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002277 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2278 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002279
2280 const int64_t value_;
2281
2282 friend class HGraph;
2283 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2284};
Dave Allison20dfc792014-06-16 20:44:29 -07002285
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002286// Conditional branch. A block ending with an HIf instruction must have
2287// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002288class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002289 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002290 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2291 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002292 SetRawInputAt(0, input);
2293 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002294
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002295 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002296
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002297 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002298 return GetBlock()->GetSuccessor(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002299 }
2300
2301 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002302 return GetBlock()->GetSuccessor(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002303 }
2304
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002305 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002306
2307 private:
2308 DISALLOW_COPY_AND_ASSIGN(HIf);
2309};
2310
David Brazdilfc6a86a2015-06-26 10:33:45 +00002311
2312// Abstract instruction which marks the beginning and/or end of a try block and
2313// links it to the respective exception handlers. Behaves the same as a Goto in
2314// non-exceptional control flow.
2315// Normal-flow successor is stored at index zero, exception handlers under
2316// higher indices in no particular order.
2317class HTryBoundary : public HTemplateInstruction<0> {
2318 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002319 enum BoundaryKind {
2320 kEntry,
2321 kExit,
2322 };
2323
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002324 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2325 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002326
2327 bool IsControlFlow() const OVERRIDE { return true; }
2328
2329 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko60584552015-09-03 13:35:12 +00002330 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessor(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002331
2332 // Returns whether `handler` is among its exception handlers (non-zero index
2333 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002334 bool HasExceptionHandler(const HBasicBlock& handler) const {
2335 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002336 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002337 }
2338
2339 // If not present already, adds `handler` to its block's list of exception
2340 // handlers.
2341 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002342 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002343 GetBlock()->AddSuccessor(handler);
2344 }
2345 }
2346
David Brazdil56e1acc2015-06-30 15:41:36 +01002347 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002348
David Brazdilffee3d32015-07-06 11:48:53 +01002349 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2350
David Brazdilfc6a86a2015-06-26 10:33:45 +00002351 DECLARE_INSTRUCTION(TryBoundary);
2352
2353 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002354 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002355
2356 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2357};
2358
David Brazdilffee3d32015-07-06 11:48:53 +01002359// Iterator over exception handlers of a given HTryBoundary, i.e. over
2360// exceptional successors of its basic block.
2361class HExceptionHandlerIterator : public ValueObject {
2362 public:
2363 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2364 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2365
Vladimir Marko60584552015-09-03 13:35:12 +00002366 bool Done() const { return index_ == block_.GetSuccessors().size(); }
2367 HBasicBlock* Current() const { return block_.GetSuccessor(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002368 size_t CurrentSuccessorIndex() const { return index_; }
2369 void Advance() { ++index_; }
2370
2371 private:
2372 const HBasicBlock& block_;
2373 size_t index_;
2374
2375 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2376};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002377
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002378// Deoptimize to interpreter, upon checking a condition.
2379class HDeoptimize : public HTemplateInstruction<1> {
2380 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002381 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2382 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002383 SetRawInputAt(0, cond);
2384 }
2385
2386 bool NeedsEnvironment() const OVERRIDE { return true; }
2387 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002388
2389 DECLARE_INSTRUCTION(Deoptimize);
2390
2391 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002392 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2393};
2394
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002395// Represents the ArtMethod that was passed as a first argument to
2396// the method. It is used by instructions that depend on it, like
2397// instructions that work with the dex cache.
2398class HCurrentMethod : public HExpression<0> {
2399 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002400 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2401 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002402
2403 DECLARE_INSTRUCTION(CurrentMethod);
2404
2405 private:
2406 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2407};
2408
Roland Levillain88cb1752014-10-20 16:36:47 +01002409class HUnaryOperation : public HExpression<1> {
2410 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002411 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2412 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002413 SetRawInputAt(0, input);
2414 }
2415
2416 HInstruction* GetInput() const { return InputAt(0); }
2417 Primitive::Type GetResultType() const { return GetType(); }
2418
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002419 bool CanBeMoved() const OVERRIDE { return true; }
2420 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002421 UNUSED(other);
2422 return true;
2423 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002424
Roland Levillain9240d6a2014-10-20 16:47:04 +01002425 // Try to statically evaluate `operation` and return a HConstant
2426 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002427 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002428 HConstant* TryStaticEvaluation() const;
2429
2430 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002431 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2432 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002433
Roland Levillain88cb1752014-10-20 16:36:47 +01002434 DECLARE_INSTRUCTION(UnaryOperation);
2435
2436 private:
2437 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2438};
2439
Dave Allison20dfc792014-06-16 20:44:29 -07002440class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002441 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002442 HBinaryOperation(Primitive::Type result_type,
2443 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002444 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002445 SideEffects side_effects = SideEffects::None(),
2446 uint32_t dex_pc = kNoDexPc)
2447 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002448 SetRawInputAt(0, left);
2449 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002450 }
2451
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002452 HInstruction* GetLeft() const { return InputAt(0); }
2453 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002454 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002455
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002456 virtual bool IsCommutative() const { return false; }
2457
2458 // Put constant on the right.
2459 // Returns whether order is changed.
2460 bool OrderInputsWithConstantOnTheRight() {
2461 HInstruction* left = InputAt(0);
2462 HInstruction* right = InputAt(1);
2463 if (left->IsConstant() && !right->IsConstant()) {
2464 ReplaceInput(right, 0);
2465 ReplaceInput(left, 1);
2466 return true;
2467 }
2468 return false;
2469 }
2470
2471 // Order inputs by instruction id, but favor constant on the right side.
2472 // This helps GVN for commutative ops.
2473 void OrderInputs() {
2474 DCHECK(IsCommutative());
2475 HInstruction* left = InputAt(0);
2476 HInstruction* right = InputAt(1);
2477 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2478 return;
2479 }
2480 if (OrderInputsWithConstantOnTheRight()) {
2481 return;
2482 }
2483 // Order according to instruction id.
2484 if (left->GetId() > right->GetId()) {
2485 ReplaceInput(right, 0);
2486 ReplaceInput(left, 1);
2487 }
2488 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002489
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002490 bool CanBeMoved() const OVERRIDE { return true; }
2491 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002492 UNUSED(other);
2493 return true;
2494 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002495
Roland Levillain9240d6a2014-10-20 16:47:04 +01002496 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002497 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002498 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002499 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002500
2501 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002502 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2503 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2504 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2505 HLongConstant* y ATTRIBUTE_UNUSED) const {
2506 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2507 return nullptr;
2508 }
2509 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2510 HIntConstant* y ATTRIBUTE_UNUSED) const {
2511 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2512 return nullptr;
2513 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002514
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002515 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002516 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002517 HConstant* GetConstantRight() const;
2518
2519 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002520 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002521 HInstruction* GetLeastConstantLeft() const;
2522
Roland Levillainccc07a92014-09-16 14:48:16 +01002523 DECLARE_INSTRUCTION(BinaryOperation);
2524
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002525 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002526 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2527};
2528
Mark Mendellc4701932015-04-10 13:18:51 -04002529// The comparison bias applies for floating point operations and indicates how NaN
2530// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002531enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002532 kNoBias, // bias is not applicable (i.e. for long operation)
2533 kGtBias, // return 1 for NaN comparisons
2534 kLtBias, // return -1 for NaN comparisons
2535};
2536
Dave Allison20dfc792014-06-16 20:44:29 -07002537class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002538 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002539 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2540 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002541 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002542 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002543
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002544 bool NeedsMaterialization() const { return needs_materialization_; }
2545 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002546
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002547 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002548 // `instruction`, and disregard moves in between.
2549 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002550
Dave Allison20dfc792014-06-16 20:44:29 -07002551 DECLARE_INSTRUCTION(Condition);
2552
2553 virtual IfCondition GetCondition() const = 0;
2554
Mark Mendellc4701932015-04-10 13:18:51 -04002555 virtual IfCondition GetOppositeCondition() const = 0;
2556
Roland Levillain4fa13f62015-07-06 18:11:54 +01002557 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002558
2559 void SetBias(ComparisonBias bias) { bias_ = bias; }
2560
2561 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2562 return bias_ == other->AsCondition()->bias_;
2563 }
2564
Roland Levillain4fa13f62015-07-06 18:11:54 +01002565 bool IsFPConditionTrueIfNaN() const {
2566 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2567 IfCondition if_cond = GetCondition();
2568 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2569 }
2570
2571 bool IsFPConditionFalseIfNaN() const {
2572 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2573 IfCondition if_cond = GetCondition();
2574 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2575 }
2576
Dave Allison20dfc792014-06-16 20:44:29 -07002577 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002578 // For register allocation purposes, returns whether this instruction needs to be
2579 // materialized (that is, not just be in the processor flags).
2580 bool needs_materialization_;
2581
Mark Mendellc4701932015-04-10 13:18:51 -04002582 // Needed if we merge a HCompare into a HCondition.
2583 ComparisonBias bias_;
2584
Dave Allison20dfc792014-06-16 20:44:29 -07002585 DISALLOW_COPY_AND_ASSIGN(HCondition);
2586};
2587
2588// Instruction to check if two inputs are equal to each other.
2589class HEqual : public HCondition {
2590 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002591 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2592 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002593
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002594 bool IsCommutative() const OVERRIDE { return true; }
2595
Roland Levillain9867bc72015-08-05 10:21:34 +01002596 template <typename T> bool Compute(T x, T y) const { return x == y; }
2597
2598 HConstant* Evaluate(HIntConstant* x, HIntConstant* 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 Levillain9867bc72015-08-05 10:21:34 +01002602 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002603 return GetBlock()->GetGraph()->GetIntConstant(
2604 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002605 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002606
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002607 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002608
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002609 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002610 return kCondEQ;
2611 }
2612
Mark Mendellc4701932015-04-10 13:18:51 -04002613 IfCondition GetOppositeCondition() const OVERRIDE {
2614 return kCondNE;
2615 }
2616
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002617 private:
2618 DISALLOW_COPY_AND_ASSIGN(HEqual);
2619};
2620
Dave Allison20dfc792014-06-16 20:44:29 -07002621class HNotEqual : public HCondition {
2622 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002623 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2624 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002625
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002626 bool IsCommutative() const OVERRIDE { return true; }
2627
Roland Levillain9867bc72015-08-05 10:21:34 +01002628 template <typename T> bool Compute(T x, T y) const { return x != y; }
2629
2630 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002631 return GetBlock()->GetGraph()->GetIntConstant(
2632 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002633 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002634 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002635 return GetBlock()->GetGraph()->GetIntConstant(
2636 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002637 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002638
Dave Allison20dfc792014-06-16 20:44:29 -07002639 DECLARE_INSTRUCTION(NotEqual);
2640
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002641 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002642 return kCondNE;
2643 }
2644
Mark Mendellc4701932015-04-10 13:18:51 -04002645 IfCondition GetOppositeCondition() const OVERRIDE {
2646 return kCondEQ;
2647 }
2648
Dave Allison20dfc792014-06-16 20:44:29 -07002649 private:
2650 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2651};
2652
2653class HLessThan : public HCondition {
2654 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002655 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2656 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002657
Roland Levillain9867bc72015-08-05 10:21:34 +01002658 template <typename T> bool Compute(T x, T y) const { return x < y; }
2659
2660 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002661 return GetBlock()->GetGraph()->GetIntConstant(
2662 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002663 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002664 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002665 return GetBlock()->GetGraph()->GetIntConstant(
2666 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002667 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002668
Dave Allison20dfc792014-06-16 20:44:29 -07002669 DECLARE_INSTRUCTION(LessThan);
2670
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002671 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002672 return kCondLT;
2673 }
2674
Mark Mendellc4701932015-04-10 13:18:51 -04002675 IfCondition GetOppositeCondition() const OVERRIDE {
2676 return kCondGE;
2677 }
2678
Dave Allison20dfc792014-06-16 20:44:29 -07002679 private:
2680 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2681};
2682
2683class HLessThanOrEqual : public HCondition {
2684 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002685 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2686 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002687
Roland Levillain9867bc72015-08-05 10:21:34 +01002688 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2689
2690 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002691 return GetBlock()->GetGraph()->GetIntConstant(
2692 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002693 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002694 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002695 return GetBlock()->GetGraph()->GetIntConstant(
2696 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002697 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002698
Dave Allison20dfc792014-06-16 20:44:29 -07002699 DECLARE_INSTRUCTION(LessThanOrEqual);
2700
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002701 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002702 return kCondLE;
2703 }
2704
Mark Mendellc4701932015-04-10 13:18:51 -04002705 IfCondition GetOppositeCondition() const OVERRIDE {
2706 return kCondGT;
2707 }
2708
Dave Allison20dfc792014-06-16 20:44:29 -07002709 private:
2710 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2711};
2712
2713class HGreaterThan : public HCondition {
2714 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002715 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2716 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002717
Roland Levillain9867bc72015-08-05 10:21:34 +01002718 template <typename T> bool Compute(T x, T y) const { return x > y; }
2719
2720 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721 return GetBlock()->GetGraph()->GetIntConstant(
2722 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002723 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002724 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002725 return GetBlock()->GetGraph()->GetIntConstant(
2726 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002727 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002728
Dave Allison20dfc792014-06-16 20:44:29 -07002729 DECLARE_INSTRUCTION(GreaterThan);
2730
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002731 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002732 return kCondGT;
2733 }
2734
Mark Mendellc4701932015-04-10 13:18:51 -04002735 IfCondition GetOppositeCondition() const OVERRIDE {
2736 return kCondLE;
2737 }
2738
Dave Allison20dfc792014-06-16 20:44:29 -07002739 private:
2740 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2741};
2742
2743class HGreaterThanOrEqual : public HCondition {
2744 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002745 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2746 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002747
Roland Levillain9867bc72015-08-05 10:21:34 +01002748 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2749
2750 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002751 return GetBlock()->GetGraph()->GetIntConstant(
2752 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002753 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002754 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002755 return GetBlock()->GetGraph()->GetIntConstant(
2756 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002757 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002758
Dave Allison20dfc792014-06-16 20:44:29 -07002759 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2760
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002761 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002762 return kCondGE;
2763 }
2764
Mark Mendellc4701932015-04-10 13:18:51 -04002765 IfCondition GetOppositeCondition() const OVERRIDE {
2766 return kCondLT;
2767 }
2768
Dave Allison20dfc792014-06-16 20:44:29 -07002769 private:
2770 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2771};
2772
2773
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002774// Instruction to check how two inputs compare to each other.
2775// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2776class HCompare : public HBinaryOperation {
2777 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002778 HCompare(Primitive::Type type,
2779 HInstruction* first,
2780 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002781 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002782 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002783 : HBinaryOperation(Primitive::kPrimInt,
2784 first,
2785 second,
2786 SideEffectsForArchRuntimeCalls(type),
2787 dex_pc),
2788 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002789 DCHECK_EQ(type, first->GetType());
2790 DCHECK_EQ(type, second->GetType());
2791 }
2792
Roland Levillain9867bc72015-08-05 10:21:34 +01002793 template <typename T>
2794 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002795
Roland Levillain9867bc72015-08-05 10:21:34 +01002796 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002797 return GetBlock()->GetGraph()->GetIntConstant(
2798 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002799 }
2800 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002801 return GetBlock()->GetGraph()->GetIntConstant(
2802 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002803 }
2804
Calin Juravleddb7df22014-11-25 20:56:51 +00002805 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2806 return bias_ == other->AsCompare()->bias_;
2807 }
2808
Mark Mendellc4701932015-04-10 13:18:51 -04002809 ComparisonBias GetBias() const { return bias_; }
2810
Roland Levillain4fa13f62015-07-06 18:11:54 +01002811 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002812
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002813
2814 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2815 // MIPS64 uses a runtime call for FP comparisons.
2816 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2817 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002818
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002819 DECLARE_INSTRUCTION(Compare);
2820
2821 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002822 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002823
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002824 DISALLOW_COPY_AND_ASSIGN(HCompare);
2825};
2826
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002827// A local in the graph. Corresponds to a Dex register.
2828class HLocal : public HTemplateInstruction<0> {
2829 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002830 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002831 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002832
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002833 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002834
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002835 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002836
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002837 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002838 // The Dex register number.
2839 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002840
2841 DISALLOW_COPY_AND_ASSIGN(HLocal);
2842};
2843
2844// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002845class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002846 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002847 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2848 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002849 SetRawInputAt(0, local);
2850 }
2851
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002852 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2853
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002854 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002855
2856 private:
2857 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2858};
2859
2860// Store a value in a given local. This instruction has two inputs: the value
2861// and the local.
2862class HStoreLocal : public HTemplateInstruction<2> {
2863 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002864 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2865 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002866 SetRawInputAt(0, local);
2867 SetRawInputAt(1, value);
2868 }
2869
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002870 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2871
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002872 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002873
2874 private:
2875 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2876};
2877
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002878class HFloatConstant : public HConstant {
2879 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002880 float GetValue() const { return value_; }
2881
David Brazdil77a48ae2015-09-15 12:34:04 +00002882 uint64_t GetValueAsUint64() const OVERRIDE {
2883 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2884 }
2885
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002886 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002887 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002888 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002889 }
2890
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002891 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002892
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002893 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002894 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002895 }
2896 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002897 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002898 }
2899 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002900 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2901 }
2902 bool IsNaN() const {
2903 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002904 }
2905
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002906 DECLARE_INSTRUCTION(FloatConstant);
2907
2908 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002909 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2910 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2911 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2912 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002913
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002914 const float value_;
2915
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002916 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002917 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002918 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002919 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2920};
2921
2922class HDoubleConstant : public HConstant {
2923 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002924 double GetValue() const { return value_; }
2925
David Brazdil77a48ae2015-09-15 12:34:04 +00002926 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002928 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002929 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002930 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002931 }
2932
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002933 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002934
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002935 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002936 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002937 }
2938 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002939 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002940 }
2941 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002942 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2943 }
2944 bool IsNaN() const {
2945 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002946 }
2947
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002948 DECLARE_INSTRUCTION(DoubleConstant);
2949
2950 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002951 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2952 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2953 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2954 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002955
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002956 const double value_;
2957
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002958 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002959 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002960 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002961 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2962};
2963
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002964enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002965#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002966#include "intrinsics_list.h"
2967 kNone,
2968 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2969#undef INTRINSICS_LIST
2970#undef OPTIMIZING_INTRINSICS
2971};
2972std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2973
Agi Csaki05f20562015-08-19 14:58:14 -07002974enum IntrinsicNeedsEnvironmentOrCache {
2975 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
2976 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07002977};
2978
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002979class HInvoke : public HInstruction {
2980 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002981 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002982
2983 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2984 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07002985 bool NeedsEnvironment() const OVERRIDE {
2986 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
2987 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002988
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002989 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002990 SetRawInputAt(index, argument);
2991 }
2992
Roland Levillain3e3d7332015-04-28 11:00:54 +01002993 // Return the number of arguments. This number can be lower than
2994 // the number of inputs returned by InputCount(), as some invoke
2995 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2996 // inputs at the end of their list of inputs.
2997 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2998
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002999 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003000
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003001
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003002 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003003 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003004
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003005 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3006
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003007 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003008 return intrinsic_;
3009 }
3010
Agi Csaki05f20562015-08-19 14:58:14 -07003011 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003012 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07003013 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003014 }
3015
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003016 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003017 return GetEnvironment()->GetParent() != nullptr;
3018 }
3019
3020 bool CanThrow() const OVERRIDE { return true; }
3021
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003022 DECLARE_INSTRUCTION(Invoke);
3023
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003024 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003025 HInvoke(ArenaAllocator* arena,
3026 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003027 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003028 Primitive::Type return_type,
3029 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003030 uint32_t dex_method_index,
3031 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003032 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003033 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003034 number_of_arguments_(number_of_arguments),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003035 inputs_(number_of_arguments + number_of_other_inputs, arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003036 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003037 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003038 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003039 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07003040 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003041 }
3042
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003043 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
3044 DCHECK_LT(index, InputCount());
3045 return inputs_[index];
3046 }
3047
David Brazdil1abb4192015-02-17 18:33:36 +00003048 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003049 DCHECK_LT(index, InputCount());
3050 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003051 }
3052
Roland Levillain3e3d7332015-04-28 11:00:54 +01003053 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003054 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003055 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003056 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003057 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003058 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003059 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003060
3061 private:
3062 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3063};
3064
Calin Juravle175dc732015-08-25 15:42:32 +01003065class HInvokeUnresolved : public HInvoke {
3066 public:
3067 HInvokeUnresolved(ArenaAllocator* arena,
3068 uint32_t number_of_arguments,
3069 Primitive::Type return_type,
3070 uint32_t dex_pc,
3071 uint32_t dex_method_index,
3072 InvokeType invoke_type)
3073 : HInvoke(arena,
3074 number_of_arguments,
3075 0u /* number_of_other_inputs */,
3076 return_type,
3077 dex_pc,
3078 dex_method_index,
3079 invoke_type) {
3080 }
3081
3082 DECLARE_INSTRUCTION(InvokeUnresolved);
3083
3084 private:
3085 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3086};
3087
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003088class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003089 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003090 // Requirements of this method call regarding the class
3091 // initialization (clinit) check of its declaring class.
3092 enum class ClinitCheckRequirement {
3093 kNone, // Class already initialized.
3094 kExplicit, // Static call having explicit clinit check as last input.
3095 kImplicit, // Static call implicitly requiring a clinit check.
3096 };
3097
Vladimir Marko58155012015-08-19 12:49:41 +00003098 // Determines how to load the target ArtMethod*.
3099 enum class MethodLoadKind {
3100 // Use a String init ArtMethod* loaded from Thread entrypoints.
3101 kStringInit,
3102
3103 // Use the method's own ArtMethod* loaded by the register allocator.
3104 kRecursive,
3105
3106 // Use ArtMethod* at a known address, embed the direct address in the code.
3107 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3108 kDirectAddress,
3109
3110 // Use ArtMethod* at an address that will be known at link time, embed the direct
3111 // address in the code. If the image is relocatable, emit .patch_oat entry.
3112 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3113 // the image relocatable or not.
3114 kDirectAddressWithFixup,
3115
3116 // Load from resoved methods array in the dex cache using a PC-relative load.
3117 // Used when we need to use the dex cache, for example for invoke-static that
3118 // may cause class initialization (the entry may point to a resolution method),
3119 // and we know that we can access the dex cache arrays using a PC-relative load.
3120 kDexCachePcRelative,
3121
3122 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3123 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3124 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3125 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3126 kDexCacheViaMethod,
3127 };
3128
3129 // Determines the location of the code pointer.
3130 enum class CodePtrLocation {
3131 // Recursive call, use local PC-relative call instruction.
3132 kCallSelf,
3133
3134 // Use PC-relative call instruction patched at link time.
3135 // Used for calls within an oat file, boot->boot or app->app.
3136 kCallPCRelative,
3137
3138 // Call to a known target address, embed the direct address in code.
3139 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3140 kCallDirect,
3141
3142 // Call to a target address that will be known at link time, embed the direct
3143 // address in code. If the image is relocatable, emit .patch_oat entry.
3144 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3145 // the image relocatable or not.
3146 kCallDirectWithFixup,
3147
3148 // Use code pointer from the ArtMethod*.
3149 // Used when we don't know the target code. This is also the last-resort-kind used when
3150 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3151 kCallArtMethod,
3152 };
3153
3154 struct DispatchInfo {
3155 const MethodLoadKind method_load_kind;
3156 const CodePtrLocation code_ptr_location;
3157 // The method load data holds
3158 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3159 // Note that there are multiple string init methods, each having its own offset.
3160 // - the method address for kDirectAddress
3161 // - the dex cache arrays offset for kDexCachePcRel.
3162 const uint64_t method_load_data;
3163 const uint64_t direct_code_ptr;
3164 };
3165
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003166 HInvokeStaticOrDirect(ArenaAllocator* arena,
3167 uint32_t number_of_arguments,
3168 Primitive::Type return_type,
3169 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003170 uint32_t method_index,
3171 MethodReference target_method,
3172 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003173 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003174 InvokeType invoke_type,
3175 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003176 : HInvoke(arena,
3177 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003178 // There is one extra argument for the HCurrentMethod node, and
3179 // potentially one other if the clinit check is explicit, and one other
3180 // if the method is a string factory.
3181 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003182 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003183 return_type,
3184 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003185 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003186 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003187 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003188 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003189 target_method_(target_method),
3190 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003191
Calin Juravle641547a2015-04-21 22:08:51 +01003192 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3193 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003194 // We access the method via the dex cache so we can't do an implicit null check.
3195 // TODO: for intrinsics we can generate implicit null checks.
3196 return false;
3197 }
3198
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003199 bool CanBeNull() const OVERRIDE {
3200 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3201 }
3202
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003203 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003204 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3205 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3206 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003207 bool NeedsDexCache() const OVERRIDE {
3208 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3209 return !IsRecursive() && !IsStringInit();
3210 }
Vladimir Marko58155012015-08-19 12:49:41 +00003211 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003212 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003213 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3214 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3215 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3216 MethodReference GetTargetMethod() const { return target_method_; }
3217
3218 int32_t GetStringInitOffset() const {
3219 DCHECK(IsStringInit());
3220 return dispatch_info_.method_load_data;
3221 }
3222
3223 uint64_t GetMethodAddress() const {
3224 DCHECK(HasMethodAddress());
3225 return dispatch_info_.method_load_data;
3226 }
3227
3228 uint32_t GetDexCacheArrayOffset() const {
3229 DCHECK(HasPcRelDexCache());
3230 return dispatch_info_.method_load_data;
3231 }
3232
3233 uint64_t GetDirectCodePtr() const {
3234 DCHECK(HasDirectCodePtr());
3235 return dispatch_info_.direct_code_ptr;
3236 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003237
Calin Juravle68ad6492015-08-18 17:08:12 +01003238 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3239
Roland Levillain4c0eb422015-04-24 16:43:49 +01003240 // Is this instruction a call to a static method?
3241 bool IsStatic() const {
3242 return GetInvokeType() == kStatic;
3243 }
3244
Roland Levillain3e3d7332015-04-28 11:00:54 +01003245 // Remove the art::HLoadClass instruction set as last input by
3246 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3247 // the initial art::HClinitCheck instruction (only relevant for
3248 // static calls with explicit clinit check).
3249 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003250 DCHECK(IsStaticWithExplicitClinitCheck());
3251 size_t last_input_index = InputCount() - 1;
3252 HInstruction* last_input = InputAt(last_input_index);
3253 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003254 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003255 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003256 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003257 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3258 DCHECK(IsStaticWithImplicitClinitCheck());
3259 }
3260
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003261 bool IsStringFactoryFor(HFakeString* str) const {
3262 if (!IsStringInit()) return false;
3263 // +1 for the current method.
3264 if (InputCount() == (number_of_arguments_ + 1)) return false;
3265 return InputAt(InputCount() - 1)->AsFakeString() == str;
3266 }
3267
3268 void RemoveFakeStringArgumentAsLastInput() {
3269 DCHECK(IsStringInit());
3270 size_t last_input_index = InputCount() - 1;
3271 HInstruction* last_input = InputAt(last_input_index);
3272 DCHECK(last_input != nullptr);
3273 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3274 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003275 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003276 }
3277
Roland Levillain4c0eb422015-04-24 16:43:49 +01003278 // Is this a call to a static method whose declaring class has an
3279 // explicit intialization check in the graph?
3280 bool IsStaticWithExplicitClinitCheck() const {
3281 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3282 }
3283
3284 // Is this a call to a static method whose declaring class has an
3285 // implicit intialization check requirement?
3286 bool IsStaticWithImplicitClinitCheck() const {
3287 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3288 }
3289
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003290 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003291
Roland Levillain4c0eb422015-04-24 16:43:49 +01003292 protected:
3293 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3294 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3295 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3296 HInstruction* input = input_record.GetInstruction();
3297 // `input` is the last input of a static invoke marked as having
3298 // an explicit clinit check. It must either be:
3299 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3300 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3301 DCHECK(input != nullptr);
3302 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3303 }
3304 return input_record;
3305 }
3306
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003307 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003308 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003309 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003310 // The target method may refer to different dex file or method index than the original
3311 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3312 // in derived class to increase visibility.
3313 MethodReference target_method_;
3314 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003315
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003316 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003317};
3318
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003319class HInvokeVirtual : public HInvoke {
3320 public:
3321 HInvokeVirtual(ArenaAllocator* arena,
3322 uint32_t number_of_arguments,
3323 Primitive::Type return_type,
3324 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003325 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003326 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003327 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003328 vtable_index_(vtable_index) {}
3329
Calin Juravle641547a2015-04-21 22:08:51 +01003330 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003331 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003332 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003333 }
3334
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003335 uint32_t GetVTableIndex() const { return vtable_index_; }
3336
3337 DECLARE_INSTRUCTION(InvokeVirtual);
3338
3339 private:
3340 const uint32_t vtable_index_;
3341
3342 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3343};
3344
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003345class HInvokeInterface : public HInvoke {
3346 public:
3347 HInvokeInterface(ArenaAllocator* arena,
3348 uint32_t number_of_arguments,
3349 Primitive::Type return_type,
3350 uint32_t dex_pc,
3351 uint32_t dex_method_index,
3352 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003353 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003354 imt_index_(imt_index) {}
3355
Calin Juravle641547a2015-04-21 22:08:51 +01003356 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003357 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003358 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003359 }
3360
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003361 uint32_t GetImtIndex() const { return imt_index_; }
3362 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3363
3364 DECLARE_INSTRUCTION(InvokeInterface);
3365
3366 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003367 const uint32_t imt_index_;
3368
3369 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3370};
3371
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003372class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003373 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003374 HNewInstance(HCurrentMethod* current_method,
3375 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003376 uint16_t type_index,
3377 const DexFile& dex_file,
3378 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003379 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003380 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003381 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003382 entrypoint_(entrypoint) {
3383 SetRawInputAt(0, current_method);
3384 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003385
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003386 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003387 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003388
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003389 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003390 bool NeedsEnvironment() const OVERRIDE { return true; }
3391 // It may throw when called on:
3392 // - interfaces
3393 // - abstract/innaccessible/unknown classes
3394 // TODO: optimize when possible.
3395 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003396
Calin Juravle10e244f2015-01-26 18:54:32 +00003397 bool CanBeNull() const OVERRIDE { return false; }
3398
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003399 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3400
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003401 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003402
3403 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003404 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003405 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003406 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003407
3408 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3409};
3410
Roland Levillain88cb1752014-10-20 16:36:47 +01003411class HNeg : public HUnaryOperation {
3412 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003413 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3414 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003415
Roland Levillain9867bc72015-08-05 10:21:34 +01003416 template <typename T> T Compute(T x) const { return -x; }
3417
3418 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003419 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003420 }
3421 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003422 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003423 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003424
Roland Levillain88cb1752014-10-20 16:36:47 +01003425 DECLARE_INSTRUCTION(Neg);
3426
3427 private:
3428 DISALLOW_COPY_AND_ASSIGN(HNeg);
3429};
3430
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003431class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003432 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003433 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003434 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003435 uint32_t dex_pc,
3436 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003437 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003438 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003439 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003440 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003441 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003442 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003443 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003444 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003445 }
3446
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003447 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003448 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003449
3450 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003451 bool NeedsEnvironment() const OVERRIDE { return true; }
3452
Mingyao Yang0c365e62015-03-31 15:09:29 -07003453 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3454 bool CanThrow() const OVERRIDE { return true; }
3455
Calin Juravle10e244f2015-01-26 18:54:32 +00003456 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003457
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003458 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3459
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003460 DECLARE_INSTRUCTION(NewArray);
3461
3462 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003463 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003464 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003465 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003466
3467 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3468};
3469
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003470class HAdd : public HBinaryOperation {
3471 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003472 HAdd(Primitive::Type result_type,
3473 HInstruction* left,
3474 HInstruction* right,
3475 uint32_t dex_pc = kNoDexPc)
3476 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003477
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003478 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003479
Roland Levillain9867bc72015-08-05 10:21:34 +01003480 template <typename T> T Compute(T x, T y) const { return x + y; }
3481
3482 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003483 return GetBlock()->GetGraph()->GetIntConstant(
3484 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003485 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003486 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003487 return GetBlock()->GetGraph()->GetLongConstant(
3488 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003489 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003490
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003491 DECLARE_INSTRUCTION(Add);
3492
3493 private:
3494 DISALLOW_COPY_AND_ASSIGN(HAdd);
3495};
3496
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003497class HSub : public HBinaryOperation {
3498 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003499 HSub(Primitive::Type result_type,
3500 HInstruction* left,
3501 HInstruction* right,
3502 uint32_t dex_pc = kNoDexPc)
3503 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003504
Roland Levillain9867bc72015-08-05 10:21:34 +01003505 template <typename T> T Compute(T x, T y) const { return x - y; }
3506
3507 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003508 return GetBlock()->GetGraph()->GetIntConstant(
3509 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003510 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003511 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003512 return GetBlock()->GetGraph()->GetLongConstant(
3513 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003514 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003515
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003516 DECLARE_INSTRUCTION(Sub);
3517
3518 private:
3519 DISALLOW_COPY_AND_ASSIGN(HSub);
3520};
3521
Calin Juravle34bacdf2014-10-07 20:23:36 +01003522class HMul : public HBinaryOperation {
3523 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003524 HMul(Primitive::Type result_type,
3525 HInstruction* left,
3526 HInstruction* right,
3527 uint32_t dex_pc = kNoDexPc)
3528 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003529
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003530 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003531
Roland Levillain9867bc72015-08-05 10:21:34 +01003532 template <typename T> T Compute(T x, T y) const { return x * y; }
3533
3534 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003535 return GetBlock()->GetGraph()->GetIntConstant(
3536 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003537 }
3538 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003539 return GetBlock()->GetGraph()->GetLongConstant(
3540 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003541 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003542
3543 DECLARE_INSTRUCTION(Mul);
3544
3545 private:
3546 DISALLOW_COPY_AND_ASSIGN(HMul);
3547};
3548
Calin Juravle7c4954d2014-10-28 16:57:40 +00003549class HDiv : public HBinaryOperation {
3550 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003551 HDiv(Primitive::Type result_type,
3552 HInstruction* left,
3553 HInstruction* right,
3554 uint32_t dex_pc)
3555 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003556
Roland Levillain9867bc72015-08-05 10:21:34 +01003557 template <typename T>
3558 T Compute(T x, T y) const {
3559 // Our graph structure ensures we never have 0 for `y` during
3560 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003561 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003562 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003563 return (y == -1) ? -x : x / y;
3564 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003565
Roland Levillain9867bc72015-08-05 10:21:34 +01003566 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003567 return GetBlock()->GetGraph()->GetIntConstant(
3568 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003569 }
3570 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003571 return GetBlock()->GetGraph()->GetLongConstant(
3572 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003573 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003574
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003575 static SideEffects SideEffectsForArchRuntimeCalls() {
3576 // The generated code can use a runtime call.
3577 return SideEffects::CanTriggerGC();
3578 }
3579
Calin Juravle7c4954d2014-10-28 16:57:40 +00003580 DECLARE_INSTRUCTION(Div);
3581
3582 private:
3583 DISALLOW_COPY_AND_ASSIGN(HDiv);
3584};
3585
Calin Juravlebacfec32014-11-14 15:54:36 +00003586class HRem : public HBinaryOperation {
3587 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003588 HRem(Primitive::Type result_type,
3589 HInstruction* left,
3590 HInstruction* right,
3591 uint32_t dex_pc)
3592 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003593
Roland Levillain9867bc72015-08-05 10:21:34 +01003594 template <typename T>
3595 T Compute(T x, T y) const {
3596 // Our graph structure ensures we never have 0 for `y` during
3597 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003598 DCHECK_NE(y, 0);
3599 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3600 return (y == -1) ? 0 : x % y;
3601 }
3602
Roland Levillain9867bc72015-08-05 10:21:34 +01003603 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003604 return GetBlock()->GetGraph()->GetIntConstant(
3605 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003606 }
3607 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003608 return GetBlock()->GetGraph()->GetLongConstant(
3609 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003610 }
3611
Calin Juravlebacfec32014-11-14 15:54:36 +00003612
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003613 static SideEffects SideEffectsForArchRuntimeCalls() {
3614 return SideEffects::CanTriggerGC();
3615 }
3616
Calin Juravlebacfec32014-11-14 15:54:36 +00003617 DECLARE_INSTRUCTION(Rem);
3618
3619 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003620 DISALLOW_COPY_AND_ASSIGN(HRem);
3621};
3622
Calin Juravled0d48522014-11-04 16:40:20 +00003623class HDivZeroCheck : public HExpression<1> {
3624 public:
3625 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003626 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003627 SetRawInputAt(0, value);
3628 }
3629
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003630 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3631
Calin Juravled0d48522014-11-04 16:40:20 +00003632 bool CanBeMoved() const OVERRIDE { return true; }
3633
3634 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3635 UNUSED(other);
3636 return true;
3637 }
3638
3639 bool NeedsEnvironment() const OVERRIDE { return true; }
3640 bool CanThrow() const OVERRIDE { return true; }
3641
Calin Juravled0d48522014-11-04 16:40:20 +00003642 DECLARE_INSTRUCTION(DivZeroCheck);
3643
3644 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003645 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3646};
3647
Calin Juravle9aec02f2014-11-18 23:06:35 +00003648class HShl : public HBinaryOperation {
3649 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003650 HShl(Primitive::Type result_type,
3651 HInstruction* left,
3652 HInstruction* right,
3653 uint32_t dex_pc = kNoDexPc)
3654 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003655
Roland Levillain9867bc72015-08-05 10:21:34 +01003656 template <typename T, typename U, typename V>
3657 T Compute(T x, U y, V max_shift_value) const {
3658 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3659 "V is not the unsigned integer type corresponding to T");
3660 return x << (y & max_shift_value);
3661 }
3662
3663 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3664 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003665 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003666 }
3667 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3668 // case is handled as `x << static_cast<int>(y)`.
3669 HConstant* Evaluate(HLongConstant* x, HIntConstant* 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 }
3673 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3674 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003675 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003676 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003677
3678 DECLARE_INSTRUCTION(Shl);
3679
3680 private:
3681 DISALLOW_COPY_AND_ASSIGN(HShl);
3682};
3683
3684class HShr : public HBinaryOperation {
3685 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003686 HShr(Primitive::Type result_type,
3687 HInstruction* left,
3688 HInstruction* right,
3689 uint32_t dex_pc = kNoDexPc)
3690 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003691
Roland Levillain9867bc72015-08-05 10:21:34 +01003692 template <typename T, typename U, typename V>
3693 T Compute(T x, U y, V max_shift_value) const {
3694 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3695 "V is not the unsigned integer type corresponding to T");
3696 return x >> (y & max_shift_value);
3697 }
3698
3699 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3700 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003701 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003702 }
3703 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3704 // case is handled as `x >> static_cast<int>(y)`.
3705 HConstant* Evaluate(HLongConstant* x, HIntConstant* 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 }
3709 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3710 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003711 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003712 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003713
3714 DECLARE_INSTRUCTION(Shr);
3715
3716 private:
3717 DISALLOW_COPY_AND_ASSIGN(HShr);
3718};
3719
3720class HUShr : public HBinaryOperation {
3721 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003722 HUShr(Primitive::Type result_type,
3723 HInstruction* left,
3724 HInstruction* right,
3725 uint32_t dex_pc = kNoDexPc)
3726 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003727
Roland Levillain9867bc72015-08-05 10:21:34 +01003728 template <typename T, typename U, typename V>
3729 T Compute(T x, U y, V max_shift_value) const {
3730 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3731 "V is not the unsigned integer type corresponding to T");
3732 V ux = static_cast<V>(x);
3733 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003734 }
3735
Roland Levillain9867bc72015-08-05 10:21:34 +01003736 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3737 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003738 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003739 }
3740 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3741 // case is handled as `x >>> static_cast<int>(y)`.
3742 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3743 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003744 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003745 }
3746 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3747 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003748 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003749 }
3750
3751 DECLARE_INSTRUCTION(UShr);
3752
3753 private:
3754 DISALLOW_COPY_AND_ASSIGN(HUShr);
3755};
3756
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003757class HAnd : public HBinaryOperation {
3758 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003759 HAnd(Primitive::Type result_type,
3760 HInstruction* left,
3761 HInstruction* right,
3762 uint32_t dex_pc = kNoDexPc)
3763 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003764
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003765 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003766
Roland Levillain9867bc72015-08-05 10:21:34 +01003767 template <typename T, typename U>
3768 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3769
3770 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003771 return GetBlock()->GetGraph()->GetIntConstant(
3772 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003773 }
3774 HConstant* Evaluate(HIntConstant* x, HLongConstant* 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, HIntConstant* 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 }
3782 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003783 return GetBlock()->GetGraph()->GetLongConstant(
3784 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003785 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003786
3787 DECLARE_INSTRUCTION(And);
3788
3789 private:
3790 DISALLOW_COPY_AND_ASSIGN(HAnd);
3791};
3792
3793class HOr : public HBinaryOperation {
3794 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003795 HOr(Primitive::Type result_type,
3796 HInstruction* left,
3797 HInstruction* right,
3798 uint32_t dex_pc = kNoDexPc)
3799 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003800
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003801 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003802
Roland Levillain9867bc72015-08-05 10:21:34 +01003803 template <typename T, typename U>
3804 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3805
3806 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003807 return GetBlock()->GetGraph()->GetIntConstant(
3808 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003809 }
3810 HConstant* Evaluate(HIntConstant* x, HLongConstant* 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, HIntConstant* 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 }
3818 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003819 return GetBlock()->GetGraph()->GetLongConstant(
3820 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003821 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003822
3823 DECLARE_INSTRUCTION(Or);
3824
3825 private:
3826 DISALLOW_COPY_AND_ASSIGN(HOr);
3827};
3828
3829class HXor : public HBinaryOperation {
3830 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003831 HXor(Primitive::Type result_type,
3832 HInstruction* left,
3833 HInstruction* right,
3834 uint32_t dex_pc = kNoDexPc)
3835 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003836
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003837 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003838
Roland Levillain9867bc72015-08-05 10:21:34 +01003839 template <typename T, typename U>
3840 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3841
3842 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003843 return GetBlock()->GetGraph()->GetIntConstant(
3844 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003845 }
3846 HConstant* Evaluate(HIntConstant* x, HLongConstant* 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, HIntConstant* 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 }
3854 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003855 return GetBlock()->GetGraph()->GetLongConstant(
3856 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003857 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003858
3859 DECLARE_INSTRUCTION(Xor);
3860
3861 private:
3862 DISALLOW_COPY_AND_ASSIGN(HXor);
3863};
3864
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003865// The value of a parameter in this method. Its location depends on
3866// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003867class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003868 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003869 HParameterValue(uint8_t index,
3870 Primitive::Type parameter_type,
3871 bool is_this = false)
3872 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003873 index_(index),
3874 is_this_(is_this),
3875 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003876
3877 uint8_t GetIndex() const { return index_; }
3878
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003879 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3880 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003881
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003882 bool IsThis() const { return is_this_; }
3883
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003884 DECLARE_INSTRUCTION(ParameterValue);
3885
3886 private:
3887 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003888 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003889 const uint8_t index_;
3890
Calin Juravle10e244f2015-01-26 18:54:32 +00003891 // Whether or not the parameter value corresponds to 'this' argument.
3892 const bool is_this_;
3893
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003894 bool can_be_null_;
3895
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003896 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3897};
3898
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003899class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003900 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003901 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3902 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003903
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003904 bool CanBeMoved() const OVERRIDE { return true; }
3905 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003906 UNUSED(other);
3907 return true;
3908 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003909
Roland Levillain9867bc72015-08-05 10:21:34 +01003910 template <typename T> T Compute(T x) const { return ~x; }
3911
3912 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003913 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003914 }
3915 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003916 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003917 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003918
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003919 DECLARE_INSTRUCTION(Not);
3920
3921 private:
3922 DISALLOW_COPY_AND_ASSIGN(HNot);
3923};
3924
David Brazdil66d126e2015-04-03 16:02:44 +01003925class HBooleanNot : public HUnaryOperation {
3926 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003927 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3928 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003929
3930 bool CanBeMoved() const OVERRIDE { return true; }
3931 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3932 UNUSED(other);
3933 return true;
3934 }
3935
Roland Levillain9867bc72015-08-05 10:21:34 +01003936 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003937 DCHECK(IsUint<1>(x));
3938 return !x;
3939 }
3940
Roland Levillain9867bc72015-08-05 10:21:34 +01003941 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003942 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003943 }
3944 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3945 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003946 UNREACHABLE();
3947 }
3948
3949 DECLARE_INSTRUCTION(BooleanNot);
3950
3951 private:
3952 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3953};
3954
Roland Levillaindff1f282014-11-05 14:15:05 +00003955class HTypeConversion : public HExpression<1> {
3956 public:
3957 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003958 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003959 : HExpression(result_type,
3960 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
3961 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003962 SetRawInputAt(0, input);
3963 DCHECK_NE(input->GetType(), result_type);
3964 }
3965
3966 HInstruction* GetInput() const { return InputAt(0); }
3967 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3968 Primitive::Type GetResultType() const { return GetType(); }
3969
Roland Levillain624279f2014-12-04 11:54:28 +00003970 // Required by the x86 and ARM code generators when producing calls
3971 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003972
Roland Levillaindff1f282014-11-05 14:15:05 +00003973 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003974 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003975
Mark Mendelle82549b2015-05-06 10:55:34 -04003976 // Try to statically evaluate the conversion and return a HConstant
3977 // containing the result. If the input cannot be converted, return nullptr.
3978 HConstant* TryStaticEvaluation() const;
3979
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003980 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3981 Primitive::Type result_type) {
3982 // Some architectures may not require the 'GC' side effects, but at this point
3983 // in the compilation process we do not know what architecture we will
3984 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003985 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3986 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003987 return SideEffects::CanTriggerGC();
3988 }
3989 return SideEffects::None();
3990 }
3991
Roland Levillaindff1f282014-11-05 14:15:05 +00003992 DECLARE_INSTRUCTION(TypeConversion);
3993
3994 private:
3995 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3996};
3997
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003998static constexpr uint32_t kNoRegNumber = -1;
3999
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004000class HPhi : public HInstruction {
4001 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004002 HPhi(ArenaAllocator* arena,
4003 uint32_t reg_number,
4004 size_t number_of_inputs,
4005 Primitive::Type type,
4006 uint32_t dex_pc = kNoDexPc)
4007 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004008 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004009 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004010 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004011 is_live_(false),
4012 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004013 }
4014
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004015 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4016 static Primitive::Type ToPhiType(Primitive::Type type) {
4017 switch (type) {
4018 case Primitive::kPrimBoolean:
4019 case Primitive::kPrimByte:
4020 case Primitive::kPrimShort:
4021 case Primitive::kPrimChar:
4022 return Primitive::kPrimInt;
4023 default:
4024 return type;
4025 }
4026 }
4027
David Brazdilffee3d32015-07-06 11:48:53 +01004028 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4029
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004030 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004031
4032 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004033 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004034
Calin Juravle10e244f2015-01-26 18:54:32 +00004035 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004036 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004037
Calin Juravle10e244f2015-01-26 18:54:32 +00004038 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4039 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4040
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004041 uint32_t GetRegNumber() const { return reg_number_; }
4042
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004043 void SetDead() { is_live_ = false; }
4044 void SetLive() { is_live_ = true; }
4045 bool IsDead() const { return !is_live_; }
4046 bool IsLive() const { return is_live_; }
4047
David Brazdil77a48ae2015-09-15 12:34:04 +00004048 bool IsVRegEquivalentOf(HInstruction* other) const {
4049 return other != nullptr
4050 && other->IsPhi()
4051 && other->AsPhi()->GetBlock() == GetBlock()
4052 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4053 }
4054
Calin Juravlea4f88312015-04-16 12:57:19 +01004055 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4056 // An equivalent phi is a phi having the same dex register and type.
4057 // It assumes that phis with the same dex register are adjacent.
4058 HPhi* GetNextEquivalentPhiWithSameType() {
4059 HInstruction* next = GetNext();
4060 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4061 if (next->GetType() == GetType()) {
4062 return next->AsPhi();
4063 }
4064 next = next->GetNext();
4065 }
4066 return nullptr;
4067 }
4068
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004069 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004070
David Brazdil1abb4192015-02-17 18:33:36 +00004071 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004072 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
4073 DCHECK_LE(index, InputCount());
4074 return inputs_[index];
4075 }
David Brazdil1abb4192015-02-17 18:33:36 +00004076
4077 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004078 DCHECK_LE(index, InputCount());
4079 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004080 }
4081
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004082 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004083 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004084 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004085 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004086 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004087 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004088
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004089 DISALLOW_COPY_AND_ASSIGN(HPhi);
4090};
4091
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004092class HNullCheck : public HExpression<1> {
4093 public:
4094 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004095 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004096 SetRawInputAt(0, value);
4097 }
4098
Calin Juravle10e244f2015-01-26 18:54:32 +00004099 bool CanBeMoved() const OVERRIDE { return true; }
4100 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004101 UNUSED(other);
4102 return true;
4103 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004104
Calin Juravle10e244f2015-01-26 18:54:32 +00004105 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004106
Calin Juravle10e244f2015-01-26 18:54:32 +00004107 bool CanThrow() const OVERRIDE { return true; }
4108
4109 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004110
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004111
4112 DECLARE_INSTRUCTION(NullCheck);
4113
4114 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004115 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4116};
4117
4118class FieldInfo : public ValueObject {
4119 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004120 FieldInfo(MemberOffset field_offset,
4121 Primitive::Type field_type,
4122 bool is_volatile,
4123 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004124 const DexFile& dex_file,
4125 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004126 : field_offset_(field_offset),
4127 field_type_(field_type),
4128 is_volatile_(is_volatile),
4129 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004130 dex_file_(dex_file),
4131 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004132
4133 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004134 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004135 uint32_t GetFieldIndex() const { return index_; }
4136 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004137 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004138 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004139
4140 private:
4141 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004142 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004143 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004144 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004145 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004146 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004147};
4148
4149class HInstanceFieldGet : public HExpression<1> {
4150 public:
4151 HInstanceFieldGet(HInstruction* value,
4152 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004153 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004154 bool is_volatile,
4155 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004156 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004157 Handle<mirror::DexCache> dex_cache,
4158 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004159 : HExpression(
4160 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004161 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004162 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004163 SetRawInputAt(0, value);
4164 }
4165
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004166 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004167
4168 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4169 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4170 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004171 }
4172
Calin Juravle641547a2015-04-21 22:08:51 +01004173 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4174 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004175 }
4176
4177 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004178 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4179 }
4180
Calin Juravle52c48962014-12-16 17:02:57 +00004181 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004182 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004183 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004184 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004185
4186 DECLARE_INSTRUCTION(InstanceFieldGet);
4187
4188 private:
4189 const FieldInfo field_info_;
4190
4191 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4192};
4193
4194class HInstanceFieldSet : public HTemplateInstruction<2> {
4195 public:
4196 HInstanceFieldSet(HInstruction* object,
4197 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004198 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004199 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004200 bool is_volatile,
4201 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004202 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004203 Handle<mirror::DexCache> dex_cache,
4204 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004205 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004206 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004207 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004208 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004209 SetRawInputAt(0, object);
4210 SetRawInputAt(1, value);
4211 }
4212
Calin Juravle641547a2015-04-21 22:08:51 +01004213 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4214 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004215 }
4216
Calin Juravle52c48962014-12-16 17:02:57 +00004217 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004218 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004219 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004220 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004221 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004222 bool GetValueCanBeNull() const { return value_can_be_null_; }
4223 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004224
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004225 DECLARE_INSTRUCTION(InstanceFieldSet);
4226
4227 private:
4228 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004229 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004230
4231 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4232};
4233
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004234class HArrayGet : public HExpression<2> {
4235 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004236 HArrayGet(HInstruction* array,
4237 HInstruction* index,
4238 Primitive::Type type,
4239 uint32_t dex_pc = kNoDexPc)
4240 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004241 SetRawInputAt(0, array);
4242 SetRawInputAt(1, index);
4243 }
4244
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004245 bool CanBeMoved() const OVERRIDE { return true; }
4246 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004247 UNUSED(other);
4248 return true;
4249 }
Calin Juravle641547a2015-04-21 22:08:51 +01004250 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4251 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004252 // TODO: We can be smarter here.
4253 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4254 // which generates the implicit null check. There are cases when these can be removed
4255 // to produce better code. If we ever add optimizations to do so we should allow an
4256 // implicit check here (as long as the address falls in the first page).
4257 return false;
4258 }
4259
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004260 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004261
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004262 HInstruction* GetArray() const { return InputAt(0); }
4263 HInstruction* GetIndex() const { return InputAt(1); }
4264
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004265 DECLARE_INSTRUCTION(ArrayGet);
4266
4267 private:
4268 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4269};
4270
4271class HArraySet : public HTemplateInstruction<3> {
4272 public:
4273 HArraySet(HInstruction* array,
4274 HInstruction* index,
4275 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004276 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004277 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004278 : HTemplateInstruction(
4279 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004280 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004281 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004282 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4283 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004284 SetRawInputAt(0, array);
4285 SetRawInputAt(1, index);
4286 SetRawInputAt(2, value);
4287 }
4288
Calin Juravle77520bc2015-01-12 18:45:46 +00004289 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004290 // We currently always call a runtime method to catch array store
4291 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004292 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004293 }
4294
Mingyao Yang81014cb2015-06-02 03:16:27 -07004295 // Can throw ArrayStoreException.
4296 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4297
Calin Juravle641547a2015-04-21 22:08:51 +01004298 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4299 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004300 // TODO: Same as for ArrayGet.
4301 return false;
4302 }
4303
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004304 void ClearNeedsTypeCheck() {
4305 needs_type_check_ = false;
4306 }
4307
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004308 void ClearValueCanBeNull() {
4309 value_can_be_null_ = false;
4310 }
4311
4312 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004313 bool NeedsTypeCheck() const { return needs_type_check_; }
4314
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004315 HInstruction* GetArray() const { return InputAt(0); }
4316 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004317 HInstruction* GetValue() const { return InputAt(2); }
4318
4319 Primitive::Type GetComponentType() const {
4320 // The Dex format does not type floating point index operations. Since the
4321 // `expected_component_type_` is set during building and can therefore not
4322 // be correct, we also check what is the value type. If it is a floating
4323 // point type, we must use that type.
4324 Primitive::Type value_type = GetValue()->GetType();
4325 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4326 ? value_type
4327 : expected_component_type_;
4328 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004329
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004330 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4331 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4332 }
4333
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004334 DECLARE_INSTRUCTION(ArraySet);
4335
4336 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004337 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004338 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004339 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004340
4341 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4342};
4343
4344class HArrayLength : public HExpression<1> {
4345 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004346 explicit HArrayLength(HInstruction* array, uint32_t dex_pc = kNoDexPc)
4347 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004348 // Note that arrays do not change length, so the instruction does not
4349 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004350 SetRawInputAt(0, array);
4351 }
4352
Calin Juravle77520bc2015-01-12 18:45:46 +00004353 bool CanBeMoved() const OVERRIDE { return true; }
4354 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004355 UNUSED(other);
4356 return true;
4357 }
Calin Juravle641547a2015-04-21 22:08:51 +01004358 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4359 return obj == InputAt(0);
4360 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004361
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004362 DECLARE_INSTRUCTION(ArrayLength);
4363
4364 private:
4365 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4366};
4367
4368class HBoundsCheck : public HExpression<2> {
4369 public:
4370 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004371 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004372 DCHECK(index->GetType() == Primitive::kPrimInt);
4373 SetRawInputAt(0, index);
4374 SetRawInputAt(1, length);
4375 }
4376
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004377 bool CanBeMoved() const OVERRIDE { return true; }
4378 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004379 UNUSED(other);
4380 return true;
4381 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004382
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004383 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004384
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004385 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004386
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004387
4388 DECLARE_INSTRUCTION(BoundsCheck);
4389
4390 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004391 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4392};
4393
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004394/**
4395 * Some DEX instructions are folded into multiple HInstructions that need
4396 * to stay live until the last HInstruction. This class
4397 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004398 * HInstruction stays live. `index` represents the stack location index of the
4399 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400 */
4401class HTemporary : public HTemplateInstruction<0> {
4402 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004403 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4404 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004405
4406 size_t GetIndex() const { return index_; }
4407
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004408 Primitive::Type GetType() const OVERRIDE {
4409 // The previous instruction is the one that will be stored in the temporary location.
4410 DCHECK(GetPrevious() != nullptr);
4411 return GetPrevious()->GetType();
4412 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004413
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004414 DECLARE_INSTRUCTION(Temporary);
4415
4416 private:
4417 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004418 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4419};
4420
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004421class HSuspendCheck : public HTemplateInstruction<0> {
4422 public:
4423 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004424 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004425
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004426 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004427 return true;
4428 }
4429
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004430 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4431 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004432
4433 DECLARE_INSTRUCTION(SuspendCheck);
4434
4435 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004436 // Only used for code generation, in order to share the same slow path between back edges
4437 // of a same loop.
4438 SlowPathCode* slow_path_;
4439
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004440 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4441};
4442
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004443/**
4444 * Instruction to load a Class object.
4445 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004446class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004447 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004448 HLoadClass(HCurrentMethod* current_method,
4449 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004450 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004451 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004452 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004453 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004454 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004455 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004456 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004457 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004458 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004459 SetRawInputAt(0, current_method);
4460 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004461
4462 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004463
4464 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4465 return other->AsLoadClass()->type_index_ == type_index_;
4466 }
4467
4468 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4469
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004470 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004471 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004472 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004473
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004474 bool NeedsEnvironment() const OVERRIDE {
4475 // Will call runtime and load the class if the class is not loaded yet.
4476 // TODO: finer grain decision.
4477 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004478 }
4479
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004480 bool MustGenerateClinitCheck() const {
4481 return generate_clinit_check_;
4482 }
4483
Calin Juravle0ba218d2015-05-19 18:46:01 +01004484 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4485 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004486 }
4487
4488 bool CanCallRuntime() const {
4489 return MustGenerateClinitCheck() || !is_referrers_class_;
4490 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004491
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004492 bool CanThrow() const OVERRIDE {
4493 // May call runtime and and therefore can throw.
4494 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004495 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004496 }
4497
Calin Juravleacf735c2015-02-12 15:25:22 +00004498 ReferenceTypeInfo GetLoadedClassRTI() {
4499 return loaded_class_rti_;
4500 }
4501
4502 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4503 // Make sure we only set exact types (the loaded class should never be merged).
4504 DCHECK(rti.IsExact());
4505 loaded_class_rti_ = rti;
4506 }
4507
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004508 const DexFile& GetDexFile() { return dex_file_; }
4509
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004510 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4511
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004512 static SideEffects SideEffectsForArchRuntimeCalls() {
4513 return SideEffects::CanTriggerGC();
4514 }
4515
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004516 DECLARE_INSTRUCTION(LoadClass);
4517
4518 private:
4519 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004520 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004521 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004522 // Whether this instruction must generate the initialization check.
4523 // Used for code generation.
4524 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004525
Calin Juravleacf735c2015-02-12 15:25:22 +00004526 ReferenceTypeInfo loaded_class_rti_;
4527
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004528 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4529};
4530
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004531class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004532 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004533 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004534 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4535 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004536 SetRawInputAt(0, current_method);
4537 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004538
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004539 bool CanBeMoved() const OVERRIDE { return true; }
4540
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004541 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4542 return other->AsLoadString()->string_index_ == string_index_;
4543 }
4544
4545 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4546
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004547 uint32_t GetStringIndex() const { return string_index_; }
4548
4549 // TODO: Can we deopt or debug when we resolve a string?
4550 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004551 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004552 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004553
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004554 static SideEffects SideEffectsForArchRuntimeCalls() {
4555 return SideEffects::CanTriggerGC();
4556 }
4557
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004558 DECLARE_INSTRUCTION(LoadString);
4559
4560 private:
4561 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004562
4563 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4564};
4565
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004566/**
4567 * Performs an initialization check on its Class object input.
4568 */
4569class HClinitCheck : public HExpression<1> {
4570 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004571 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004572 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004573 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004574 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4575 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004576 SetRawInputAt(0, constant);
4577 }
4578
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004579 bool CanBeMoved() const OVERRIDE { return true; }
4580 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4581 UNUSED(other);
4582 return true;
4583 }
4584
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004585 bool NeedsEnvironment() const OVERRIDE {
4586 // May call runtime to initialize the class.
4587 return true;
4588 }
4589
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004590
4591 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4592
4593 DECLARE_INSTRUCTION(ClinitCheck);
4594
4595 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004596 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4597};
4598
4599class HStaticFieldGet : public HExpression<1> {
4600 public:
4601 HStaticFieldGet(HInstruction* cls,
4602 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004603 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004604 bool is_volatile,
4605 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004606 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004607 Handle<mirror::DexCache> dex_cache,
4608 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004609 : HExpression(
4610 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004611 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004612 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004613 SetRawInputAt(0, cls);
4614 }
4615
Calin Juravle52c48962014-12-16 17:02:57 +00004616
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004617 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004618
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004619 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004620 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4621 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004622 }
4623
4624 size_t ComputeHashCode() const OVERRIDE {
4625 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4626 }
4627
Calin Juravle52c48962014-12-16 17:02:57 +00004628 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004629 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4630 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004631 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004632
4633 DECLARE_INSTRUCTION(StaticFieldGet);
4634
4635 private:
4636 const FieldInfo field_info_;
4637
4638 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4639};
4640
4641class HStaticFieldSet : public HTemplateInstruction<2> {
4642 public:
4643 HStaticFieldSet(HInstruction* cls,
4644 HInstruction* value,
4645 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004646 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004647 bool is_volatile,
4648 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004649 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004650 Handle<mirror::DexCache> dex_cache,
4651 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004652 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004653 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004654 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004655 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004656 SetRawInputAt(0, cls);
4657 SetRawInputAt(1, value);
4658 }
4659
Calin Juravle52c48962014-12-16 17:02:57 +00004660 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004661 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4662 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004663 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004664
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004665 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004666 bool GetValueCanBeNull() const { return value_can_be_null_; }
4667 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004668
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004669 DECLARE_INSTRUCTION(StaticFieldSet);
4670
4671 private:
4672 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004673 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004674
4675 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4676};
4677
Calin Juravle23a8e352015-09-08 19:56:31 +01004678class HUnresolvedInstanceFieldGet : public HExpression<1> {
4679 public:
4680 HUnresolvedInstanceFieldGet(HInstruction* obj,
4681 Primitive::Type field_type,
4682 uint32_t field_index,
4683 uint32_t dex_pc)
4684 : HExpression(field_type, SideEffects::AllExceptGCDependency()),
4685 field_index_(field_index),
4686 dex_pc_(dex_pc) {
4687 SetRawInputAt(0, obj);
4688 }
4689
4690 bool NeedsEnvironment() const OVERRIDE { return true; }
4691 bool CanThrow() const OVERRIDE { return true; }
4692
4693 Primitive::Type GetFieldType() const { return GetType(); }
4694 uint32_t GetFieldIndex() const { return field_index_; }
4695 uint32_t GetDexPc() const { return dex_pc_; }
4696
4697 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4698
4699 private:
4700 const uint32_t field_index_;
4701 const uint32_t dex_pc_;
4702
4703 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4704};
4705
4706class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4707 public:
4708 HUnresolvedInstanceFieldSet(HInstruction* obj,
4709 HInstruction* value,
4710 Primitive::Type field_type,
4711 uint32_t field_index,
4712 uint32_t dex_pc)
4713 : HTemplateInstruction(SideEffects::AllExceptGCDependency()),
4714 field_type_(field_type),
4715 field_index_(field_index),
4716 dex_pc_(dex_pc) {
4717 DCHECK_EQ(field_type, value->GetType());
4718 SetRawInputAt(0, obj);
4719 SetRawInputAt(1, value);
4720 }
4721
4722 bool NeedsEnvironment() const OVERRIDE { return true; }
4723 bool CanThrow() const OVERRIDE { return true; }
4724
4725 Primitive::Type GetFieldType() const { return field_type_; }
4726 uint32_t GetFieldIndex() const { return field_index_; }
4727 uint32_t GetDexPc() const { return dex_pc_; }
4728
4729 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
4730
4731 private:
4732 const Primitive::Type field_type_;
4733 const uint32_t field_index_;
4734 const uint32_t dex_pc_;
4735
4736 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
4737};
4738
4739class HUnresolvedStaticFieldGet : public HExpression<0> {
4740 public:
4741 HUnresolvedStaticFieldGet(Primitive::Type field_type,
4742 uint32_t field_index,
4743 uint32_t dex_pc)
4744 : HExpression(field_type, SideEffects::AllExceptGCDependency()),
4745 field_index_(field_index),
4746 dex_pc_(dex_pc) {
4747 }
4748
4749 bool NeedsEnvironment() const OVERRIDE { return true; }
4750 bool CanThrow() const OVERRIDE { return true; }
4751
4752 Primitive::Type GetFieldType() const { return GetType(); }
4753 uint32_t GetFieldIndex() const { return field_index_; }
4754 uint32_t GetDexPc() const { return dex_pc_; }
4755
4756 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
4757
4758 private:
4759 const uint32_t field_index_;
4760 const uint32_t dex_pc_;
4761
4762 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
4763};
4764
4765class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
4766 public:
4767 HUnresolvedStaticFieldSet(HInstruction* value,
4768 Primitive::Type field_type,
4769 uint32_t field_index,
4770 uint32_t dex_pc)
4771 : HTemplateInstruction(SideEffects::AllExceptGCDependency()),
4772 field_type_(field_type),
4773 field_index_(field_index),
4774 dex_pc_(dex_pc) {
4775 DCHECK_EQ(field_type, value->GetType());
4776 SetRawInputAt(0, value);
4777 }
4778
4779 bool NeedsEnvironment() const OVERRIDE { return true; }
4780 bool CanThrow() const OVERRIDE { return true; }
4781
4782 Primitive::Type GetFieldType() const { return field_type_; }
4783 uint32_t GetFieldIndex() const { return field_index_; }
4784 uint32_t GetDexPc() const { return dex_pc_; }
4785
4786 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
4787
4788 private:
4789 const Primitive::Type field_type_;
4790 const uint32_t field_index_;
4791 const uint32_t dex_pc_;
4792
4793 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
4794};
4795
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004796// Implement the move-exception DEX instruction.
4797class HLoadException : public HExpression<0> {
4798 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004799 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4800 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004801
David Brazdilbbd733e2015-08-18 17:48:17 +01004802 bool CanBeNull() const OVERRIDE { return false; }
4803
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004804 DECLARE_INSTRUCTION(LoadException);
4805
4806 private:
4807 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4808};
4809
David Brazdilcb1c0552015-08-04 16:22:25 +01004810// Implicit part of move-exception which clears thread-local exception storage.
4811// Must not be removed because the runtime expects the TLS to get cleared.
4812class HClearException : public HTemplateInstruction<0> {
4813 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004814 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4815 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004816
4817 DECLARE_INSTRUCTION(ClearException);
4818
4819 private:
4820 DISALLOW_COPY_AND_ASSIGN(HClearException);
4821};
4822
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004823class HThrow : public HTemplateInstruction<1> {
4824 public:
4825 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004826 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004827 SetRawInputAt(0, exception);
4828 }
4829
4830 bool IsControlFlow() const OVERRIDE { return true; }
4831
4832 bool NeedsEnvironment() const OVERRIDE { return true; }
4833
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004834 bool CanThrow() const OVERRIDE { return true; }
4835
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004836
4837 DECLARE_INSTRUCTION(Throw);
4838
4839 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004840 DISALLOW_COPY_AND_ASSIGN(HThrow);
4841};
4842
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004843/**
4844 * Implementation strategies for the code generator of a HInstanceOf
4845 * or `HCheckCast`.
4846 */
4847enum class TypeCheckKind {
4848 kExactCheck, // Can do a single class compare.
4849 kClassHierarchyCheck, // Can just walk the super class chain.
4850 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
4851 kInterfaceCheck, // No optimization yet when checking against an interface.
4852 kArrayObjectCheck, // Can just check if the array is not primitive.
4853 kArrayCheck // No optimization yet when checking against a generic array.
4854};
4855
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004856class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004857 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004858 HInstanceOf(HInstruction* object,
4859 HLoadClass* constant,
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004860 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004861 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004862 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004863 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004864 dex_pc),
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004865 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004866 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004867 SetRawInputAt(0, object);
4868 SetRawInputAt(1, constant);
4869 }
4870
4871 bool CanBeMoved() const OVERRIDE { return true; }
4872
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004873 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004874 return true;
4875 }
4876
4877 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004878 return false;
4879 }
4880
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004881 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
4882
4883 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004884
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004885 // Used only in code generation.
4886 bool MustDoNullCheck() const { return must_do_null_check_; }
4887 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4888
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004889 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
4890 return (check_kind == TypeCheckKind::kExactCheck)
4891 ? SideEffects::None()
4892 // Mips currently does runtime calls for any other checks.
4893 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004894 }
4895
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004896 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004897
4898 private:
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004899 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004900 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004901
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004902 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4903};
4904
Calin Juravleb1498f62015-02-16 13:13:29 +00004905class HBoundType : public HExpression<1> {
4906 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004907 // Constructs an HBoundType with the given upper_bound.
4908 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004909 HBoundType(HInstruction* input,
4910 ReferenceTypeInfo upper_bound,
4911 bool upper_can_be_null,
4912 uint32_t dex_pc = kNoDexPc)
4913 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004914 upper_bound_(upper_bound),
4915 upper_can_be_null_(upper_can_be_null),
4916 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004917 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004918 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004919 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004920 }
4921
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004922 // GetUpper* should only be used in reference type propagation.
4923 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4924 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004925
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004926 void SetCanBeNull(bool can_be_null) {
4927 DCHECK(upper_can_be_null_ || !can_be_null);
4928 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004929 }
4930
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004931 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4932
Calin Juravleb1498f62015-02-16 13:13:29 +00004933 DECLARE_INSTRUCTION(BoundType);
4934
4935 private:
4936 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004937 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4938 // It is used to bound the type in cases like:
4939 // if (x instanceof ClassX) {
4940 // // uper_bound_ will be ClassX
4941 // }
4942 const ReferenceTypeInfo upper_bound_;
4943 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4944 // is false then can_be_null_ cannot be true).
4945 const bool upper_can_be_null_;
4946 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004947
4948 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4949};
4950
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004951class HCheckCast : public HTemplateInstruction<2> {
4952 public:
4953 HCheckCast(HInstruction* object,
4954 HLoadClass* constant,
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004955 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004956 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004957 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004958 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004959 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004960 SetRawInputAt(0, object);
4961 SetRawInputAt(1, constant);
4962 }
4963
4964 bool CanBeMoved() const OVERRIDE { return true; }
4965
4966 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4967 return true;
4968 }
4969
4970 bool NeedsEnvironment() const OVERRIDE {
4971 // Instruction may throw a CheckCastError.
4972 return true;
4973 }
4974
4975 bool CanThrow() const OVERRIDE { return true; }
4976
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004977 bool MustDoNullCheck() const { return must_do_null_check_; }
4978 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004979 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004980
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004981 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004982
4983 DECLARE_INSTRUCTION(CheckCast);
4984
4985 private:
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004986 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004987 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004988
4989 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004990};
4991
Calin Juravle27df7582015-04-17 19:12:31 +01004992class HMemoryBarrier : public HTemplateInstruction<0> {
4993 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004994 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004995 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004996 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004997 barrier_kind_(barrier_kind) {}
4998
4999 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5000
5001 DECLARE_INSTRUCTION(MemoryBarrier);
5002
5003 private:
5004 const MemBarrierKind barrier_kind_;
5005
5006 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5007};
5008
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005009class HMonitorOperation : public HTemplateInstruction<1> {
5010 public:
5011 enum OperationKind {
5012 kEnter,
5013 kExit,
5014 };
5015
5016 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005017 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005018 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5019 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005020 SetRawInputAt(0, object);
5021 }
5022
5023 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005024 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5025
5026 bool CanThrow() const OVERRIDE {
5027 // Verifier guarantees that monitor-exit cannot throw.
5028 // This is important because it allows the HGraphBuilder to remove
5029 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5030 return IsEnter();
5031 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005032
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005033
5034 bool IsEnter() const { return kind_ == kEnter; }
5035
5036 DECLARE_INSTRUCTION(MonitorOperation);
5037
Calin Juravle52c48962014-12-16 17:02:57 +00005038 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005039 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005040
5041 private:
5042 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5043};
5044
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005045/**
5046 * A HInstruction used as a marker for the replacement of new + <init>
5047 * of a String to a call to a StringFactory. Only baseline will see
5048 * the node at code generation, where it will be be treated as null.
5049 * When compiling non-baseline, `HFakeString` instructions are being removed
5050 * in the instruction simplifier.
5051 */
5052class HFakeString : public HTemplateInstruction<0> {
5053 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005054 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5055 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005056
5057 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5058
5059 DECLARE_INSTRUCTION(FakeString);
5060
5061 private:
5062 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5063};
5064
Vladimir Markof9f64412015-09-02 14:05:49 +01005065class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005066 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005067 MoveOperands(Location source,
5068 Location destination,
5069 Primitive::Type type,
5070 HInstruction* instruction)
5071 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005072
5073 Location GetSource() const { return source_; }
5074 Location GetDestination() const { return destination_; }
5075
5076 void SetSource(Location value) { source_ = value; }
5077 void SetDestination(Location value) { destination_ = value; }
5078
5079 // The parallel move resolver marks moves as "in-progress" by clearing the
5080 // destination (but not the source).
5081 Location MarkPending() {
5082 DCHECK(!IsPending());
5083 Location dest = destination_;
5084 destination_ = Location::NoLocation();
5085 return dest;
5086 }
5087
5088 void ClearPending(Location dest) {
5089 DCHECK(IsPending());
5090 destination_ = dest;
5091 }
5092
5093 bool IsPending() const {
5094 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5095 return destination_.IsInvalid() && !source_.IsInvalid();
5096 }
5097
5098 // True if this blocks a move from the given location.
5099 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005100 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005101 }
5102
5103 // A move is redundant if it's been eliminated, if its source and
5104 // destination are the same, or if its destination is unneeded.
5105 bool IsRedundant() const {
5106 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5107 }
5108
5109 // We clear both operands to indicate move that's been eliminated.
5110 void Eliminate() {
5111 source_ = destination_ = Location::NoLocation();
5112 }
5113
5114 bool IsEliminated() const {
5115 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5116 return source_.IsInvalid();
5117 }
5118
Alexey Frunze4dda3372015-06-01 18:31:49 -07005119 Primitive::Type GetType() const { return type_; }
5120
Nicolas Geoffray90218252015-04-15 11:56:51 +01005121 bool Is64BitMove() const {
5122 return Primitive::Is64BitType(type_);
5123 }
5124
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005125 HInstruction* GetInstruction() const { return instruction_; }
5126
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005127 private:
5128 Location source_;
5129 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005130 // The type this move is for.
5131 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005132 // The instruction this move is assocatied with. Null when this move is
5133 // for moving an input in the expected locations of user (including a phi user).
5134 // This is only used in debug mode, to ensure we do not connect interval siblings
5135 // in the same parallel move.
5136 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005137};
5138
5139static constexpr size_t kDefaultNumberOfMoves = 4;
5140
5141class HParallelMove : public HTemplateInstruction<0> {
5142 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005143 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
5144 : HTemplateInstruction(SideEffects::None(), dex_pc), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005145
Nicolas Geoffray90218252015-04-15 11:56:51 +01005146 void AddMove(Location source,
5147 Location destination,
5148 Primitive::Type type,
5149 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005150 DCHECK(source.IsValid());
5151 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005152 if (kIsDebugBuild) {
5153 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005154 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005155 if (moves_.Get(i).GetInstruction() == instruction) {
5156 // Special case the situation where the move is for the spill slot
5157 // of the instruction.
5158 if ((GetPrevious() == instruction)
5159 || ((GetPrevious() == nullptr)
5160 && instruction->IsPhi()
5161 && instruction->GetBlock() == GetBlock())) {
5162 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
5163 << "Doing parallel moves for the same instruction.";
5164 } else {
5165 DCHECK(false) << "Doing parallel moves for the same instruction.";
5166 }
5167 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005168 }
5169 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005170 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08005171 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005172 << "Overlapped destination for two moves in a parallel move: "
5173 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
5174 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005175 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005176 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01005177 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005178 }
5179
5180 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005181 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005182 }
5183
5184 size_t NumMoves() const { return moves_.Size(); }
5185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005186 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005187
5188 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005189 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005190
5191 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5192};
5193
Mark Mendell0616ae02015-04-17 12:49:27 -04005194} // namespace art
5195
5196#ifdef ART_ENABLE_CODEGEN_x86
5197#include "nodes_x86.h"
5198#endif
5199
5200namespace art {
5201
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005202class HGraphVisitor : public ValueObject {
5203 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005204 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5205 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005206
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005207 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005208 virtual void VisitBasicBlock(HBasicBlock* block);
5209
Roland Levillain633021e2014-10-01 14:12:25 +01005210 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005211 void VisitInsertionOrder();
5212
Roland Levillain633021e2014-10-01 14:12:25 +01005213 // Visit the graph following dominator tree reverse post-order.
5214 void VisitReversePostOrder();
5215
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005216 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005217
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005218 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005219#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005220 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5221
5222 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5223
5224#undef DECLARE_VISIT_INSTRUCTION
5225
5226 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005227 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005228
5229 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5230};
5231
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005232class HGraphDelegateVisitor : public HGraphVisitor {
5233 public:
5234 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5235 virtual ~HGraphDelegateVisitor() {}
5236
5237 // Visit functions that delegate to to super class.
5238#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005239 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005240
5241 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5242
5243#undef DECLARE_VISIT_INSTRUCTION
5244
5245 private:
5246 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5247};
5248
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005249class HInsertionOrderIterator : public ValueObject {
5250 public:
5251 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5252
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005253 bool Done() const { return index_ == graph_.GetBlocks().size(); }
5254 HBasicBlock* Current() const { return graph_.GetBlock(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005255 void Advance() { ++index_; }
5256
5257 private:
5258 const HGraph& graph_;
5259 size_t index_;
5260
5261 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5262};
5263
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005264class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005265 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005266 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5267 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005268 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005269 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005270
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005271 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5272 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005273 void Advance() { ++index_; }
5274
5275 private:
5276 const HGraph& graph_;
5277 size_t index_;
5278
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005279 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005280};
5281
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005282class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005283 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005284 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005285 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005286 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005287 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005288 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005289
5290 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005291 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005292 void Advance() { --index_; }
5293
5294 private:
5295 const HGraph& graph_;
5296 size_t index_;
5297
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005298 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005299};
5300
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005301class HLinearPostOrderIterator : public ValueObject {
5302 public:
5303 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005304 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005305
5306 bool Done() const { return index_ == 0; }
5307
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005308 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005309
5310 void Advance() {
5311 --index_;
5312 DCHECK_GE(index_, 0U);
5313 }
5314
5315 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005316 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005317 size_t index_;
5318
5319 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5320};
5321
5322class HLinearOrderIterator : public ValueObject {
5323 public:
5324 explicit HLinearOrderIterator(const HGraph& graph)
5325 : order_(graph.GetLinearOrder()), index_(0) {}
5326
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005327 bool Done() const { return index_ == order_.size(); }
5328 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005329 void Advance() { ++index_; }
5330
5331 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005332 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005333 size_t index_;
5334
5335 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5336};
5337
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005338// Iterator over the blocks that art part of the loop. Includes blocks part
5339// of an inner loop. The order in which the blocks are iterated is on their
5340// block id.
5341class HBlocksInLoopIterator : public ValueObject {
5342 public:
5343 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5344 : blocks_in_loop_(info.GetBlocks()),
5345 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5346 index_(0) {
5347 if (!blocks_in_loop_.IsBitSet(index_)) {
5348 Advance();
5349 }
5350 }
5351
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005352 bool Done() const { return index_ == blocks_.size(); }
5353 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005354 void Advance() {
5355 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005356 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005357 if (blocks_in_loop_.IsBitSet(index_)) {
5358 break;
5359 }
5360 }
5361 }
5362
5363 private:
5364 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005365 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005366 size_t index_;
5367
5368 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5369};
5370
Mingyao Yang3584bce2015-05-19 16:01:59 -07005371// Iterator over the blocks that art part of the loop. Includes blocks part
5372// of an inner loop. The order in which the blocks are iterated is reverse
5373// post order.
5374class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5375 public:
5376 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5377 : blocks_in_loop_(info.GetBlocks()),
5378 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5379 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005380 DCHECK(!blocks_.empty());
5381 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005382 Advance();
5383 }
5384 }
5385
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005386 bool Done() const { return index_ == blocks_.size(); }
5387 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005388 void Advance() {
5389 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005390 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5391 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005392 break;
5393 }
5394 }
5395 }
5396
5397 private:
5398 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005399 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005400 size_t index_;
5401
5402 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5403};
5404
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005405inline int64_t Int64FromConstant(HConstant* constant) {
5406 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5407 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5408 : constant->AsLongConstant()->GetValue();
5409}
5410
Vladimir Marko58155012015-08-19 12:49:41 +00005411inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5412 // For the purposes of the compiler, the dex files must actually be the same object
5413 // if we want to safely treat them as the same. This is especially important for JIT
5414 // as custom class loaders can open the same underlying file (or memory) multiple
5415 // times and provide different class resolution but no two class loaders should ever
5416 // use the same DexFile object - doing so is an unsupported hack that can lead to
5417 // all sorts of weird failures.
5418 return &lhs == &rhs;
5419}
5420
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005421} // namespace art
5422
5423#endif // ART_COMPILER_OPTIMIZING_NODES_H_