blob: 753c95b477f71851127f9003a7b15711d412a77e [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
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
David Brazdil8d5b8b22015-03-24 10:51:52 +000038class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000040class HFloatConstant;
41class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000044class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000046class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010048class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010049class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050class LocationSummary;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
53static const int kDefaultNumberOfBlocks = 8;
54static const int kDefaultNumberOfSuccessors = 2;
55static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010056static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000057static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058
Calin Juravle9aec02f2014-11-18 23:06:35 +000059static constexpr uint32_t kMaxIntShiftValue = 0x1f;
60static constexpr uint64_t kMaxLongShiftValue = 0x3f;
61
Dave Allison20dfc792014-06-16 20:44:29 -070062enum IfCondition {
63 kCondEQ,
64 kCondNE,
65 kCondLT,
66 kCondLE,
67 kCondGT,
68 kCondGE,
69};
70
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010071class HInstructionList {
72 public:
73 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
74
75 void AddInstruction(HInstruction* instruction);
76 void RemoveInstruction(HInstruction* instruction);
77
David Brazdilc3d743f2015-04-22 13:40:50 +010078 // Insert `instruction` before/after an existing instruction `cursor`.
79 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
80 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
81
Roland Levillain6b469232014-09-25 10:10:38 +010082 // Return true if this list contains `instruction`.
83 bool Contains(HInstruction* instruction) const;
84
Roland Levillainccc07a92014-09-16 14:48:16 +010085 // Return true if `instruction1` is found before `instruction2` in
86 // this instruction list and false otherwise. Abort if none
87 // of these instructions is found.
88 bool FoundBefore(const HInstruction* instruction1,
89 const HInstruction* instruction2) const;
90
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000091 bool IsEmpty() const { return first_instruction_ == nullptr; }
92 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
93
94 // Update the block of all instructions to be `block`.
95 void SetBlockOfInstructions(HBasicBlock* block) const;
96
97 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
98 void Add(const HInstructionList& instruction_list);
99
David Brazdil2d7352b2015-04-20 14:52:42 +0100100 // Return the number of instructions in the list. This is an expensive operation.
101 size_t CountSize() const;
102
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100103 private:
104 HInstruction* first_instruction_;
105 HInstruction* last_instruction_;
106
107 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000108 friend class HGraph;
109 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100110 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100111 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112
113 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
114};
115
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000116// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700117class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000118 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000119 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000120 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000121 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100122 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100123 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700124 entry_block_(nullptr),
125 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100126 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100127 number_of_vregs_(0),
128 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000129 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400130 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000131 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000132 current_instruction_id_(start_instruction_id),
133 cached_null_constant_(nullptr),
134 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
135 cached_long_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000136
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000137 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100138 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100139 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000140
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000141 HBasicBlock* GetEntryBlock() const { return entry_block_; }
142 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000143
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000144 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
145 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000146
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100148
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000149 // Try building the SSA form of this graph, with dominance computation and loop
150 // recognition. Returns whether it was successful in doing all these steps.
151 bool TryBuildingSsa() {
152 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000153 // The SSA builder requires loops to all be natural. Specifically, the dead phi
154 // elimination phase checks the consistency of the graph when doing a post-order
155 // visit for eliminating dead phis: a dead phi can only have loop header phi
156 // users remaining when being visited.
157 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000158 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000159 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 }
161
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000162 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000163 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100164 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000165
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000166 // Analyze all natural loops in this graph. Returns false if one
167 // loop is not natural, that is the header does not dominate the
168 // back edge.
169 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100170
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
172 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
173
David Brazdil2d7352b2015-04-20 14:52:42 +0100174 // Removes `block` from the graph.
175 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000176
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100177 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
178 void SimplifyLoop(HBasicBlock* header);
179
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000180 int32_t GetNextInstructionId() {
181 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000182 return current_instruction_id_++;
183 }
184
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000185 int32_t GetCurrentInstructionId() const {
186 return current_instruction_id_;
187 }
188
189 void SetCurrentInstructionId(int32_t id) {
190 current_instruction_id_ = id;
191 }
192
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100193 uint16_t GetMaximumNumberOfOutVRegs() const {
194 return maximum_number_of_out_vregs_;
195 }
196
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000197 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
198 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100199 }
200
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000201 void UpdateTemporariesVRegSlots(size_t slots) {
202 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100203 }
204
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000205 size_t GetTemporariesVRegSlots() const {
206 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100207 }
208
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100209 void SetNumberOfVRegs(uint16_t number_of_vregs) {
210 number_of_vregs_ = number_of_vregs;
211 }
212
213 uint16_t GetNumberOfVRegs() const {
214 return number_of_vregs_;
215 }
216
217 void SetNumberOfInVRegs(uint16_t value) {
218 number_of_in_vregs_ = value;
219 }
220
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100221 uint16_t GetNumberOfLocalVRegs() const {
222 return number_of_vregs_ - number_of_in_vregs_;
223 }
224
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100225 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
226 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100227 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100228
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100229 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
230 return linear_order_;
231 }
232
Mark Mendell1152c922015-04-24 17:06:35 -0400233 bool HasBoundsChecks() const {
234 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800235 }
236
Mark Mendell1152c922015-04-24 17:06:35 -0400237 void SetHasBoundsChecks(bool value) {
238 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800239 }
240
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000241 bool IsDebuggable() const { return debuggable_; }
242
David Brazdil8d5b8b22015-03-24 10:51:52 +0000243 // Returns a constant of the given type and value. If it does not exist
244 // already, it is created and inserted into the graph. Only integral types
Nicolas Geoffray067cae22015-04-26 16:43:00 +0000245 // are currently supported.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000246 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000247 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000248 HIntConstant* GetIntConstant(int32_t value) {
249 return CreateConstant(value, &cached_int_constants_);
250 }
251 HLongConstant* GetLongConstant(int64_t value) {
252 return CreateConstant(value, &cached_long_constants_);
253 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000254
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000255 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100256
257 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000258 void VisitBlockForDominatorTree(HBasicBlock* block,
259 HBasicBlock* predecessor,
260 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100261 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 void VisitBlockForBackEdges(HBasicBlock* block,
263 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100264 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000265 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100266 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000267
David Brazdil8d5b8b22015-03-24 10:51:52 +0000268 template <class InstType, typename ValueType>
269 InstType* CreateConstant(ValueType value, ArenaSafeMap<ValueType, InstType*>* cache);
270 void InsertConstant(HConstant* instruction);
271
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000272 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000273
274 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000275 GrowableArray<HBasicBlock*> blocks_;
276
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100277 // List of blocks to perform a reverse post order tree traversal.
278 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000279
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100280 // List of blocks to perform a linear order tree traversal.
281 GrowableArray<HBasicBlock*> linear_order_;
282
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000283 HBasicBlock* entry_block_;
284 HBasicBlock* exit_block_;
285
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100286 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100287 uint16_t maximum_number_of_out_vregs_;
288
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100289 // The number of virtual registers in this method. Contains the parameters.
290 uint16_t number_of_vregs_;
291
292 // The number of virtual registers used by parameters of this method.
293 uint16_t number_of_in_vregs_;
294
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000295 // Number of vreg size slots that the temporaries use (used in baseline compiler).
296 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100297
Mark Mendell1152c922015-04-24 17:06:35 -0400298 // Has bounds checks. We can totally skip BCE if it's false.
299 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800300
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000301 // Indicates whether the graph should be compiled in a way that
302 // ensures full debuggability. If false, we can apply more
303 // aggressive optimizations that may limit the level of debugging.
304 const bool debuggable_;
305
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000306 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000307 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000308
David Brazdil46e2a392015-03-16 17:31:52 +0000309 // Cached common constants often needed by optimization passes.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000310 HNullConstant* cached_null_constant_;
311 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
312 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000313
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100314 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000315 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000316 DISALLOW_COPY_AND_ASSIGN(HGraph);
317};
318
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700319class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320 public:
321 HLoopInformation(HBasicBlock* header, HGraph* graph)
322 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100323 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100325 // Make bit vector growable, as the number of blocks may change.
326 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100327
328 HBasicBlock* GetHeader() const {
329 return header_;
330 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000331
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000332 void SetHeader(HBasicBlock* block) {
333 header_ = block;
334 }
335
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100336 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
337 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
338 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
339
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000340 void AddBackEdge(HBasicBlock* back_edge) {
341 back_edges_.Add(back_edge);
342 }
343
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100344 void RemoveBackEdge(HBasicBlock* back_edge) {
345 back_edges_.Delete(back_edge);
346 }
347
David Brazdil46e2a392015-03-16 17:31:52 +0000348 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100349 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000350 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100351 }
352 return false;
353 }
354
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000355 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000356 return back_edges_.Size();
357 }
358
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100360
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100361 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
362 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100363 }
364
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100365 void ClearBackEdges() {
366 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100367 }
368
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100369 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
370 // that is the header dominates the back edge.
371 bool Populate();
372
373 // Returns whether this loop information contains `block`.
374 // Note that this loop information *must* be populated before entering this function.
375 bool Contains(const HBasicBlock& block) const;
376
377 // Returns whether this loop information is an inner loop of `other`.
378 // Note that `other` *must* be populated before entering this function.
379 bool IsIn(const HLoopInformation& other) const;
380
381 const ArenaBitVector& GetBlocks() const { return blocks_; }
382
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000383 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000384 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000385
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000386 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100387 // Internal recursive implementation of `Populate`.
388 void PopulateRecursive(HBasicBlock* block);
389
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000390 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100391 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000392 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100393 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000394
395 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
396};
397
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100398static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100399static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100400
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000401// A block in a method. Contains the list of instructions represented
402// as a double linked list. Each block knows its predecessors and
403// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100404
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700405class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000406 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100407 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000408 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000409 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
410 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000411 loop_information_(nullptr),
412 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100413 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100414 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100415 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100416 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000417 lifetime_end_(kNoLifetime),
418 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000419
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100420 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
421 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000422 }
423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100424 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
425 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000426 }
427
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100428 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
429 return dominated_blocks_;
430 }
431
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100432 bool IsEntryBlock() const {
433 return graph_->GetEntryBlock() == this;
434 }
435
436 bool IsExitBlock() const {
437 return graph_->GetExitBlock() == this;
438 }
439
David Brazdil46e2a392015-03-16 17:31:52 +0000440 bool IsSingleGoto() const;
441
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000442 void AddBackEdge(HBasicBlock* back_edge) {
443 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000444 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000445 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100446 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000447 loop_information_->AddBackEdge(back_edge);
448 }
449
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000450 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000451 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000452
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000453 int GetBlockId() const { return block_id_; }
454 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000455
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000456 HBasicBlock* GetDominator() const { return dominator_; }
457 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100458 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100459 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000460 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
461 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
462 if (dominated_blocks_.Get(i) == existing) {
463 dominated_blocks_.Put(i, new_block);
464 return;
465 }
466 }
467 LOG(FATAL) << "Unreachable";
468 UNREACHABLE();
469 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000470
471 int NumberOfBackEdges() const {
472 return loop_information_ == nullptr
473 ? 0
474 : loop_information_->NumberOfBackEdges();
475 }
476
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100477 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
478 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100479 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100480 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100481 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
482 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000483
484 void AddSuccessor(HBasicBlock* block) {
485 successors_.Add(block);
486 block->predecessors_.Add(this);
487 }
488
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100489 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
490 size_t successor_index = GetSuccessorIndexOf(existing);
491 DCHECK_NE(successor_index, static_cast<size_t>(-1));
492 existing->RemovePredecessor(this);
493 new_block->predecessors_.Add(this);
494 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000495 }
496
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000497 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
498 size_t predecessor_index = GetPredecessorIndexOf(existing);
499 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
500 existing->RemoveSuccessor(this);
501 new_block->successors_.Add(this);
502 predecessors_.Put(predecessor_index, new_block);
503 }
504
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100505 void RemovePredecessor(HBasicBlock* block) {
506 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100507 }
508
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000509 void RemoveSuccessor(HBasicBlock* block) {
510 successors_.Delete(block);
511 }
512
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100513 void ClearAllPredecessors() {
514 predecessors_.Reset();
515 }
516
517 void AddPredecessor(HBasicBlock* block) {
518 predecessors_.Add(block);
519 block->successors_.Add(this);
520 }
521
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100522 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100523 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100524 HBasicBlock* temp = predecessors_.Get(0);
525 predecessors_.Put(0, predecessors_.Get(1));
526 predecessors_.Put(1, temp);
527 }
528
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100529 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
530 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
531 if (predecessors_.Get(i) == predecessor) {
532 return i;
533 }
534 }
535 return -1;
536 }
537
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100538 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
539 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
540 if (successors_.Get(i) == successor) {
541 return i;
542 }
543 }
544 return -1;
545 }
546
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000547 // Split the block into two blocks just after `cursor`. Returns the newly
548 // created block. Note that this method just updates raw block information,
549 // like predecessors, successors, dominators, and instruction list. It does not
550 // update the graph, reverse post order, loop information, nor make sure the
551 // blocks are consistent (for example ending with a control flow instruction).
552 HBasicBlock* SplitAfter(HInstruction* cursor);
553
554 // Merge `other` at the end of `this`. Successors and dominated blocks of
555 // `other` are changed to be successors and dominated blocks of `this`. Note
556 // that this method does not update the graph, reverse post order, loop
557 // information, nor make sure the blocks are consistent (for example ending
558 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100559 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000560
561 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
562 // of `this` are moved to `other`.
563 // Note that this method does not update the graph, reverse post order, loop
564 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000565 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000566 void ReplaceWith(HBasicBlock* other);
567
David Brazdil2d7352b2015-04-20 14:52:42 +0100568 // Merge `other` at the end of `this`. This method updates loops, reverse post
569 // order, links to predecessors, successors, dominators and deletes the block
570 // from the graph. The two blocks must be successive, i.e. `this` the only
571 // predecessor of `other` and vice versa.
572 void MergeWith(HBasicBlock* other);
573
574 // Disconnects `this` from all its predecessors, successors and dominator,
575 // removes it from all loops it is included in and eventually from the graph.
576 // The block must not dominate any other block. Predecessors and successors
577 // are safely updated.
578 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000579
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000580 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100581 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100582 // Replace instruction `initial` with `replacement` within this block.
583 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
584 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100585 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100586 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000587 // RemoveInstruction and RemovePhi delete a given instruction from the respective
588 // instruction list. With 'ensure_safety' set to true, it verifies that the
589 // instruction is not in use and removes it from the use lists of its inputs.
590 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
591 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100592 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100593
594 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100595 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100596 }
597
Roland Levillain6b879dd2014-09-22 17:13:44 +0100598 bool IsLoopPreHeaderFirstPredecessor() const {
599 DCHECK(IsLoopHeader());
600 DCHECK(!GetPredecessors().IsEmpty());
601 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
602 }
603
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100604 HLoopInformation* GetLoopInformation() const {
605 return loop_information_;
606 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000607
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000608 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100609 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000610 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100611 void SetInLoop(HLoopInformation* info) {
612 if (IsLoopHeader()) {
613 // Nothing to do. This just means `info` is an outer loop.
614 } else if (loop_information_ == nullptr) {
615 loop_information_ = info;
616 } else if (loop_information_->Contains(*info->GetHeader())) {
617 // Block is currently part of an outer loop. Make it part of this inner loop.
618 // Note that a non loop header having a loop information means this loop information
619 // has already been populated
620 loop_information_ = info;
621 } else {
622 // Block is part of an inner loop. Do not update the loop information.
623 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
624 // at this point, because this method is being called while populating `info`.
625 }
626 }
627
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000628 // Raw update of the loop information.
629 void SetLoopInformation(HLoopInformation* info) {
630 loop_information_ = info;
631 }
632
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100633 bool IsInLoop() const { return loop_information_ != nullptr; }
634
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100635 // Returns wheter this block dominates the blocked passed as parameter.
636 bool Dominates(HBasicBlock* block) const;
637
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100638 size_t GetLifetimeStart() const { return lifetime_start_; }
639 size_t GetLifetimeEnd() const { return lifetime_end_; }
640
641 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
642 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
643
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100644 uint32_t GetDexPc() const { return dex_pc_; }
645
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000646 bool IsCatchBlock() const { return is_catch_block_; }
647 void SetIsCatchBlock() { is_catch_block_ = true; }
648
David Brazdil8d5b8b22015-03-24 10:51:52 +0000649 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000650 bool EndsWithIf() const;
651 bool HasSinglePhi() const;
652
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000653 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000654 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000655 GrowableArray<HBasicBlock*> predecessors_;
656 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100657 HInstructionList instructions_;
658 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000659 HLoopInformation* loop_information_;
660 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100661 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000662 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100663 // The dex program counter of the first instruction of this block.
664 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100665 size_t lifetime_start_;
666 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000667 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000668
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000669 friend class HGraph;
670 friend class HInstruction;
671
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000672 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
673};
674
David Brazdilb2bd1c52015-03-25 11:17:37 +0000675// Iterates over the LoopInformation of all loops which contain 'block'
676// from the innermost to the outermost.
677class HLoopInformationOutwardIterator : public ValueObject {
678 public:
679 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
680 : current_(block.GetLoopInformation()) {}
681
682 bool Done() const { return current_ == nullptr; }
683
684 void Advance() {
685 DCHECK(!Done());
686 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
687 }
688
689 HLoopInformation* Current() const {
690 DCHECK(!Done());
691 return current_;
692 }
693
694 private:
695 HLoopInformation* current_;
696
697 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
698};
699
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100700#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
701 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000702 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000703 M(ArrayGet, Instruction) \
704 M(ArrayLength, Instruction) \
705 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100706 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000707 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000708 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000709 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100710 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000711 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100712 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700713 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000714 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000715 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000716 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100717 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000718 M(Exit, Instruction) \
719 M(FloatConstant, Constant) \
720 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100721 M(GreaterThan, Condition) \
722 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100723 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000724 M(InstanceFieldGet, Instruction) \
725 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000726 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100727 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000728 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000729 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100730 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000731 M(LessThan, Condition) \
732 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000733 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000734 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100735 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000736 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100737 M(Local, Instruction) \
738 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100739 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000740 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000741 M(Mul, BinaryOperation) \
742 M(Neg, UnaryOperation) \
743 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100744 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100745 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000746 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000747 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000748 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000749 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100750 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000751 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100752 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000753 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100754 M(Return, Instruction) \
755 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000756 M(Shl, BinaryOperation) \
757 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100758 M(StaticFieldGet, Instruction) \
759 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100760 M(StoreLocal, Instruction) \
761 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100762 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000763 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000764 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000765 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000766 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000767 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000768
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100769#define FOR_EACH_INSTRUCTION(M) \
770 FOR_EACH_CONCRETE_INSTRUCTION(M) \
771 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100772 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100773 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100774 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700775
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100776#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000777FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
778#undef FORWARD_DECLARATION
779
Roland Levillainccc07a92014-09-16 14:48:16 +0100780#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000781 InstructionKind GetKind() const OVERRIDE { return k##type; } \
782 const char* DebugName() const OVERRIDE { return #type; } \
783 const H##type* As##type() const OVERRIDE { return this; } \
784 H##type* As##type() OVERRIDE { return this; } \
785 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100786 return other->Is##type(); \
787 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000788 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000789
David Brazdiled596192015-01-23 10:39:45 +0000790template <typename T> class HUseList;
791
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100792template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700793class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000794 public:
David Brazdiled596192015-01-23 10:39:45 +0000795 HUseListNode* GetPrevious() const { return prev_; }
796 HUseListNode* GetNext() const { return next_; }
797 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100798 size_t GetIndex() const { return index_; }
799
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000800 private:
David Brazdiled596192015-01-23 10:39:45 +0000801 HUseListNode(T user, size_t index)
802 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
803
804 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100805 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000806 HUseListNode<T>* prev_;
807 HUseListNode<T>* next_;
808
809 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000810
811 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
812};
813
David Brazdiled596192015-01-23 10:39:45 +0000814template <typename T>
815class HUseList : public ValueObject {
816 public:
817 HUseList() : first_(nullptr) {}
818
819 void Clear() {
820 first_ = nullptr;
821 }
822
823 // Adds a new entry at the beginning of the use list and returns
824 // the newly created node.
825 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000826 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000827 if (IsEmpty()) {
828 first_ = new_node;
829 } else {
830 first_->prev_ = new_node;
831 new_node->next_ = first_;
832 first_ = new_node;
833 }
834 return new_node;
835 }
836
837 HUseListNode<T>* GetFirst() const {
838 return first_;
839 }
840
841 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000842 DCHECK(node != nullptr);
843 DCHECK(Contains(node));
844
David Brazdiled596192015-01-23 10:39:45 +0000845 if (node->prev_ != nullptr) {
846 node->prev_->next_ = node->next_;
847 }
848 if (node->next_ != nullptr) {
849 node->next_->prev_ = node->prev_;
850 }
851 if (node == first_) {
852 first_ = node->next_;
853 }
854 }
855
David Brazdil1abb4192015-02-17 18:33:36 +0000856 bool Contains(const HUseListNode<T>* node) const {
857 if (node == nullptr) {
858 return false;
859 }
860 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
861 if (current == node) {
862 return true;
863 }
864 }
865 return false;
866 }
867
David Brazdiled596192015-01-23 10:39:45 +0000868 bool IsEmpty() const {
869 return first_ == nullptr;
870 }
871
872 bool HasOnlyOneUse() const {
873 return first_ != nullptr && first_->next_ == nullptr;
874 }
875
876 private:
877 HUseListNode<T>* first_;
878};
879
880template<typename T>
881class HUseIterator : public ValueObject {
882 public:
883 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
884
885 bool Done() const { return current_ == nullptr; }
886
887 void Advance() {
888 DCHECK(!Done());
889 current_ = current_->GetNext();
890 }
891
892 HUseListNode<T>* Current() const {
893 DCHECK(!Done());
894 return current_;
895 }
896
897 private:
898 HUseListNode<T>* current_;
899
900 friend class HValue;
901};
902
David Brazdil1abb4192015-02-17 18:33:36 +0000903// This class is used by HEnvironment and HInstruction classes to record the
904// instructions they use and pointers to the corresponding HUseListNodes kept
905// by the used instructions.
906template <typename T>
907class HUserRecord : public ValueObject {
908 public:
909 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
910 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
911
912 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
913 : instruction_(old_record.instruction_), use_node_(use_node) {
914 DCHECK(instruction_ != nullptr);
915 DCHECK(use_node_ != nullptr);
916 DCHECK(old_record.use_node_ == nullptr);
917 }
918
919 HInstruction* GetInstruction() const { return instruction_; }
920 HUseListNode<T>* GetUseNode() const { return use_node_; }
921
922 private:
923 // Instruction used by the user.
924 HInstruction* instruction_;
925
926 // Corresponding entry in the use list kept by 'instruction_'.
927 HUseListNode<T>* use_node_;
928};
929
Calin Juravle27df7582015-04-17 19:12:31 +0100930// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
931// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
932// flag is consider.
933// - DependsOn suggests that there is a real dependency between side effects but it only
934// checks DependendsOnSomething flag.
935//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100936// Represents the side effects an instruction may have.
937class SideEffects : public ValueObject {
938 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100939 SideEffects() : flags_(0) {}
940
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100941 static SideEffects None() {
942 return SideEffects(0);
943 }
944
945 static SideEffects All() {
946 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
947 }
948
949 static SideEffects ChangesSomething() {
950 return SideEffects((1 << kFlagChangesCount) - 1);
951 }
952
953 static SideEffects DependsOnSomething() {
954 int count = kFlagDependsOnCount - kFlagChangesCount;
955 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
956 }
957
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100958 SideEffects Union(SideEffects other) const {
959 return SideEffects(flags_ | other.flags_);
960 }
961
Roland Levillain72bceff2014-09-15 18:29:00 +0100962 bool HasSideEffects() const {
963 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
964 return (flags_ & all_bits_set) != 0;
965 }
966
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100967 bool HasAllSideEffects() const {
968 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
969 return all_bits_set == (flags_ & all_bits_set);
970 }
971
972 bool DependsOn(SideEffects other) const {
973 size_t depends_flags = other.ComputeDependsFlags();
974 return (flags_ & depends_flags) != 0;
975 }
976
977 bool HasDependencies() const {
978 int count = kFlagDependsOnCount - kFlagChangesCount;
979 size_t all_bits_set = (1 << count) - 1;
980 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
981 }
982
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100983 private:
984 static constexpr int kFlagChangesSomething = 0;
985 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
986
987 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
988 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
989
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100990 explicit SideEffects(size_t flags) : flags_(flags) {}
991
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100992 size_t ComputeDependsFlags() const {
993 return flags_ << kFlagChangesCount;
994 }
995
996 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100997};
998
David Brazdiled596192015-01-23 10:39:45 +0000999// A HEnvironment object contains the values of virtual registers at a given location.
1000class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1001 public:
1002 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
1003 : vregs_(arena, number_of_vregs) {
1004 vregs_.SetSize(number_of_vregs);
1005 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001006 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001007 }
1008 }
1009
1010 void CopyFrom(HEnvironment* env);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001011 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1012 // input to the loop phi instead. This is for inserting instructions that
1013 // require an environment (like HDeoptimization) in the loop pre-header.
1014 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001015
1016 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001017 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001018 }
1019
1020 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001021 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001022 }
1023
David Brazdil1abb4192015-02-17 18:33:36 +00001024 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001025
1026 size_t Size() const { return vregs_.Size(); }
1027
1028 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001029 // Record instructions' use entries of this environment for constant-time removal.
1030 // It should only be called by HInstruction when a new environment use is added.
1031 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1032 DCHECK(env_use->GetUser() == this);
1033 size_t index = env_use->GetIndex();
1034 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1035 }
David Brazdiled596192015-01-23 10:39:45 +00001036
David Brazdil1abb4192015-02-17 18:33:36 +00001037 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001038
David Brazdil1abb4192015-02-17 18:33:36 +00001039 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001040
1041 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1042};
1043
Calin Juravleacf735c2015-02-12 15:25:22 +00001044class ReferenceTypeInfo : ValueObject {
1045 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001046 typedef Handle<mirror::Class> TypeHandle;
1047
1048 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1049 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1050 if (type_handle->IsObjectClass()) {
1051 // Override the type handle to be consistent with the case when we get to
1052 // Top but don't have the Object class available. It avoids having to guess
1053 // what value the type_handle has when it's Top.
1054 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1055 } else {
1056 return ReferenceTypeInfo(type_handle, is_exact, false);
1057 }
1058 }
1059
1060 static ReferenceTypeInfo CreateTop(bool is_exact) {
1061 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001062 }
1063
1064 bool IsExact() const { return is_exact_; }
1065 bool IsTop() const { return is_top_; }
1066
1067 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1068
Calin Juravleb1498f62015-02-16 13:13:29 +00001069 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001070 if (IsTop()) {
1071 // Top (equivalent for java.lang.Object) is supertype of anything.
1072 return true;
1073 }
1074 if (rti.IsTop()) {
1075 // If we get here `this` is not Top() so it can't be a supertype.
1076 return false;
1077 }
1078 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1079 }
1080
1081 // Returns true if the type information provide the same amount of details.
1082 // Note that it does not mean that the instructions have the same actual type
1083 // (e.g. tops are equal but they can be the result of a merge).
1084 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1085 if (IsExact() != rti.IsExact()) {
1086 return false;
1087 }
1088 if (IsTop() && rti.IsTop()) {
1089 // `Top` means java.lang.Object, so the types are equivalent.
1090 return true;
1091 }
1092 if (IsTop() || rti.IsTop()) {
1093 // If only one is top or object than they are not equivalent.
1094 // NB: We need this extra check because the type_handle of `Top` is invalid
1095 // and we cannot inspect its reference.
1096 return false;
1097 }
1098
1099 // Finally check the types.
1100 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1101 }
1102
1103 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001104 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1105 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1106 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1107
Calin Juravleacf735c2015-02-12 15:25:22 +00001108 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001109 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001110 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001111 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001112 bool is_exact_;
1113 // A true value here means that the object type should be java.lang.Object.
1114 // We don't have access to the corresponding mirror object every time so this
1115 // flag acts as a substitute. When true, the TypeHandle refers to a null
1116 // pointer and should not be used.
1117 bool is_top_;
1118};
1119
1120std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1121
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001122class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001123 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001124 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001125 : previous_(nullptr),
1126 next_(nullptr),
1127 block_(nullptr),
1128 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001129 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001130 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001131 locations_(nullptr),
1132 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001133 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001134 side_effects_(side_effects),
1135 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001136
Dave Allison20dfc792014-06-16 20:44:29 -07001137 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001138
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001139#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001140 enum InstructionKind {
1141 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1142 };
1143#undef DECLARE_KIND
1144
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001145 HInstruction* GetNext() const { return next_; }
1146 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001147
Calin Juravle77520bc2015-01-12 18:45:46 +00001148 HInstruction* GetNextDisregardingMoves() const;
1149 HInstruction* GetPreviousDisregardingMoves() const;
1150
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001151 HBasicBlock* GetBlock() const { return block_; }
1152 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001153 bool IsInBlock() const { return block_ != nullptr; }
1154 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001155 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001156
Roland Levillain6b879dd2014-09-22 17:13:44 +01001157 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001158 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001159
1160 virtual void Accept(HGraphVisitor* visitor) = 0;
1161 virtual const char* DebugName() const = 0;
1162
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001163 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001164 void SetRawInputAt(size_t index, HInstruction* input) {
1165 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1166 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001167
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001168 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001169 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001170 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001171 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001172
Calin Juravle10e244f2015-01-26 18:54:32 +00001173 // Does not apply for all instructions, but having this at top level greatly
1174 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001175 virtual bool CanBeNull() const {
1176 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1177 return true;
1178 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001179
Calin Juravle641547a2015-04-21 22:08:51 +01001180 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1181 UNUSED(obj);
1182 return false;
1183 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001184
Calin Juravleacf735c2015-02-12 15:25:22 +00001185 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001186 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001187 reference_type_info_ = reference_type_info;
1188 }
1189
Calin Juravle61d544b2015-02-23 16:46:57 +00001190 ReferenceTypeInfo GetReferenceTypeInfo() const {
1191 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1192 return reference_type_info_;
1193 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001194
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001195 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001196 DCHECK(user != nullptr);
1197 HUseListNode<HInstruction*>* use =
1198 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1199 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001200 }
1201
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001202 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001203 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001204 HUseListNode<HEnvironment*>* env_use =
1205 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1206 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001207 }
1208
David Brazdil1abb4192015-02-17 18:33:36 +00001209 void RemoveAsUserOfInput(size_t input) {
1210 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1211 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1212 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001213
David Brazdil1abb4192015-02-17 18:33:36 +00001214 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1215 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001216
David Brazdiled596192015-01-23 10:39:45 +00001217 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1218 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001219 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001220 bool HasOnlyOneNonEnvironmentUse() const {
1221 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1222 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001223
Roland Levillain6c82d402014-10-13 16:10:27 +01001224 // Does this instruction strictly dominate `other_instruction`?
1225 // Returns false if this instruction and `other_instruction` are the same.
1226 // Aborts if this instruction and `other_instruction` are both phis.
1227 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001228
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001229 int GetId() const { return id_; }
1230 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001231
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001232 int GetSsaIndex() const { return ssa_index_; }
1233 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1234 bool HasSsaIndex() const { return ssa_index_ != -1; }
1235
1236 bool HasEnvironment() const { return environment_ != nullptr; }
1237 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001238 // Set the `environment_` field. Raw because this method does not
1239 // update the uses lists.
1240 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1241
1242 // Set the environment of this instruction, copying it from `environment`. While
1243 // copying, the uses lists are being updated.
1244 void CopyEnvironmentFrom(HEnvironment* environment) {
1245 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1246 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1247 environment_->CopyFrom(environment);
1248 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001249
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001250 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1251 HBasicBlock* block) {
1252 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1253 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1254 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1255 }
1256
Nicolas Geoffray39468442014-09-02 15:17:15 +01001257 // Returns the number of entries in the environment. Typically, that is the
1258 // number of dex registers in a method. It could be more in case of inlining.
1259 size_t EnvironmentSize() const;
1260
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001261 LocationSummary* GetLocations() const { return locations_; }
1262 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001263
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001264 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001265 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001266
Alexandre Rames188d4312015-04-09 18:30:21 +01001267 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1268 // uses of this instruction by `other` are *not* updated.
1269 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1270 ReplaceWith(other);
1271 other->ReplaceInput(this, use_index);
1272 }
1273
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001274 // Move `this` instruction before `cursor`.
1275 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001276
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001277#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001278 bool Is##type() const { return (As##type() != nullptr); } \
1279 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001280 virtual H##type* As##type() { return nullptr; }
1281
1282 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1283#undef INSTRUCTION_TYPE_CHECK
1284
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001285 // Returns whether the instruction can be moved within the graph.
1286 virtual bool CanBeMoved() const { return false; }
1287
1288 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001289 virtual bool InstructionTypeEquals(HInstruction* other) const {
1290 UNUSED(other);
1291 return false;
1292 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001293
1294 // Returns whether any data encoded in the two instructions is equal.
1295 // This method does not look at the inputs. Both instructions must be
1296 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001297 virtual bool InstructionDataEquals(HInstruction* other) const {
1298 UNUSED(other);
1299 return false;
1300 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001301
1302 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001303 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001304 // 2) Their inputs are identical.
1305 bool Equals(HInstruction* other) const;
1306
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001307 virtual InstructionKind GetKind() const = 0;
1308
1309 virtual size_t ComputeHashCode() const {
1310 size_t result = GetKind();
1311 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1312 result = (result * 31) + InputAt(i)->GetId();
1313 }
1314 return result;
1315 }
1316
1317 SideEffects GetSideEffects() const { return side_effects_; }
1318
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001319 size_t GetLifetimePosition() const { return lifetime_position_; }
1320 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1321 LiveInterval* GetLiveInterval() const { return live_interval_; }
1322 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1323 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1324
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001325 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1326
1327 // Returns whether the code generation of the instruction will require to have access
1328 // to the current method. Such instructions are:
1329 // (1): Instructions that require an environment, as calling the runtime requires
1330 // to walk the stack and have the current method stored at a specific stack address.
1331 // (2): Object literals like classes and strings, that are loaded from the dex cache
1332 // fields of the current method.
1333 bool NeedsCurrentMethod() const {
1334 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1335 }
1336
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001337 virtual bool NeedsDexCache() const { return false; }
1338
David Brazdil1abb4192015-02-17 18:33:36 +00001339 protected:
1340 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1341 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1342
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001343 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001344 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1345
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001346 HInstruction* previous_;
1347 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001348 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001349
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001350 // An instruction gets an id when it is added to the graph.
1351 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001352 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001353 int id_;
1354
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001355 // When doing liveness analysis, instructions that have uses get an SSA index.
1356 int ssa_index_;
1357
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001358 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001359 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001360
1361 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001362 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001363
Nicolas Geoffray39468442014-09-02 15:17:15 +01001364 // The environment associated with this instruction. Not null if the instruction
1365 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001366 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001367
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001368 // Set by the code generator.
1369 LocationSummary* locations_;
1370
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001371 // Set by the liveness analysis.
1372 LiveInterval* live_interval_;
1373
1374 // Set by the liveness analysis, this is the position in a linear
1375 // order of blocks where this instruction's live interval start.
1376 size_t lifetime_position_;
1377
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001378 const SideEffects side_effects_;
1379
Calin Juravleacf735c2015-02-12 15:25:22 +00001380 // TODO: for primitive types this should be marked as invalid.
1381 ReferenceTypeInfo reference_type_info_;
1382
David Brazdil1abb4192015-02-17 18:33:36 +00001383 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001384 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001385 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001386 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001387 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001388
1389 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1390};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001391std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001392
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001393class HInputIterator : public ValueObject {
1394 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001395 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001396
1397 bool Done() const { return index_ == instruction_->InputCount(); }
1398 HInstruction* Current() const { return instruction_->InputAt(index_); }
1399 void Advance() { index_++; }
1400
1401 private:
1402 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001403 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001404
1405 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1406};
1407
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001408class HInstructionIterator : public ValueObject {
1409 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001410 explicit HInstructionIterator(const HInstructionList& instructions)
1411 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001412 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001413 }
1414
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001415 bool Done() const { return instruction_ == nullptr; }
1416 HInstruction* Current() const { return instruction_; }
1417 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001418 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001419 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001420 }
1421
1422 private:
1423 HInstruction* instruction_;
1424 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001425
1426 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001427};
1428
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001429class HBackwardInstructionIterator : public ValueObject {
1430 public:
1431 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1432 : instruction_(instructions.last_instruction_) {
1433 next_ = Done() ? nullptr : instruction_->GetPrevious();
1434 }
1435
1436 bool Done() const { return instruction_ == nullptr; }
1437 HInstruction* Current() const { return instruction_; }
1438 void Advance() {
1439 instruction_ = next_;
1440 next_ = Done() ? nullptr : instruction_->GetPrevious();
1441 }
1442
1443 private:
1444 HInstruction* instruction_;
1445 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001446
1447 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001448};
1449
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001450// An embedded container with N elements of type T. Used (with partial
1451// specialization for N=0) because embedded arrays cannot have size 0.
1452template<typename T, intptr_t N>
1453class EmbeddedArray {
1454 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001455 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001456
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001457 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001458
1459 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001460 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001461 return elements_[i];
1462 }
1463
1464 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001465 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001466 return elements_[i];
1467 }
1468
1469 const T& At(intptr_t i) const {
1470 return (*this)[i];
1471 }
1472
1473 void SetAt(intptr_t i, const T& val) {
1474 (*this)[i] = val;
1475 }
1476
1477 private:
1478 T elements_[N];
1479};
1480
1481template<typename T>
1482class EmbeddedArray<T, 0> {
1483 public:
1484 intptr_t length() const { return 0; }
1485 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001486 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001487 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001488 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001489 }
1490 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001491 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001492 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001493 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001494 }
1495};
1496
1497template<intptr_t N>
1498class HTemplateInstruction: public HInstruction {
1499 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001500 HTemplateInstruction<N>(SideEffects side_effects)
1501 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001502 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001503
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001504 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001505
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001506 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001507 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1508
1509 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1510 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001511 }
1512
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001513 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001514 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001515
1516 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001517};
1518
Dave Allison20dfc792014-06-16 20:44:29 -07001519template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001520class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001521 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001522 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1523 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001524 virtual ~HExpression() {}
1525
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001526 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001527
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001528 protected:
1529 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001530};
1531
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001532// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1533// instruction that branches to the exit block.
1534class HReturnVoid : public HTemplateInstruction<0> {
1535 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001536 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001537
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001538 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001539
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001540 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001541
1542 private:
1543 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1544};
1545
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001546// Represents dex's RETURN opcodes. A HReturn is a control flow
1547// instruction that branches to the exit block.
1548class HReturn : public HTemplateInstruction<1> {
1549 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001550 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001551 SetRawInputAt(0, value);
1552 }
1553
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001554 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001555
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001556 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001557
1558 private:
1559 DISALLOW_COPY_AND_ASSIGN(HReturn);
1560};
1561
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001562// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001563// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001564// exit block.
1565class HExit : public HTemplateInstruction<0> {
1566 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001567 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001568
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001569 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001570
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001571 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001572
1573 private:
1574 DISALLOW_COPY_AND_ASSIGN(HExit);
1575};
1576
1577// Jumps from one block to another.
1578class HGoto : public HTemplateInstruction<0> {
1579 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001580 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1581
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001582 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001583
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001584 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001585 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001586 }
1587
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001588 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001589
1590 private:
1591 DISALLOW_COPY_AND_ASSIGN(HGoto);
1592};
1593
Dave Allison20dfc792014-06-16 20:44:29 -07001594
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001595// Conditional branch. A block ending with an HIf instruction must have
1596// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001597class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001598 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001599 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001600 SetRawInputAt(0, input);
1601 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001602
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001603 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001604
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001605 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001606 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001607 }
1608
1609 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001610 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001611 }
1612
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001613 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001614
1615 private:
1616 DISALLOW_COPY_AND_ASSIGN(HIf);
1617};
1618
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001619// Deoptimize to interpreter, upon checking a condition.
1620class HDeoptimize : public HTemplateInstruction<1> {
1621 public:
1622 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1623 : HTemplateInstruction(SideEffects::None()),
1624 dex_pc_(dex_pc) {
1625 SetRawInputAt(0, cond);
1626 }
1627
1628 bool NeedsEnvironment() const OVERRIDE { return true; }
1629 bool CanThrow() const OVERRIDE { return true; }
1630 uint32_t GetDexPc() const { return dex_pc_; }
1631
1632 DECLARE_INSTRUCTION(Deoptimize);
1633
1634 private:
1635 uint32_t dex_pc_;
1636
1637 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1638};
1639
Roland Levillain88cb1752014-10-20 16:36:47 +01001640class HUnaryOperation : public HExpression<1> {
1641 public:
1642 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1643 : HExpression(result_type, SideEffects::None()) {
1644 SetRawInputAt(0, input);
1645 }
1646
1647 HInstruction* GetInput() const { return InputAt(0); }
1648 Primitive::Type GetResultType() const { return GetType(); }
1649
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001650 bool CanBeMoved() const OVERRIDE { return true; }
1651 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001652 UNUSED(other);
1653 return true;
1654 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001655
Roland Levillain9240d6a2014-10-20 16:47:04 +01001656 // Try to statically evaluate `operation` and return a HConstant
1657 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001658 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001659 HConstant* TryStaticEvaluation() const;
1660
1661 // Apply this operation to `x`.
1662 virtual int32_t Evaluate(int32_t x) const = 0;
1663 virtual int64_t Evaluate(int64_t x) const = 0;
1664
Roland Levillain88cb1752014-10-20 16:36:47 +01001665 DECLARE_INSTRUCTION(UnaryOperation);
1666
1667 private:
1668 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1669};
1670
Dave Allison20dfc792014-06-16 20:44:29 -07001671class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001672 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001673 HBinaryOperation(Primitive::Type result_type,
1674 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001675 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001676 SetRawInputAt(0, left);
1677 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001678 }
1679
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001680 HInstruction* GetLeft() const { return InputAt(0); }
1681 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001682 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001683
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001684 virtual bool IsCommutative() const { return false; }
1685
1686 // Put constant on the right.
1687 // Returns whether order is changed.
1688 bool OrderInputsWithConstantOnTheRight() {
1689 HInstruction* left = InputAt(0);
1690 HInstruction* right = InputAt(1);
1691 if (left->IsConstant() && !right->IsConstant()) {
1692 ReplaceInput(right, 0);
1693 ReplaceInput(left, 1);
1694 return true;
1695 }
1696 return false;
1697 }
1698
1699 // Order inputs by instruction id, but favor constant on the right side.
1700 // This helps GVN for commutative ops.
1701 void OrderInputs() {
1702 DCHECK(IsCommutative());
1703 HInstruction* left = InputAt(0);
1704 HInstruction* right = InputAt(1);
1705 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1706 return;
1707 }
1708 if (OrderInputsWithConstantOnTheRight()) {
1709 return;
1710 }
1711 // Order according to instruction id.
1712 if (left->GetId() > right->GetId()) {
1713 ReplaceInput(right, 0);
1714 ReplaceInput(left, 1);
1715 }
1716 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001717
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001718 bool CanBeMoved() const OVERRIDE { return true; }
1719 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001720 UNUSED(other);
1721 return true;
1722 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001723
Roland Levillain9240d6a2014-10-20 16:47:04 +01001724 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001725 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001726 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001727 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001728
1729 // Apply this operation to `x` and `y`.
1730 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1731 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1732
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001733 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001734 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001735 HConstant* GetConstantRight() const;
1736
1737 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001738 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001739 HInstruction* GetLeastConstantLeft() const;
1740
Roland Levillainccc07a92014-09-16 14:48:16 +01001741 DECLARE_INSTRUCTION(BinaryOperation);
1742
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001743 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001744 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1745};
1746
Dave Allison20dfc792014-06-16 20:44:29 -07001747class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001748 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001749 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001750 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1751 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001752
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001753 bool NeedsMaterialization() const { return needs_materialization_; }
1754 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001755
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001756 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001757 // `instruction`, and disregard moves in between.
1758 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001759
Dave Allison20dfc792014-06-16 20:44:29 -07001760 DECLARE_INSTRUCTION(Condition);
1761
1762 virtual IfCondition GetCondition() const = 0;
1763
1764 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001765 // For register allocation purposes, returns whether this instruction needs to be
1766 // materialized (that is, not just be in the processor flags).
1767 bool needs_materialization_;
1768
Dave Allison20dfc792014-06-16 20:44:29 -07001769 DISALLOW_COPY_AND_ASSIGN(HCondition);
1770};
1771
1772// Instruction to check if two inputs are equal to each other.
1773class HEqual : public HCondition {
1774 public:
1775 HEqual(HInstruction* first, HInstruction* second)
1776 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001777
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001778 bool IsCommutative() const OVERRIDE { return true; }
1779
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001780 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001781 return x == y ? 1 : 0;
1782 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001783 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001784 return x == y ? 1 : 0;
1785 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001786
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001787 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001788
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001789 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001790 return kCondEQ;
1791 }
1792
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001793 private:
1794 DISALLOW_COPY_AND_ASSIGN(HEqual);
1795};
1796
Dave Allison20dfc792014-06-16 20:44:29 -07001797class HNotEqual : public HCondition {
1798 public:
1799 HNotEqual(HInstruction* first, HInstruction* second)
1800 : HCondition(first, second) {}
1801
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001802 bool IsCommutative() const OVERRIDE { return true; }
1803
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001804 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001805 return x != y ? 1 : 0;
1806 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001807 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001808 return x != y ? 1 : 0;
1809 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001810
Dave Allison20dfc792014-06-16 20:44:29 -07001811 DECLARE_INSTRUCTION(NotEqual);
1812
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001813 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001814 return kCondNE;
1815 }
1816
1817 private:
1818 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1819};
1820
1821class HLessThan : public HCondition {
1822 public:
1823 HLessThan(HInstruction* first, HInstruction* second)
1824 : HCondition(first, second) {}
1825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001826 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001827 return x < y ? 1 : 0;
1828 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001829 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001830 return x < y ? 1 : 0;
1831 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001832
Dave Allison20dfc792014-06-16 20:44:29 -07001833 DECLARE_INSTRUCTION(LessThan);
1834
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001835 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001836 return kCondLT;
1837 }
1838
1839 private:
1840 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1841};
1842
1843class HLessThanOrEqual : public HCondition {
1844 public:
1845 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1846 : HCondition(first, second) {}
1847
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001848 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001849 return x <= y ? 1 : 0;
1850 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001851 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001852 return x <= y ? 1 : 0;
1853 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001854
Dave Allison20dfc792014-06-16 20:44:29 -07001855 DECLARE_INSTRUCTION(LessThanOrEqual);
1856
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001857 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001858 return kCondLE;
1859 }
1860
1861 private:
1862 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1863};
1864
1865class HGreaterThan : public HCondition {
1866 public:
1867 HGreaterThan(HInstruction* first, HInstruction* second)
1868 : HCondition(first, second) {}
1869
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001870 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001871 return x > y ? 1 : 0;
1872 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001873 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001874 return x > y ? 1 : 0;
1875 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001876
Dave Allison20dfc792014-06-16 20:44:29 -07001877 DECLARE_INSTRUCTION(GreaterThan);
1878
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001879 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001880 return kCondGT;
1881 }
1882
1883 private:
1884 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1885};
1886
1887class HGreaterThanOrEqual : public HCondition {
1888 public:
1889 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1890 : HCondition(first, second) {}
1891
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001892 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001893 return x >= y ? 1 : 0;
1894 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001895 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001896 return x >= y ? 1 : 0;
1897 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001898
Dave Allison20dfc792014-06-16 20:44:29 -07001899 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1900
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001901 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001902 return kCondGE;
1903 }
1904
1905 private:
1906 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1907};
1908
1909
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001910// Instruction to check how two inputs compare to each other.
1911// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1912class HCompare : public HBinaryOperation {
1913 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001914 // The bias applies for floating point operations and indicates how NaN
1915 // comparisons are treated:
1916 enum Bias {
1917 kNoBias, // bias is not applicable (i.e. for long operation)
1918 kGtBias, // return 1 for NaN comparisons
1919 kLtBias, // return -1 for NaN comparisons
1920 };
1921
1922 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1923 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001924 DCHECK_EQ(type, first->GetType());
1925 DCHECK_EQ(type, second->GetType());
1926 }
1927
Calin Juravleddb7df22014-11-25 20:56:51 +00001928 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001929 return
1930 x == y ? 0 :
1931 x > y ? 1 :
1932 -1;
1933 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001934
1935 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001936 return
1937 x == y ? 0 :
1938 x > y ? 1 :
1939 -1;
1940 }
1941
Calin Juravleddb7df22014-11-25 20:56:51 +00001942 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1943 return bias_ == other->AsCompare()->bias_;
1944 }
1945
1946 bool IsGtBias() { return bias_ == kGtBias; }
1947
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001948 DECLARE_INSTRUCTION(Compare);
1949
1950 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001951 const Bias bias_;
1952
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001953 DISALLOW_COPY_AND_ASSIGN(HCompare);
1954};
1955
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001956// A local in the graph. Corresponds to a Dex register.
1957class HLocal : public HTemplateInstruction<0> {
1958 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001959 explicit HLocal(uint16_t reg_number)
1960 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001961
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001962 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001963
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001964 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001965
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001966 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001967 // The Dex register number.
1968 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001969
1970 DISALLOW_COPY_AND_ASSIGN(HLocal);
1971};
1972
1973// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001974class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001975 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001976 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001977 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001978 SetRawInputAt(0, local);
1979 }
1980
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001981 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1982
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001983 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001984
1985 private:
1986 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1987};
1988
1989// Store a value in a given local. This instruction has two inputs: the value
1990// and the local.
1991class HStoreLocal : public HTemplateInstruction<2> {
1992 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001993 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001994 SetRawInputAt(0, local);
1995 SetRawInputAt(1, value);
1996 }
1997
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001998 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1999
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002000 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002001
2002 private:
2003 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2004};
2005
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002006class HConstant : public HExpression<0> {
2007 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002008 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2009
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002010 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002011
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002012 virtual bool IsMinusOne() const { return false; }
2013 virtual bool IsZero() const { return false; }
2014 virtual bool IsOne() const { return false; }
2015
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002016 DECLARE_INSTRUCTION(Constant);
2017
2018 private:
2019 DISALLOW_COPY_AND_ASSIGN(HConstant);
2020};
2021
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002022class HFloatConstant : public HConstant {
2023 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002024 float GetValue() const { return value_; }
2025
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002026 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002027 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2028 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002029 }
2030
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002031 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002032
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002033 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002034 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2035 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002036 }
2037 bool IsZero() const OVERRIDE {
2038 return AsFloatConstant()->GetValue() == 0.0f;
2039 }
2040 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002041 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2042 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002043 }
2044
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002045 DECLARE_INSTRUCTION(FloatConstant);
2046
2047 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002048 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
2049
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002050 const float value_;
2051
Nicolas Geoffray067cae22015-04-26 16:43:00 +00002052 // Only the SsaBuilder can currently create floating-point constants. If we
2053 // ever need to create them later in the pipeline, we will have to handle them
2054 // the same way as integral constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002055 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002056 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2057};
2058
2059class HDoubleConstant : public HConstant {
2060 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002061 double GetValue() const { return value_; }
2062
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002063 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002064 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2065 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002066 }
2067
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002068 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002069
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002070 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002071 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2072 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002073 }
2074 bool IsZero() const OVERRIDE {
2075 return AsDoubleConstant()->GetValue() == 0.0;
2076 }
2077 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002078 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2079 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002080 }
2081
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002082 DECLARE_INSTRUCTION(DoubleConstant);
2083
2084 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002085 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
2086
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002087 const double value_;
2088
Nicolas Geoffray067cae22015-04-26 16:43:00 +00002089 // Only the SsaBuilder can currently create floating-point constants. If we
2090 // ever need to create them later in the pipeline, we will have to handle them
2091 // the same way as integral constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002092 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002093 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2094};
2095
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002096class HNullConstant : public HConstant {
2097 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002098 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2099 return true;
2100 }
2101
2102 size_t ComputeHashCode() const OVERRIDE { return 0; }
2103
2104 DECLARE_INSTRUCTION(NullConstant);
2105
2106 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002107 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2108
2109 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002110 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2111};
2112
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002113// Constants of the type int. Those can be from Dex instructions, or
2114// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002115class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002116 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002117 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002118
Calin Juravle61d544b2015-02-23 16:46:57 +00002119 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002120 return other->AsIntConstant()->value_ == value_;
2121 }
2122
Calin Juravle61d544b2015-02-23 16:46:57 +00002123 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2124
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002125 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2126 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2127 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2128
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002129 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002130
2131 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002132 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2133
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002134 const int32_t value_;
2135
David Brazdil8d5b8b22015-03-24 10:51:52 +00002136 friend class HGraph;
2137 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002138 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002139 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2140};
2141
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002142class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002143 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002144 int64_t GetValue() const { return value_; }
2145
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002146 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002147 return other->AsLongConstant()->value_ == value_;
2148 }
2149
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002150 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002151
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002152 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2153 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2154 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2155
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002156 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002157
2158 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002159 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2160
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002161 const int64_t value_;
2162
David Brazdil8d5b8b22015-03-24 10:51:52 +00002163 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002164 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2165};
2166
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002167enum class Intrinsics {
2168#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2169#include "intrinsics_list.h"
2170 kNone,
2171 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2172#undef INTRINSICS_LIST
2173#undef OPTIMIZING_INTRINSICS
2174};
2175std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2176
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002177class HInvoke : public HInstruction {
2178 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002179 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002180
2181 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2182 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002183 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002184
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002185 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002186 SetRawInputAt(index, argument);
2187 }
2188
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002189 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002190
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002191 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002192
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002193 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2194
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002195 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002196 return intrinsic_;
2197 }
2198
2199 void SetIntrinsic(Intrinsics intrinsic) {
2200 intrinsic_ = intrinsic;
2201 }
2202
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002203 DECLARE_INSTRUCTION(Invoke);
2204
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002205 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002206 HInvoke(ArenaAllocator* arena,
2207 uint32_t number_of_arguments,
2208 Primitive::Type return_type,
2209 uint32_t dex_pc,
2210 uint32_t dex_method_index)
2211 : HInstruction(SideEffects::All()),
2212 inputs_(arena, number_of_arguments),
2213 return_type_(return_type),
2214 dex_pc_(dex_pc),
2215 dex_method_index_(dex_method_index),
2216 intrinsic_(Intrinsics::kNone) {
2217 inputs_.SetSize(number_of_arguments);
2218 }
2219
David Brazdil1abb4192015-02-17 18:33:36 +00002220 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2221 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2222 inputs_.Put(index, input);
2223 }
2224
2225 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002226 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002227 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002228 const uint32_t dex_method_index_;
2229 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002230
2231 private:
2232 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2233};
2234
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002235class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002236 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002237 // Requirements of this method call regarding the class
2238 // initialization (clinit) check of its declaring class.
2239 enum class ClinitCheckRequirement {
2240 kNone, // Class already initialized.
2241 kExplicit, // Static call having explicit clinit check as last input.
2242 kImplicit, // Static call implicitly requiring a clinit check.
2243 };
2244
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002245 HInvokeStaticOrDirect(ArenaAllocator* arena,
2246 uint32_t number_of_arguments,
2247 Primitive::Type return_type,
2248 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002249 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002250 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002251 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002252 InvokeType invoke_type,
2253 ClinitCheckRequirement clinit_check_requirement)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002254 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002255 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002256 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002257 is_recursive_(is_recursive),
2258 clinit_check_requirement_(clinit_check_requirement) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002259
Calin Juravle641547a2015-04-21 22:08:51 +01002260 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2261 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002262 // We access the method via the dex cache so we can't do an implicit null check.
2263 // TODO: for intrinsics we can generate implicit null checks.
2264 return false;
2265 }
2266
Nicolas Geoffray79041292015-03-26 10:05:54 +00002267 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002268 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002269 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002270 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002271
Roland Levillain4c0eb422015-04-24 16:43:49 +01002272 // Is this instruction a call to a static method?
2273 bool IsStatic() const {
2274 return GetInvokeType() == kStatic;
2275 }
2276
Roland Levillain0379f822015-04-24 23:01:24 +01002277 // Remove the art::HClinitCheck or art::HLoadClass instruction as
2278 // last input (only relevant for static calls with explicit clinit
2279 // check).
2280 void RemoveClinitCheckOrLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002281 DCHECK(IsStaticWithExplicitClinitCheck());
2282 size_t last_input_index = InputCount() - 1;
2283 HInstruction* last_input = InputAt(last_input_index);
2284 DCHECK(last_input != nullptr);
Roland Levillain0379f822015-04-24 23:01:24 +01002285 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002286 RemoveAsUserOfInput(last_input_index);
2287 inputs_.DeleteAt(last_input_index);
2288 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2289 DCHECK(IsStaticWithImplicitClinitCheck());
2290 }
2291
2292 // Is this a call to a static method whose declaring class has an
2293 // explicit intialization check in the graph?
2294 bool IsStaticWithExplicitClinitCheck() const {
2295 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2296 }
2297
2298 // Is this a call to a static method whose declaring class has an
2299 // implicit intialization check requirement?
2300 bool IsStaticWithImplicitClinitCheck() const {
2301 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2302 }
2303
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002304 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002305
Roland Levillain4c0eb422015-04-24 16:43:49 +01002306 protected:
2307 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2308 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2309 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2310 HInstruction* input = input_record.GetInstruction();
2311 // `input` is the last input of a static invoke marked as having
2312 // an explicit clinit check. It must either be:
2313 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2314 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2315 DCHECK(input != nullptr);
2316 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2317 }
2318 return input_record;
2319 }
2320
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002321 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002322 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002323 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002324 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002325 ClinitCheckRequirement clinit_check_requirement_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002326
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002327 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002328};
2329
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002330class HInvokeVirtual : public HInvoke {
2331 public:
2332 HInvokeVirtual(ArenaAllocator* arena,
2333 uint32_t number_of_arguments,
2334 Primitive::Type return_type,
2335 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002336 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002337 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002338 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002339 vtable_index_(vtable_index) {}
2340
Calin Juravle641547a2015-04-21 22:08:51 +01002341 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002342 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002343 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002344 }
2345
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002346 uint32_t GetVTableIndex() const { return vtable_index_; }
2347
2348 DECLARE_INSTRUCTION(InvokeVirtual);
2349
2350 private:
2351 const uint32_t vtable_index_;
2352
2353 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2354};
2355
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002356class HInvokeInterface : public HInvoke {
2357 public:
2358 HInvokeInterface(ArenaAllocator* arena,
2359 uint32_t number_of_arguments,
2360 Primitive::Type return_type,
2361 uint32_t dex_pc,
2362 uint32_t dex_method_index,
2363 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002364 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002365 imt_index_(imt_index) {}
2366
Calin Juravle641547a2015-04-21 22:08:51 +01002367 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002368 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002369 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002370 }
2371
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002372 uint32_t GetImtIndex() const { return imt_index_; }
2373 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2374
2375 DECLARE_INSTRUCTION(InvokeInterface);
2376
2377 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002378 const uint32_t imt_index_;
2379
2380 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2381};
2382
Dave Allison20dfc792014-06-16 20:44:29 -07002383class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002384 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002385 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002386 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2387 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002388 type_index_(type_index),
2389 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002390
2391 uint32_t GetDexPc() const { return dex_pc_; }
2392 uint16_t GetTypeIndex() const { return type_index_; }
2393
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002394 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002395 bool NeedsEnvironment() const OVERRIDE { return true; }
2396 // It may throw when called on:
2397 // - interfaces
2398 // - abstract/innaccessible/unknown classes
2399 // TODO: optimize when possible.
2400 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002401
Calin Juravle10e244f2015-01-26 18:54:32 +00002402 bool CanBeNull() const OVERRIDE { return false; }
2403
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002404 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2405
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002406 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002407
2408 private:
2409 const uint32_t dex_pc_;
2410 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002411 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002412
2413 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2414};
2415
Roland Levillain88cb1752014-10-20 16:36:47 +01002416class HNeg : public HUnaryOperation {
2417 public:
2418 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2419 : HUnaryOperation(result_type, input) {}
2420
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002421 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2422 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002423
Roland Levillain88cb1752014-10-20 16:36:47 +01002424 DECLARE_INSTRUCTION(Neg);
2425
2426 private:
2427 DISALLOW_COPY_AND_ASSIGN(HNeg);
2428};
2429
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002430class HNewArray : public HExpression<1> {
2431 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002432 HNewArray(HInstruction* length,
2433 uint32_t dex_pc,
2434 uint16_t type_index,
2435 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002436 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2437 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002438 type_index_(type_index),
2439 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002440 SetRawInputAt(0, length);
2441 }
2442
2443 uint32_t GetDexPc() const { return dex_pc_; }
2444 uint16_t GetTypeIndex() const { return type_index_; }
2445
2446 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002447 bool NeedsEnvironment() const OVERRIDE { return true; }
2448
Mingyao Yang0c365e62015-03-31 15:09:29 -07002449 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2450 bool CanThrow() const OVERRIDE { return true; }
2451
Calin Juravle10e244f2015-01-26 18:54:32 +00002452 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002453
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002454 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2455
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002456 DECLARE_INSTRUCTION(NewArray);
2457
2458 private:
2459 const uint32_t dex_pc_;
2460 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002461 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002462
2463 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2464};
2465
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002466class HAdd : public HBinaryOperation {
2467 public:
2468 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2469 : HBinaryOperation(result_type, left, right) {}
2470
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002471 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002472
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002473 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002474 return x + y;
2475 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002476 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002477 return x + y;
2478 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002479
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002480 DECLARE_INSTRUCTION(Add);
2481
2482 private:
2483 DISALLOW_COPY_AND_ASSIGN(HAdd);
2484};
2485
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002486class HSub : public HBinaryOperation {
2487 public:
2488 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2489 : HBinaryOperation(result_type, left, right) {}
2490
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002491 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002492 return x - y;
2493 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002494 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002495 return x - y;
2496 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002497
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002498 DECLARE_INSTRUCTION(Sub);
2499
2500 private:
2501 DISALLOW_COPY_AND_ASSIGN(HSub);
2502};
2503
Calin Juravle34bacdf2014-10-07 20:23:36 +01002504class HMul : public HBinaryOperation {
2505 public:
2506 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2507 : HBinaryOperation(result_type, left, right) {}
2508
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002509 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002510
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002511 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2512 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002513
2514 DECLARE_INSTRUCTION(Mul);
2515
2516 private:
2517 DISALLOW_COPY_AND_ASSIGN(HMul);
2518};
2519
Calin Juravle7c4954d2014-10-28 16:57:40 +00002520class HDiv : public HBinaryOperation {
2521 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002522 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2523 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002524
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002525 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002526 // Our graph structure ensures we never have 0 for `y` during constant folding.
2527 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002528 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002529 return (y == -1) ? -x : x / y;
2530 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002531
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002532 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002533 DCHECK_NE(y, 0);
2534 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2535 return (y == -1) ? -x : x / y;
2536 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002537
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002538 uint32_t GetDexPc() const { return dex_pc_; }
2539
Calin Juravle7c4954d2014-10-28 16:57:40 +00002540 DECLARE_INSTRUCTION(Div);
2541
2542 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002543 const uint32_t dex_pc_;
2544
Calin Juravle7c4954d2014-10-28 16:57:40 +00002545 DISALLOW_COPY_AND_ASSIGN(HDiv);
2546};
2547
Calin Juravlebacfec32014-11-14 15:54:36 +00002548class HRem : public HBinaryOperation {
2549 public:
2550 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2551 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2552
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002553 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002554 DCHECK_NE(y, 0);
2555 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2556 return (y == -1) ? 0 : x % y;
2557 }
2558
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002559 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002560 DCHECK_NE(y, 0);
2561 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2562 return (y == -1) ? 0 : x % y;
2563 }
2564
2565 uint32_t GetDexPc() const { return dex_pc_; }
2566
2567 DECLARE_INSTRUCTION(Rem);
2568
2569 private:
2570 const uint32_t dex_pc_;
2571
2572 DISALLOW_COPY_AND_ASSIGN(HRem);
2573};
2574
Calin Juravled0d48522014-11-04 16:40:20 +00002575class HDivZeroCheck : public HExpression<1> {
2576 public:
2577 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2578 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2579 SetRawInputAt(0, value);
2580 }
2581
2582 bool CanBeMoved() const OVERRIDE { return true; }
2583
2584 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2585 UNUSED(other);
2586 return true;
2587 }
2588
2589 bool NeedsEnvironment() const OVERRIDE { return true; }
2590 bool CanThrow() const OVERRIDE { return true; }
2591
2592 uint32_t GetDexPc() const { return dex_pc_; }
2593
2594 DECLARE_INSTRUCTION(DivZeroCheck);
2595
2596 private:
2597 const uint32_t dex_pc_;
2598
2599 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2600};
2601
Calin Juravle9aec02f2014-11-18 23:06:35 +00002602class HShl : public HBinaryOperation {
2603 public:
2604 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2605 : HBinaryOperation(result_type, left, right) {}
2606
2607 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2608 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2609
2610 DECLARE_INSTRUCTION(Shl);
2611
2612 private:
2613 DISALLOW_COPY_AND_ASSIGN(HShl);
2614};
2615
2616class HShr : public HBinaryOperation {
2617 public:
2618 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2619 : HBinaryOperation(result_type, left, right) {}
2620
2621 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2622 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2623
2624 DECLARE_INSTRUCTION(Shr);
2625
2626 private:
2627 DISALLOW_COPY_AND_ASSIGN(HShr);
2628};
2629
2630class HUShr : public HBinaryOperation {
2631 public:
2632 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2633 : HBinaryOperation(result_type, left, right) {}
2634
2635 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2636 uint32_t ux = static_cast<uint32_t>(x);
2637 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2638 return static_cast<int32_t>(ux >> uy);
2639 }
2640
2641 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2642 uint64_t ux = static_cast<uint64_t>(x);
2643 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2644 return static_cast<int64_t>(ux >> uy);
2645 }
2646
2647 DECLARE_INSTRUCTION(UShr);
2648
2649 private:
2650 DISALLOW_COPY_AND_ASSIGN(HUShr);
2651};
2652
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002653class HAnd : public HBinaryOperation {
2654 public:
2655 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2656 : HBinaryOperation(result_type, left, right) {}
2657
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002658 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002659
2660 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2661 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2662
2663 DECLARE_INSTRUCTION(And);
2664
2665 private:
2666 DISALLOW_COPY_AND_ASSIGN(HAnd);
2667};
2668
2669class HOr : public HBinaryOperation {
2670 public:
2671 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2672 : HBinaryOperation(result_type, left, right) {}
2673
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002674 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002675
2676 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2677 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2678
2679 DECLARE_INSTRUCTION(Or);
2680
2681 private:
2682 DISALLOW_COPY_AND_ASSIGN(HOr);
2683};
2684
2685class HXor : public HBinaryOperation {
2686 public:
2687 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2688 : HBinaryOperation(result_type, left, right) {}
2689
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002690 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002691
2692 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2693 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2694
2695 DECLARE_INSTRUCTION(Xor);
2696
2697 private:
2698 DISALLOW_COPY_AND_ASSIGN(HXor);
2699};
2700
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002701// The value of a parameter in this method. Its location depends on
2702// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002703class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002704 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002705 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2706 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002707
2708 uint8_t GetIndex() const { return index_; }
2709
Calin Juravle10e244f2015-01-26 18:54:32 +00002710 bool CanBeNull() const OVERRIDE { return !is_this_; }
2711
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002712 DECLARE_INSTRUCTION(ParameterValue);
2713
2714 private:
2715 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002716 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002717 const uint8_t index_;
2718
Calin Juravle10e244f2015-01-26 18:54:32 +00002719 // Whether or not the parameter value corresponds to 'this' argument.
2720 const bool is_this_;
2721
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002722 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2723};
2724
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002725class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002726 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002727 explicit HNot(Primitive::Type result_type, HInstruction* input)
2728 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002729
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002730 bool CanBeMoved() const OVERRIDE { return true; }
2731 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002732 UNUSED(other);
2733 return true;
2734 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002735
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002736 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2737 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002738
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002739 DECLARE_INSTRUCTION(Not);
2740
2741 private:
2742 DISALLOW_COPY_AND_ASSIGN(HNot);
2743};
2744
David Brazdil66d126e2015-04-03 16:02:44 +01002745class HBooleanNot : public HUnaryOperation {
2746 public:
2747 explicit HBooleanNot(HInstruction* input)
2748 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2749
2750 bool CanBeMoved() const OVERRIDE { return true; }
2751 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2752 UNUSED(other);
2753 return true;
2754 }
2755
2756 int32_t Evaluate(int32_t x) const OVERRIDE {
2757 DCHECK(IsUint<1>(x));
2758 return !x;
2759 }
2760
2761 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2762 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2763 UNREACHABLE();
2764 }
2765
2766 DECLARE_INSTRUCTION(BooleanNot);
2767
2768 private:
2769 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2770};
2771
Roland Levillaindff1f282014-11-05 14:15:05 +00002772class HTypeConversion : public HExpression<1> {
2773 public:
2774 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002775 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2776 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002777 SetRawInputAt(0, input);
2778 DCHECK_NE(input->GetType(), result_type);
2779 }
2780
2781 HInstruction* GetInput() const { return InputAt(0); }
2782 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2783 Primitive::Type GetResultType() const { return GetType(); }
2784
Roland Levillain624279f2014-12-04 11:54:28 +00002785 // Required by the x86 and ARM code generators when producing calls
2786 // to the runtime.
2787 uint32_t GetDexPc() const { return dex_pc_; }
2788
Roland Levillaindff1f282014-11-05 14:15:05 +00002789 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002790 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002791
2792 DECLARE_INSTRUCTION(TypeConversion);
2793
2794 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002795 const uint32_t dex_pc_;
2796
Roland Levillaindff1f282014-11-05 14:15:05 +00002797 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2798};
2799
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002800static constexpr uint32_t kNoRegNumber = -1;
2801
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002802class HPhi : public HInstruction {
2803 public:
2804 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002805 : HInstruction(SideEffects::None()),
2806 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002807 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002808 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002809 is_live_(false),
2810 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002811 inputs_.SetSize(number_of_inputs);
2812 }
2813
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002814 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2815 static Primitive::Type ToPhiType(Primitive::Type type) {
2816 switch (type) {
2817 case Primitive::kPrimBoolean:
2818 case Primitive::kPrimByte:
2819 case Primitive::kPrimShort:
2820 case Primitive::kPrimChar:
2821 return Primitive::kPrimInt;
2822 default:
2823 return type;
2824 }
2825 }
2826
Calin Juravle10e244f2015-01-26 18:54:32 +00002827 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002828
2829 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002830 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002831
Calin Juravle10e244f2015-01-26 18:54:32 +00002832 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002833 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002834
Calin Juravle10e244f2015-01-26 18:54:32 +00002835 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2836 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2837
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002838 uint32_t GetRegNumber() const { return reg_number_; }
2839
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002840 void SetDead() { is_live_ = false; }
2841 void SetLive() { is_live_ = true; }
2842 bool IsDead() const { return !is_live_; }
2843 bool IsLive() const { return is_live_; }
2844
Calin Juravlea4f88312015-04-16 12:57:19 +01002845 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2846 // An equivalent phi is a phi having the same dex register and type.
2847 // It assumes that phis with the same dex register are adjacent.
2848 HPhi* GetNextEquivalentPhiWithSameType() {
2849 HInstruction* next = GetNext();
2850 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2851 if (next->GetType() == GetType()) {
2852 return next->AsPhi();
2853 }
2854 next = next->GetNext();
2855 }
2856 return nullptr;
2857 }
2858
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002859 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002860
David Brazdil1abb4192015-02-17 18:33:36 +00002861 protected:
2862 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2863
2864 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2865 inputs_.Put(index, input);
2866 }
2867
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002868 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002869 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002870 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002871 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002872 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002873 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002874
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002875 DISALLOW_COPY_AND_ASSIGN(HPhi);
2876};
2877
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002878class HNullCheck : public HExpression<1> {
2879 public:
2880 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002881 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002882 SetRawInputAt(0, value);
2883 }
2884
Calin Juravle10e244f2015-01-26 18:54:32 +00002885 bool CanBeMoved() const OVERRIDE { return true; }
2886 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002887 UNUSED(other);
2888 return true;
2889 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002890
Calin Juravle10e244f2015-01-26 18:54:32 +00002891 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002892
Calin Juravle10e244f2015-01-26 18:54:32 +00002893 bool CanThrow() const OVERRIDE { return true; }
2894
2895 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002896
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002897 uint32_t GetDexPc() const { return dex_pc_; }
2898
2899 DECLARE_INSTRUCTION(NullCheck);
2900
2901 private:
2902 const uint32_t dex_pc_;
2903
2904 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2905};
2906
2907class FieldInfo : public ValueObject {
2908 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002909 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2910 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002911
2912 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002913 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002914 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002915
2916 private:
2917 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002918 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002919 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002920};
2921
2922class HInstanceFieldGet : public HExpression<1> {
2923 public:
2924 HInstanceFieldGet(HInstruction* value,
2925 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002926 MemberOffset field_offset,
2927 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002928 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002929 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002930 SetRawInputAt(0, value);
2931 }
2932
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002933 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002934
2935 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2936 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2937 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002938 }
2939
Calin Juravle641547a2015-04-21 22:08:51 +01002940 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2941 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002942 }
2943
2944 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002945 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2946 }
2947
Calin Juravle52c48962014-12-16 17:02:57 +00002948 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002949 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002950 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002951 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002952
2953 DECLARE_INSTRUCTION(InstanceFieldGet);
2954
2955 private:
2956 const FieldInfo field_info_;
2957
2958 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2959};
2960
2961class HInstanceFieldSet : public HTemplateInstruction<2> {
2962 public:
2963 HInstanceFieldSet(HInstruction* object,
2964 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002965 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002966 MemberOffset field_offset,
2967 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002968 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002969 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002970 SetRawInputAt(0, object);
2971 SetRawInputAt(1, value);
2972 }
2973
Calin Juravle641547a2015-04-21 22:08:51 +01002974 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2975 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002976 }
2977
Calin Juravle52c48962014-12-16 17:02:57 +00002978 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002979 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002980 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002981 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002982 HInstruction* GetValue() const { return InputAt(1); }
2983
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002984 DECLARE_INSTRUCTION(InstanceFieldSet);
2985
2986 private:
2987 const FieldInfo field_info_;
2988
2989 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2990};
2991
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002992class HArrayGet : public HExpression<2> {
2993 public:
2994 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002995 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002996 SetRawInputAt(0, array);
2997 SetRawInputAt(1, index);
2998 }
2999
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003000 bool CanBeMoved() const OVERRIDE { return true; }
3001 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003002 UNUSED(other);
3003 return true;
3004 }
Calin Juravle641547a2015-04-21 22:08:51 +01003005 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3006 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003007 // TODO: We can be smarter here.
3008 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3009 // which generates the implicit null check. There are cases when these can be removed
3010 // to produce better code. If we ever add optimizations to do so we should allow an
3011 // implicit check here (as long as the address falls in the first page).
3012 return false;
3013 }
3014
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003015 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003016
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003017 HInstruction* GetArray() const { return InputAt(0); }
3018 HInstruction* GetIndex() const { return InputAt(1); }
3019
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003020 DECLARE_INSTRUCTION(ArrayGet);
3021
3022 private:
3023 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3024};
3025
3026class HArraySet : public HTemplateInstruction<3> {
3027 public:
3028 HArraySet(HInstruction* array,
3029 HInstruction* index,
3030 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003031 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003032 uint32_t dex_pc)
3033 : HTemplateInstruction(SideEffects::ChangesSomething()),
3034 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003035 expected_component_type_(expected_component_type),
3036 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003037 SetRawInputAt(0, array);
3038 SetRawInputAt(1, index);
3039 SetRawInputAt(2, value);
3040 }
3041
Calin Juravle77520bc2015-01-12 18:45:46 +00003042 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003043 // We currently always call a runtime method to catch array store
3044 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003045 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003046 }
3047
Calin Juravle641547a2015-04-21 22:08:51 +01003048 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3049 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003050 // TODO: Same as for ArrayGet.
3051 return false;
3052 }
3053
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003054 void ClearNeedsTypeCheck() {
3055 needs_type_check_ = false;
3056 }
3057
3058 bool NeedsTypeCheck() const { return needs_type_check_; }
3059
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003060 uint32_t GetDexPc() const { return dex_pc_; }
3061
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003062 HInstruction* GetArray() const { return InputAt(0); }
3063 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003064 HInstruction* GetValue() const { return InputAt(2); }
3065
3066 Primitive::Type GetComponentType() const {
3067 // The Dex format does not type floating point index operations. Since the
3068 // `expected_component_type_` is set during building and can therefore not
3069 // be correct, we also check what is the value type. If it is a floating
3070 // point type, we must use that type.
3071 Primitive::Type value_type = GetValue()->GetType();
3072 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3073 ? value_type
3074 : expected_component_type_;
3075 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003076
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003077 DECLARE_INSTRUCTION(ArraySet);
3078
3079 private:
3080 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003081 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003082 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003083
3084 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3085};
3086
3087class HArrayLength : public HExpression<1> {
3088 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003089 explicit HArrayLength(HInstruction* array)
3090 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3091 // Note that arrays do not change length, so the instruction does not
3092 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003093 SetRawInputAt(0, array);
3094 }
3095
Calin Juravle77520bc2015-01-12 18:45:46 +00003096 bool CanBeMoved() const OVERRIDE { return true; }
3097 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003098 UNUSED(other);
3099 return true;
3100 }
Calin Juravle641547a2015-04-21 22:08:51 +01003101 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3102 return obj == InputAt(0);
3103 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003104
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003105 DECLARE_INSTRUCTION(ArrayLength);
3106
3107 private:
3108 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3109};
3110
3111class HBoundsCheck : public HExpression<2> {
3112 public:
3113 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003114 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003115 DCHECK(index->GetType() == Primitive::kPrimInt);
3116 SetRawInputAt(0, index);
3117 SetRawInputAt(1, length);
3118 }
3119
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003120 bool CanBeMoved() const OVERRIDE { return true; }
3121 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003122 UNUSED(other);
3123 return true;
3124 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003125
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003126 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003127
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003128 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003129
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003130 uint32_t GetDexPc() const { return dex_pc_; }
3131
3132 DECLARE_INSTRUCTION(BoundsCheck);
3133
3134 private:
3135 const uint32_t dex_pc_;
3136
3137 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3138};
3139
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003140/**
3141 * Some DEX instructions are folded into multiple HInstructions that need
3142 * to stay live until the last HInstruction. This class
3143 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003144 * HInstruction stays live. `index` represents the stack location index of the
3145 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003146 */
3147class HTemporary : public HTemplateInstruction<0> {
3148 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003149 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003150
3151 size_t GetIndex() const { return index_; }
3152
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003153 Primitive::Type GetType() const OVERRIDE {
3154 // The previous instruction is the one that will be stored in the temporary location.
3155 DCHECK(GetPrevious() != nullptr);
3156 return GetPrevious()->GetType();
3157 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003158
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003159 DECLARE_INSTRUCTION(Temporary);
3160
3161 private:
3162 const size_t index_;
3163
3164 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3165};
3166
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003167class HSuspendCheck : public HTemplateInstruction<0> {
3168 public:
3169 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003170 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003171
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003172 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003173 return true;
3174 }
3175
3176 uint32_t GetDexPc() const { return dex_pc_; }
3177
3178 DECLARE_INSTRUCTION(SuspendCheck);
3179
3180 private:
3181 const uint32_t dex_pc_;
3182
3183 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3184};
3185
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003186/**
3187 * Instruction to load a Class object.
3188 */
3189class HLoadClass : public HExpression<0> {
3190 public:
3191 HLoadClass(uint16_t type_index,
3192 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003193 uint32_t dex_pc)
3194 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3195 type_index_(type_index),
3196 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003197 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003198 generate_clinit_check_(false),
3199 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003200
3201 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003202
3203 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3204 return other->AsLoadClass()->type_index_ == type_index_;
3205 }
3206
3207 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3208
3209 uint32_t GetDexPc() const { return dex_pc_; }
3210 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003211 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003212
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003213 bool NeedsEnvironment() const OVERRIDE {
3214 // Will call runtime and load the class if the class is not loaded yet.
3215 // TODO: finer grain decision.
3216 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003217 }
3218
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003219 bool MustGenerateClinitCheck() const {
3220 return generate_clinit_check_;
3221 }
3222
3223 void SetMustGenerateClinitCheck() {
3224 generate_clinit_check_ = true;
3225 }
3226
3227 bool CanCallRuntime() const {
3228 return MustGenerateClinitCheck() || !is_referrers_class_;
3229 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003230
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003231 bool CanThrow() const OVERRIDE {
3232 // May call runtime and and therefore can throw.
3233 // TODO: finer grain decision.
3234 return !is_referrers_class_;
3235 }
3236
Calin Juravleacf735c2015-02-12 15:25:22 +00003237 ReferenceTypeInfo GetLoadedClassRTI() {
3238 return loaded_class_rti_;
3239 }
3240
3241 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3242 // Make sure we only set exact types (the loaded class should never be merged).
3243 DCHECK(rti.IsExact());
3244 loaded_class_rti_ = rti;
3245 }
3246
3247 bool IsResolved() {
3248 return loaded_class_rti_.IsExact();
3249 }
3250
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003251 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3252
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003253 DECLARE_INSTRUCTION(LoadClass);
3254
3255 private:
3256 const uint16_t type_index_;
3257 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003258 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003259 // Whether this instruction must generate the initialization check.
3260 // Used for code generation.
3261 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003262
Calin Juravleacf735c2015-02-12 15:25:22 +00003263 ReferenceTypeInfo loaded_class_rti_;
3264
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003265 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3266};
3267
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003268class HLoadString : public HExpression<0> {
3269 public:
3270 HLoadString(uint32_t string_index, uint32_t dex_pc)
3271 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3272 string_index_(string_index),
3273 dex_pc_(dex_pc) {}
3274
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003275 bool CanBeMoved() const OVERRIDE { return true; }
3276
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003277 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3278 return other->AsLoadString()->string_index_ == string_index_;
3279 }
3280
3281 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3282
3283 uint32_t GetDexPc() const { return dex_pc_; }
3284 uint32_t GetStringIndex() const { return string_index_; }
3285
3286 // TODO: Can we deopt or debug when we resolve a string?
3287 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003288 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003289
3290 DECLARE_INSTRUCTION(LoadString);
3291
3292 private:
3293 const uint32_t string_index_;
3294 const uint32_t dex_pc_;
3295
3296 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3297};
3298
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003299/**
3300 * Performs an initialization check on its Class object input.
3301 */
3302class HClinitCheck : public HExpression<1> {
3303 public:
3304 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003305 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003306 dex_pc_(dex_pc) {
3307 SetRawInputAt(0, constant);
3308 }
3309
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003310 bool CanBeMoved() const OVERRIDE { return true; }
3311 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3312 UNUSED(other);
3313 return true;
3314 }
3315
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003316 bool NeedsEnvironment() const OVERRIDE {
3317 // May call runtime to initialize the class.
3318 return true;
3319 }
3320
3321 uint32_t GetDexPc() const { return dex_pc_; }
3322
3323 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3324
3325 DECLARE_INSTRUCTION(ClinitCheck);
3326
3327 private:
3328 const uint32_t dex_pc_;
3329
3330 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3331};
3332
3333class HStaticFieldGet : public HExpression<1> {
3334 public:
3335 HStaticFieldGet(HInstruction* cls,
3336 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003337 MemberOffset field_offset,
3338 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003339 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003340 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003341 SetRawInputAt(0, cls);
3342 }
3343
Calin Juravle52c48962014-12-16 17:02:57 +00003344
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003345 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003346
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003347 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003348 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3349 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003350 }
3351
3352 size_t ComputeHashCode() const OVERRIDE {
3353 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3354 }
3355
Calin Juravle52c48962014-12-16 17:02:57 +00003356 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003357 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3358 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003359 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003360
3361 DECLARE_INSTRUCTION(StaticFieldGet);
3362
3363 private:
3364 const FieldInfo field_info_;
3365
3366 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3367};
3368
3369class HStaticFieldSet : public HTemplateInstruction<2> {
3370 public:
3371 HStaticFieldSet(HInstruction* cls,
3372 HInstruction* value,
3373 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003374 MemberOffset field_offset,
3375 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003376 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003377 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003378 SetRawInputAt(0, cls);
3379 SetRawInputAt(1, value);
3380 }
3381
Calin Juravle52c48962014-12-16 17:02:57 +00003382 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003383 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3384 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003385 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003386
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003387 HInstruction* GetValue() const { return InputAt(1); }
3388
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003389 DECLARE_INSTRUCTION(StaticFieldSet);
3390
3391 private:
3392 const FieldInfo field_info_;
3393
3394 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3395};
3396
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003397// Implement the move-exception DEX instruction.
3398class HLoadException : public HExpression<0> {
3399 public:
3400 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3401
3402 DECLARE_INSTRUCTION(LoadException);
3403
3404 private:
3405 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3406};
3407
3408class HThrow : public HTemplateInstruction<1> {
3409 public:
3410 HThrow(HInstruction* exception, uint32_t dex_pc)
3411 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3412 SetRawInputAt(0, exception);
3413 }
3414
3415 bool IsControlFlow() const OVERRIDE { return true; }
3416
3417 bool NeedsEnvironment() const OVERRIDE { return true; }
3418
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003419 bool CanThrow() const OVERRIDE { return true; }
3420
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003421 uint32_t GetDexPc() const { return dex_pc_; }
3422
3423 DECLARE_INSTRUCTION(Throw);
3424
3425 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003426 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003427
3428 DISALLOW_COPY_AND_ASSIGN(HThrow);
3429};
3430
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003431class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003432 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003433 HInstanceOf(HInstruction* object,
3434 HLoadClass* constant,
3435 bool class_is_final,
3436 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003437 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3438 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003439 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003440 dex_pc_(dex_pc) {
3441 SetRawInputAt(0, object);
3442 SetRawInputAt(1, constant);
3443 }
3444
3445 bool CanBeMoved() const OVERRIDE { return true; }
3446
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003447 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003448 return true;
3449 }
3450
3451 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003452 return false;
3453 }
3454
3455 uint32_t GetDexPc() const { return dex_pc_; }
3456
3457 bool IsClassFinal() const { return class_is_final_; }
3458
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003459 // Used only in code generation.
3460 bool MustDoNullCheck() const { return must_do_null_check_; }
3461 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3462
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003463 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003464
3465 private:
3466 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003467 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003468 const uint32_t dex_pc_;
3469
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003470 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3471};
3472
Calin Juravleb1498f62015-02-16 13:13:29 +00003473class HBoundType : public HExpression<1> {
3474 public:
3475 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3476 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3477 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003478 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003479 SetRawInputAt(0, input);
3480 }
3481
3482 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3483
3484 bool CanBeNull() const OVERRIDE {
3485 // `null instanceof ClassX` always return false so we can't be null.
3486 return false;
3487 }
3488
3489 DECLARE_INSTRUCTION(BoundType);
3490
3491 private:
3492 // Encodes the most upper class that this instruction can have. In other words
3493 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3494 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3495 const ReferenceTypeInfo bound_type_;
3496
3497 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3498};
3499
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003500class HCheckCast : public HTemplateInstruction<2> {
3501 public:
3502 HCheckCast(HInstruction* object,
3503 HLoadClass* constant,
3504 bool class_is_final,
3505 uint32_t dex_pc)
3506 : HTemplateInstruction(SideEffects::None()),
3507 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003508 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003509 dex_pc_(dex_pc) {
3510 SetRawInputAt(0, object);
3511 SetRawInputAt(1, constant);
3512 }
3513
3514 bool CanBeMoved() const OVERRIDE { return true; }
3515
3516 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3517 return true;
3518 }
3519
3520 bool NeedsEnvironment() const OVERRIDE {
3521 // Instruction may throw a CheckCastError.
3522 return true;
3523 }
3524
3525 bool CanThrow() const OVERRIDE { return true; }
3526
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003527 bool MustDoNullCheck() const { return must_do_null_check_; }
3528 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3529
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003530 uint32_t GetDexPc() const { return dex_pc_; }
3531
3532 bool IsClassFinal() const { return class_is_final_; }
3533
3534 DECLARE_INSTRUCTION(CheckCast);
3535
3536 private:
3537 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003538 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003539 const uint32_t dex_pc_;
3540
3541 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003542};
3543
Calin Juravle27df7582015-04-17 19:12:31 +01003544class HMemoryBarrier : public HTemplateInstruction<0> {
3545 public:
3546 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3547 : HTemplateInstruction(SideEffects::None()),
3548 barrier_kind_(barrier_kind) {}
3549
3550 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3551
3552 DECLARE_INSTRUCTION(MemoryBarrier);
3553
3554 private:
3555 const MemBarrierKind barrier_kind_;
3556
3557 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3558};
3559
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003560class HMonitorOperation : public HTemplateInstruction<1> {
3561 public:
3562 enum OperationKind {
3563 kEnter,
3564 kExit,
3565 };
3566
3567 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3568 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3569 SetRawInputAt(0, object);
3570 }
3571
3572 // Instruction may throw a Java exception, so we need an environment.
3573 bool NeedsEnvironment() const OVERRIDE { return true; }
3574 bool CanThrow() const OVERRIDE { return true; }
3575
3576 uint32_t GetDexPc() const { return dex_pc_; }
3577
3578 bool IsEnter() const { return kind_ == kEnter; }
3579
3580 DECLARE_INSTRUCTION(MonitorOperation);
3581
Calin Juravle52c48962014-12-16 17:02:57 +00003582 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003583 const OperationKind kind_;
3584 const uint32_t dex_pc_;
3585
3586 private:
3587 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3588};
3589
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003590class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003591 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003592 MoveOperands(Location source,
3593 Location destination,
3594 Primitive::Type type,
3595 HInstruction* instruction)
3596 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003597
3598 Location GetSource() const { return source_; }
3599 Location GetDestination() const { return destination_; }
3600
3601 void SetSource(Location value) { source_ = value; }
3602 void SetDestination(Location value) { destination_ = value; }
3603
3604 // The parallel move resolver marks moves as "in-progress" by clearing the
3605 // destination (but not the source).
3606 Location MarkPending() {
3607 DCHECK(!IsPending());
3608 Location dest = destination_;
3609 destination_ = Location::NoLocation();
3610 return dest;
3611 }
3612
3613 void ClearPending(Location dest) {
3614 DCHECK(IsPending());
3615 destination_ = dest;
3616 }
3617
3618 bool IsPending() const {
3619 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3620 return destination_.IsInvalid() && !source_.IsInvalid();
3621 }
3622
3623 // True if this blocks a move from the given location.
3624 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003625 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003626 }
3627
3628 // A move is redundant if it's been eliminated, if its source and
3629 // destination are the same, or if its destination is unneeded.
3630 bool IsRedundant() const {
3631 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3632 }
3633
3634 // We clear both operands to indicate move that's been eliminated.
3635 void Eliminate() {
3636 source_ = destination_ = Location::NoLocation();
3637 }
3638
3639 bool IsEliminated() const {
3640 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3641 return source_.IsInvalid();
3642 }
3643
Nicolas Geoffray90218252015-04-15 11:56:51 +01003644 bool Is64BitMove() const {
3645 return Primitive::Is64BitType(type_);
3646 }
3647
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003648 HInstruction* GetInstruction() const { return instruction_; }
3649
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003650 private:
3651 Location source_;
3652 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003653 // The type this move is for.
3654 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003655 // The instruction this move is assocatied with. Null when this move is
3656 // for moving an input in the expected locations of user (including a phi user).
3657 // This is only used in debug mode, to ensure we do not connect interval siblings
3658 // in the same parallel move.
3659 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003660};
3661
3662static constexpr size_t kDefaultNumberOfMoves = 4;
3663
3664class HParallelMove : public HTemplateInstruction<0> {
3665 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003666 explicit HParallelMove(ArenaAllocator* arena)
3667 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003668
Nicolas Geoffray90218252015-04-15 11:56:51 +01003669 void AddMove(Location source,
3670 Location destination,
3671 Primitive::Type type,
3672 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003673 DCHECK(source.IsValid());
3674 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003675 if (kIsDebugBuild) {
3676 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003677 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003678 if (moves_.Get(i).GetInstruction() == instruction) {
3679 // Special case the situation where the move is for the spill slot
3680 // of the instruction.
3681 if ((GetPrevious() == instruction)
3682 || ((GetPrevious() == nullptr)
3683 && instruction->IsPhi()
3684 && instruction->GetBlock() == GetBlock())) {
3685 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3686 << "Doing parallel moves for the same instruction.";
3687 } else {
3688 DCHECK(false) << "Doing parallel moves for the same instruction.";
3689 }
3690 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003691 }
3692 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003693 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003694 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3695 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003696 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003697 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003698 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003699 }
3700
3701 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003702 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003703 }
3704
3705 size_t NumMoves() const { return moves_.Size(); }
3706
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003707 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003708
3709 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003710 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003711
3712 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3713};
3714
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003715class HGraphVisitor : public ValueObject {
3716 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003717 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3718 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003719
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003720 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003721 virtual void VisitBasicBlock(HBasicBlock* block);
3722
Roland Levillain633021e2014-10-01 14:12:25 +01003723 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003724 void VisitInsertionOrder();
3725
Roland Levillain633021e2014-10-01 14:12:25 +01003726 // Visit the graph following dominator tree reverse post-order.
3727 void VisitReversePostOrder();
3728
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003729 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003730
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003731 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003732#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003733 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3734
3735 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3736
3737#undef DECLARE_VISIT_INSTRUCTION
3738
3739 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003740 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003741
3742 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3743};
3744
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003745class HGraphDelegateVisitor : public HGraphVisitor {
3746 public:
3747 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3748 virtual ~HGraphDelegateVisitor() {}
3749
3750 // Visit functions that delegate to to super class.
3751#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003752 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003753
3754 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3755
3756#undef DECLARE_VISIT_INSTRUCTION
3757
3758 private:
3759 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3760};
3761
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003762class HInsertionOrderIterator : public ValueObject {
3763 public:
3764 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3765
3766 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3767 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3768 void Advance() { ++index_; }
3769
3770 private:
3771 const HGraph& graph_;
3772 size_t index_;
3773
3774 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3775};
3776
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003777class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003778 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003779 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3780 // Check that reverse post order of the graph has been built.
3781 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3782 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003783
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003784 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3785 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003786 void Advance() { ++index_; }
3787
3788 private:
3789 const HGraph& graph_;
3790 size_t index_;
3791
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003792 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003793};
3794
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003795class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003796 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003797 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003798 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3799 // Check that reverse post order of the graph has been built.
3800 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3801 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003802
3803 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003804 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003805 void Advance() { --index_; }
3806
3807 private:
3808 const HGraph& graph_;
3809 size_t index_;
3810
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003811 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003812};
3813
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003814class HLinearPostOrderIterator : public ValueObject {
3815 public:
3816 explicit HLinearPostOrderIterator(const HGraph& graph)
3817 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3818
3819 bool Done() const { return index_ == 0; }
3820
3821 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3822
3823 void Advance() {
3824 --index_;
3825 DCHECK_GE(index_, 0U);
3826 }
3827
3828 private:
3829 const GrowableArray<HBasicBlock*>& order_;
3830 size_t index_;
3831
3832 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3833};
3834
3835class HLinearOrderIterator : public ValueObject {
3836 public:
3837 explicit HLinearOrderIterator(const HGraph& graph)
3838 : order_(graph.GetLinearOrder()), index_(0) {}
3839
3840 bool Done() const { return index_ == order_.Size(); }
3841 HBasicBlock* Current() const { return order_.Get(index_); }
3842 void Advance() { ++index_; }
3843
3844 private:
3845 const GrowableArray<HBasicBlock*>& order_;
3846 size_t index_;
3847
3848 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3849};
3850
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003851// Iterator over the blocks that art part of the loop. Includes blocks part
3852// of an inner loop. The order in which the blocks are iterated is on their
3853// block id.
3854class HBlocksInLoopIterator : public ValueObject {
3855 public:
3856 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3857 : blocks_in_loop_(info.GetBlocks()),
3858 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3859 index_(0) {
3860 if (!blocks_in_loop_.IsBitSet(index_)) {
3861 Advance();
3862 }
3863 }
3864
3865 bool Done() const { return index_ == blocks_.Size(); }
3866 HBasicBlock* Current() const { return blocks_.Get(index_); }
3867 void Advance() {
3868 ++index_;
3869 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3870 if (blocks_in_loop_.IsBitSet(index_)) {
3871 break;
3872 }
3873 }
3874 }
3875
3876 private:
3877 const BitVector& blocks_in_loop_;
3878 const GrowableArray<HBasicBlock*>& blocks_;
3879 size_t index_;
3880
3881 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3882};
3883
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003884inline int64_t Int64FromConstant(HConstant* constant) {
3885 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3886 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3887 : constant->AsLongConstant()->GetValue();
3888}
3889
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003890} // namespace art
3891
3892#endif // ART_COMPILER_OPTIMIZING_NODES_H_