blob: 6b9d72ddf63c7add6814cfd6f2ac34e38f3f9146 [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),
Mingyao Yange4335eb2015-03-02 15:14:13 -0800130 has_array_accesses_(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
Mingyao Yange4335eb2015-03-02 15:14:13 -0800233 bool HasArrayAccesses() const {
234 return has_array_accesses_;
235 }
236
237 void SetHasArrayAccesses(bool value) {
238 has_array_accesses_ = value;
239 }
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
245 // are currently supported.
246 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
Mingyao Yange4335eb2015-03-02 15:14:13 -0800298 // Has array accesses. We can totally skip BCE if it's false.
299 bool has_array_accesses_;
300
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);
1010
1011 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001012 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001013 }
1014
1015 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001016 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001017 }
1018
David Brazdil1abb4192015-02-17 18:33:36 +00001019 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001020
1021 size_t Size() const { return vregs_.Size(); }
1022
1023 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001024 // Record instructions' use entries of this environment for constant-time removal.
1025 // It should only be called by HInstruction when a new environment use is added.
1026 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1027 DCHECK(env_use->GetUser() == this);
1028 size_t index = env_use->GetIndex();
1029 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1030 }
David Brazdiled596192015-01-23 10:39:45 +00001031
David Brazdil1abb4192015-02-17 18:33:36 +00001032 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001033
David Brazdil1abb4192015-02-17 18:33:36 +00001034 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001035
1036 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1037};
1038
Calin Juravleacf735c2015-02-12 15:25:22 +00001039class ReferenceTypeInfo : ValueObject {
1040 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001041 typedef Handle<mirror::Class> TypeHandle;
1042
1043 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1044 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1045 if (type_handle->IsObjectClass()) {
1046 // Override the type handle to be consistent with the case when we get to
1047 // Top but don't have the Object class available. It avoids having to guess
1048 // what value the type_handle has when it's Top.
1049 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1050 } else {
1051 return ReferenceTypeInfo(type_handle, is_exact, false);
1052 }
1053 }
1054
1055 static ReferenceTypeInfo CreateTop(bool is_exact) {
1056 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001057 }
1058
1059 bool IsExact() const { return is_exact_; }
1060 bool IsTop() const { return is_top_; }
1061
1062 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1063
Calin Juravleb1498f62015-02-16 13:13:29 +00001064 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001065 if (IsTop()) {
1066 // Top (equivalent for java.lang.Object) is supertype of anything.
1067 return true;
1068 }
1069 if (rti.IsTop()) {
1070 // If we get here `this` is not Top() so it can't be a supertype.
1071 return false;
1072 }
1073 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1074 }
1075
1076 // Returns true if the type information provide the same amount of details.
1077 // Note that it does not mean that the instructions have the same actual type
1078 // (e.g. tops are equal but they can be the result of a merge).
1079 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1080 if (IsExact() != rti.IsExact()) {
1081 return false;
1082 }
1083 if (IsTop() && rti.IsTop()) {
1084 // `Top` means java.lang.Object, so the types are equivalent.
1085 return true;
1086 }
1087 if (IsTop() || rti.IsTop()) {
1088 // If only one is top or object than they are not equivalent.
1089 // NB: We need this extra check because the type_handle of `Top` is invalid
1090 // and we cannot inspect its reference.
1091 return false;
1092 }
1093
1094 // Finally check the types.
1095 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1096 }
1097
1098 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001099 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1100 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1101 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1102
Calin Juravleacf735c2015-02-12 15:25:22 +00001103 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001104 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001105 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001106 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001107 bool is_exact_;
1108 // A true value here means that the object type should be java.lang.Object.
1109 // We don't have access to the corresponding mirror object every time so this
1110 // flag acts as a substitute. When true, the TypeHandle refers to a null
1111 // pointer and should not be used.
1112 bool is_top_;
1113};
1114
1115std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1116
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001117class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001118 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001119 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001120 : previous_(nullptr),
1121 next_(nullptr),
1122 block_(nullptr),
1123 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001124 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001125 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001126 locations_(nullptr),
1127 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001128 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001129 side_effects_(side_effects),
1130 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001131
Dave Allison20dfc792014-06-16 20:44:29 -07001132 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001133
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001134#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001135 enum InstructionKind {
1136 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1137 };
1138#undef DECLARE_KIND
1139
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001140 HInstruction* GetNext() const { return next_; }
1141 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001142
Calin Juravle77520bc2015-01-12 18:45:46 +00001143 HInstruction* GetNextDisregardingMoves() const;
1144 HInstruction* GetPreviousDisregardingMoves() const;
1145
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001146 HBasicBlock* GetBlock() const { return block_; }
1147 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001148 bool IsInBlock() const { return block_ != nullptr; }
1149 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001150 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001151
Roland Levillain6b879dd2014-09-22 17:13:44 +01001152 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001153 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001154
1155 virtual void Accept(HGraphVisitor* visitor) = 0;
1156 virtual const char* DebugName() const = 0;
1157
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001158 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001159 void SetRawInputAt(size_t index, HInstruction* input) {
1160 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1161 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001162
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001163 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001164 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001165 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001166 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001167
Calin Juravle10e244f2015-01-26 18:54:32 +00001168 // Does not apply for all instructions, but having this at top level greatly
1169 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001170 virtual bool CanBeNull() const {
1171 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1172 return true;
1173 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001174
Calin Juravle641547a2015-04-21 22:08:51 +01001175 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1176 UNUSED(obj);
1177 return false;
1178 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001179
Calin Juravleacf735c2015-02-12 15:25:22 +00001180 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001181 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001182 reference_type_info_ = reference_type_info;
1183 }
1184
Calin Juravle61d544b2015-02-23 16:46:57 +00001185 ReferenceTypeInfo GetReferenceTypeInfo() const {
1186 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1187 return reference_type_info_;
1188 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001189
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001190 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001191 DCHECK(user != nullptr);
1192 HUseListNode<HInstruction*>* use =
1193 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1194 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001195 }
1196
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001197 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001198 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001199 HUseListNode<HEnvironment*>* env_use =
1200 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1201 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001202 }
1203
David Brazdil1abb4192015-02-17 18:33:36 +00001204 void RemoveAsUserOfInput(size_t input) {
1205 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1206 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1207 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001208
David Brazdil1abb4192015-02-17 18:33:36 +00001209 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1210 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001211
David Brazdiled596192015-01-23 10:39:45 +00001212 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1213 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001214 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001215 bool HasOnlyOneNonEnvironmentUse() const {
1216 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1217 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001218
Roland Levillain6c82d402014-10-13 16:10:27 +01001219 // Does this instruction strictly dominate `other_instruction`?
1220 // Returns false if this instruction and `other_instruction` are the same.
1221 // Aborts if this instruction and `other_instruction` are both phis.
1222 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001223
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001224 int GetId() const { return id_; }
1225 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001226
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001227 int GetSsaIndex() const { return ssa_index_; }
1228 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1229 bool HasSsaIndex() const { return ssa_index_ != -1; }
1230
1231 bool HasEnvironment() const { return environment_ != nullptr; }
1232 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001233 // Set the `environment_` field. Raw because this method does not
1234 // update the uses lists.
1235 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1236
1237 // Set the environment of this instruction, copying it from `environment`. While
1238 // copying, the uses lists are being updated.
1239 void CopyEnvironmentFrom(HEnvironment* environment) {
1240 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1241 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1242 environment_->CopyFrom(environment);
1243 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001244
Nicolas Geoffray39468442014-09-02 15:17:15 +01001245 // Returns the number of entries in the environment. Typically, that is the
1246 // number of dex registers in a method. It could be more in case of inlining.
1247 size_t EnvironmentSize() const;
1248
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001249 LocationSummary* GetLocations() const { return locations_; }
1250 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001251
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001252 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001253 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001254
Alexandre Rames188d4312015-04-09 18:30:21 +01001255 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1256 // uses of this instruction by `other` are *not* updated.
1257 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1258 ReplaceWith(other);
1259 other->ReplaceInput(this, use_index);
1260 }
1261
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001262 // Move `this` instruction before `cursor`.
1263 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001264
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001265#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001266 bool Is##type() const { return (As##type() != nullptr); } \
1267 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001268 virtual H##type* As##type() { return nullptr; }
1269
1270 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1271#undef INSTRUCTION_TYPE_CHECK
1272
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001273 // Returns whether the instruction can be moved within the graph.
1274 virtual bool CanBeMoved() const { return false; }
1275
1276 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001277 virtual bool InstructionTypeEquals(HInstruction* other) const {
1278 UNUSED(other);
1279 return false;
1280 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001281
1282 // Returns whether any data encoded in the two instructions is equal.
1283 // This method does not look at the inputs. Both instructions must be
1284 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001285 virtual bool InstructionDataEquals(HInstruction* other) const {
1286 UNUSED(other);
1287 return false;
1288 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001289
1290 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001291 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001292 // 2) Their inputs are identical.
1293 bool Equals(HInstruction* other) const;
1294
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001295 virtual InstructionKind GetKind() const = 0;
1296
1297 virtual size_t ComputeHashCode() const {
1298 size_t result = GetKind();
1299 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1300 result = (result * 31) + InputAt(i)->GetId();
1301 }
1302 return result;
1303 }
1304
1305 SideEffects GetSideEffects() const { return side_effects_; }
1306
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001307 size_t GetLifetimePosition() const { return lifetime_position_; }
1308 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1309 LiveInterval* GetLiveInterval() const { return live_interval_; }
1310 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1311 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1312
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001313 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1314
1315 // Returns whether the code generation of the instruction will require to have access
1316 // to the current method. Such instructions are:
1317 // (1): Instructions that require an environment, as calling the runtime requires
1318 // to walk the stack and have the current method stored at a specific stack address.
1319 // (2): Object literals like classes and strings, that are loaded from the dex cache
1320 // fields of the current method.
1321 bool NeedsCurrentMethod() const {
1322 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1323 }
1324
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001325 virtual bool NeedsDexCache() const { return false; }
1326
David Brazdil1abb4192015-02-17 18:33:36 +00001327 protected:
1328 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1329 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1330
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001331 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001332 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1333
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001334 HInstruction* previous_;
1335 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001336 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001337
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001338 // An instruction gets an id when it is added to the graph.
1339 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001340 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001341 int id_;
1342
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001343 // When doing liveness analysis, instructions that have uses get an SSA index.
1344 int ssa_index_;
1345
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001346 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001347 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001348
1349 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001350 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001351
Nicolas Geoffray39468442014-09-02 15:17:15 +01001352 // The environment associated with this instruction. Not null if the instruction
1353 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001354 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001355
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001356 // Set by the code generator.
1357 LocationSummary* locations_;
1358
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001359 // Set by the liveness analysis.
1360 LiveInterval* live_interval_;
1361
1362 // Set by the liveness analysis, this is the position in a linear
1363 // order of blocks where this instruction's live interval start.
1364 size_t lifetime_position_;
1365
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001366 const SideEffects side_effects_;
1367
Calin Juravleacf735c2015-02-12 15:25:22 +00001368 // TODO: for primitive types this should be marked as invalid.
1369 ReferenceTypeInfo reference_type_info_;
1370
David Brazdil1abb4192015-02-17 18:33:36 +00001371 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001372 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001373 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001374 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001375 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001376
1377 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1378};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001379std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001380
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001381class HInputIterator : public ValueObject {
1382 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001383 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001384
1385 bool Done() const { return index_ == instruction_->InputCount(); }
1386 HInstruction* Current() const { return instruction_->InputAt(index_); }
1387 void Advance() { index_++; }
1388
1389 private:
1390 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001391 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001392
1393 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1394};
1395
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001396class HInstructionIterator : public ValueObject {
1397 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001398 explicit HInstructionIterator(const HInstructionList& instructions)
1399 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001400 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001401 }
1402
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001403 bool Done() const { return instruction_ == nullptr; }
1404 HInstruction* Current() const { return instruction_; }
1405 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001406 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001407 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001408 }
1409
1410 private:
1411 HInstruction* instruction_;
1412 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001413
1414 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001415};
1416
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001417class HBackwardInstructionIterator : public ValueObject {
1418 public:
1419 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1420 : instruction_(instructions.last_instruction_) {
1421 next_ = Done() ? nullptr : instruction_->GetPrevious();
1422 }
1423
1424 bool Done() const { return instruction_ == nullptr; }
1425 HInstruction* Current() const { return instruction_; }
1426 void Advance() {
1427 instruction_ = next_;
1428 next_ = Done() ? nullptr : instruction_->GetPrevious();
1429 }
1430
1431 private:
1432 HInstruction* instruction_;
1433 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001434
1435 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001436};
1437
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001438// An embedded container with N elements of type T. Used (with partial
1439// specialization for N=0) because embedded arrays cannot have size 0.
1440template<typename T, intptr_t N>
1441class EmbeddedArray {
1442 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001443 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001444
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001445 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001446
1447 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001448 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001449 return elements_[i];
1450 }
1451
1452 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001453 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001454 return elements_[i];
1455 }
1456
1457 const T& At(intptr_t i) const {
1458 return (*this)[i];
1459 }
1460
1461 void SetAt(intptr_t i, const T& val) {
1462 (*this)[i] = val;
1463 }
1464
1465 private:
1466 T elements_[N];
1467};
1468
1469template<typename T>
1470class EmbeddedArray<T, 0> {
1471 public:
1472 intptr_t length() const { return 0; }
1473 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001474 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001475 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001476 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001477 }
1478 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001479 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001480 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001481 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001482 }
1483};
1484
1485template<intptr_t N>
1486class HTemplateInstruction: public HInstruction {
1487 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001488 HTemplateInstruction<N>(SideEffects side_effects)
1489 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001490 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001491
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001492 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001493
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001494 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001495 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1496
1497 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1498 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001499 }
1500
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001501 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001502 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001503
1504 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001505};
1506
Dave Allison20dfc792014-06-16 20:44:29 -07001507template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001508class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001509 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001510 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1511 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001512 virtual ~HExpression() {}
1513
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001514 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001515
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001516 protected:
1517 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001518};
1519
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001520// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1521// instruction that branches to the exit block.
1522class HReturnVoid : public HTemplateInstruction<0> {
1523 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001524 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001525
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001526 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001527
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001528 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001529
1530 private:
1531 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1532};
1533
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001534// Represents dex's RETURN opcodes. A HReturn is a control flow
1535// instruction that branches to the exit block.
1536class HReturn : public HTemplateInstruction<1> {
1537 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001538 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001539 SetRawInputAt(0, value);
1540 }
1541
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001542 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001543
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001544 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001545
1546 private:
1547 DISALLOW_COPY_AND_ASSIGN(HReturn);
1548};
1549
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001550// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001551// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001552// exit block.
1553class HExit : public HTemplateInstruction<0> {
1554 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001555 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001556
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001557 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001558
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001559 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001560
1561 private:
1562 DISALLOW_COPY_AND_ASSIGN(HExit);
1563};
1564
1565// Jumps from one block to another.
1566class HGoto : public HTemplateInstruction<0> {
1567 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001568 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1569
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001570 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001571
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001572 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001573 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001574 }
1575
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001576 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001577
1578 private:
1579 DISALLOW_COPY_AND_ASSIGN(HGoto);
1580};
1581
Dave Allison20dfc792014-06-16 20:44:29 -07001582
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001583// Conditional branch. A block ending with an HIf instruction must have
1584// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001585class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001586 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001587 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001588 SetRawInputAt(0, input);
1589 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001590
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001591 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001592
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001593 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001594 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001595 }
1596
1597 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001598 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001599 }
1600
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001601 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001602
1603 private:
1604 DISALLOW_COPY_AND_ASSIGN(HIf);
1605};
1606
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001607// Deoptimize to interpreter, upon checking a condition.
1608class HDeoptimize : public HTemplateInstruction<1> {
1609 public:
1610 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1611 : HTemplateInstruction(SideEffects::None()),
1612 dex_pc_(dex_pc) {
1613 SetRawInputAt(0, cond);
1614 }
1615
1616 bool NeedsEnvironment() const OVERRIDE { return true; }
1617 bool CanThrow() const OVERRIDE { return true; }
1618 uint32_t GetDexPc() const { return dex_pc_; }
1619
1620 DECLARE_INSTRUCTION(Deoptimize);
1621
1622 private:
1623 uint32_t dex_pc_;
1624
1625 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1626};
1627
Roland Levillain88cb1752014-10-20 16:36:47 +01001628class HUnaryOperation : public HExpression<1> {
1629 public:
1630 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1631 : HExpression(result_type, SideEffects::None()) {
1632 SetRawInputAt(0, input);
1633 }
1634
1635 HInstruction* GetInput() const { return InputAt(0); }
1636 Primitive::Type GetResultType() const { return GetType(); }
1637
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001638 bool CanBeMoved() const OVERRIDE { return true; }
1639 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001640 UNUSED(other);
1641 return true;
1642 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001643
Roland Levillain9240d6a2014-10-20 16:47:04 +01001644 // Try to statically evaluate `operation` and return a HConstant
1645 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001646 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001647 HConstant* TryStaticEvaluation() const;
1648
1649 // Apply this operation to `x`.
1650 virtual int32_t Evaluate(int32_t x) const = 0;
1651 virtual int64_t Evaluate(int64_t x) const = 0;
1652
Roland Levillain88cb1752014-10-20 16:36:47 +01001653 DECLARE_INSTRUCTION(UnaryOperation);
1654
1655 private:
1656 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1657};
1658
Dave Allison20dfc792014-06-16 20:44:29 -07001659class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001660 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001661 HBinaryOperation(Primitive::Type result_type,
1662 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001663 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001664 SetRawInputAt(0, left);
1665 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001666 }
1667
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001668 HInstruction* GetLeft() const { return InputAt(0); }
1669 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001670 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001671
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001672 virtual bool IsCommutative() const { return false; }
1673
1674 // Put constant on the right.
1675 // Returns whether order is changed.
1676 bool OrderInputsWithConstantOnTheRight() {
1677 HInstruction* left = InputAt(0);
1678 HInstruction* right = InputAt(1);
1679 if (left->IsConstant() && !right->IsConstant()) {
1680 ReplaceInput(right, 0);
1681 ReplaceInput(left, 1);
1682 return true;
1683 }
1684 return false;
1685 }
1686
1687 // Order inputs by instruction id, but favor constant on the right side.
1688 // This helps GVN for commutative ops.
1689 void OrderInputs() {
1690 DCHECK(IsCommutative());
1691 HInstruction* left = InputAt(0);
1692 HInstruction* right = InputAt(1);
1693 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1694 return;
1695 }
1696 if (OrderInputsWithConstantOnTheRight()) {
1697 return;
1698 }
1699 // Order according to instruction id.
1700 if (left->GetId() > right->GetId()) {
1701 ReplaceInput(right, 0);
1702 ReplaceInput(left, 1);
1703 }
1704 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001705
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001706 bool CanBeMoved() const OVERRIDE { return true; }
1707 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001708 UNUSED(other);
1709 return true;
1710 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001711
Roland Levillain9240d6a2014-10-20 16:47:04 +01001712 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001713 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001714 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001715 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001716
1717 // Apply this operation to `x` and `y`.
1718 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1719 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1720
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001721 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001722 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001723 HConstant* GetConstantRight() const;
1724
1725 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001726 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001727 HInstruction* GetLeastConstantLeft() const;
1728
Roland Levillainccc07a92014-09-16 14:48:16 +01001729 DECLARE_INSTRUCTION(BinaryOperation);
1730
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001731 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001732 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1733};
1734
Dave Allison20dfc792014-06-16 20:44:29 -07001735class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001736 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001737 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001738 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1739 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001740
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001741 bool NeedsMaterialization() const { return needs_materialization_; }
1742 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001743
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001744 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001745 // `instruction`, and disregard moves in between.
1746 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001747
Dave Allison20dfc792014-06-16 20:44:29 -07001748 DECLARE_INSTRUCTION(Condition);
1749
1750 virtual IfCondition GetCondition() const = 0;
1751
1752 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001753 // For register allocation purposes, returns whether this instruction needs to be
1754 // materialized (that is, not just be in the processor flags).
1755 bool needs_materialization_;
1756
Dave Allison20dfc792014-06-16 20:44:29 -07001757 DISALLOW_COPY_AND_ASSIGN(HCondition);
1758};
1759
1760// Instruction to check if two inputs are equal to each other.
1761class HEqual : public HCondition {
1762 public:
1763 HEqual(HInstruction* first, HInstruction* second)
1764 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001765
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001766 bool IsCommutative() const OVERRIDE { return true; }
1767
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001768 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001769 return x == y ? 1 : 0;
1770 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001771 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001772 return x == y ? 1 : 0;
1773 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001774
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001775 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001776
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001777 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001778 return kCondEQ;
1779 }
1780
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001781 private:
1782 DISALLOW_COPY_AND_ASSIGN(HEqual);
1783};
1784
Dave Allison20dfc792014-06-16 20:44:29 -07001785class HNotEqual : public HCondition {
1786 public:
1787 HNotEqual(HInstruction* first, HInstruction* second)
1788 : HCondition(first, second) {}
1789
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001790 bool IsCommutative() const OVERRIDE { return true; }
1791
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001792 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001793 return x != y ? 1 : 0;
1794 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001795 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001796 return x != y ? 1 : 0;
1797 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001798
Dave Allison20dfc792014-06-16 20:44:29 -07001799 DECLARE_INSTRUCTION(NotEqual);
1800
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001801 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001802 return kCondNE;
1803 }
1804
1805 private:
1806 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1807};
1808
1809class HLessThan : public HCondition {
1810 public:
1811 HLessThan(HInstruction* first, HInstruction* second)
1812 : HCondition(first, second) {}
1813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001814 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001815 return x < y ? 1 : 0;
1816 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001817 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001818 return x < y ? 1 : 0;
1819 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001820
Dave Allison20dfc792014-06-16 20:44:29 -07001821 DECLARE_INSTRUCTION(LessThan);
1822
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001823 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001824 return kCondLT;
1825 }
1826
1827 private:
1828 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1829};
1830
1831class HLessThanOrEqual : public HCondition {
1832 public:
1833 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1834 : HCondition(first, second) {}
1835
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001836 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001837 return x <= y ? 1 : 0;
1838 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001839 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001840 return x <= y ? 1 : 0;
1841 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001842
Dave Allison20dfc792014-06-16 20:44:29 -07001843 DECLARE_INSTRUCTION(LessThanOrEqual);
1844
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001845 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001846 return kCondLE;
1847 }
1848
1849 private:
1850 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1851};
1852
1853class HGreaterThan : public HCondition {
1854 public:
1855 HGreaterThan(HInstruction* first, HInstruction* second)
1856 : HCondition(first, second) {}
1857
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001858 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001859 return x > y ? 1 : 0;
1860 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001861 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001862 return x > y ? 1 : 0;
1863 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001864
Dave Allison20dfc792014-06-16 20:44:29 -07001865 DECLARE_INSTRUCTION(GreaterThan);
1866
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001867 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001868 return kCondGT;
1869 }
1870
1871 private:
1872 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1873};
1874
1875class HGreaterThanOrEqual : public HCondition {
1876 public:
1877 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1878 : HCondition(first, second) {}
1879
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001880 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001881 return x >= y ? 1 : 0;
1882 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001883 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001884 return x >= y ? 1 : 0;
1885 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001886
Dave Allison20dfc792014-06-16 20:44:29 -07001887 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1888
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001889 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001890 return kCondGE;
1891 }
1892
1893 private:
1894 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1895};
1896
1897
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001898// Instruction to check how two inputs compare to each other.
1899// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1900class HCompare : public HBinaryOperation {
1901 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001902 // The bias applies for floating point operations and indicates how NaN
1903 // comparisons are treated:
1904 enum Bias {
1905 kNoBias, // bias is not applicable (i.e. for long operation)
1906 kGtBias, // return 1 for NaN comparisons
1907 kLtBias, // return -1 for NaN comparisons
1908 };
1909
1910 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1911 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001912 DCHECK_EQ(type, first->GetType());
1913 DCHECK_EQ(type, second->GetType());
1914 }
1915
Calin Juravleddb7df22014-11-25 20:56:51 +00001916 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001917 return
1918 x == y ? 0 :
1919 x > y ? 1 :
1920 -1;
1921 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001922
1923 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001924 return
1925 x == y ? 0 :
1926 x > y ? 1 :
1927 -1;
1928 }
1929
Calin Juravleddb7df22014-11-25 20:56:51 +00001930 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1931 return bias_ == other->AsCompare()->bias_;
1932 }
1933
1934 bool IsGtBias() { return bias_ == kGtBias; }
1935
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001936 DECLARE_INSTRUCTION(Compare);
1937
1938 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001939 const Bias bias_;
1940
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001941 DISALLOW_COPY_AND_ASSIGN(HCompare);
1942};
1943
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001944// A local in the graph. Corresponds to a Dex register.
1945class HLocal : public HTemplateInstruction<0> {
1946 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001947 explicit HLocal(uint16_t reg_number)
1948 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001949
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001950 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001951
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001952 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001953
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001955 // The Dex register number.
1956 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001957
1958 DISALLOW_COPY_AND_ASSIGN(HLocal);
1959};
1960
1961// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001962class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001963 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001964 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001965 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001966 SetRawInputAt(0, local);
1967 }
1968
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001969 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1970
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001971 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001972
1973 private:
1974 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1975};
1976
1977// Store a value in a given local. This instruction has two inputs: the value
1978// and the local.
1979class HStoreLocal : public HTemplateInstruction<2> {
1980 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001981 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001982 SetRawInputAt(0, local);
1983 SetRawInputAt(1, value);
1984 }
1985
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001986 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1987
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001988 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001989
1990 private:
1991 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1992};
1993
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001994class HConstant : public HExpression<0> {
1995 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001996 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1997
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001998 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001999
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002000 virtual bool IsMinusOne() const { return false; }
2001 virtual bool IsZero() const { return false; }
2002 virtual bool IsOne() const { return false; }
2003
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002004 DECLARE_INSTRUCTION(Constant);
2005
2006 private:
2007 DISALLOW_COPY_AND_ASSIGN(HConstant);
2008};
2009
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002010class HFloatConstant : public HConstant {
2011 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002012 float GetValue() const { return value_; }
2013
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002014 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002015 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2016 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002017 }
2018
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002019 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002020
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002021 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002022 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2023 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002024 }
2025 bool IsZero() const OVERRIDE {
2026 return AsFloatConstant()->GetValue() == 0.0f;
2027 }
2028 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002029 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2030 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002031 }
2032
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002033 DECLARE_INSTRUCTION(FloatConstant);
2034
2035 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002036 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
2037
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002038 const float value_;
2039
David Brazdil8d5b8b22015-03-24 10:51:52 +00002040 // Only the SsaBuilder can currently create floating-point constants. If we
2041 // ever need to create them later in the pipeline, we will have to handle them
2042 // the same way as integral constants.
2043 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002044 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2045};
2046
2047class HDoubleConstant : public HConstant {
2048 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002049 double GetValue() const { return value_; }
2050
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002051 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002052 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2053 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002054 }
2055
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002056 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002057
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002058 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002059 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2060 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002061 }
2062 bool IsZero() const OVERRIDE {
2063 return AsDoubleConstant()->GetValue() == 0.0;
2064 }
2065 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002066 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2067 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002068 }
2069
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002070 DECLARE_INSTRUCTION(DoubleConstant);
2071
2072 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002073 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
2074
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002075 const double value_;
2076
David Brazdil8d5b8b22015-03-24 10:51:52 +00002077 // Only the SsaBuilder can currently create floating-point constants. If we
2078 // ever need to create them later in the pipeline, we will have to handle them
2079 // the same way as integral constants.
2080 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002081 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2082};
2083
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002084class HNullConstant : public HConstant {
2085 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002086 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2087 return true;
2088 }
2089
2090 size_t ComputeHashCode() const OVERRIDE { return 0; }
2091
2092 DECLARE_INSTRUCTION(NullConstant);
2093
2094 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002095 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2096
2097 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002098 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2099};
2100
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002101// Constants of the type int. Those can be from Dex instructions, or
2102// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002103class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002104 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002105 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002106
Calin Juravle61d544b2015-02-23 16:46:57 +00002107 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002108 return other->AsIntConstant()->value_ == value_;
2109 }
2110
Calin Juravle61d544b2015-02-23 16:46:57 +00002111 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2112
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002113 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2114 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2115 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2116
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002117 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002118
2119 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002120 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002122 const int32_t value_;
2123
David Brazdil8d5b8b22015-03-24 10:51:52 +00002124 friend class HGraph;
2125 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002126 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002127 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2128};
2129
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002130class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002131 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002132 int64_t GetValue() const { return value_; }
2133
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002134 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002135 return other->AsLongConstant()->value_ == value_;
2136 }
2137
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002138 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002139
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002140 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2141 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2142 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2143
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002144 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002145
2146 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002147 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2148
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002149 const int64_t value_;
2150
David Brazdil8d5b8b22015-03-24 10:51:52 +00002151 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002152 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2153};
2154
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002155enum class Intrinsics {
2156#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2157#include "intrinsics_list.h"
2158 kNone,
2159 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2160#undef INTRINSICS_LIST
2161#undef OPTIMIZING_INTRINSICS
2162};
2163std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2164
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002165class HInvoke : public HInstruction {
2166 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002167 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002168
2169 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2170 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002171 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002172
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002173 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002174 SetRawInputAt(index, argument);
2175 }
2176
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002177 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002178
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002179 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002180
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002181 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2182
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002183 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002184 return intrinsic_;
2185 }
2186
2187 void SetIntrinsic(Intrinsics intrinsic) {
2188 intrinsic_ = intrinsic;
2189 }
2190
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002191 DECLARE_INSTRUCTION(Invoke);
2192
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002193 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002194 HInvoke(ArenaAllocator* arena,
2195 uint32_t number_of_arguments,
2196 Primitive::Type return_type,
2197 uint32_t dex_pc,
2198 uint32_t dex_method_index)
2199 : HInstruction(SideEffects::All()),
2200 inputs_(arena, number_of_arguments),
2201 return_type_(return_type),
2202 dex_pc_(dex_pc),
2203 dex_method_index_(dex_method_index),
2204 intrinsic_(Intrinsics::kNone) {
2205 inputs_.SetSize(number_of_arguments);
2206 }
2207
David Brazdil1abb4192015-02-17 18:33:36 +00002208 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2209 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2210 inputs_.Put(index, input);
2211 }
2212
2213 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002214 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002215 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002216 const uint32_t dex_method_index_;
2217 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002218
2219 private:
2220 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2221};
2222
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002223class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002224 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002225 // Requirements of this method call regarding the class
2226 // initialization (clinit) check of its declaring class.
2227 enum class ClinitCheckRequirement {
2228 kNone, // Class already initialized.
2229 kExplicit, // Static call having explicit clinit check as last input.
2230 kImplicit, // Static call implicitly requiring a clinit check.
2231 };
2232
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002233 HInvokeStaticOrDirect(ArenaAllocator* arena,
2234 uint32_t number_of_arguments,
2235 Primitive::Type return_type,
2236 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002237 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002238 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002239 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002240 InvokeType invoke_type,
2241 ClinitCheckRequirement clinit_check_requirement)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002242 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002243 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002244 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002245 is_recursive_(is_recursive),
2246 clinit_check_requirement_(clinit_check_requirement) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002247
Calin Juravle641547a2015-04-21 22:08:51 +01002248 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2249 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002250 // We access the method via the dex cache so we can't do an implicit null check.
2251 // TODO: for intrinsics we can generate implicit null checks.
2252 return false;
2253 }
2254
Nicolas Geoffray79041292015-03-26 10:05:54 +00002255 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002256 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002257 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002258 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002259
Roland Levillain4c0eb422015-04-24 16:43:49 +01002260 // Is this instruction a call to a static method?
2261 bool IsStatic() const {
2262 return GetInvokeType() == kStatic;
2263 }
2264
2265 // Remove the art::HLoadClass instruction set as last input by
2266 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2267 // the initial art::HClinitCheck instruction (only relevant for
2268 // static calls with explicit clinit check).
2269 void RemoveLoadClassAsLastInput() {
2270 DCHECK(IsStaticWithExplicitClinitCheck());
2271 size_t last_input_index = InputCount() - 1;
2272 HInstruction* last_input = InputAt(last_input_index);
2273 DCHECK(last_input != nullptr);
2274 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
2275 RemoveAsUserOfInput(last_input_index);
2276 inputs_.DeleteAt(last_input_index);
2277 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2278 DCHECK(IsStaticWithImplicitClinitCheck());
2279 }
2280
2281 // Is this a call to a static method whose declaring class has an
2282 // explicit intialization check in the graph?
2283 bool IsStaticWithExplicitClinitCheck() const {
2284 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2285 }
2286
2287 // Is this a call to a static method whose declaring class has an
2288 // implicit intialization check requirement?
2289 bool IsStaticWithImplicitClinitCheck() const {
2290 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2291 }
2292
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002293 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002294
Roland Levillain4c0eb422015-04-24 16:43:49 +01002295 protected:
2296 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2297 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2298 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2299 HInstruction* input = input_record.GetInstruction();
2300 // `input` is the last input of a static invoke marked as having
2301 // an explicit clinit check. It must either be:
2302 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2303 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2304 DCHECK(input != nullptr);
2305 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2306 }
2307 return input_record;
2308 }
2309
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002310 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002311 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002312 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002313 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002314 ClinitCheckRequirement clinit_check_requirement_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002315
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002316 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002317};
2318
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002319class HInvokeVirtual : public HInvoke {
2320 public:
2321 HInvokeVirtual(ArenaAllocator* arena,
2322 uint32_t number_of_arguments,
2323 Primitive::Type return_type,
2324 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002325 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002326 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002327 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002328 vtable_index_(vtable_index) {}
2329
Calin Juravle641547a2015-04-21 22:08:51 +01002330 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002331 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002332 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002333 }
2334
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002335 uint32_t GetVTableIndex() const { return vtable_index_; }
2336
2337 DECLARE_INSTRUCTION(InvokeVirtual);
2338
2339 private:
2340 const uint32_t vtable_index_;
2341
2342 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2343};
2344
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002345class HInvokeInterface : public HInvoke {
2346 public:
2347 HInvokeInterface(ArenaAllocator* arena,
2348 uint32_t number_of_arguments,
2349 Primitive::Type return_type,
2350 uint32_t dex_pc,
2351 uint32_t dex_method_index,
2352 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002353 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002354 imt_index_(imt_index) {}
2355
Calin Juravle641547a2015-04-21 22:08:51 +01002356 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002357 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002358 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002359 }
2360
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002361 uint32_t GetImtIndex() const { return imt_index_; }
2362 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2363
2364 DECLARE_INSTRUCTION(InvokeInterface);
2365
2366 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002367 const uint32_t imt_index_;
2368
2369 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2370};
2371
Dave Allison20dfc792014-06-16 20:44:29 -07002372class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002373 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002374 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002375 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2376 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002377 type_index_(type_index),
2378 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002379
2380 uint32_t GetDexPc() const { return dex_pc_; }
2381 uint16_t GetTypeIndex() const { return type_index_; }
2382
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002383 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002384 bool NeedsEnvironment() const OVERRIDE { return true; }
2385 // It may throw when called on:
2386 // - interfaces
2387 // - abstract/innaccessible/unknown classes
2388 // TODO: optimize when possible.
2389 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002390
Calin Juravle10e244f2015-01-26 18:54:32 +00002391 bool CanBeNull() const OVERRIDE { return false; }
2392
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002393 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2394
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002395 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002396
2397 private:
2398 const uint32_t dex_pc_;
2399 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002400 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002401
2402 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2403};
2404
Roland Levillain88cb1752014-10-20 16:36:47 +01002405class HNeg : public HUnaryOperation {
2406 public:
2407 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2408 : HUnaryOperation(result_type, input) {}
2409
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002410 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2411 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002412
Roland Levillain88cb1752014-10-20 16:36:47 +01002413 DECLARE_INSTRUCTION(Neg);
2414
2415 private:
2416 DISALLOW_COPY_AND_ASSIGN(HNeg);
2417};
2418
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002419class HNewArray : public HExpression<1> {
2420 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002421 HNewArray(HInstruction* length,
2422 uint32_t dex_pc,
2423 uint16_t type_index,
2424 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002425 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2426 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002427 type_index_(type_index),
2428 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002429 SetRawInputAt(0, length);
2430 }
2431
2432 uint32_t GetDexPc() const { return dex_pc_; }
2433 uint16_t GetTypeIndex() const { return type_index_; }
2434
2435 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002436 bool NeedsEnvironment() const OVERRIDE { return true; }
2437
Mingyao Yang0c365e62015-03-31 15:09:29 -07002438 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2439 bool CanThrow() const OVERRIDE { return true; }
2440
Calin Juravle10e244f2015-01-26 18:54:32 +00002441 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002442
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002443 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2444
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002445 DECLARE_INSTRUCTION(NewArray);
2446
2447 private:
2448 const uint32_t dex_pc_;
2449 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002450 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002451
2452 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2453};
2454
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002455class HAdd : public HBinaryOperation {
2456 public:
2457 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2458 : HBinaryOperation(result_type, left, right) {}
2459
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002460 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002461
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002462 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002463 return x + y;
2464 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002465 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002466 return x + y;
2467 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002468
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002469 DECLARE_INSTRUCTION(Add);
2470
2471 private:
2472 DISALLOW_COPY_AND_ASSIGN(HAdd);
2473};
2474
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002475class HSub : public HBinaryOperation {
2476 public:
2477 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2478 : HBinaryOperation(result_type, left, right) {}
2479
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002480 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002481 return x - y;
2482 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002483 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002484 return x - y;
2485 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002486
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002487 DECLARE_INSTRUCTION(Sub);
2488
2489 private:
2490 DISALLOW_COPY_AND_ASSIGN(HSub);
2491};
2492
Calin Juravle34bacdf2014-10-07 20:23:36 +01002493class HMul : public HBinaryOperation {
2494 public:
2495 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2496 : HBinaryOperation(result_type, left, right) {}
2497
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002498 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002499
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002500 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2501 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002502
2503 DECLARE_INSTRUCTION(Mul);
2504
2505 private:
2506 DISALLOW_COPY_AND_ASSIGN(HMul);
2507};
2508
Calin Juravle7c4954d2014-10-28 16:57:40 +00002509class HDiv : public HBinaryOperation {
2510 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002511 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2512 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002513
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002514 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002515 // Our graph structure ensures we never have 0 for `y` during constant folding.
2516 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002517 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002518 return (y == -1) ? -x : x / y;
2519 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002520
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002521 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002522 DCHECK_NE(y, 0);
2523 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2524 return (y == -1) ? -x : x / y;
2525 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002526
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002527 uint32_t GetDexPc() const { return dex_pc_; }
2528
Calin Juravle7c4954d2014-10-28 16:57:40 +00002529 DECLARE_INSTRUCTION(Div);
2530
2531 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002532 const uint32_t dex_pc_;
2533
Calin Juravle7c4954d2014-10-28 16:57:40 +00002534 DISALLOW_COPY_AND_ASSIGN(HDiv);
2535};
2536
Calin Juravlebacfec32014-11-14 15:54:36 +00002537class HRem : public HBinaryOperation {
2538 public:
2539 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2540 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2541
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002542 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002543 DCHECK_NE(y, 0);
2544 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2545 return (y == -1) ? 0 : x % y;
2546 }
2547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002548 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002549 DCHECK_NE(y, 0);
2550 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2551 return (y == -1) ? 0 : x % y;
2552 }
2553
2554 uint32_t GetDexPc() const { return dex_pc_; }
2555
2556 DECLARE_INSTRUCTION(Rem);
2557
2558 private:
2559 const uint32_t dex_pc_;
2560
2561 DISALLOW_COPY_AND_ASSIGN(HRem);
2562};
2563
Calin Juravled0d48522014-11-04 16:40:20 +00002564class HDivZeroCheck : public HExpression<1> {
2565 public:
2566 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2567 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2568 SetRawInputAt(0, value);
2569 }
2570
2571 bool CanBeMoved() const OVERRIDE { return true; }
2572
2573 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2574 UNUSED(other);
2575 return true;
2576 }
2577
2578 bool NeedsEnvironment() const OVERRIDE { return true; }
2579 bool CanThrow() const OVERRIDE { return true; }
2580
2581 uint32_t GetDexPc() const { return dex_pc_; }
2582
2583 DECLARE_INSTRUCTION(DivZeroCheck);
2584
2585 private:
2586 const uint32_t dex_pc_;
2587
2588 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2589};
2590
Calin Juravle9aec02f2014-11-18 23:06:35 +00002591class HShl : public HBinaryOperation {
2592 public:
2593 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2594 : HBinaryOperation(result_type, left, right) {}
2595
2596 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2597 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2598
2599 DECLARE_INSTRUCTION(Shl);
2600
2601 private:
2602 DISALLOW_COPY_AND_ASSIGN(HShl);
2603};
2604
2605class HShr : public HBinaryOperation {
2606 public:
2607 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2608 : HBinaryOperation(result_type, left, right) {}
2609
2610 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2611 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2612
2613 DECLARE_INSTRUCTION(Shr);
2614
2615 private:
2616 DISALLOW_COPY_AND_ASSIGN(HShr);
2617};
2618
2619class HUShr : public HBinaryOperation {
2620 public:
2621 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2622 : HBinaryOperation(result_type, left, right) {}
2623
2624 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2625 uint32_t ux = static_cast<uint32_t>(x);
2626 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2627 return static_cast<int32_t>(ux >> uy);
2628 }
2629
2630 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2631 uint64_t ux = static_cast<uint64_t>(x);
2632 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2633 return static_cast<int64_t>(ux >> uy);
2634 }
2635
2636 DECLARE_INSTRUCTION(UShr);
2637
2638 private:
2639 DISALLOW_COPY_AND_ASSIGN(HUShr);
2640};
2641
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002642class HAnd : public HBinaryOperation {
2643 public:
2644 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2645 : HBinaryOperation(result_type, left, right) {}
2646
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002647 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002648
2649 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2650 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2651
2652 DECLARE_INSTRUCTION(And);
2653
2654 private:
2655 DISALLOW_COPY_AND_ASSIGN(HAnd);
2656};
2657
2658class HOr : public HBinaryOperation {
2659 public:
2660 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2661 : HBinaryOperation(result_type, left, right) {}
2662
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002663 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002664
2665 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2666 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2667
2668 DECLARE_INSTRUCTION(Or);
2669
2670 private:
2671 DISALLOW_COPY_AND_ASSIGN(HOr);
2672};
2673
2674class HXor : public HBinaryOperation {
2675 public:
2676 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2677 : HBinaryOperation(result_type, left, right) {}
2678
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002679 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002680
2681 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2682 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2683
2684 DECLARE_INSTRUCTION(Xor);
2685
2686 private:
2687 DISALLOW_COPY_AND_ASSIGN(HXor);
2688};
2689
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002690// The value of a parameter in this method. Its location depends on
2691// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002692class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002693 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002694 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2695 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002696
2697 uint8_t GetIndex() const { return index_; }
2698
Calin Juravle10e244f2015-01-26 18:54:32 +00002699 bool CanBeNull() const OVERRIDE { return !is_this_; }
2700
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002701 DECLARE_INSTRUCTION(ParameterValue);
2702
2703 private:
2704 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002705 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002706 const uint8_t index_;
2707
Calin Juravle10e244f2015-01-26 18:54:32 +00002708 // Whether or not the parameter value corresponds to 'this' argument.
2709 const bool is_this_;
2710
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002711 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2712};
2713
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002714class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002715 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002716 explicit HNot(Primitive::Type result_type, HInstruction* input)
2717 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002718
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002719 bool CanBeMoved() const OVERRIDE { return true; }
2720 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002721 UNUSED(other);
2722 return true;
2723 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002724
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002725 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2726 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002727
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002728 DECLARE_INSTRUCTION(Not);
2729
2730 private:
2731 DISALLOW_COPY_AND_ASSIGN(HNot);
2732};
2733
David Brazdil66d126e2015-04-03 16:02:44 +01002734class HBooleanNot : public HUnaryOperation {
2735 public:
2736 explicit HBooleanNot(HInstruction* input)
2737 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2738
2739 bool CanBeMoved() const OVERRIDE { return true; }
2740 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2741 UNUSED(other);
2742 return true;
2743 }
2744
2745 int32_t Evaluate(int32_t x) const OVERRIDE {
2746 DCHECK(IsUint<1>(x));
2747 return !x;
2748 }
2749
2750 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2751 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2752 UNREACHABLE();
2753 }
2754
2755 DECLARE_INSTRUCTION(BooleanNot);
2756
2757 private:
2758 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2759};
2760
Roland Levillaindff1f282014-11-05 14:15:05 +00002761class HTypeConversion : public HExpression<1> {
2762 public:
2763 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002764 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2765 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002766 SetRawInputAt(0, input);
2767 DCHECK_NE(input->GetType(), result_type);
2768 }
2769
2770 HInstruction* GetInput() const { return InputAt(0); }
2771 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2772 Primitive::Type GetResultType() const { return GetType(); }
2773
Roland Levillain624279f2014-12-04 11:54:28 +00002774 // Required by the x86 and ARM code generators when producing calls
2775 // to the runtime.
2776 uint32_t GetDexPc() const { return dex_pc_; }
2777
Roland Levillaindff1f282014-11-05 14:15:05 +00002778 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002779 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002780
2781 DECLARE_INSTRUCTION(TypeConversion);
2782
2783 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002784 const uint32_t dex_pc_;
2785
Roland Levillaindff1f282014-11-05 14:15:05 +00002786 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2787};
2788
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002789static constexpr uint32_t kNoRegNumber = -1;
2790
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002791class HPhi : public HInstruction {
2792 public:
2793 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002794 : HInstruction(SideEffects::None()),
2795 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002796 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002797 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002798 is_live_(false),
2799 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002800 inputs_.SetSize(number_of_inputs);
2801 }
2802
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002803 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2804 static Primitive::Type ToPhiType(Primitive::Type type) {
2805 switch (type) {
2806 case Primitive::kPrimBoolean:
2807 case Primitive::kPrimByte:
2808 case Primitive::kPrimShort:
2809 case Primitive::kPrimChar:
2810 return Primitive::kPrimInt;
2811 default:
2812 return type;
2813 }
2814 }
2815
Calin Juravle10e244f2015-01-26 18:54:32 +00002816 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002817
2818 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002819 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002820
Calin Juravle10e244f2015-01-26 18:54:32 +00002821 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002822 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002823
Calin Juravle10e244f2015-01-26 18:54:32 +00002824 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2825 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2826
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002827 uint32_t GetRegNumber() const { return reg_number_; }
2828
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002829 void SetDead() { is_live_ = false; }
2830 void SetLive() { is_live_ = true; }
2831 bool IsDead() const { return !is_live_; }
2832 bool IsLive() const { return is_live_; }
2833
Calin Juravlea4f88312015-04-16 12:57:19 +01002834 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2835 // An equivalent phi is a phi having the same dex register and type.
2836 // It assumes that phis with the same dex register are adjacent.
2837 HPhi* GetNextEquivalentPhiWithSameType() {
2838 HInstruction* next = GetNext();
2839 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2840 if (next->GetType() == GetType()) {
2841 return next->AsPhi();
2842 }
2843 next = next->GetNext();
2844 }
2845 return nullptr;
2846 }
2847
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002848 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002849
David Brazdil1abb4192015-02-17 18:33:36 +00002850 protected:
2851 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2852
2853 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2854 inputs_.Put(index, input);
2855 }
2856
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002857 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002858 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002859 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002860 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002861 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002862 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002863
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002864 DISALLOW_COPY_AND_ASSIGN(HPhi);
2865};
2866
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002867class HNullCheck : public HExpression<1> {
2868 public:
2869 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002870 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002871 SetRawInputAt(0, value);
2872 }
2873
Calin Juravle10e244f2015-01-26 18:54:32 +00002874 bool CanBeMoved() const OVERRIDE { return true; }
2875 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002876 UNUSED(other);
2877 return true;
2878 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002879
Calin Juravle10e244f2015-01-26 18:54:32 +00002880 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002881
Calin Juravle10e244f2015-01-26 18:54:32 +00002882 bool CanThrow() const OVERRIDE { return true; }
2883
2884 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002885
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002886 uint32_t GetDexPc() const { return dex_pc_; }
2887
2888 DECLARE_INSTRUCTION(NullCheck);
2889
2890 private:
2891 const uint32_t dex_pc_;
2892
2893 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2894};
2895
2896class FieldInfo : public ValueObject {
2897 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002898 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2899 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002900
2901 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002902 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002903 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002904
2905 private:
2906 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002907 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002908 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002909};
2910
2911class HInstanceFieldGet : public HExpression<1> {
2912 public:
2913 HInstanceFieldGet(HInstruction* value,
2914 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002915 MemberOffset field_offset,
2916 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002917 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002918 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002919 SetRawInputAt(0, value);
2920 }
2921
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002922 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002923
2924 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2925 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2926 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002927 }
2928
Calin Juravle641547a2015-04-21 22:08:51 +01002929 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2930 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002931 }
2932
2933 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002934 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2935 }
2936
Calin Juravle52c48962014-12-16 17:02:57 +00002937 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002938 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002939 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002940 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002941
2942 DECLARE_INSTRUCTION(InstanceFieldGet);
2943
2944 private:
2945 const FieldInfo field_info_;
2946
2947 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2948};
2949
2950class HInstanceFieldSet : public HTemplateInstruction<2> {
2951 public:
2952 HInstanceFieldSet(HInstruction* object,
2953 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002954 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002955 MemberOffset field_offset,
2956 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002957 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002958 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002959 SetRawInputAt(0, object);
2960 SetRawInputAt(1, value);
2961 }
2962
Calin Juravle641547a2015-04-21 22:08:51 +01002963 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2964 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002965 }
2966
Calin Juravle52c48962014-12-16 17:02:57 +00002967 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002968 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002969 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002970 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002971 HInstruction* GetValue() const { return InputAt(1); }
2972
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002973 DECLARE_INSTRUCTION(InstanceFieldSet);
2974
2975 private:
2976 const FieldInfo field_info_;
2977
2978 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2979};
2980
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002981class HArrayGet : public HExpression<2> {
2982 public:
2983 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002984 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002985 SetRawInputAt(0, array);
2986 SetRawInputAt(1, index);
2987 }
2988
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002989 bool CanBeMoved() const OVERRIDE { return true; }
2990 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002991 UNUSED(other);
2992 return true;
2993 }
Calin Juravle641547a2015-04-21 22:08:51 +01002994 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2995 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002996 // TODO: We can be smarter here.
2997 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2998 // which generates the implicit null check. There are cases when these can be removed
2999 // to produce better code. If we ever add optimizations to do so we should allow an
3000 // implicit check here (as long as the address falls in the first page).
3001 return false;
3002 }
3003
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003004 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003005
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003006 HInstruction* GetArray() const { return InputAt(0); }
3007 HInstruction* GetIndex() const { return InputAt(1); }
3008
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003009 DECLARE_INSTRUCTION(ArrayGet);
3010
3011 private:
3012 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3013};
3014
3015class HArraySet : public HTemplateInstruction<3> {
3016 public:
3017 HArraySet(HInstruction* array,
3018 HInstruction* index,
3019 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003020 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003021 uint32_t dex_pc)
3022 : HTemplateInstruction(SideEffects::ChangesSomething()),
3023 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003024 expected_component_type_(expected_component_type),
3025 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003026 SetRawInputAt(0, array);
3027 SetRawInputAt(1, index);
3028 SetRawInputAt(2, value);
3029 }
3030
Calin Juravle77520bc2015-01-12 18:45:46 +00003031 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003032 // We currently always call a runtime method to catch array store
3033 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003034 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003035 }
3036
Calin Juravle641547a2015-04-21 22:08:51 +01003037 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3038 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003039 // TODO: Same as for ArrayGet.
3040 return false;
3041 }
3042
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003043 void ClearNeedsTypeCheck() {
3044 needs_type_check_ = false;
3045 }
3046
3047 bool NeedsTypeCheck() const { return needs_type_check_; }
3048
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003049 uint32_t GetDexPc() const { return dex_pc_; }
3050
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003051 HInstruction* GetArray() const { return InputAt(0); }
3052 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003053 HInstruction* GetValue() const { return InputAt(2); }
3054
3055 Primitive::Type GetComponentType() const {
3056 // The Dex format does not type floating point index operations. Since the
3057 // `expected_component_type_` is set during building and can therefore not
3058 // be correct, we also check what is the value type. If it is a floating
3059 // point type, we must use that type.
3060 Primitive::Type value_type = GetValue()->GetType();
3061 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3062 ? value_type
3063 : expected_component_type_;
3064 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003065
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003066 DECLARE_INSTRUCTION(ArraySet);
3067
3068 private:
3069 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003070 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003071 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003072
3073 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3074};
3075
3076class HArrayLength : public HExpression<1> {
3077 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003078 explicit HArrayLength(HInstruction* array)
3079 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3080 // Note that arrays do not change length, so the instruction does not
3081 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003082 SetRawInputAt(0, array);
3083 }
3084
Calin Juravle77520bc2015-01-12 18:45:46 +00003085 bool CanBeMoved() const OVERRIDE { return true; }
3086 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003087 UNUSED(other);
3088 return true;
3089 }
Calin Juravle641547a2015-04-21 22:08:51 +01003090 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3091 return obj == InputAt(0);
3092 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003093
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003094 DECLARE_INSTRUCTION(ArrayLength);
3095
3096 private:
3097 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3098};
3099
3100class HBoundsCheck : public HExpression<2> {
3101 public:
3102 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003103 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003104 DCHECK(index->GetType() == Primitive::kPrimInt);
3105 SetRawInputAt(0, index);
3106 SetRawInputAt(1, length);
3107 }
3108
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003109 bool CanBeMoved() const OVERRIDE { return true; }
3110 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003111 UNUSED(other);
3112 return true;
3113 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003114
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003115 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003116
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003117 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003118
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003119 uint32_t GetDexPc() const { return dex_pc_; }
3120
3121 DECLARE_INSTRUCTION(BoundsCheck);
3122
3123 private:
3124 const uint32_t dex_pc_;
3125
3126 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3127};
3128
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003129/**
3130 * Some DEX instructions are folded into multiple HInstructions that need
3131 * to stay live until the last HInstruction. This class
3132 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003133 * HInstruction stays live. `index` represents the stack location index of the
3134 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003135 */
3136class HTemporary : public HTemplateInstruction<0> {
3137 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003138 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003139
3140 size_t GetIndex() const { return index_; }
3141
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003142 Primitive::Type GetType() const OVERRIDE {
3143 // The previous instruction is the one that will be stored in the temporary location.
3144 DCHECK(GetPrevious() != nullptr);
3145 return GetPrevious()->GetType();
3146 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003147
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003148 DECLARE_INSTRUCTION(Temporary);
3149
3150 private:
3151 const size_t index_;
3152
3153 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3154};
3155
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003156class HSuspendCheck : public HTemplateInstruction<0> {
3157 public:
3158 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003159 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003160
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003161 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003162 return true;
3163 }
3164
3165 uint32_t GetDexPc() const { return dex_pc_; }
3166
3167 DECLARE_INSTRUCTION(SuspendCheck);
3168
3169 private:
3170 const uint32_t dex_pc_;
3171
3172 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3173};
3174
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003175/**
3176 * Instruction to load a Class object.
3177 */
3178class HLoadClass : public HExpression<0> {
3179 public:
3180 HLoadClass(uint16_t type_index,
3181 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003182 uint32_t dex_pc)
3183 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3184 type_index_(type_index),
3185 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003186 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003187 generate_clinit_check_(false),
3188 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003189
3190 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003191
3192 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3193 return other->AsLoadClass()->type_index_ == type_index_;
3194 }
3195
3196 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3197
3198 uint32_t GetDexPc() const { return dex_pc_; }
3199 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003200 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003201
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003202 bool NeedsEnvironment() const OVERRIDE {
3203 // Will call runtime and load the class if the class is not loaded yet.
3204 // TODO: finer grain decision.
3205 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003206 }
3207
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003208 bool MustGenerateClinitCheck() const {
3209 return generate_clinit_check_;
3210 }
3211
3212 void SetMustGenerateClinitCheck() {
3213 generate_clinit_check_ = true;
3214 }
3215
3216 bool CanCallRuntime() const {
3217 return MustGenerateClinitCheck() || !is_referrers_class_;
3218 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003219
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003220 bool CanThrow() const OVERRIDE {
3221 // May call runtime and and therefore can throw.
3222 // TODO: finer grain decision.
3223 return !is_referrers_class_;
3224 }
3225
Calin Juravleacf735c2015-02-12 15:25:22 +00003226 ReferenceTypeInfo GetLoadedClassRTI() {
3227 return loaded_class_rti_;
3228 }
3229
3230 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3231 // Make sure we only set exact types (the loaded class should never be merged).
3232 DCHECK(rti.IsExact());
3233 loaded_class_rti_ = rti;
3234 }
3235
3236 bool IsResolved() {
3237 return loaded_class_rti_.IsExact();
3238 }
3239
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003240 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3241
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003242 DECLARE_INSTRUCTION(LoadClass);
3243
3244 private:
3245 const uint16_t type_index_;
3246 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003247 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003248 // Whether this instruction must generate the initialization check.
3249 // Used for code generation.
3250 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003251
Calin Juravleacf735c2015-02-12 15:25:22 +00003252 ReferenceTypeInfo loaded_class_rti_;
3253
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003254 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3255};
3256
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003257class HLoadString : public HExpression<0> {
3258 public:
3259 HLoadString(uint32_t string_index, uint32_t dex_pc)
3260 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3261 string_index_(string_index),
3262 dex_pc_(dex_pc) {}
3263
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003264 bool CanBeMoved() const OVERRIDE { return true; }
3265
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003266 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3267 return other->AsLoadString()->string_index_ == string_index_;
3268 }
3269
3270 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3271
3272 uint32_t GetDexPc() const { return dex_pc_; }
3273 uint32_t GetStringIndex() const { return string_index_; }
3274
3275 // TODO: Can we deopt or debug when we resolve a string?
3276 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003277 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003278
3279 DECLARE_INSTRUCTION(LoadString);
3280
3281 private:
3282 const uint32_t string_index_;
3283 const uint32_t dex_pc_;
3284
3285 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3286};
3287
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003288/**
3289 * Performs an initialization check on its Class object input.
3290 */
3291class HClinitCheck : public HExpression<1> {
3292 public:
3293 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003294 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003295 dex_pc_(dex_pc) {
3296 SetRawInputAt(0, constant);
3297 }
3298
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003299 bool CanBeMoved() const OVERRIDE { return true; }
3300 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3301 UNUSED(other);
3302 return true;
3303 }
3304
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003305 bool NeedsEnvironment() const OVERRIDE {
3306 // May call runtime to initialize the class.
3307 return true;
3308 }
3309
3310 uint32_t GetDexPc() const { return dex_pc_; }
3311
3312 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3313
3314 DECLARE_INSTRUCTION(ClinitCheck);
3315
3316 private:
3317 const uint32_t dex_pc_;
3318
3319 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3320};
3321
3322class HStaticFieldGet : public HExpression<1> {
3323 public:
3324 HStaticFieldGet(HInstruction* cls,
3325 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003326 MemberOffset field_offset,
3327 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003328 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003329 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003330 SetRawInputAt(0, cls);
3331 }
3332
Calin Juravle52c48962014-12-16 17:02:57 +00003333
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003334 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003335
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003336 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003337 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3338 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003339 }
3340
3341 size_t ComputeHashCode() const OVERRIDE {
3342 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3343 }
3344
Calin Juravle52c48962014-12-16 17:02:57 +00003345 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003346 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3347 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003348 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003349
3350 DECLARE_INSTRUCTION(StaticFieldGet);
3351
3352 private:
3353 const FieldInfo field_info_;
3354
3355 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3356};
3357
3358class HStaticFieldSet : public HTemplateInstruction<2> {
3359 public:
3360 HStaticFieldSet(HInstruction* cls,
3361 HInstruction* value,
3362 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003363 MemberOffset field_offset,
3364 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003365 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003366 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003367 SetRawInputAt(0, cls);
3368 SetRawInputAt(1, value);
3369 }
3370
Calin Juravle52c48962014-12-16 17:02:57 +00003371 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003372 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3373 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003374 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003375
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003376 HInstruction* GetValue() const { return InputAt(1); }
3377
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003378 DECLARE_INSTRUCTION(StaticFieldSet);
3379
3380 private:
3381 const FieldInfo field_info_;
3382
3383 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3384};
3385
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003386// Implement the move-exception DEX instruction.
3387class HLoadException : public HExpression<0> {
3388 public:
3389 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3390
3391 DECLARE_INSTRUCTION(LoadException);
3392
3393 private:
3394 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3395};
3396
3397class HThrow : public HTemplateInstruction<1> {
3398 public:
3399 HThrow(HInstruction* exception, uint32_t dex_pc)
3400 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3401 SetRawInputAt(0, exception);
3402 }
3403
3404 bool IsControlFlow() const OVERRIDE { return true; }
3405
3406 bool NeedsEnvironment() const OVERRIDE { return true; }
3407
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003408 bool CanThrow() const OVERRIDE { return true; }
3409
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003410 uint32_t GetDexPc() const { return dex_pc_; }
3411
3412 DECLARE_INSTRUCTION(Throw);
3413
3414 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003415 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003416
3417 DISALLOW_COPY_AND_ASSIGN(HThrow);
3418};
3419
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003420class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003421 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003422 HInstanceOf(HInstruction* object,
3423 HLoadClass* constant,
3424 bool class_is_final,
3425 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003426 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3427 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003428 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003429 dex_pc_(dex_pc) {
3430 SetRawInputAt(0, object);
3431 SetRawInputAt(1, constant);
3432 }
3433
3434 bool CanBeMoved() const OVERRIDE { return true; }
3435
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003436 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003437 return true;
3438 }
3439
3440 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003441 return false;
3442 }
3443
3444 uint32_t GetDexPc() const { return dex_pc_; }
3445
3446 bool IsClassFinal() const { return class_is_final_; }
3447
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003448 // Used only in code generation.
3449 bool MustDoNullCheck() const { return must_do_null_check_; }
3450 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3451
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003452 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003453
3454 private:
3455 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003456 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003457 const uint32_t dex_pc_;
3458
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003459 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3460};
3461
Calin Juravleb1498f62015-02-16 13:13:29 +00003462class HBoundType : public HExpression<1> {
3463 public:
3464 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3465 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3466 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003467 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003468 SetRawInputAt(0, input);
3469 }
3470
3471 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3472
3473 bool CanBeNull() const OVERRIDE {
3474 // `null instanceof ClassX` always return false so we can't be null.
3475 return false;
3476 }
3477
3478 DECLARE_INSTRUCTION(BoundType);
3479
3480 private:
3481 // Encodes the most upper class that this instruction can have. In other words
3482 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3483 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3484 const ReferenceTypeInfo bound_type_;
3485
3486 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3487};
3488
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003489class HCheckCast : public HTemplateInstruction<2> {
3490 public:
3491 HCheckCast(HInstruction* object,
3492 HLoadClass* constant,
3493 bool class_is_final,
3494 uint32_t dex_pc)
3495 : HTemplateInstruction(SideEffects::None()),
3496 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003497 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003498 dex_pc_(dex_pc) {
3499 SetRawInputAt(0, object);
3500 SetRawInputAt(1, constant);
3501 }
3502
3503 bool CanBeMoved() const OVERRIDE { return true; }
3504
3505 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3506 return true;
3507 }
3508
3509 bool NeedsEnvironment() const OVERRIDE {
3510 // Instruction may throw a CheckCastError.
3511 return true;
3512 }
3513
3514 bool CanThrow() const OVERRIDE { return true; }
3515
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003516 bool MustDoNullCheck() const { return must_do_null_check_; }
3517 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3518
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003519 uint32_t GetDexPc() const { return dex_pc_; }
3520
3521 bool IsClassFinal() const { return class_is_final_; }
3522
3523 DECLARE_INSTRUCTION(CheckCast);
3524
3525 private:
3526 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003527 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003528 const uint32_t dex_pc_;
3529
3530 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003531};
3532
Calin Juravle27df7582015-04-17 19:12:31 +01003533class HMemoryBarrier : public HTemplateInstruction<0> {
3534 public:
3535 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3536 : HTemplateInstruction(SideEffects::None()),
3537 barrier_kind_(barrier_kind) {}
3538
3539 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3540
3541 DECLARE_INSTRUCTION(MemoryBarrier);
3542
3543 private:
3544 const MemBarrierKind barrier_kind_;
3545
3546 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3547};
3548
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003549class HMonitorOperation : public HTemplateInstruction<1> {
3550 public:
3551 enum OperationKind {
3552 kEnter,
3553 kExit,
3554 };
3555
3556 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3557 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3558 SetRawInputAt(0, object);
3559 }
3560
3561 // Instruction may throw a Java exception, so we need an environment.
3562 bool NeedsEnvironment() const OVERRIDE { return true; }
3563 bool CanThrow() const OVERRIDE { return true; }
3564
3565 uint32_t GetDexPc() const { return dex_pc_; }
3566
3567 bool IsEnter() const { return kind_ == kEnter; }
3568
3569 DECLARE_INSTRUCTION(MonitorOperation);
3570
Calin Juravle52c48962014-12-16 17:02:57 +00003571 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003572 const OperationKind kind_;
3573 const uint32_t dex_pc_;
3574
3575 private:
3576 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3577};
3578
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003579class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003580 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003581 MoveOperands(Location source,
3582 Location destination,
3583 Primitive::Type type,
3584 HInstruction* instruction)
3585 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003586
3587 Location GetSource() const { return source_; }
3588 Location GetDestination() const { return destination_; }
3589
3590 void SetSource(Location value) { source_ = value; }
3591 void SetDestination(Location value) { destination_ = value; }
3592
3593 // The parallel move resolver marks moves as "in-progress" by clearing the
3594 // destination (but not the source).
3595 Location MarkPending() {
3596 DCHECK(!IsPending());
3597 Location dest = destination_;
3598 destination_ = Location::NoLocation();
3599 return dest;
3600 }
3601
3602 void ClearPending(Location dest) {
3603 DCHECK(IsPending());
3604 destination_ = dest;
3605 }
3606
3607 bool IsPending() const {
3608 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3609 return destination_.IsInvalid() && !source_.IsInvalid();
3610 }
3611
3612 // True if this blocks a move from the given location.
3613 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003614 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003615 }
3616
3617 // A move is redundant if it's been eliminated, if its source and
3618 // destination are the same, or if its destination is unneeded.
3619 bool IsRedundant() const {
3620 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3621 }
3622
3623 // We clear both operands to indicate move that's been eliminated.
3624 void Eliminate() {
3625 source_ = destination_ = Location::NoLocation();
3626 }
3627
3628 bool IsEliminated() const {
3629 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3630 return source_.IsInvalid();
3631 }
3632
Nicolas Geoffray90218252015-04-15 11:56:51 +01003633 bool Is64BitMove() const {
3634 return Primitive::Is64BitType(type_);
3635 }
3636
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003637 HInstruction* GetInstruction() const { return instruction_; }
3638
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003639 private:
3640 Location source_;
3641 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003642 // The type this move is for.
3643 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003644 // The instruction this move is assocatied with. Null when this move is
3645 // for moving an input in the expected locations of user (including a phi user).
3646 // This is only used in debug mode, to ensure we do not connect interval siblings
3647 // in the same parallel move.
3648 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003649};
3650
3651static constexpr size_t kDefaultNumberOfMoves = 4;
3652
3653class HParallelMove : public HTemplateInstruction<0> {
3654 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003655 explicit HParallelMove(ArenaAllocator* arena)
3656 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003657
Nicolas Geoffray90218252015-04-15 11:56:51 +01003658 void AddMove(Location source,
3659 Location destination,
3660 Primitive::Type type,
3661 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003662 DCHECK(source.IsValid());
3663 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003664 if (kIsDebugBuild) {
3665 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003666 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003667 if (moves_.Get(i).GetInstruction() == instruction) {
3668 // Special case the situation where the move is for the spill slot
3669 // of the instruction.
3670 if ((GetPrevious() == instruction)
3671 || ((GetPrevious() == nullptr)
3672 && instruction->IsPhi()
3673 && instruction->GetBlock() == GetBlock())) {
3674 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3675 << "Doing parallel moves for the same instruction.";
3676 } else {
3677 DCHECK(false) << "Doing parallel moves for the same instruction.";
3678 }
3679 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003680 }
3681 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003682 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003683 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3684 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003685 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003686 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003687 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003688 }
3689
3690 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003691 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003692 }
3693
3694 size_t NumMoves() const { return moves_.Size(); }
3695
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003696 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003697
3698 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003699 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003700
3701 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3702};
3703
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003704class HGraphVisitor : public ValueObject {
3705 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003706 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3707 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003708
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003709 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003710 virtual void VisitBasicBlock(HBasicBlock* block);
3711
Roland Levillain633021e2014-10-01 14:12:25 +01003712 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003713 void VisitInsertionOrder();
3714
Roland Levillain633021e2014-10-01 14:12:25 +01003715 // Visit the graph following dominator tree reverse post-order.
3716 void VisitReversePostOrder();
3717
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003718 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003719
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003720 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003721#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003722 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3723
3724 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3725
3726#undef DECLARE_VISIT_INSTRUCTION
3727
3728 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003729 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003730
3731 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3732};
3733
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003734class HGraphDelegateVisitor : public HGraphVisitor {
3735 public:
3736 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3737 virtual ~HGraphDelegateVisitor() {}
3738
3739 // Visit functions that delegate to to super class.
3740#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003741 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003742
3743 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3744
3745#undef DECLARE_VISIT_INSTRUCTION
3746
3747 private:
3748 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3749};
3750
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003751class HInsertionOrderIterator : public ValueObject {
3752 public:
3753 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3754
3755 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3756 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3757 void Advance() { ++index_; }
3758
3759 private:
3760 const HGraph& graph_;
3761 size_t index_;
3762
3763 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3764};
3765
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003766class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003767 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003768 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3769 // Check that reverse post order of the graph has been built.
3770 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3771 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003772
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003773 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3774 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003775 void Advance() { ++index_; }
3776
3777 private:
3778 const HGraph& graph_;
3779 size_t index_;
3780
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003781 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003782};
3783
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003784class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003785 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003786 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003787 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3788 // Check that reverse post order of the graph has been built.
3789 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3790 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003791
3792 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003793 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003794 void Advance() { --index_; }
3795
3796 private:
3797 const HGraph& graph_;
3798 size_t index_;
3799
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003800 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003801};
3802
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003803class HLinearPostOrderIterator : public ValueObject {
3804 public:
3805 explicit HLinearPostOrderIterator(const HGraph& graph)
3806 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3807
3808 bool Done() const { return index_ == 0; }
3809
3810 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3811
3812 void Advance() {
3813 --index_;
3814 DCHECK_GE(index_, 0U);
3815 }
3816
3817 private:
3818 const GrowableArray<HBasicBlock*>& order_;
3819 size_t index_;
3820
3821 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3822};
3823
3824class HLinearOrderIterator : public ValueObject {
3825 public:
3826 explicit HLinearOrderIterator(const HGraph& graph)
3827 : order_(graph.GetLinearOrder()), index_(0) {}
3828
3829 bool Done() const { return index_ == order_.Size(); }
3830 HBasicBlock* Current() const { return order_.Get(index_); }
3831 void Advance() { ++index_; }
3832
3833 private:
3834 const GrowableArray<HBasicBlock*>& order_;
3835 size_t index_;
3836
3837 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3838};
3839
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003840// Iterator over the blocks that art part of the loop. Includes blocks part
3841// of an inner loop. The order in which the blocks are iterated is on their
3842// block id.
3843class HBlocksInLoopIterator : public ValueObject {
3844 public:
3845 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3846 : blocks_in_loop_(info.GetBlocks()),
3847 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3848 index_(0) {
3849 if (!blocks_in_loop_.IsBitSet(index_)) {
3850 Advance();
3851 }
3852 }
3853
3854 bool Done() const { return index_ == blocks_.Size(); }
3855 HBasicBlock* Current() const { return blocks_.Get(index_); }
3856 void Advance() {
3857 ++index_;
3858 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3859 if (blocks_in_loop_.IsBitSet(index_)) {
3860 break;
3861 }
3862 }
3863 }
3864
3865 private:
3866 const BitVector& blocks_in_loop_;
3867 const GrowableArray<HBasicBlock*>& blocks_;
3868 size_t index_;
3869
3870 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3871};
3872
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003873inline int64_t Int64FromConstant(HConstant* constant) {
3874 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3875 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3876 : constant->AsLongConstant()->GetValue();
3877}
3878
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003879} // namespace art
3880
3881#endif // ART_COMPILER_OPTIMIZING_NODES_H_