blob: d970e383edca6dbfd4e08f2f6977d1fa6acd3f0a [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);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592
593 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100594 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100595 }
596
Roland Levillain6b879dd2014-09-22 17:13:44 +0100597 bool IsLoopPreHeaderFirstPredecessor() const {
598 DCHECK(IsLoopHeader());
599 DCHECK(!GetPredecessors().IsEmpty());
600 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
601 }
602
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100603 HLoopInformation* GetLoopInformation() const {
604 return loop_information_;
605 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000606
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000607 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100608 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000609 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100610 void SetInLoop(HLoopInformation* info) {
611 if (IsLoopHeader()) {
612 // Nothing to do. This just means `info` is an outer loop.
613 } else if (loop_information_ == nullptr) {
614 loop_information_ = info;
615 } else if (loop_information_->Contains(*info->GetHeader())) {
616 // Block is currently part of an outer loop. Make it part of this inner loop.
617 // Note that a non loop header having a loop information means this loop information
618 // has already been populated
619 loop_information_ = info;
620 } else {
621 // Block is part of an inner loop. Do not update the loop information.
622 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
623 // at this point, because this method is being called while populating `info`.
624 }
625 }
626
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000627 // Raw update of the loop information.
628 void SetLoopInformation(HLoopInformation* info) {
629 loop_information_ = info;
630 }
631
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100632 bool IsInLoop() const { return loop_information_ != nullptr; }
633
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100634 // Returns wheter this block dominates the blocked passed as parameter.
635 bool Dominates(HBasicBlock* block) const;
636
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100637 size_t GetLifetimeStart() const { return lifetime_start_; }
638 size_t GetLifetimeEnd() const { return lifetime_end_; }
639
640 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
641 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
642
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100643 uint32_t GetDexPc() const { return dex_pc_; }
644
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000645 bool IsCatchBlock() const { return is_catch_block_; }
646 void SetIsCatchBlock() { is_catch_block_ = true; }
647
David Brazdil8d5b8b22015-03-24 10:51:52 +0000648 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000649 bool EndsWithIf() const;
650 bool HasSinglePhi() const;
651
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000653 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654 GrowableArray<HBasicBlock*> predecessors_;
655 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100656 HInstructionList instructions_;
657 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000658 HLoopInformation* loop_information_;
659 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100660 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000661 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100662 // The dex program counter of the first instruction of this block.
663 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100664 size_t lifetime_start_;
665 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000666 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000667
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000668 friend class HGraph;
669 friend class HInstruction;
670
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000671 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
672};
673
David Brazdilb2bd1c52015-03-25 11:17:37 +0000674// Iterates over the LoopInformation of all loops which contain 'block'
675// from the innermost to the outermost.
676class HLoopInformationOutwardIterator : public ValueObject {
677 public:
678 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
679 : current_(block.GetLoopInformation()) {}
680
681 bool Done() const { return current_ == nullptr; }
682
683 void Advance() {
684 DCHECK(!Done());
685 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
686 }
687
688 HLoopInformation* Current() const {
689 DCHECK(!Done());
690 return current_;
691 }
692
693 private:
694 HLoopInformation* current_;
695
696 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
697};
698
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100699#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
700 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000701 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000702 M(ArrayGet, Instruction) \
703 M(ArrayLength, Instruction) \
704 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100705 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000706 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000707 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000708 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100709 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000710 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100711 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700712 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000713 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000714 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000715 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100716 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000717 M(Exit, Instruction) \
718 M(FloatConstant, Constant) \
719 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100720 M(GreaterThan, Condition) \
721 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100722 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000723 M(InstanceFieldGet, Instruction) \
724 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000725 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100726 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000727 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000728 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100729 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000730 M(LessThan, Condition) \
731 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000732 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000733 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100734 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000735 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100736 M(Local, Instruction) \
737 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100738 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000739 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000740 M(Mul, BinaryOperation) \
741 M(Neg, UnaryOperation) \
742 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100743 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100744 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000745 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000746 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000747 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000748 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100749 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000750 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100751 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000752 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100753 M(Return, Instruction) \
754 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000755 M(Shl, BinaryOperation) \
756 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100757 M(StaticFieldGet, Instruction) \
758 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100759 M(StoreLocal, Instruction) \
760 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100761 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000762 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000763 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000764 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000765 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000766 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000767
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100768#define FOR_EACH_INSTRUCTION(M) \
769 FOR_EACH_CONCRETE_INSTRUCTION(M) \
770 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100771 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100772 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100773 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700774
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100775#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000776FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
777#undef FORWARD_DECLARATION
778
Roland Levillainccc07a92014-09-16 14:48:16 +0100779#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000780 InstructionKind GetKind() const OVERRIDE { return k##type; } \
781 const char* DebugName() const OVERRIDE { return #type; } \
782 const H##type* As##type() const OVERRIDE { return this; } \
783 H##type* As##type() OVERRIDE { return this; } \
784 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100785 return other->Is##type(); \
786 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000787 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000788
David Brazdiled596192015-01-23 10:39:45 +0000789template <typename T> class HUseList;
790
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100791template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700792class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000793 public:
David Brazdiled596192015-01-23 10:39:45 +0000794 HUseListNode* GetPrevious() const { return prev_; }
795 HUseListNode* GetNext() const { return next_; }
796 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100797 size_t GetIndex() const { return index_; }
798
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000799 private:
David Brazdiled596192015-01-23 10:39:45 +0000800 HUseListNode(T user, size_t index)
801 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
802
803 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100804 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000805 HUseListNode<T>* prev_;
806 HUseListNode<T>* next_;
807
808 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000809
810 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
811};
812
David Brazdiled596192015-01-23 10:39:45 +0000813template <typename T>
814class HUseList : public ValueObject {
815 public:
816 HUseList() : first_(nullptr) {}
817
818 void Clear() {
819 first_ = nullptr;
820 }
821
822 // Adds a new entry at the beginning of the use list and returns
823 // the newly created node.
824 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000825 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000826 if (IsEmpty()) {
827 first_ = new_node;
828 } else {
829 first_->prev_ = new_node;
830 new_node->next_ = first_;
831 first_ = new_node;
832 }
833 return new_node;
834 }
835
836 HUseListNode<T>* GetFirst() const {
837 return first_;
838 }
839
840 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000841 DCHECK(node != nullptr);
842 DCHECK(Contains(node));
843
David Brazdiled596192015-01-23 10:39:45 +0000844 if (node->prev_ != nullptr) {
845 node->prev_->next_ = node->next_;
846 }
847 if (node->next_ != nullptr) {
848 node->next_->prev_ = node->prev_;
849 }
850 if (node == first_) {
851 first_ = node->next_;
852 }
853 }
854
David Brazdil1abb4192015-02-17 18:33:36 +0000855 bool Contains(const HUseListNode<T>* node) const {
856 if (node == nullptr) {
857 return false;
858 }
859 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
860 if (current == node) {
861 return true;
862 }
863 }
864 return false;
865 }
866
David Brazdiled596192015-01-23 10:39:45 +0000867 bool IsEmpty() const {
868 return first_ == nullptr;
869 }
870
871 bool HasOnlyOneUse() const {
872 return first_ != nullptr && first_->next_ == nullptr;
873 }
874
875 private:
876 HUseListNode<T>* first_;
877};
878
879template<typename T>
880class HUseIterator : public ValueObject {
881 public:
882 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
883
884 bool Done() const { return current_ == nullptr; }
885
886 void Advance() {
887 DCHECK(!Done());
888 current_ = current_->GetNext();
889 }
890
891 HUseListNode<T>* Current() const {
892 DCHECK(!Done());
893 return current_;
894 }
895
896 private:
897 HUseListNode<T>* current_;
898
899 friend class HValue;
900};
901
David Brazdil1abb4192015-02-17 18:33:36 +0000902// This class is used by HEnvironment and HInstruction classes to record the
903// instructions they use and pointers to the corresponding HUseListNodes kept
904// by the used instructions.
905template <typename T>
906class HUserRecord : public ValueObject {
907 public:
908 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
909 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
910
911 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
912 : instruction_(old_record.instruction_), use_node_(use_node) {
913 DCHECK(instruction_ != nullptr);
914 DCHECK(use_node_ != nullptr);
915 DCHECK(old_record.use_node_ == nullptr);
916 }
917
918 HInstruction* GetInstruction() const { return instruction_; }
919 HUseListNode<T>* GetUseNode() const { return use_node_; }
920
921 private:
922 // Instruction used by the user.
923 HInstruction* instruction_;
924
925 // Corresponding entry in the use list kept by 'instruction_'.
926 HUseListNode<T>* use_node_;
927};
928
Calin Juravle27df7582015-04-17 19:12:31 +0100929// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
930// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
931// flag is consider.
932// - DependsOn suggests that there is a real dependency between side effects but it only
933// checks DependendsOnSomething flag.
934//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100935// Represents the side effects an instruction may have.
936class SideEffects : public ValueObject {
937 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100938 SideEffects() : flags_(0) {}
939
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100940 static SideEffects None() {
941 return SideEffects(0);
942 }
943
944 static SideEffects All() {
945 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
946 }
947
948 static SideEffects ChangesSomething() {
949 return SideEffects((1 << kFlagChangesCount) - 1);
950 }
951
952 static SideEffects DependsOnSomething() {
953 int count = kFlagDependsOnCount - kFlagChangesCount;
954 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
955 }
956
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100957 SideEffects Union(SideEffects other) const {
958 return SideEffects(flags_ | other.flags_);
959 }
960
Roland Levillain72bceff2014-09-15 18:29:00 +0100961 bool HasSideEffects() const {
962 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
963 return (flags_ & all_bits_set) != 0;
964 }
965
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100966 bool HasAllSideEffects() const {
967 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
968 return all_bits_set == (flags_ & all_bits_set);
969 }
970
971 bool DependsOn(SideEffects other) const {
972 size_t depends_flags = other.ComputeDependsFlags();
973 return (flags_ & depends_flags) != 0;
974 }
975
976 bool HasDependencies() const {
977 int count = kFlagDependsOnCount - kFlagChangesCount;
978 size_t all_bits_set = (1 << count) - 1;
979 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
980 }
981
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100982 private:
983 static constexpr int kFlagChangesSomething = 0;
984 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
985
986 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
987 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
988
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100989 explicit SideEffects(size_t flags) : flags_(flags) {}
990
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100991 size_t ComputeDependsFlags() const {
992 return flags_ << kFlagChangesCount;
993 }
994
995 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100996};
997
David Brazdiled596192015-01-23 10:39:45 +0000998// A HEnvironment object contains the values of virtual registers at a given location.
999class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1000 public:
1001 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
1002 : vregs_(arena, number_of_vregs) {
1003 vregs_.SetSize(number_of_vregs);
1004 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001005 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001006 }
1007 }
1008
1009 void CopyFrom(HEnvironment* env);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001010 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1011 // input to the loop phi instead. This is for inserting instructions that
1012 // require an environment (like HDeoptimization) in the loop pre-header.
1013 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001014
1015 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001016 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001017 }
1018
1019 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001020 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001021 }
1022
David Brazdil1abb4192015-02-17 18:33:36 +00001023 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001024
1025 size_t Size() const { return vregs_.Size(); }
1026
1027 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001028 // Record instructions' use entries of this environment for constant-time removal.
1029 // It should only be called by HInstruction when a new environment use is added.
1030 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1031 DCHECK(env_use->GetUser() == this);
1032 size_t index = env_use->GetIndex();
1033 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1034 }
David Brazdiled596192015-01-23 10:39:45 +00001035
David Brazdil1abb4192015-02-17 18:33:36 +00001036 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001037
David Brazdil1abb4192015-02-17 18:33:36 +00001038 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001039
1040 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1041};
1042
Calin Juravleacf735c2015-02-12 15:25:22 +00001043class ReferenceTypeInfo : ValueObject {
1044 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001045 typedef Handle<mirror::Class> TypeHandle;
1046
1047 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1048 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1049 if (type_handle->IsObjectClass()) {
1050 // Override the type handle to be consistent with the case when we get to
1051 // Top but don't have the Object class available. It avoids having to guess
1052 // what value the type_handle has when it's Top.
1053 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1054 } else {
1055 return ReferenceTypeInfo(type_handle, is_exact, false);
1056 }
1057 }
1058
1059 static ReferenceTypeInfo CreateTop(bool is_exact) {
1060 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001061 }
1062
1063 bool IsExact() const { return is_exact_; }
1064 bool IsTop() const { return is_top_; }
1065
1066 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1067
Calin Juravleb1498f62015-02-16 13:13:29 +00001068 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001069 if (IsTop()) {
1070 // Top (equivalent for java.lang.Object) is supertype of anything.
1071 return true;
1072 }
1073 if (rti.IsTop()) {
1074 // If we get here `this` is not Top() so it can't be a supertype.
1075 return false;
1076 }
1077 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1078 }
1079
1080 // Returns true if the type information provide the same amount of details.
1081 // Note that it does not mean that the instructions have the same actual type
1082 // (e.g. tops are equal but they can be the result of a merge).
1083 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1084 if (IsExact() != rti.IsExact()) {
1085 return false;
1086 }
1087 if (IsTop() && rti.IsTop()) {
1088 // `Top` means java.lang.Object, so the types are equivalent.
1089 return true;
1090 }
1091 if (IsTop() || rti.IsTop()) {
1092 // If only one is top or object than they are not equivalent.
1093 // NB: We need this extra check because the type_handle of `Top` is invalid
1094 // and we cannot inspect its reference.
1095 return false;
1096 }
1097
1098 // Finally check the types.
1099 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1100 }
1101
1102 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001103 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1104 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1105 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1106
Calin Juravleacf735c2015-02-12 15:25:22 +00001107 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001108 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001109 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001110 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001111 bool is_exact_;
1112 // A true value here means that the object type should be java.lang.Object.
1113 // We don't have access to the corresponding mirror object every time so this
1114 // flag acts as a substitute. When true, the TypeHandle refers to a null
1115 // pointer and should not be used.
1116 bool is_top_;
1117};
1118
1119std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1120
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001121class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001122 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001123 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001124 : previous_(nullptr),
1125 next_(nullptr),
1126 block_(nullptr),
1127 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001128 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001129 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001130 locations_(nullptr),
1131 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001132 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001133 side_effects_(side_effects),
1134 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001135
Dave Allison20dfc792014-06-16 20:44:29 -07001136 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001137
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001138#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001139 enum InstructionKind {
1140 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1141 };
1142#undef DECLARE_KIND
1143
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001144 HInstruction* GetNext() const { return next_; }
1145 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001146
Calin Juravle77520bc2015-01-12 18:45:46 +00001147 HInstruction* GetNextDisregardingMoves() const;
1148 HInstruction* GetPreviousDisregardingMoves() const;
1149
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001150 HBasicBlock* GetBlock() const { return block_; }
1151 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001152 bool IsInBlock() const { return block_ != nullptr; }
1153 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001154 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001155
Roland Levillain6b879dd2014-09-22 17:13:44 +01001156 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001157 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001158
1159 virtual void Accept(HGraphVisitor* visitor) = 0;
1160 virtual const char* DebugName() const = 0;
1161
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001162 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001163 void SetRawInputAt(size_t index, HInstruction* input) {
1164 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1165 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001166
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001167 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001168 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001169 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001170 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001171
Calin Juravle10e244f2015-01-26 18:54:32 +00001172 // Does not apply for all instructions, but having this at top level greatly
1173 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001174 virtual bool CanBeNull() const {
1175 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1176 return true;
1177 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001178
Calin Juravle641547a2015-04-21 22:08:51 +01001179 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1180 UNUSED(obj);
1181 return false;
1182 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001183
Calin Juravleacf735c2015-02-12 15:25:22 +00001184 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001185 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001186 reference_type_info_ = reference_type_info;
1187 }
1188
Calin Juravle61d544b2015-02-23 16:46:57 +00001189 ReferenceTypeInfo GetReferenceTypeInfo() const {
1190 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1191 return reference_type_info_;
1192 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001193
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001194 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001195 DCHECK(user != nullptr);
1196 HUseListNode<HInstruction*>* use =
1197 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1198 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001199 }
1200
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001201 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001202 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001203 HUseListNode<HEnvironment*>* env_use =
1204 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1205 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001206 }
1207
David Brazdil1abb4192015-02-17 18:33:36 +00001208 void RemoveAsUserOfInput(size_t input) {
1209 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1210 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1211 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001212
David Brazdil1abb4192015-02-17 18:33:36 +00001213 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1214 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001215
David Brazdiled596192015-01-23 10:39:45 +00001216 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1217 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001218 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001219 bool HasOnlyOneNonEnvironmentUse() const {
1220 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1221 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001222
Roland Levillain6c82d402014-10-13 16:10:27 +01001223 // Does this instruction strictly dominate `other_instruction`?
1224 // Returns false if this instruction and `other_instruction` are the same.
1225 // Aborts if this instruction and `other_instruction` are both phis.
1226 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001227
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001228 int GetId() const { return id_; }
1229 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001230
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001231 int GetSsaIndex() const { return ssa_index_; }
1232 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1233 bool HasSsaIndex() const { return ssa_index_ != -1; }
1234
1235 bool HasEnvironment() const { return environment_ != nullptr; }
1236 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001237 // Set the `environment_` field. Raw because this method does not
1238 // update the uses lists.
1239 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1240
1241 // Set the environment of this instruction, copying it from `environment`. While
1242 // copying, the uses lists are being updated.
1243 void CopyEnvironmentFrom(HEnvironment* environment) {
1244 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1245 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1246 environment_->CopyFrom(environment);
1247 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001248
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001249 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1250 HBasicBlock* block) {
1251 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1252 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1253 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1254 }
1255
Nicolas Geoffray39468442014-09-02 15:17:15 +01001256 // Returns the number of entries in the environment. Typically, that is the
1257 // number of dex registers in a method. It could be more in case of inlining.
1258 size_t EnvironmentSize() const;
1259
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001260 LocationSummary* GetLocations() const { return locations_; }
1261 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001262
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001263 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001264 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001265
Alexandre Rames188d4312015-04-09 18:30:21 +01001266 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1267 // uses of this instruction by `other` are *not* updated.
1268 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1269 ReplaceWith(other);
1270 other->ReplaceInput(this, use_index);
1271 }
1272
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001273 // Move `this` instruction before `cursor`.
1274 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001275
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001276#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001277 bool Is##type() const { return (As##type() != nullptr); } \
1278 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001279 virtual H##type* As##type() { return nullptr; }
1280
1281 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1282#undef INSTRUCTION_TYPE_CHECK
1283
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001284 // Returns whether the instruction can be moved within the graph.
1285 virtual bool CanBeMoved() const { return false; }
1286
1287 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001288 virtual bool InstructionTypeEquals(HInstruction* other) const {
1289 UNUSED(other);
1290 return false;
1291 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001292
1293 // Returns whether any data encoded in the two instructions is equal.
1294 // This method does not look at the inputs. Both instructions must be
1295 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001296 virtual bool InstructionDataEquals(HInstruction* other) const {
1297 UNUSED(other);
1298 return false;
1299 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001300
1301 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001302 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001303 // 2) Their inputs are identical.
1304 bool Equals(HInstruction* other) const;
1305
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001306 virtual InstructionKind GetKind() const = 0;
1307
1308 virtual size_t ComputeHashCode() const {
1309 size_t result = GetKind();
1310 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1311 result = (result * 31) + InputAt(i)->GetId();
1312 }
1313 return result;
1314 }
1315
1316 SideEffects GetSideEffects() const { return side_effects_; }
1317
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001318 size_t GetLifetimePosition() const { return lifetime_position_; }
1319 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1320 LiveInterval* GetLiveInterval() const { return live_interval_; }
1321 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1322 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1323
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001324 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1325
1326 // Returns whether the code generation of the instruction will require to have access
1327 // to the current method. Such instructions are:
1328 // (1): Instructions that require an environment, as calling the runtime requires
1329 // to walk the stack and have the current method stored at a specific stack address.
1330 // (2): Object literals like classes and strings, that are loaded from the dex cache
1331 // fields of the current method.
1332 bool NeedsCurrentMethod() const {
1333 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1334 }
1335
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001336 virtual bool NeedsDexCache() const { return false; }
1337
David Brazdil1abb4192015-02-17 18:33:36 +00001338 protected:
1339 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1340 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1341
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001342 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001343 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1344
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001345 HInstruction* previous_;
1346 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001347 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001348
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001349 // An instruction gets an id when it is added to the graph.
1350 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001351 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001352 int id_;
1353
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001354 // When doing liveness analysis, instructions that have uses get an SSA index.
1355 int ssa_index_;
1356
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001357 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001358 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001359
1360 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001361 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001362
Nicolas Geoffray39468442014-09-02 15:17:15 +01001363 // The environment associated with this instruction. Not null if the instruction
1364 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001365 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001366
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001367 // Set by the code generator.
1368 LocationSummary* locations_;
1369
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001370 // Set by the liveness analysis.
1371 LiveInterval* live_interval_;
1372
1373 // Set by the liveness analysis, this is the position in a linear
1374 // order of blocks where this instruction's live interval start.
1375 size_t lifetime_position_;
1376
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001377 const SideEffects side_effects_;
1378
Calin Juravleacf735c2015-02-12 15:25:22 +00001379 // TODO: for primitive types this should be marked as invalid.
1380 ReferenceTypeInfo reference_type_info_;
1381
David Brazdil1abb4192015-02-17 18:33:36 +00001382 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001383 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001384 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001385 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001386 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001387
1388 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1389};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001390std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001391
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001392class HInputIterator : public ValueObject {
1393 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001394 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001395
1396 bool Done() const { return index_ == instruction_->InputCount(); }
1397 HInstruction* Current() const { return instruction_->InputAt(index_); }
1398 void Advance() { index_++; }
1399
1400 private:
1401 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001402 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001403
1404 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1405};
1406
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001407class HInstructionIterator : public ValueObject {
1408 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001409 explicit HInstructionIterator(const HInstructionList& instructions)
1410 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001411 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001412 }
1413
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001414 bool Done() const { return instruction_ == nullptr; }
1415 HInstruction* Current() const { return instruction_; }
1416 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001417 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001418 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001419 }
1420
1421 private:
1422 HInstruction* instruction_;
1423 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001424
1425 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001426};
1427
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001428class HBackwardInstructionIterator : public ValueObject {
1429 public:
1430 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1431 : instruction_(instructions.last_instruction_) {
1432 next_ = Done() ? nullptr : instruction_->GetPrevious();
1433 }
1434
1435 bool Done() const { return instruction_ == nullptr; }
1436 HInstruction* Current() const { return instruction_; }
1437 void Advance() {
1438 instruction_ = next_;
1439 next_ = Done() ? nullptr : instruction_->GetPrevious();
1440 }
1441
1442 private:
1443 HInstruction* instruction_;
1444 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001445
1446 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001447};
1448
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001449// An embedded container with N elements of type T. Used (with partial
1450// specialization for N=0) because embedded arrays cannot have size 0.
1451template<typename T, intptr_t N>
1452class EmbeddedArray {
1453 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001454 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001455
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001456 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001457
1458 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001459 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001460 return elements_[i];
1461 }
1462
1463 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001464 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001465 return elements_[i];
1466 }
1467
1468 const T& At(intptr_t i) const {
1469 return (*this)[i];
1470 }
1471
1472 void SetAt(intptr_t i, const T& val) {
1473 (*this)[i] = val;
1474 }
1475
1476 private:
1477 T elements_[N];
1478};
1479
1480template<typename T>
1481class EmbeddedArray<T, 0> {
1482 public:
1483 intptr_t length() const { return 0; }
1484 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001485 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001486 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001487 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001488 }
1489 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001490 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001491 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001492 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001493 }
1494};
1495
1496template<intptr_t N>
1497class HTemplateInstruction: public HInstruction {
1498 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499 HTemplateInstruction<N>(SideEffects side_effects)
1500 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001501 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001502
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001503 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001504
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001505 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001506 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1507
1508 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1509 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001510 }
1511
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001512 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001513 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001514
1515 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001516};
1517
Dave Allison20dfc792014-06-16 20:44:29 -07001518template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001520 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001521 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1522 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001523 virtual ~HExpression() {}
1524
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001525 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001526
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001527 protected:
1528 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001529};
1530
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001531// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1532// instruction that branches to the exit block.
1533class HReturnVoid : public HTemplateInstruction<0> {
1534 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001535 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001536
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001537 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001538
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001539 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001540
1541 private:
1542 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1543};
1544
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001545// Represents dex's RETURN opcodes. A HReturn is a control flow
1546// instruction that branches to the exit block.
1547class HReturn : public HTemplateInstruction<1> {
1548 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001549 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001550 SetRawInputAt(0, value);
1551 }
1552
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001553 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001554
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001555 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001556
1557 private:
1558 DISALLOW_COPY_AND_ASSIGN(HReturn);
1559};
1560
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001561// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001562// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001563// exit block.
1564class HExit : public HTemplateInstruction<0> {
1565 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001566 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001567
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001568 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001569
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001570 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001571
1572 private:
1573 DISALLOW_COPY_AND_ASSIGN(HExit);
1574};
1575
1576// Jumps from one block to another.
1577class HGoto : public HTemplateInstruction<0> {
1578 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001579 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1580
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001581 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001582
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001583 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001584 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001585 }
1586
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001587 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001588
1589 private:
1590 DISALLOW_COPY_AND_ASSIGN(HGoto);
1591};
1592
Dave Allison20dfc792014-06-16 20:44:29 -07001593
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001594// Conditional branch. A block ending with an HIf instruction must have
1595// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001596class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001597 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001598 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001599 SetRawInputAt(0, input);
1600 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001601
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001602 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001603
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001604 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001605 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001606 }
1607
1608 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001609 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001610 }
1611
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001612 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001613
1614 private:
1615 DISALLOW_COPY_AND_ASSIGN(HIf);
1616};
1617
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001618// Deoptimize to interpreter, upon checking a condition.
1619class HDeoptimize : public HTemplateInstruction<1> {
1620 public:
1621 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1622 : HTemplateInstruction(SideEffects::None()),
1623 dex_pc_(dex_pc) {
1624 SetRawInputAt(0, cond);
1625 }
1626
1627 bool NeedsEnvironment() const OVERRIDE { return true; }
1628 bool CanThrow() const OVERRIDE { return true; }
1629 uint32_t GetDexPc() const { return dex_pc_; }
1630
1631 DECLARE_INSTRUCTION(Deoptimize);
1632
1633 private:
1634 uint32_t dex_pc_;
1635
1636 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1637};
1638
Roland Levillain88cb1752014-10-20 16:36:47 +01001639class HUnaryOperation : public HExpression<1> {
1640 public:
1641 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1642 : HExpression(result_type, SideEffects::None()) {
1643 SetRawInputAt(0, input);
1644 }
1645
1646 HInstruction* GetInput() const { return InputAt(0); }
1647 Primitive::Type GetResultType() const { return GetType(); }
1648
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001649 bool CanBeMoved() const OVERRIDE { return true; }
1650 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001651 UNUSED(other);
1652 return true;
1653 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001654
Roland Levillain9240d6a2014-10-20 16:47:04 +01001655 // Try to statically evaluate `operation` and return a HConstant
1656 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001657 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001658 HConstant* TryStaticEvaluation() const;
1659
1660 // Apply this operation to `x`.
1661 virtual int32_t Evaluate(int32_t x) const = 0;
1662 virtual int64_t Evaluate(int64_t x) const = 0;
1663
Roland Levillain88cb1752014-10-20 16:36:47 +01001664 DECLARE_INSTRUCTION(UnaryOperation);
1665
1666 private:
1667 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1668};
1669
Dave Allison20dfc792014-06-16 20:44:29 -07001670class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001671 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001672 HBinaryOperation(Primitive::Type result_type,
1673 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001674 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001675 SetRawInputAt(0, left);
1676 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001677 }
1678
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001679 HInstruction* GetLeft() const { return InputAt(0); }
1680 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001681 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001682
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001683 virtual bool IsCommutative() const { return false; }
1684
1685 // Put constant on the right.
1686 // Returns whether order is changed.
1687 bool OrderInputsWithConstantOnTheRight() {
1688 HInstruction* left = InputAt(0);
1689 HInstruction* right = InputAt(1);
1690 if (left->IsConstant() && !right->IsConstant()) {
1691 ReplaceInput(right, 0);
1692 ReplaceInput(left, 1);
1693 return true;
1694 }
1695 return false;
1696 }
1697
1698 // Order inputs by instruction id, but favor constant on the right side.
1699 // This helps GVN for commutative ops.
1700 void OrderInputs() {
1701 DCHECK(IsCommutative());
1702 HInstruction* left = InputAt(0);
1703 HInstruction* right = InputAt(1);
1704 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1705 return;
1706 }
1707 if (OrderInputsWithConstantOnTheRight()) {
1708 return;
1709 }
1710 // Order according to instruction id.
1711 if (left->GetId() > right->GetId()) {
1712 ReplaceInput(right, 0);
1713 ReplaceInput(left, 1);
1714 }
1715 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001716
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001717 bool CanBeMoved() const OVERRIDE { return true; }
1718 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001719 UNUSED(other);
1720 return true;
1721 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001722
Roland Levillain9240d6a2014-10-20 16:47:04 +01001723 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001724 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001725 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001726 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001727
1728 // Apply this operation to `x` and `y`.
1729 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1730 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1731
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001732 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001733 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001734 HConstant* GetConstantRight() const;
1735
1736 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001737 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001738 HInstruction* GetLeastConstantLeft() const;
1739
Roland Levillainccc07a92014-09-16 14:48:16 +01001740 DECLARE_INSTRUCTION(BinaryOperation);
1741
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001742 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001743 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1744};
1745
Dave Allison20dfc792014-06-16 20:44:29 -07001746class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001747 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001748 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001749 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1750 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001751
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001752 bool NeedsMaterialization() const { return needs_materialization_; }
1753 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001754
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001755 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001756 // `instruction`, and disregard moves in between.
1757 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001758
Dave Allison20dfc792014-06-16 20:44:29 -07001759 DECLARE_INSTRUCTION(Condition);
1760
1761 virtual IfCondition GetCondition() const = 0;
1762
1763 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001764 // For register allocation purposes, returns whether this instruction needs to be
1765 // materialized (that is, not just be in the processor flags).
1766 bool needs_materialization_;
1767
Dave Allison20dfc792014-06-16 20:44:29 -07001768 DISALLOW_COPY_AND_ASSIGN(HCondition);
1769};
1770
1771// Instruction to check if two inputs are equal to each other.
1772class HEqual : public HCondition {
1773 public:
1774 HEqual(HInstruction* first, HInstruction* second)
1775 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001776
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001777 bool IsCommutative() const OVERRIDE { return true; }
1778
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001779 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001780 return x == y ? 1 : 0;
1781 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001782 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001783 return x == y ? 1 : 0;
1784 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001785
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001786 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001787
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001788 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001789 return kCondEQ;
1790 }
1791
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001792 private:
1793 DISALLOW_COPY_AND_ASSIGN(HEqual);
1794};
1795
Dave Allison20dfc792014-06-16 20:44:29 -07001796class HNotEqual : public HCondition {
1797 public:
1798 HNotEqual(HInstruction* first, HInstruction* second)
1799 : HCondition(first, second) {}
1800
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001801 bool IsCommutative() const OVERRIDE { return true; }
1802
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001803 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001804 return x != y ? 1 : 0;
1805 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001806 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001807 return x != y ? 1 : 0;
1808 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001809
Dave Allison20dfc792014-06-16 20:44:29 -07001810 DECLARE_INSTRUCTION(NotEqual);
1811
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001812 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001813 return kCondNE;
1814 }
1815
1816 private:
1817 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1818};
1819
1820class HLessThan : public HCondition {
1821 public:
1822 HLessThan(HInstruction* first, HInstruction* second)
1823 : HCondition(first, second) {}
1824
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001825 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001826 return x < y ? 1 : 0;
1827 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001828 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001829 return x < y ? 1 : 0;
1830 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001831
Dave Allison20dfc792014-06-16 20:44:29 -07001832 DECLARE_INSTRUCTION(LessThan);
1833
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001834 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001835 return kCondLT;
1836 }
1837
1838 private:
1839 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1840};
1841
1842class HLessThanOrEqual : public HCondition {
1843 public:
1844 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1845 : HCondition(first, second) {}
1846
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001847 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001848 return x <= y ? 1 : 0;
1849 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001850 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001851 return x <= y ? 1 : 0;
1852 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001853
Dave Allison20dfc792014-06-16 20:44:29 -07001854 DECLARE_INSTRUCTION(LessThanOrEqual);
1855
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001856 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001857 return kCondLE;
1858 }
1859
1860 private:
1861 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1862};
1863
1864class HGreaterThan : public HCondition {
1865 public:
1866 HGreaterThan(HInstruction* first, HInstruction* second)
1867 : HCondition(first, second) {}
1868
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001869 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001870 return x > y ? 1 : 0;
1871 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001872 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001873 return x > y ? 1 : 0;
1874 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001875
Dave Allison20dfc792014-06-16 20:44:29 -07001876 DECLARE_INSTRUCTION(GreaterThan);
1877
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001878 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001879 return kCondGT;
1880 }
1881
1882 private:
1883 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1884};
1885
1886class HGreaterThanOrEqual : public HCondition {
1887 public:
1888 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1889 : HCondition(first, second) {}
1890
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001891 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001892 return x >= y ? 1 : 0;
1893 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001894 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001895 return x >= y ? 1 : 0;
1896 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001897
Dave Allison20dfc792014-06-16 20:44:29 -07001898 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1899
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001900 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001901 return kCondGE;
1902 }
1903
1904 private:
1905 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1906};
1907
1908
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001909// Instruction to check how two inputs compare to each other.
1910// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1911class HCompare : public HBinaryOperation {
1912 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001913 // The bias applies for floating point operations and indicates how NaN
1914 // comparisons are treated:
1915 enum Bias {
1916 kNoBias, // bias is not applicable (i.e. for long operation)
1917 kGtBias, // return 1 for NaN comparisons
1918 kLtBias, // return -1 for NaN comparisons
1919 };
1920
1921 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1922 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001923 DCHECK_EQ(type, first->GetType());
1924 DCHECK_EQ(type, second->GetType());
1925 }
1926
Calin Juravleddb7df22014-11-25 20:56:51 +00001927 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001928 return
1929 x == y ? 0 :
1930 x > y ? 1 :
1931 -1;
1932 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001933
1934 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001935 return
1936 x == y ? 0 :
1937 x > y ? 1 :
1938 -1;
1939 }
1940
Calin Juravleddb7df22014-11-25 20:56:51 +00001941 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1942 return bias_ == other->AsCompare()->bias_;
1943 }
1944
1945 bool IsGtBias() { return bias_ == kGtBias; }
1946
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001947 DECLARE_INSTRUCTION(Compare);
1948
1949 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001950 const Bias bias_;
1951
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001952 DISALLOW_COPY_AND_ASSIGN(HCompare);
1953};
1954
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001955// A local in the graph. Corresponds to a Dex register.
1956class HLocal : public HTemplateInstruction<0> {
1957 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001958 explicit HLocal(uint16_t reg_number)
1959 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001960
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001961 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001962
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001963 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001964
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001965 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001966 // The Dex register number.
1967 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001968
1969 DISALLOW_COPY_AND_ASSIGN(HLocal);
1970};
1971
1972// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001973class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001974 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001975 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001976 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001977 SetRawInputAt(0, local);
1978 }
1979
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001980 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1981
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001982 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001983
1984 private:
1985 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1986};
1987
1988// Store a value in a given local. This instruction has two inputs: the value
1989// and the local.
1990class HStoreLocal : public HTemplateInstruction<2> {
1991 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001992 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001993 SetRawInputAt(0, local);
1994 SetRawInputAt(1, value);
1995 }
1996
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001997 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1998
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001999 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002000
2001 private:
2002 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2003};
2004
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002005class HConstant : public HExpression<0> {
2006 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002007 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2008
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002009 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002010
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002011 virtual bool IsMinusOne() const { return false; }
2012 virtual bool IsZero() const { return false; }
2013 virtual bool IsOne() const { return false; }
2014
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002015 DECLARE_INSTRUCTION(Constant);
2016
2017 private:
2018 DISALLOW_COPY_AND_ASSIGN(HConstant);
2019};
2020
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002021class HFloatConstant : public HConstant {
2022 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002023 float GetValue() const { return value_; }
2024
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002025 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002026 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2027 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002028 }
2029
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002030 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002031
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002032 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002033 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2034 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002035 }
2036 bool IsZero() const OVERRIDE {
2037 return AsFloatConstant()->GetValue() == 0.0f;
2038 }
2039 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002040 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2041 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002042 }
2043
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002044 DECLARE_INSTRUCTION(FloatConstant);
2045
2046 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002047 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
2048
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002049 const float value_;
2050
Nicolas Geoffray067cae22015-04-26 16:43:00 +00002051 // Only the SsaBuilder can currently create floating-point constants. If we
2052 // ever need to create them later in the pipeline, we will have to handle them
2053 // the same way as integral constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002054 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002055 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2056};
2057
2058class HDoubleConstant : public HConstant {
2059 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002060 double GetValue() const { return value_; }
2061
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002062 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002063 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2064 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002065 }
2066
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002067 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002068
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002069 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002070 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2071 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002072 }
2073 bool IsZero() const OVERRIDE {
2074 return AsDoubleConstant()->GetValue() == 0.0;
2075 }
2076 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002077 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2078 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002079 }
2080
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002081 DECLARE_INSTRUCTION(DoubleConstant);
2082
2083 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002084 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
2085
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002086 const double value_;
2087
Nicolas Geoffray067cae22015-04-26 16:43:00 +00002088 // Only the SsaBuilder can currently create floating-point constants. If we
2089 // ever need to create them later in the pipeline, we will have to handle them
2090 // the same way as integral constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002091 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002092 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2093};
2094
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002095class HNullConstant : public HConstant {
2096 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002097 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2098 return true;
2099 }
2100
2101 size_t ComputeHashCode() const OVERRIDE { return 0; }
2102
2103 DECLARE_INSTRUCTION(NullConstant);
2104
2105 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002106 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2107
2108 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002109 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2110};
2111
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002112// Constants of the type int. Those can be from Dex instructions, or
2113// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002114class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002115 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002116 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002117
Calin Juravle61d544b2015-02-23 16:46:57 +00002118 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002119 return other->AsIntConstant()->value_ == value_;
2120 }
2121
Calin Juravle61d544b2015-02-23 16:46:57 +00002122 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2123
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002124 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2125 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2126 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2127
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002128 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002129
2130 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002131 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2132
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002133 const int32_t value_;
2134
David Brazdil8d5b8b22015-03-24 10:51:52 +00002135 friend class HGraph;
2136 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002137 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002138 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2139};
2140
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002141class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002142 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002143 int64_t GetValue() const { return value_; }
2144
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002145 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002146 return other->AsLongConstant()->value_ == value_;
2147 }
2148
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002149 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002150
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002151 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2152 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2153 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2154
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002155 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002156
2157 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002158 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2159
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002160 const int64_t value_;
2161
David Brazdil8d5b8b22015-03-24 10:51:52 +00002162 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002163 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2164};
2165
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002166enum class Intrinsics {
2167#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2168#include "intrinsics_list.h"
2169 kNone,
2170 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2171#undef INTRINSICS_LIST
2172#undef OPTIMIZING_INTRINSICS
2173};
2174std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2175
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002176class HInvoke : public HInstruction {
2177 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002178 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002179
2180 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2181 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002182 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002183
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002184 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185 SetRawInputAt(index, argument);
2186 }
2187
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002188 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002189
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002190 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002191
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002192 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2193
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002194 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002195 return intrinsic_;
2196 }
2197
2198 void SetIntrinsic(Intrinsics intrinsic) {
2199 intrinsic_ = intrinsic;
2200 }
2201
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002202 DECLARE_INSTRUCTION(Invoke);
2203
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002204 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002205 HInvoke(ArenaAllocator* arena,
2206 uint32_t number_of_arguments,
2207 Primitive::Type return_type,
2208 uint32_t dex_pc,
2209 uint32_t dex_method_index)
2210 : HInstruction(SideEffects::All()),
2211 inputs_(arena, number_of_arguments),
2212 return_type_(return_type),
2213 dex_pc_(dex_pc),
2214 dex_method_index_(dex_method_index),
2215 intrinsic_(Intrinsics::kNone) {
2216 inputs_.SetSize(number_of_arguments);
2217 }
2218
David Brazdil1abb4192015-02-17 18:33:36 +00002219 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2220 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2221 inputs_.Put(index, input);
2222 }
2223
2224 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002225 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002226 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002227 const uint32_t dex_method_index_;
2228 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002229
2230 private:
2231 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2232};
2233
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002234class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002235 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002236 // Requirements of this method call regarding the class
2237 // initialization (clinit) check of its declaring class.
2238 enum class ClinitCheckRequirement {
2239 kNone, // Class already initialized.
2240 kExplicit, // Static call having explicit clinit check as last input.
2241 kImplicit, // Static call implicitly requiring a clinit check.
2242 };
2243
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002244 HInvokeStaticOrDirect(ArenaAllocator* arena,
2245 uint32_t number_of_arguments,
2246 Primitive::Type return_type,
2247 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002248 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002249 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002250 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002251 InvokeType invoke_type,
2252 ClinitCheckRequirement clinit_check_requirement)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002253 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002254 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002255 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002256 is_recursive_(is_recursive),
2257 clinit_check_requirement_(clinit_check_requirement) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002258
Calin Juravle641547a2015-04-21 22:08:51 +01002259 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2260 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002261 // We access the method via the dex cache so we can't do an implicit null check.
2262 // TODO: for intrinsics we can generate implicit null checks.
2263 return false;
2264 }
2265
Nicolas Geoffray79041292015-03-26 10:05:54 +00002266 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002267 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002268 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002269 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002270
Roland Levillain4c0eb422015-04-24 16:43:49 +01002271 // Is this instruction a call to a static method?
2272 bool IsStatic() const {
2273 return GetInvokeType() == kStatic;
2274 }
2275
Roland Levillain0379f822015-04-24 23:01:24 +01002276 // Remove the art::HClinitCheck or art::HLoadClass instruction as
2277 // last input (only relevant for static calls with explicit clinit
2278 // check).
2279 void RemoveClinitCheckOrLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002280 DCHECK(IsStaticWithExplicitClinitCheck());
2281 size_t last_input_index = InputCount() - 1;
2282 HInstruction* last_input = InputAt(last_input_index);
2283 DCHECK(last_input != nullptr);
Roland Levillain0379f822015-04-24 23:01:24 +01002284 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002285 RemoveAsUserOfInput(last_input_index);
2286 inputs_.DeleteAt(last_input_index);
2287 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2288 DCHECK(IsStaticWithImplicitClinitCheck());
2289 }
2290
2291 // Is this a call to a static method whose declaring class has an
2292 // explicit intialization check in the graph?
2293 bool IsStaticWithExplicitClinitCheck() const {
2294 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2295 }
2296
2297 // Is this a call to a static method whose declaring class has an
2298 // implicit intialization check requirement?
2299 bool IsStaticWithImplicitClinitCheck() const {
2300 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2301 }
2302
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002303 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002304
Roland Levillain4c0eb422015-04-24 16:43:49 +01002305 protected:
2306 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2307 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2308 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2309 HInstruction* input = input_record.GetInstruction();
2310 // `input` is the last input of a static invoke marked as having
2311 // an explicit clinit check. It must either be:
2312 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2313 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2314 DCHECK(input != nullptr);
2315 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2316 }
2317 return input_record;
2318 }
2319
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002320 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002321 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002322 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002323 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002324 ClinitCheckRequirement clinit_check_requirement_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002325
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002326 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002327};
2328
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002329class HInvokeVirtual : public HInvoke {
2330 public:
2331 HInvokeVirtual(ArenaAllocator* arena,
2332 uint32_t number_of_arguments,
2333 Primitive::Type return_type,
2334 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002335 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002336 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002337 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002338 vtable_index_(vtable_index) {}
2339
Calin Juravle641547a2015-04-21 22:08:51 +01002340 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002341 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002342 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002343 }
2344
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002345 uint32_t GetVTableIndex() const { return vtable_index_; }
2346
2347 DECLARE_INSTRUCTION(InvokeVirtual);
2348
2349 private:
2350 const uint32_t vtable_index_;
2351
2352 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2353};
2354
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002355class HInvokeInterface : public HInvoke {
2356 public:
2357 HInvokeInterface(ArenaAllocator* arena,
2358 uint32_t number_of_arguments,
2359 Primitive::Type return_type,
2360 uint32_t dex_pc,
2361 uint32_t dex_method_index,
2362 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002363 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002364 imt_index_(imt_index) {}
2365
Calin Juravle641547a2015-04-21 22:08:51 +01002366 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002367 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002368 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002369 }
2370
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002371 uint32_t GetImtIndex() const { return imt_index_; }
2372 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2373
2374 DECLARE_INSTRUCTION(InvokeInterface);
2375
2376 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002377 const uint32_t imt_index_;
2378
2379 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2380};
2381
Dave Allison20dfc792014-06-16 20:44:29 -07002382class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002383 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002384 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002385 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2386 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002387 type_index_(type_index),
2388 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002389
2390 uint32_t GetDexPc() const { return dex_pc_; }
2391 uint16_t GetTypeIndex() const { return type_index_; }
2392
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002393 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002394 bool NeedsEnvironment() const OVERRIDE { return true; }
2395 // It may throw when called on:
2396 // - interfaces
2397 // - abstract/innaccessible/unknown classes
2398 // TODO: optimize when possible.
2399 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002400
Calin Juravle10e244f2015-01-26 18:54:32 +00002401 bool CanBeNull() const OVERRIDE { return false; }
2402
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002403 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2404
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002405 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002406
2407 private:
2408 const uint32_t dex_pc_;
2409 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002410 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002411
2412 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2413};
2414
Roland Levillain88cb1752014-10-20 16:36:47 +01002415class HNeg : public HUnaryOperation {
2416 public:
2417 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2418 : HUnaryOperation(result_type, input) {}
2419
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002420 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2421 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002422
Roland Levillain88cb1752014-10-20 16:36:47 +01002423 DECLARE_INSTRUCTION(Neg);
2424
2425 private:
2426 DISALLOW_COPY_AND_ASSIGN(HNeg);
2427};
2428
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002429class HNewArray : public HExpression<1> {
2430 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002431 HNewArray(HInstruction* length,
2432 uint32_t dex_pc,
2433 uint16_t type_index,
2434 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002435 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2436 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002437 type_index_(type_index),
2438 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002439 SetRawInputAt(0, length);
2440 }
2441
2442 uint32_t GetDexPc() const { return dex_pc_; }
2443 uint16_t GetTypeIndex() const { return type_index_; }
2444
2445 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002446 bool NeedsEnvironment() const OVERRIDE { return true; }
2447
Mingyao Yang0c365e62015-03-31 15:09:29 -07002448 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2449 bool CanThrow() const OVERRIDE { return true; }
2450
Calin Juravle10e244f2015-01-26 18:54:32 +00002451 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002452
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002453 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2454
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002455 DECLARE_INSTRUCTION(NewArray);
2456
2457 private:
2458 const uint32_t dex_pc_;
2459 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002460 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002461
2462 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2463};
2464
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002465class HAdd : public HBinaryOperation {
2466 public:
2467 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2468 : HBinaryOperation(result_type, left, right) {}
2469
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002470 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002471
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002472 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002473 return x + y;
2474 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002475 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002476 return x + y;
2477 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002478
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002479 DECLARE_INSTRUCTION(Add);
2480
2481 private:
2482 DISALLOW_COPY_AND_ASSIGN(HAdd);
2483};
2484
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002485class HSub : public HBinaryOperation {
2486 public:
2487 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2488 : HBinaryOperation(result_type, left, right) {}
2489
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002490 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002491 return x - y;
2492 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002493 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002494 return x - y;
2495 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002496
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002497 DECLARE_INSTRUCTION(Sub);
2498
2499 private:
2500 DISALLOW_COPY_AND_ASSIGN(HSub);
2501};
2502
Calin Juravle34bacdf2014-10-07 20:23:36 +01002503class HMul : public HBinaryOperation {
2504 public:
2505 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2506 : HBinaryOperation(result_type, left, right) {}
2507
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002508 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002509
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002510 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2511 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002512
2513 DECLARE_INSTRUCTION(Mul);
2514
2515 private:
2516 DISALLOW_COPY_AND_ASSIGN(HMul);
2517};
2518
Calin Juravle7c4954d2014-10-28 16:57:40 +00002519class HDiv : public HBinaryOperation {
2520 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002521 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2522 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002523
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002524 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002525 // Our graph structure ensures we never have 0 for `y` during constant folding.
2526 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002527 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002528 return (y == -1) ? -x : x / y;
2529 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002530
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002531 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002532 DCHECK_NE(y, 0);
2533 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2534 return (y == -1) ? -x : x / y;
2535 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002536
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002537 uint32_t GetDexPc() const { return dex_pc_; }
2538
Calin Juravle7c4954d2014-10-28 16:57:40 +00002539 DECLARE_INSTRUCTION(Div);
2540
2541 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002542 const uint32_t dex_pc_;
2543
Calin Juravle7c4954d2014-10-28 16:57:40 +00002544 DISALLOW_COPY_AND_ASSIGN(HDiv);
2545};
2546
Calin Juravlebacfec32014-11-14 15:54:36 +00002547class HRem : public HBinaryOperation {
2548 public:
2549 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2550 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2551
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002552 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002553 DCHECK_NE(y, 0);
2554 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2555 return (y == -1) ? 0 : x % y;
2556 }
2557
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002558 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002559 DCHECK_NE(y, 0);
2560 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2561 return (y == -1) ? 0 : x % y;
2562 }
2563
2564 uint32_t GetDexPc() const { return dex_pc_; }
2565
2566 DECLARE_INSTRUCTION(Rem);
2567
2568 private:
2569 const uint32_t dex_pc_;
2570
2571 DISALLOW_COPY_AND_ASSIGN(HRem);
2572};
2573
Calin Juravled0d48522014-11-04 16:40:20 +00002574class HDivZeroCheck : public HExpression<1> {
2575 public:
2576 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2577 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2578 SetRawInputAt(0, value);
2579 }
2580
2581 bool CanBeMoved() const OVERRIDE { return true; }
2582
2583 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2584 UNUSED(other);
2585 return true;
2586 }
2587
2588 bool NeedsEnvironment() const OVERRIDE { return true; }
2589 bool CanThrow() const OVERRIDE { return true; }
2590
2591 uint32_t GetDexPc() const { return dex_pc_; }
2592
2593 DECLARE_INSTRUCTION(DivZeroCheck);
2594
2595 private:
2596 const uint32_t dex_pc_;
2597
2598 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2599};
2600
Calin Juravle9aec02f2014-11-18 23:06:35 +00002601class HShl : public HBinaryOperation {
2602 public:
2603 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2604 : HBinaryOperation(result_type, left, right) {}
2605
2606 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2607 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2608
2609 DECLARE_INSTRUCTION(Shl);
2610
2611 private:
2612 DISALLOW_COPY_AND_ASSIGN(HShl);
2613};
2614
2615class HShr : public HBinaryOperation {
2616 public:
2617 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2618 : HBinaryOperation(result_type, left, right) {}
2619
2620 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2621 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2622
2623 DECLARE_INSTRUCTION(Shr);
2624
2625 private:
2626 DISALLOW_COPY_AND_ASSIGN(HShr);
2627};
2628
2629class HUShr : public HBinaryOperation {
2630 public:
2631 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2632 : HBinaryOperation(result_type, left, right) {}
2633
2634 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2635 uint32_t ux = static_cast<uint32_t>(x);
2636 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2637 return static_cast<int32_t>(ux >> uy);
2638 }
2639
2640 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2641 uint64_t ux = static_cast<uint64_t>(x);
2642 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2643 return static_cast<int64_t>(ux >> uy);
2644 }
2645
2646 DECLARE_INSTRUCTION(UShr);
2647
2648 private:
2649 DISALLOW_COPY_AND_ASSIGN(HUShr);
2650};
2651
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002652class HAnd : public HBinaryOperation {
2653 public:
2654 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2655 : HBinaryOperation(result_type, left, right) {}
2656
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002657 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002658
2659 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2660 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2661
2662 DECLARE_INSTRUCTION(And);
2663
2664 private:
2665 DISALLOW_COPY_AND_ASSIGN(HAnd);
2666};
2667
2668class HOr : public HBinaryOperation {
2669 public:
2670 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2671 : HBinaryOperation(result_type, left, right) {}
2672
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002673 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002674
2675 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2676 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2677
2678 DECLARE_INSTRUCTION(Or);
2679
2680 private:
2681 DISALLOW_COPY_AND_ASSIGN(HOr);
2682};
2683
2684class HXor : public HBinaryOperation {
2685 public:
2686 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2687 : HBinaryOperation(result_type, left, right) {}
2688
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002689 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002690
2691 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2692 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2693
2694 DECLARE_INSTRUCTION(Xor);
2695
2696 private:
2697 DISALLOW_COPY_AND_ASSIGN(HXor);
2698};
2699
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002700// The value of a parameter in this method. Its location depends on
2701// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002702class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002703 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002704 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2705 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002706
2707 uint8_t GetIndex() const { return index_; }
2708
Calin Juravle10e244f2015-01-26 18:54:32 +00002709 bool CanBeNull() const OVERRIDE { return !is_this_; }
2710
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002711 DECLARE_INSTRUCTION(ParameterValue);
2712
2713 private:
2714 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002715 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002716 const uint8_t index_;
2717
Calin Juravle10e244f2015-01-26 18:54:32 +00002718 // Whether or not the parameter value corresponds to 'this' argument.
2719 const bool is_this_;
2720
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002721 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2722};
2723
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002724class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002725 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002726 explicit HNot(Primitive::Type result_type, HInstruction* input)
2727 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002728
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002729 bool CanBeMoved() const OVERRIDE { return true; }
2730 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002731 UNUSED(other);
2732 return true;
2733 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002734
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002735 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2736 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002737
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002738 DECLARE_INSTRUCTION(Not);
2739
2740 private:
2741 DISALLOW_COPY_AND_ASSIGN(HNot);
2742};
2743
David Brazdil66d126e2015-04-03 16:02:44 +01002744class HBooleanNot : public HUnaryOperation {
2745 public:
2746 explicit HBooleanNot(HInstruction* input)
2747 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2748
2749 bool CanBeMoved() const OVERRIDE { return true; }
2750 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2751 UNUSED(other);
2752 return true;
2753 }
2754
2755 int32_t Evaluate(int32_t x) const OVERRIDE {
2756 DCHECK(IsUint<1>(x));
2757 return !x;
2758 }
2759
2760 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2761 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2762 UNREACHABLE();
2763 }
2764
2765 DECLARE_INSTRUCTION(BooleanNot);
2766
2767 private:
2768 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2769};
2770
Roland Levillaindff1f282014-11-05 14:15:05 +00002771class HTypeConversion : public HExpression<1> {
2772 public:
2773 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002774 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2775 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002776 SetRawInputAt(0, input);
2777 DCHECK_NE(input->GetType(), result_type);
2778 }
2779
2780 HInstruction* GetInput() const { return InputAt(0); }
2781 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2782 Primitive::Type GetResultType() const { return GetType(); }
2783
Roland Levillain624279f2014-12-04 11:54:28 +00002784 // Required by the x86 and ARM code generators when producing calls
2785 // to the runtime.
2786 uint32_t GetDexPc() const { return dex_pc_; }
2787
Roland Levillaindff1f282014-11-05 14:15:05 +00002788 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002789 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002790
2791 DECLARE_INSTRUCTION(TypeConversion);
2792
2793 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002794 const uint32_t dex_pc_;
2795
Roland Levillaindff1f282014-11-05 14:15:05 +00002796 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2797};
2798
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002799static constexpr uint32_t kNoRegNumber = -1;
2800
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002801class HPhi : public HInstruction {
2802 public:
2803 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002804 : HInstruction(SideEffects::None()),
2805 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002806 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002807 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002808 is_live_(false),
2809 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002810 inputs_.SetSize(number_of_inputs);
2811 }
2812
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002813 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2814 static Primitive::Type ToPhiType(Primitive::Type type) {
2815 switch (type) {
2816 case Primitive::kPrimBoolean:
2817 case Primitive::kPrimByte:
2818 case Primitive::kPrimShort:
2819 case Primitive::kPrimChar:
2820 return Primitive::kPrimInt;
2821 default:
2822 return type;
2823 }
2824 }
2825
Calin Juravle10e244f2015-01-26 18:54:32 +00002826 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002827
2828 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002829 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002830
Calin Juravle10e244f2015-01-26 18:54:32 +00002831 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002832 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002833
Calin Juravle10e244f2015-01-26 18:54:32 +00002834 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2835 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2836
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002837 uint32_t GetRegNumber() const { return reg_number_; }
2838
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002839 void SetDead() { is_live_ = false; }
2840 void SetLive() { is_live_ = true; }
2841 bool IsDead() const { return !is_live_; }
2842 bool IsLive() const { return is_live_; }
2843
Calin Juravlea4f88312015-04-16 12:57:19 +01002844 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2845 // An equivalent phi is a phi having the same dex register and type.
2846 // It assumes that phis with the same dex register are adjacent.
2847 HPhi* GetNextEquivalentPhiWithSameType() {
2848 HInstruction* next = GetNext();
2849 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2850 if (next->GetType() == GetType()) {
2851 return next->AsPhi();
2852 }
2853 next = next->GetNext();
2854 }
2855 return nullptr;
2856 }
2857
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002858 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002859
David Brazdil1abb4192015-02-17 18:33:36 +00002860 protected:
2861 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2862
2863 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2864 inputs_.Put(index, input);
2865 }
2866
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002867 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002868 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002869 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002870 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002871 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002872 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002873
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002874 DISALLOW_COPY_AND_ASSIGN(HPhi);
2875};
2876
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002877class HNullCheck : public HExpression<1> {
2878 public:
2879 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002880 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002881 SetRawInputAt(0, value);
2882 }
2883
Calin Juravle10e244f2015-01-26 18:54:32 +00002884 bool CanBeMoved() const OVERRIDE { return true; }
2885 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002886 UNUSED(other);
2887 return true;
2888 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002889
Calin Juravle10e244f2015-01-26 18:54:32 +00002890 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002891
Calin Juravle10e244f2015-01-26 18:54:32 +00002892 bool CanThrow() const OVERRIDE { return true; }
2893
2894 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002895
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002896 uint32_t GetDexPc() const { return dex_pc_; }
2897
2898 DECLARE_INSTRUCTION(NullCheck);
2899
2900 private:
2901 const uint32_t dex_pc_;
2902
2903 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2904};
2905
2906class FieldInfo : public ValueObject {
2907 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002908 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2909 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002910
2911 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002912 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002913 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002914
2915 private:
2916 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002917 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002918 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002919};
2920
2921class HInstanceFieldGet : public HExpression<1> {
2922 public:
2923 HInstanceFieldGet(HInstruction* value,
2924 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002925 MemberOffset field_offset,
2926 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002927 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002928 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002929 SetRawInputAt(0, value);
2930 }
2931
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002932 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002933
2934 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2935 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2936 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002937 }
2938
Calin Juravle641547a2015-04-21 22:08:51 +01002939 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2940 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002941 }
2942
2943 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002944 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2945 }
2946
Calin Juravle52c48962014-12-16 17:02:57 +00002947 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002948 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002949 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002950 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002951
2952 DECLARE_INSTRUCTION(InstanceFieldGet);
2953
2954 private:
2955 const FieldInfo field_info_;
2956
2957 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2958};
2959
2960class HInstanceFieldSet : public HTemplateInstruction<2> {
2961 public:
2962 HInstanceFieldSet(HInstruction* object,
2963 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002964 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002965 MemberOffset field_offset,
2966 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002967 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002968 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002969 SetRawInputAt(0, object);
2970 SetRawInputAt(1, value);
2971 }
2972
Calin Juravle641547a2015-04-21 22:08:51 +01002973 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2974 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002975 }
2976
Calin Juravle52c48962014-12-16 17:02:57 +00002977 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002978 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002979 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002980 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002981 HInstruction* GetValue() const { return InputAt(1); }
2982
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002983 DECLARE_INSTRUCTION(InstanceFieldSet);
2984
2985 private:
2986 const FieldInfo field_info_;
2987
2988 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2989};
2990
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002991class HArrayGet : public HExpression<2> {
2992 public:
2993 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002994 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002995 SetRawInputAt(0, array);
2996 SetRawInputAt(1, index);
2997 }
2998
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002999 bool CanBeMoved() const OVERRIDE { return true; }
3000 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003001 UNUSED(other);
3002 return true;
3003 }
Calin Juravle641547a2015-04-21 22:08:51 +01003004 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3005 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003006 // TODO: We can be smarter here.
3007 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3008 // which generates the implicit null check. There are cases when these can be removed
3009 // to produce better code. If we ever add optimizations to do so we should allow an
3010 // implicit check here (as long as the address falls in the first page).
3011 return false;
3012 }
3013
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003014 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003015
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003016 HInstruction* GetArray() const { return InputAt(0); }
3017 HInstruction* GetIndex() const { return InputAt(1); }
3018
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003019 DECLARE_INSTRUCTION(ArrayGet);
3020
3021 private:
3022 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3023};
3024
3025class HArraySet : public HTemplateInstruction<3> {
3026 public:
3027 HArraySet(HInstruction* array,
3028 HInstruction* index,
3029 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003030 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003031 uint32_t dex_pc)
3032 : HTemplateInstruction(SideEffects::ChangesSomething()),
3033 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003034 expected_component_type_(expected_component_type),
3035 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003036 SetRawInputAt(0, array);
3037 SetRawInputAt(1, index);
3038 SetRawInputAt(2, value);
3039 }
3040
Calin Juravle77520bc2015-01-12 18:45:46 +00003041 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003042 // We currently always call a runtime method to catch array store
3043 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003044 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003045 }
3046
Calin Juravle641547a2015-04-21 22:08:51 +01003047 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3048 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003049 // TODO: Same as for ArrayGet.
3050 return false;
3051 }
3052
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003053 void ClearNeedsTypeCheck() {
3054 needs_type_check_ = false;
3055 }
3056
3057 bool NeedsTypeCheck() const { return needs_type_check_; }
3058
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003059 uint32_t GetDexPc() const { return dex_pc_; }
3060
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003061 HInstruction* GetArray() const { return InputAt(0); }
3062 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003063 HInstruction* GetValue() const { return InputAt(2); }
3064
3065 Primitive::Type GetComponentType() const {
3066 // The Dex format does not type floating point index operations. Since the
3067 // `expected_component_type_` is set during building and can therefore not
3068 // be correct, we also check what is the value type. If it is a floating
3069 // point type, we must use that type.
3070 Primitive::Type value_type = GetValue()->GetType();
3071 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3072 ? value_type
3073 : expected_component_type_;
3074 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003075
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003076 DECLARE_INSTRUCTION(ArraySet);
3077
3078 private:
3079 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003080 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003081 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003082
3083 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3084};
3085
3086class HArrayLength : public HExpression<1> {
3087 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003088 explicit HArrayLength(HInstruction* array)
3089 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3090 // Note that arrays do not change length, so the instruction does not
3091 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003092 SetRawInputAt(0, array);
3093 }
3094
Calin Juravle77520bc2015-01-12 18:45:46 +00003095 bool CanBeMoved() const OVERRIDE { return true; }
3096 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003097 UNUSED(other);
3098 return true;
3099 }
Calin Juravle641547a2015-04-21 22:08:51 +01003100 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3101 return obj == InputAt(0);
3102 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003103
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003104 DECLARE_INSTRUCTION(ArrayLength);
3105
3106 private:
3107 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3108};
3109
3110class HBoundsCheck : public HExpression<2> {
3111 public:
3112 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003113 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003114 DCHECK(index->GetType() == Primitive::kPrimInt);
3115 SetRawInputAt(0, index);
3116 SetRawInputAt(1, length);
3117 }
3118
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003119 bool CanBeMoved() const OVERRIDE { return true; }
3120 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003121 UNUSED(other);
3122 return true;
3123 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003124
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003125 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003126
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003127 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003128
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003129 uint32_t GetDexPc() const { return dex_pc_; }
3130
3131 DECLARE_INSTRUCTION(BoundsCheck);
3132
3133 private:
3134 const uint32_t dex_pc_;
3135
3136 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3137};
3138
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003139/**
3140 * Some DEX instructions are folded into multiple HInstructions that need
3141 * to stay live until the last HInstruction. This class
3142 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003143 * HInstruction stays live. `index` represents the stack location index of the
3144 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003145 */
3146class HTemporary : public HTemplateInstruction<0> {
3147 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003148 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003149
3150 size_t GetIndex() const { return index_; }
3151
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003152 Primitive::Type GetType() const OVERRIDE {
3153 // The previous instruction is the one that will be stored in the temporary location.
3154 DCHECK(GetPrevious() != nullptr);
3155 return GetPrevious()->GetType();
3156 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003157
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003158 DECLARE_INSTRUCTION(Temporary);
3159
3160 private:
3161 const size_t index_;
3162
3163 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3164};
3165
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003166class HSuspendCheck : public HTemplateInstruction<0> {
3167 public:
3168 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003169 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003170
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003171 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003172 return true;
3173 }
3174
3175 uint32_t GetDexPc() const { return dex_pc_; }
3176
3177 DECLARE_INSTRUCTION(SuspendCheck);
3178
3179 private:
3180 const uint32_t dex_pc_;
3181
3182 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3183};
3184
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003185/**
3186 * Instruction to load a Class object.
3187 */
3188class HLoadClass : public HExpression<0> {
3189 public:
3190 HLoadClass(uint16_t type_index,
3191 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003192 uint32_t dex_pc)
3193 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3194 type_index_(type_index),
3195 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003196 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003197 generate_clinit_check_(false),
3198 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003199
3200 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003201
3202 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3203 return other->AsLoadClass()->type_index_ == type_index_;
3204 }
3205
3206 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3207
3208 uint32_t GetDexPc() const { return dex_pc_; }
3209 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003210 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003211
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003212 bool NeedsEnvironment() const OVERRIDE {
3213 // Will call runtime and load the class if the class is not loaded yet.
3214 // TODO: finer grain decision.
3215 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003216 }
3217
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003218 bool MustGenerateClinitCheck() const {
3219 return generate_clinit_check_;
3220 }
3221
3222 void SetMustGenerateClinitCheck() {
3223 generate_clinit_check_ = true;
3224 }
3225
3226 bool CanCallRuntime() const {
3227 return MustGenerateClinitCheck() || !is_referrers_class_;
3228 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003229
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003230 bool CanThrow() const OVERRIDE {
3231 // May call runtime and and therefore can throw.
3232 // TODO: finer grain decision.
3233 return !is_referrers_class_;
3234 }
3235
Calin Juravleacf735c2015-02-12 15:25:22 +00003236 ReferenceTypeInfo GetLoadedClassRTI() {
3237 return loaded_class_rti_;
3238 }
3239
3240 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3241 // Make sure we only set exact types (the loaded class should never be merged).
3242 DCHECK(rti.IsExact());
3243 loaded_class_rti_ = rti;
3244 }
3245
3246 bool IsResolved() {
3247 return loaded_class_rti_.IsExact();
3248 }
3249
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003250 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3251
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003252 DECLARE_INSTRUCTION(LoadClass);
3253
3254 private:
3255 const uint16_t type_index_;
3256 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003257 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003258 // Whether this instruction must generate the initialization check.
3259 // Used for code generation.
3260 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003261
Calin Juravleacf735c2015-02-12 15:25:22 +00003262 ReferenceTypeInfo loaded_class_rti_;
3263
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003264 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3265};
3266
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003267class HLoadString : public HExpression<0> {
3268 public:
3269 HLoadString(uint32_t string_index, uint32_t dex_pc)
3270 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3271 string_index_(string_index),
3272 dex_pc_(dex_pc) {}
3273
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003274 bool CanBeMoved() const OVERRIDE { return true; }
3275
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003276 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3277 return other->AsLoadString()->string_index_ == string_index_;
3278 }
3279
3280 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3281
3282 uint32_t GetDexPc() const { return dex_pc_; }
3283 uint32_t GetStringIndex() const { return string_index_; }
3284
3285 // TODO: Can we deopt or debug when we resolve a string?
3286 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003287 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003288
3289 DECLARE_INSTRUCTION(LoadString);
3290
3291 private:
3292 const uint32_t string_index_;
3293 const uint32_t dex_pc_;
3294
3295 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3296};
3297
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003298/**
3299 * Performs an initialization check on its Class object input.
3300 */
3301class HClinitCheck : public HExpression<1> {
3302 public:
3303 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003304 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003305 dex_pc_(dex_pc) {
3306 SetRawInputAt(0, constant);
3307 }
3308
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003309 bool CanBeMoved() const OVERRIDE { return true; }
3310 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3311 UNUSED(other);
3312 return true;
3313 }
3314
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003315 bool NeedsEnvironment() const OVERRIDE {
3316 // May call runtime to initialize the class.
3317 return true;
3318 }
3319
3320 uint32_t GetDexPc() const { return dex_pc_; }
3321
3322 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3323
3324 DECLARE_INSTRUCTION(ClinitCheck);
3325
3326 private:
3327 const uint32_t dex_pc_;
3328
3329 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3330};
3331
3332class HStaticFieldGet : public HExpression<1> {
3333 public:
3334 HStaticFieldGet(HInstruction* cls,
3335 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003336 MemberOffset field_offset,
3337 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003338 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003339 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003340 SetRawInputAt(0, cls);
3341 }
3342
Calin Juravle52c48962014-12-16 17:02:57 +00003343
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003344 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003345
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003346 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003347 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3348 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003349 }
3350
3351 size_t ComputeHashCode() const OVERRIDE {
3352 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3353 }
3354
Calin Juravle52c48962014-12-16 17:02:57 +00003355 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003356 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3357 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003358 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003359
3360 DECLARE_INSTRUCTION(StaticFieldGet);
3361
3362 private:
3363 const FieldInfo field_info_;
3364
3365 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3366};
3367
3368class HStaticFieldSet : public HTemplateInstruction<2> {
3369 public:
3370 HStaticFieldSet(HInstruction* cls,
3371 HInstruction* value,
3372 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003373 MemberOffset field_offset,
3374 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003375 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003376 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003377 SetRawInputAt(0, cls);
3378 SetRawInputAt(1, value);
3379 }
3380
Calin Juravle52c48962014-12-16 17:02:57 +00003381 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003382 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3383 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003384 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003385
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003386 HInstruction* GetValue() const { return InputAt(1); }
3387
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003388 DECLARE_INSTRUCTION(StaticFieldSet);
3389
3390 private:
3391 const FieldInfo field_info_;
3392
3393 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3394};
3395
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003396// Implement the move-exception DEX instruction.
3397class HLoadException : public HExpression<0> {
3398 public:
3399 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3400
3401 DECLARE_INSTRUCTION(LoadException);
3402
3403 private:
3404 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3405};
3406
3407class HThrow : public HTemplateInstruction<1> {
3408 public:
3409 HThrow(HInstruction* exception, uint32_t dex_pc)
3410 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3411 SetRawInputAt(0, exception);
3412 }
3413
3414 bool IsControlFlow() const OVERRIDE { return true; }
3415
3416 bool NeedsEnvironment() const OVERRIDE { return true; }
3417
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003418 bool CanThrow() const OVERRIDE { return true; }
3419
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003420 uint32_t GetDexPc() const { return dex_pc_; }
3421
3422 DECLARE_INSTRUCTION(Throw);
3423
3424 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003425 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003426
3427 DISALLOW_COPY_AND_ASSIGN(HThrow);
3428};
3429
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003430class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003431 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003432 HInstanceOf(HInstruction* object,
3433 HLoadClass* constant,
3434 bool class_is_final,
3435 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003436 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3437 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003438 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003439 dex_pc_(dex_pc) {
3440 SetRawInputAt(0, object);
3441 SetRawInputAt(1, constant);
3442 }
3443
3444 bool CanBeMoved() const OVERRIDE { return true; }
3445
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003446 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003447 return true;
3448 }
3449
3450 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003451 return false;
3452 }
3453
3454 uint32_t GetDexPc() const { return dex_pc_; }
3455
3456 bool IsClassFinal() const { return class_is_final_; }
3457
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003458 // Used only in code generation.
3459 bool MustDoNullCheck() const { return must_do_null_check_; }
3460 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3461
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003462 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003463
3464 private:
3465 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003466 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003467 const uint32_t dex_pc_;
3468
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003469 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3470};
3471
Calin Juravleb1498f62015-02-16 13:13:29 +00003472class HBoundType : public HExpression<1> {
3473 public:
3474 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3475 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3476 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003477 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003478 SetRawInputAt(0, input);
3479 }
3480
3481 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3482
3483 bool CanBeNull() const OVERRIDE {
3484 // `null instanceof ClassX` always return false so we can't be null.
3485 return false;
3486 }
3487
3488 DECLARE_INSTRUCTION(BoundType);
3489
3490 private:
3491 // Encodes the most upper class that this instruction can have. In other words
3492 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3493 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3494 const ReferenceTypeInfo bound_type_;
3495
3496 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3497};
3498
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003499class HCheckCast : public HTemplateInstruction<2> {
3500 public:
3501 HCheckCast(HInstruction* object,
3502 HLoadClass* constant,
3503 bool class_is_final,
3504 uint32_t dex_pc)
3505 : HTemplateInstruction(SideEffects::None()),
3506 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003507 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003508 dex_pc_(dex_pc) {
3509 SetRawInputAt(0, object);
3510 SetRawInputAt(1, constant);
3511 }
3512
3513 bool CanBeMoved() const OVERRIDE { return true; }
3514
3515 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3516 return true;
3517 }
3518
3519 bool NeedsEnvironment() const OVERRIDE {
3520 // Instruction may throw a CheckCastError.
3521 return true;
3522 }
3523
3524 bool CanThrow() const OVERRIDE { return true; }
3525
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003526 bool MustDoNullCheck() const { return must_do_null_check_; }
3527 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3528
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003529 uint32_t GetDexPc() const { return dex_pc_; }
3530
3531 bool IsClassFinal() const { return class_is_final_; }
3532
3533 DECLARE_INSTRUCTION(CheckCast);
3534
3535 private:
3536 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003537 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003538 const uint32_t dex_pc_;
3539
3540 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003541};
3542
Calin Juravle27df7582015-04-17 19:12:31 +01003543class HMemoryBarrier : public HTemplateInstruction<0> {
3544 public:
3545 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3546 : HTemplateInstruction(SideEffects::None()),
3547 barrier_kind_(barrier_kind) {}
3548
3549 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3550
3551 DECLARE_INSTRUCTION(MemoryBarrier);
3552
3553 private:
3554 const MemBarrierKind barrier_kind_;
3555
3556 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3557};
3558
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003559class HMonitorOperation : public HTemplateInstruction<1> {
3560 public:
3561 enum OperationKind {
3562 kEnter,
3563 kExit,
3564 };
3565
3566 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3567 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3568 SetRawInputAt(0, object);
3569 }
3570
3571 // Instruction may throw a Java exception, so we need an environment.
3572 bool NeedsEnvironment() const OVERRIDE { return true; }
3573 bool CanThrow() const OVERRIDE { return true; }
3574
3575 uint32_t GetDexPc() const { return dex_pc_; }
3576
3577 bool IsEnter() const { return kind_ == kEnter; }
3578
3579 DECLARE_INSTRUCTION(MonitorOperation);
3580
Calin Juravle52c48962014-12-16 17:02:57 +00003581 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003582 const OperationKind kind_;
3583 const uint32_t dex_pc_;
3584
3585 private:
3586 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3587};
3588
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003589class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003590 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003591 MoveOperands(Location source,
3592 Location destination,
3593 Primitive::Type type,
3594 HInstruction* instruction)
3595 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003596
3597 Location GetSource() const { return source_; }
3598 Location GetDestination() const { return destination_; }
3599
3600 void SetSource(Location value) { source_ = value; }
3601 void SetDestination(Location value) { destination_ = value; }
3602
3603 // The parallel move resolver marks moves as "in-progress" by clearing the
3604 // destination (but not the source).
3605 Location MarkPending() {
3606 DCHECK(!IsPending());
3607 Location dest = destination_;
3608 destination_ = Location::NoLocation();
3609 return dest;
3610 }
3611
3612 void ClearPending(Location dest) {
3613 DCHECK(IsPending());
3614 destination_ = dest;
3615 }
3616
3617 bool IsPending() const {
3618 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3619 return destination_.IsInvalid() && !source_.IsInvalid();
3620 }
3621
3622 // True if this blocks a move from the given location.
3623 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003624 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003625 }
3626
3627 // A move is redundant if it's been eliminated, if its source and
3628 // destination are the same, or if its destination is unneeded.
3629 bool IsRedundant() const {
3630 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3631 }
3632
3633 // We clear both operands to indicate move that's been eliminated.
3634 void Eliminate() {
3635 source_ = destination_ = Location::NoLocation();
3636 }
3637
3638 bool IsEliminated() const {
3639 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3640 return source_.IsInvalid();
3641 }
3642
Nicolas Geoffray90218252015-04-15 11:56:51 +01003643 bool Is64BitMove() const {
3644 return Primitive::Is64BitType(type_);
3645 }
3646
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003647 HInstruction* GetInstruction() const { return instruction_; }
3648
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003649 private:
3650 Location source_;
3651 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003652 // The type this move is for.
3653 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003654 // The instruction this move is assocatied with. Null when this move is
3655 // for moving an input in the expected locations of user (including a phi user).
3656 // This is only used in debug mode, to ensure we do not connect interval siblings
3657 // in the same parallel move.
3658 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003659};
3660
3661static constexpr size_t kDefaultNumberOfMoves = 4;
3662
3663class HParallelMove : public HTemplateInstruction<0> {
3664 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003665 explicit HParallelMove(ArenaAllocator* arena)
3666 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003667
Nicolas Geoffray90218252015-04-15 11:56:51 +01003668 void AddMove(Location source,
3669 Location destination,
3670 Primitive::Type type,
3671 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003672 DCHECK(source.IsValid());
3673 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003674 if (kIsDebugBuild) {
3675 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003676 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003677 if (moves_.Get(i).GetInstruction() == instruction) {
3678 // Special case the situation where the move is for the spill slot
3679 // of the instruction.
3680 if ((GetPrevious() == instruction)
3681 || ((GetPrevious() == nullptr)
3682 && instruction->IsPhi()
3683 && instruction->GetBlock() == GetBlock())) {
3684 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3685 << "Doing parallel moves for the same instruction.";
3686 } else {
3687 DCHECK(false) << "Doing parallel moves for the same instruction.";
3688 }
3689 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003690 }
3691 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003692 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003693 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3694 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003695 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003696 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003697 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003698 }
3699
3700 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003701 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003702 }
3703
3704 size_t NumMoves() const { return moves_.Size(); }
3705
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003706 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003707
3708 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003709 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003710
3711 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3712};
3713
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003714class HGraphVisitor : public ValueObject {
3715 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003716 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3717 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003718
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003719 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003720 virtual void VisitBasicBlock(HBasicBlock* block);
3721
Roland Levillain633021e2014-10-01 14:12:25 +01003722 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003723 void VisitInsertionOrder();
3724
Roland Levillain633021e2014-10-01 14:12:25 +01003725 // Visit the graph following dominator tree reverse post-order.
3726 void VisitReversePostOrder();
3727
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003728 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003729
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003730 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003731#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003732 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3733
3734 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3735
3736#undef DECLARE_VISIT_INSTRUCTION
3737
3738 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003739 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003740
3741 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3742};
3743
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003744class HGraphDelegateVisitor : public HGraphVisitor {
3745 public:
3746 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3747 virtual ~HGraphDelegateVisitor() {}
3748
3749 // Visit functions that delegate to to super class.
3750#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003751 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003752
3753 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3754
3755#undef DECLARE_VISIT_INSTRUCTION
3756
3757 private:
3758 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3759};
3760
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003761class HInsertionOrderIterator : public ValueObject {
3762 public:
3763 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3764
3765 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3766 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3767 void Advance() { ++index_; }
3768
3769 private:
3770 const HGraph& graph_;
3771 size_t index_;
3772
3773 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3774};
3775
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003776class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003777 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003778 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3779 // Check that reverse post order of the graph has been built.
3780 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3781 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003782
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003783 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3784 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003785 void Advance() { ++index_; }
3786
3787 private:
3788 const HGraph& graph_;
3789 size_t index_;
3790
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003791 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003792};
3793
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003794class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003795 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003796 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003797 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3798 // Check that reverse post order of the graph has been built.
3799 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3800 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003801
3802 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003803 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003804 void Advance() { --index_; }
3805
3806 private:
3807 const HGraph& graph_;
3808 size_t index_;
3809
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003810 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003811};
3812
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003813class HLinearPostOrderIterator : public ValueObject {
3814 public:
3815 explicit HLinearPostOrderIterator(const HGraph& graph)
3816 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3817
3818 bool Done() const { return index_ == 0; }
3819
3820 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3821
3822 void Advance() {
3823 --index_;
3824 DCHECK_GE(index_, 0U);
3825 }
3826
3827 private:
3828 const GrowableArray<HBasicBlock*>& order_;
3829 size_t index_;
3830
3831 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3832};
3833
3834class HLinearOrderIterator : public ValueObject {
3835 public:
3836 explicit HLinearOrderIterator(const HGraph& graph)
3837 : order_(graph.GetLinearOrder()), index_(0) {}
3838
3839 bool Done() const { return index_ == order_.Size(); }
3840 HBasicBlock* Current() const { return order_.Get(index_); }
3841 void Advance() { ++index_; }
3842
3843 private:
3844 const GrowableArray<HBasicBlock*>& order_;
3845 size_t index_;
3846
3847 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3848};
3849
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003850// Iterator over the blocks that art part of the loop. Includes blocks part
3851// of an inner loop. The order in which the blocks are iterated is on their
3852// block id.
3853class HBlocksInLoopIterator : public ValueObject {
3854 public:
3855 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3856 : blocks_in_loop_(info.GetBlocks()),
3857 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3858 index_(0) {
3859 if (!blocks_in_loop_.IsBitSet(index_)) {
3860 Advance();
3861 }
3862 }
3863
3864 bool Done() const { return index_ == blocks_.Size(); }
3865 HBasicBlock* Current() const { return blocks_.Get(index_); }
3866 void Advance() {
3867 ++index_;
3868 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3869 if (blocks_in_loop_.IsBitSet(index_)) {
3870 break;
3871 }
3872 }
3873 }
3874
3875 private:
3876 const BitVector& blocks_in_loop_;
3877 const GrowableArray<HBasicBlock*>& blocks_;
3878 size_t index_;
3879
3880 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3881};
3882
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003883inline int64_t Int64FromConstant(HConstant* constant) {
3884 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3885 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3886 : constant->AsLongConstant()->GetValue();
3887}
3888
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003889} // namespace art
3890
3891#endif // ART_COMPILER_OPTIMIZING_NODES_H_